Skip to content
Snippets Groups Projects
Commit ef4c021d authored by Kristian Klausen's avatar Kristian Klausen :tada:
Browse files

gitlab_runner: Inject the SSH public key at boot rather than burning it into the image

This means that there is no need to make runner-specific changes to the
image, so in theory the image could be build centrally (e.g. in the
arch-boxes project[1]) and then distributed to the runner hosts.

This change also make the SSH keys ephemeral.

[1] https://gitlab.archlinux.org/archlinux/arch-boxes
parent 61e267c3
No related branches found
No related tags found
No related merge requests found
This commit is part of merge request !881. Comments created here will be created in the context of that merge request.
......@@ -4,7 +4,7 @@ readonly libvirt_pool="images"
ssh() {
command ssh \
-i "/etc/libvirt-executor/id_ed25519" \
-i "/run/libvirt-executor/id_ed25519_$(vm_name)" \
-F /dev/null \
-o ServerAliveCountMax=2 \
-o ServerAliveInterval=15 \
......@@ -58,8 +58,15 @@ prepare() {
local backing_volume
backing_volume="$(</usr/local/lib/libvirt-executor/backing-vol-name)"
mkdir -p /run/libvirt-executor
chmod 700 /run/libvirt-executor
ssh-keygen -q -N "" -f /run/libvirt-executor/id_ed25519_$(vm_name) -t ed25519
local tmpfiles_extra
tmpfiles_extra="$(echo -e "d /root/.ssh 0750 root root -\nf~ /root/.ssh/authorized_keys 0600 root root - $(base64 -w 0 /run/libvirt-executor/id_ed25519_$(vm_name).pub)" | base64 -w 0)"
virsh vol-create-as "${libvirt_pool}" "$(vm_name).qcow2" 0 --format qcow2 --backing-vol "${backing_volume}" --backing-vol-format qcow2
virsh define <(sed "s/\$vm_name/$(vm_name)/" /usr/local/lib/libvirt-executor/domain_template.xml)
virsh define <(sed -e "s/\$vm_name/$(vm_name)/" -e "s/\$tmpfiles_extra/${tmpfiles_extra}/" /usr/local/lib/libvirt-executor/domain_template.xml)
virsh start "$(vm_name)"
wait_for_ssh "$(vm_name)"
......@@ -80,6 +87,7 @@ run() {
# https://docs.gitlab.com/runner/executors/custom.html#cleanup
cleanup() {
rm /run/libvirt-executor/id_ed25519_$(vm_name){,.pub}
virsh destroy "$(vm_name)" || true
virsh undefine --remove-all-storage "$(vm_name)"
}
......
......@@ -43,8 +43,6 @@ sed -E 's/^#(IgnorePkg *=)/\1 linux/' -i mnt/etc/pacman.conf
arch-chroot mnt userdel -r arch
sed 's/^\(GRUB_CMDLINE_LINUX=".*\)"$/\1 lockdown=confidentiality"/' -i mnt/etc/default/grub
arch-chroot mnt /usr/bin/grub-mkconfig -o /boot/grub/grub.cfg
install -d -m0700 mnt/root/.ssh
install -m0600 /etc/libvirt-executor/id_ed25519.pub mnt/root/.ssh/authorized_keys
# We want to use the transient hostname
# https://github.com/systemd/systemd/pull/30814
rm -f mnt/etc/machine-id mnt/etc/hostname
......
......@@ -69,11 +69,8 @@
- name: Install libvirt-executor-update-base-image dependencies
pacman: name=arch-install-scripts,sequoia-sq state=present
- name: Create libvirt-executor configuration and data directories
file: path={{ item }} state=directory owner=root group=root mode=0755
loop:
- /etc/libvirt-executor
- /usr/local/lib/libvirt-executor
- name: Create libvirt-executor data directory
file: path=/usr/local/lib/libvirt-executor state=directory owner=root group=root mode=0755
- name: Install libvirt-executor
copy: src={{ item.src }} dest={{ item.dest }} owner=root group=root mode={{ item.mode }}
......@@ -85,11 +82,6 @@
- name: Install libvirt-executor domain template
template: src=domain_template.xml.j2 dest=/usr/local/lib/libvirt-executor/domain_template.xml owner=root group=root mode=0644
- name: Create SSH keys for libvirt-executor
command: ssh-keygen -N "" -f /etc/libvirt-executor/id_ed25519 -t ed25519
args:
creates: /etc/libvirt-executor/id_ed25519
- name: Install libvirt-executor-update-base-image.{service,timer}
copy: src={{ item }} dest=/etc/systemd/system/{{ item }} owner=root group=root mode=0644
loop:
......
......@@ -5,6 +5,7 @@
<sysinfo type='smbios'>
<oemStrings>
<entry>io.systemd.credential:system.hostname=$vm_name</entry>
<entry>io.systemd.credential.binary:tmpfiles.extra=$tmpfiles_extra</entry>
</oemStrings>
</sysinfo>
<os>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment