Skip to content

Commit

Permalink
add scripts to start vm and install rpms
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Robert Fairley authored and rfairley committed Jun 29, 2020
1 parent 282fc07 commit f9b6d9f
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
build/
vm/
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
29 changes: 29 additions & 0 deletions install_vm_rpms.sh
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions run_vm.sh
Original file line number Diff line number Diff line change
@@ -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" <<EOF
mkdir-p /root/.ssh/
chmod 0700 /root/.ssh/
copy-in "$sshkey_path" "${sshkey_path}.pub" /root/.ssh/
write /root/.ssh/authorized_keys "${ssh_pubkey}"
chmod 0600 /root/.ssh/authorized_keys
chmod 0600 /root/.ssh/id_rsa
chmod 0644 /root/.ssh/id_rsa.pub
write /etc/ssh/sshd_config.d/99-enable-root-login "PermitRootLogin without-password\n"
write /etc/ssh/sshd_config.d/99-enable-pubkey "PubkeyAuthentication yes\n"
selinux-relabel /etc/selinux/targeted/contexts/files/file_contexts /root/.ssh
EOF

qemu-kvm -m 2048 -cpu host -nographic -snapshot \
-drive if=virtio,file="${image_path}" \
-nic user,model=virtio,hostfwd=tcp::${ssh_port}-:22

0 comments on commit f9b6d9f

Please sign in to comment.