From f9b6d9f2b7d4817fc0d2b04b70a67e1b9992c1dd Mon Sep 17 00:00:00 2001 From: Robert Fairley Date: Wed, 24 Jun 2020 03:51:04 -0400 Subject: [PATCH] add scripts to start vm and install rpms Add `./run_vm.sh` which starts a VM, from an image passed in as the first argument, with an authorized SSH keypair to access the VM. `./install_vm_rpms.sh` then adds the RPMs to the running VM. The VM can then be accessed using the generated SSH key id for manual testing/verification. --- .gitignore | 1 + README.md | 23 +++++++++++++++++++++++ install_vm_rpms.sh | 29 +++++++++++++++++++++++++++++ run_vm.sh | 30 ++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+) create mode 100755 install_vm_rpms.sh create mode 100755 run_vm.sh diff --git a/.gitignore b/.gitignore index 567609b..021128b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ build/ +vm/ diff --git a/README.md b/README.md index 708caf0..e3cb6a9 100644 --- a/README.md +++ b/README.md @@ -79,3 +79,26 @@ To build without using a container, execute: ``` make rpm ``` + +## Testing in a VM + +This requires `libguestfs-tools-c` and `qemu-kvm` installed. + +`./run_vm.sh` will provision a VM image (`.qcow2` or `.raw`) passed as +the first argument, and start up a VM which you can SSH into. After +running `./run_vm.sh`, run `./install_vm_rpms.sh` to install the last +built RPMs from `./build_rpm.sh` in the VM. You can then SSH into the +machine using the details that `./install_vm_rpms.sh` outputs to the +terminal. + +``` +./clean_rpm.sh && ./build_rpm.sh +./run_vm.sh path/to/image.qcow2 +./install_vm_rpms.sh +``` + +To iterate while the VM is running, after committing changes locally: + +``` +./clean_rpm.sh && ./build_rpm.sh && ./install_vm_rpms.sh +``` diff --git a/install_vm_rpms.sh b/install_vm_rpms.sh new file mode 100755 index 0000000..2073aa7 --- /dev/null +++ b/install_vm_rpms.sh @@ -0,0 +1,29 @@ +#!/usr/bin/bash + +set -xeuo pipefail + +pkg=console-login-helper-messages +vmdir=./vm +sshkey_path="$vmdir/id_rsa" +ssh_port=2226 + +rpms_paths=$(ls ./build/$pkg/rpms/noarch/${pkg}-*.noarch.rpm | tr '\n' ' ') +ssh_opts="-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" + +# Remove old RPMs in VM image. +ssh $ssh_opts -i $sshkey_path -p $ssh_port root@localhost "pkgs=\$(rpm -qa | grep ${pkg}) ; if [ -n \"\$pkgs\" ]; then echo \"\$pkgs\" | xargs rpm -e; fi" +ssh $ssh_opts -i $sshkey_path -p $ssh_port root@localhost "rm -f /root/${pkg}-*.noarch.rpm" + +# Copy new RPMs to VM image. +scp $ssh_opts -i $sshkey_path -P $ssh_port $rpms_paths root@localhost:/root + +# Install new RPMs in VM. +ssh $ssh_opts -i $sshkey_path -p $ssh_port root@localhost "rpm -i /root/${pkg}-*.noarch.rpm" + +# Enable applicable units, and reboot the VM so the services start as +# if installed already. +ssh $ssh_opts -i $sshkey_path -p $ssh_port root@localhost "systemctl enable console-login-helper-messages-issuegen.path" +ssh $ssh_opts -i $sshkey_path -p $ssh_port root@localhost "systemctl enable console-login-helper-messages-motdgen.path" +ssh $ssh_opts -i $sshkey_path -p $ssh_port root@localhost "systemctl reboot" + +echo SSH into the VM with: ssh -i $sshkey_path -p $ssh_port root@localhost diff --git a/run_vm.sh b/run_vm.sh new file mode 100755 index 0000000..9731437 --- /dev/null +++ b/run_vm.sh @@ -0,0 +1,30 @@ +#!/usr/bin/bash + +set -xeuo pipefail + +vmdir=./vm +ssh_port=2226 +mkdir -p $vmdir + +image_path="$1" + +sshkey_path="$vmdir/id_rsa" +ssh-keygen -t rsa -f "$sshkey_path" -q -N "" +ssh_pubkey=$(cat "${sshkey_path}.pub") + +guestfish --rw -i -a "$image_path" <