From 23e02f3fb1f76a2d3c1fe2aab3ab7e4c471a4ae8 Mon Sep 17 00:00:00 2001 From: Kristian Klausen Date: Mon, 9 Nov 2020 19:41:41 +0100 Subject: [PATCH 1/4] Remove noop http/install-cloud.sh --- build-inside-vm.sh | 2 +- http/install-cloud.sh | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) delete mode 100644 http/install-cloud.sh diff --git a/build-inside-vm.sh b/build-inside-vm.sh index 2f620d4..888944a 100755 --- a/build-inside-vm.sh +++ b/build-inside-vm.sh @@ -167,7 +167,7 @@ function create_image() { } function cloud_image() { - arch-chroot "${MOUNT}" /bin/bash < <(cat "${ORIG_PWD}"/http/install-{cloud,common}.sh) + arch-chroot "${MOUNT}" /bin/bash < <(cat "${ORIG_PWD}"/http/install-common.sh) # The growpart module[1] requires the growpart program, provided by the # cloud-guest-utils package # [1] https://cloudinit.readthedocs.io/en/latest/topics/modules.html#growpart diff --git a/http/install-cloud.sh b/http/install-cloud.sh deleted file mode 100644 index f359bb7..0000000 --- a/http/install-cloud.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash -# shellcheck disable=SC2034 -NEWUSER="arch" -- GitLab From ebeb0bcb9e623cb1c77767acdd343bbf22ccc445 Mon Sep 17 00:00:00 2001 From: Kristian Klausen Date: Mon, 9 Nov 2020 19:58:11 +0100 Subject: [PATCH 2/4] Merge http/* into build-inside-vm.sh --- .gitlab-ci.yml | 4 +- build-host.sh | 2 +- build-inside-vm.sh | 91 +++++++++++++++++++++++++++++++++++++++-- http/install-common.sh | 60 --------------------------- http/install-vagrant.sh | 31 -------------- 5 files changed, 90 insertions(+), 98 deletions(-) delete mode 100644 http/install-common.sh delete mode 100644 http/install-vagrant.sh diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index ad3366e..beb94c6 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -12,14 +12,14 @@ shellcheck: before_script: - pacman -Syu --needed --noconfirm shellcheck script: - - shellcheck **/*.sh + - shellcheck *.sh shfmt: stage: lint before_script: - pacman -Syu --needed --noconfirm shfmt script: - - shfmt -i 2 -ci -d **/*.sh + - shfmt -i 2 -ci -d *.sh .build: stage: build diff --git a/build-host.sh b/build-host.sh index 265b78d..7078457 100755 --- a/build-host.sh +++ b/build-host.sh @@ -118,7 +118,7 @@ function main() { expect "# " send "mkfs.ext4 /dev/vda && mkdir /mnt/scratch-disk/ && mount /dev/vda /mnt/scratch-disk && cd /mnt/scratch-disk\n" expect "# " - send "cp -a /mnt/arch-boxes/{box.ovf,build-inside-vm.sh,http} .\n" + send "cp -a /mnt/arch-boxes/{box.ovf,build-inside-vm.sh} .\n" expect "# " send "mkdir pkg && mount --bind pkg /var/cache/pacman/pkg\n" expect "# " diff --git a/build-inside-vm.sh b/build-inside-vm.sh index 888944a..ecf622e 100755 --- a/build-inside-vm.sh +++ b/build-inside-vm.sh @@ -98,6 +98,63 @@ function postinstall() { arch-chroot "${MOUNT}" /usr/bin/locale-gen arch-chroot "${MOUNT}" /usr/bin/systemd-firstboot --locale=en_US.UTF-8 --timezone=UTC --hostname=archlinux --keymap=us ln -sf /run/systemd/resolve/stub-resolv.conf "${MOUNT}/etc/resolv.conf" + + # Setup pacman-init.service for clean pacman keyring initialization + cat <"${MOUNT}/etc/systemd/system/pacman-init.service" +[Unit] +Description=Initializes Pacman keyring +Wants=haveged.service +After=haveged.service +Before=sshd.service cloud-final.service +ConditionFirstBoot=yes + +[Service] +Type=oneshot +RemainAfterExit=yes +ExecStart=/usr/bin/pacman-key --init +ExecStart=/usr/bin/pacman-key --populate archlinux + +[Install] +WantedBy=multi-user.target +EOF + + # Add service for running reflector on first boot + cat <"${MOUNT}/etc/systemd/system/reflector-init.service" +[Unit] +Description=Initializes mirrors for the VM +After=network-online.target +Wants=network-online.target +Before=sshd.service cloud-final.service +ConditionFirstBoot=yes + +[Service] +Type=oneshot +RemainAfterExit=yes +ExecStart=reflector --latest 20 --protocol https --sort rate --save /etc/pacman.d/mirrorlist + +[Install] +WantedBy=multi-user.target +EOF + + # enabling important services + arch-chroot "${MOUNT}" /bin/bash -e <"${MOUNT}/etc/sudoers.d/${NEWUSER}" +Defaults:${NEWUSER} !requiretty +${NEWUSER} ALL=(ALL) NOPASSWD: ALL +EOF + chmod 440 "${MOUNT}/etc/sudoers.d/${NEWUSER}" + + # setup network + cat <"${MOUNT}/etc/systemd/network/eth0.network" +[Match] +Name=eth0 + +[Network] +DHCP=ipv4 +EOF + + # install vagrant ssh key + arch-chroot "${MOUNT}" /bin/bash -e <"${MOUNT}/etc/polkit-1/rules.d/49-nopasswd_global.rules" polkit.addRule(function(action, subject) { @@ -255,8 +340,6 @@ function main() { setup_disk bootstrap postinstall - # We run it here as it is the easiest solution and we do not want anything to go wrong! - arch-chroot "${MOUNT}" grub-install --target=i386-pc "${LOOPDEV}" unmount_image local build_version diff --git a/http/install-common.sh b/http/install-common.sh deleted file mode 100644 index 45c4a20..0000000 --- a/http/install-common.sh +++ /dev/null @@ -1,60 +0,0 @@ -#!/bin/bash - -set -e -set -x - -# Setup pacman-init.service for clean pacman keyring initialization -cat </etc/systemd/system/pacman-init.service -[Unit] -Description=Initializes Pacman keyring -Wants=haveged.service -After=haveged.service -Before=sshd.service cloud-final.service -ConditionFirstBoot=yes - -[Service] -Type=oneshot -RemainAfterExit=yes -ExecStart=/usr/bin/pacman-key --init -ExecStart=/usr/bin/pacman-key --populate archlinux - -[Install] -WantedBy=multi-user.target -EOF - -# Add service for running reflector on first boot -cat </etc/systemd/system/reflector-init.service -[Unit] -Description=Initializes mirrors for the VM -After=network-online.target -Wants=network-online.target -Before=sshd.service cloud-final.service -ConditionFirstBoot=yes - -[Service] -Type=oneshot -RemainAfterExit=yes -ExecStart=reflector --latest 20 --protocol https --sort rate --save /etc/pacman.d/mirrorlist - -[Install] -WantedBy=multi-user.target -EOF - -# enabling important services -systemctl enable sshd -systemctl enable haveged -systemctl enable systemd-networkd -systemctl enable systemd-resolved -systemctl enable systemd-timesyncd -systemctl enable pacman-init.service -systemctl enable reflector-init.service - -sed -i 's/^GRUB_TIMEOUT=.*$/GRUB_TIMEOUT=1/' /etc/default/grub -# setup unpredictable kernel names -sed -i 's/^GRUB_CMDLINE_LINUX=.*$/GRUB_CMDLINE_LINUX="net.ifnames=0"/' /etc/default/grub -sed -i 's/^GRUB_CMDLINE_LINUX_DEFAULT=.*/GRUB_CMDLINE_LINUX_DEFAULT=\"rootflags=compress-force=zstd\"/' /etc/default/grub -grub-mkconfig -o /boot/grub/grub.cfg - -if declare -f post >/dev/null; then - post -fi diff --git a/http/install-vagrant.sh b/http/install-vagrant.sh deleted file mode 100644 index d1eb3ba..0000000 --- a/http/install-vagrant.sh +++ /dev/null @@ -1,31 +0,0 @@ -#!/bin/bash -# shellcheck disable=SC2034 -NEWUSER="vagrant" - -post() { - # setting the user credentials - useradd -m -U "${NEWUSER}" - echo -e "${NEWUSER}\n${NEWUSER}" | passwd "${NEWUSER}" - - # setting sudo for the user - cat <"/etc/sudoers.d/${NEWUSER}" -Defaults:${NEWUSER} !requiretty -${NEWUSER} ALL=(ALL) NOPASSWD: ALL -EOF - chmod 440 "/etc/sudoers.d/${NEWUSER}" - - # setup network - cat </etc/systemd/network/eth0.network -[Match] -Name=eth0 - -[Network] -DHCP=ipv4 -EOF - - # install vagrant ssh key - install --directory --owner=vagrant --group=vagrant --mode=0700 /home/vagrant/.ssh - curl --output /home/vagrant/.ssh/authorized_keys --location https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub - chown vagrant:vagrant /home/vagrant/.ssh/authorized_keys - chmod 0600 /home/vagrant/.ssh/authorized_keys -} -- GitLab From e89025e7921d255c7ff7b33164ef4ddbbc955a81 Mon Sep 17 00:00:00 2001 From: Kristian Klausen Date: Mon, 9 Nov 2020 20:03:26 +0100 Subject: [PATCH 3/4] Fix shellcheck + shfmt --- build-inside-vm.sh | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/build-inside-vm.sh b/build-inside-vm.sh index ecf622e..d6499c0 100755 --- a/build-inside-vm.sh +++ b/build-inside-vm.sh @@ -170,10 +170,10 @@ function image_cleanup() { # ${1} - loop device function wait_until_settled() { udevadm settle - blockdev --flushbufs --rereadpt ${1} + blockdev --flushbufs --rereadpt "${1}" until test -e "${1}p2"; do - echo "${1}p2 doesn't exist yet..." - sleep 1 + echo "${1}p2 doesn't exist yet..." + sleep 1 done } @@ -181,7 +181,7 @@ function wait_until_settled() { function mount_image() { LOOPDEV=$(losetup --find --partscan --show "${1:-${IMAGE}}") # Partscan is racy - wait_until_settled ${LOOPDEV} + wait_until_settled "${LOOPDEV}" mount -o compress-force=zstd "${LOOPDEV}p2" "${MOUNT}" # Setup bind mount to package cache mount --bind "/var/cache/pacman/pkg" "${MOUNT}/var/cache/pacman/pkg" @@ -214,7 +214,8 @@ function mv_to_output() { # ${2} - pre # ${3} - post function create_image() { - local tmp_image="$(basename "$(mktemp -u)")" + local tmp_image + tmp_image="$(basename "$(mktemp -u)")" copy_and_mount_image "${tmp_image}" "${2}" image_cleanup -- GitLab From fdb4bd89020fd995de625019906e10a185f7a5c3 Mon Sep 17 00:00:00 2001 From: Kristian Klausen Date: Mon, 9 Nov 2020 20:25:20 +0100 Subject: [PATCH 4/4] Split build-inside-vm.sh per-image logic into separate files --- .gitlab-ci.yml | 6 +- build-host.sh | 2 +- build-inside-vm.sh | 208 +++-------------------------------- images/base.sh | 81 ++++++++++++++ images/cloud-image.sh | 17 +++ images/common.sh | 43 ++++++++ images/vagrant-libvirt.sh | 28 +++++ images/vagrant-virtualbox.sh | 35 ++++++ 8 files changed, 227 insertions(+), 193 deletions(-) create mode 100644 images/base.sh create mode 100644 images/cloud-image.sh create mode 100644 images/common.sh create mode 100644 images/vagrant-libvirt.sh create mode 100644 images/vagrant-virtualbox.sh diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index beb94c6..aebe34a 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -12,14 +12,16 @@ shellcheck: before_script: - pacman -Syu --needed --noconfirm shellcheck script: - - shellcheck *.sh + - shopt -s globstar + - shellcheck **/*.sh shfmt: stage: lint before_script: - pacman -Syu --needed --noconfirm shfmt script: - - shfmt -i 2 -ci -d *.sh + - shopt -s globstar + - shfmt -i 2 -ci -d **/*.sh .build: stage: build diff --git a/build-host.sh b/build-host.sh index 7078457..5dd84b7 100755 --- a/build-host.sh +++ b/build-host.sh @@ -118,7 +118,7 @@ function main() { expect "# " send "mkfs.ext4 /dev/vda && mkdir /mnt/scratch-disk/ && mount /dev/vda /mnt/scratch-disk && cd /mnt/scratch-disk\n" expect "# " - send "cp -a /mnt/arch-boxes/{box.ovf,build-inside-vm.sh} .\n" + send "cp -a /mnt/arch-boxes/{box.ovf,build-inside-vm.sh,images} .\n" expect "# " send "mkdir pkg && mount --bind pkg /var/cache/pacman/pkg\n" expect "# " diff --git a/build-inside-vm.sh b/build-inside-vm.sh index d6499c0..8966820 100755 --- a/build-inside-vm.sh +++ b/build-inside-vm.sh @@ -4,6 +4,7 @@ # nounset: "Treat unset variables and parameters [...] as an error when performing parameter expansion." # errexit: "Exit immediately if [...] command exits with a non-zero status." set -o nounset -o errexit +shopt -s extglob readonly DISK_SIZE="20G" readonly IMAGE="image.img" # shellcheck disable=SC2016 @@ -77,86 +78,6 @@ EOF cp mirrorlist "${MOUNT}/etc/pacman.d/" } -# Misc "tweaks" done after bootstrapping -function postinstall() { - # Remove machine-id see: - # https://gitlab.archlinux.org/archlinux/arch-boxes/-/issues/25 - # https://gitlab.archlinux.org/archlinux/arch-boxes/-/issues/117 - rm "${MOUNT}/etc/machine-id" - - arch-chroot "${MOUNT}" /usr/bin/btrfs subvolume create /swap - chattr +C "${MOUNT}/swap" - chmod 0700 "${MOUNT}/swap" - fallocate -l 512M "${MOUNT}/swap/swapfile" - mkswap "${MOUNT}/swap/swapfile" - echo -e "/swap/swapfile none swap defaults 0 0" >>"${MOUNT}/etc/fstab" - - echo "COMPRESSION=\"zstd\"" >>"${MOUNT}/etc/mkinitcpio.conf" - arch-chroot "${MOUNT}" /usr/bin/mkinitcpio -p linux - - sed -i -e 's/^#\(en_US.UTF-8\)/\1/' "${MOUNT}/etc/locale.gen" - arch-chroot "${MOUNT}" /usr/bin/locale-gen - arch-chroot "${MOUNT}" /usr/bin/systemd-firstboot --locale=en_US.UTF-8 --timezone=UTC --hostname=archlinux --keymap=us - ln -sf /run/systemd/resolve/stub-resolv.conf "${MOUNT}/etc/resolv.conf" - - # Setup pacman-init.service for clean pacman keyring initialization - cat <"${MOUNT}/etc/systemd/system/pacman-init.service" -[Unit] -Description=Initializes Pacman keyring -Wants=haveged.service -After=haveged.service -Before=sshd.service cloud-final.service -ConditionFirstBoot=yes - -[Service] -Type=oneshot -RemainAfterExit=yes -ExecStart=/usr/bin/pacman-key --init -ExecStart=/usr/bin/pacman-key --populate archlinux - -[Install] -WantedBy=multi-user.target -EOF - - # Add service for running reflector on first boot - cat <"${MOUNT}/etc/systemd/system/reflector-init.service" -[Unit] -Description=Initializes mirrors for the VM -After=network-online.target -Wants=network-online.target -Before=sshd.service cloud-final.service -ConditionFirstBoot=yes - -[Service] -Type=oneshot -RemainAfterExit=yes -ExecStart=reflector --latest 20 --protocol https --sort rate --save /etc/pacman.d/mirrorlist - -[Install] -WantedBy=multi-user.target -EOF - - # enabling important services - arch-chroot "${MOUNT}" /bin/bash -e <"${MOUNT}/etc/sudoers.d/${NEWUSER}" -Defaults:${NEWUSER} !requiretty -${NEWUSER} ALL=(ALL) NOPASSWD: ALL -EOF - chmod 440 "${MOUNT}/etc/sudoers.d/${NEWUSER}" - - # setup network - cat <"${MOUNT}/etc/systemd/network/eth0.network" -[Match] -Name=eth0 - -[Network] -DHCP=ipv4 -EOF - - # install vagrant ssh key - arch-chroot "${MOUNT}" /bin/bash -e <"${MOUNT}/etc/polkit-1/rules.d/49-nopasswd_global.rules" -polkit.addRule(function(action, subject) { - if (subject.isInGroup("vagrant")) { - return polkit.Result.YES; - } -}); -EOF -} - -function vagrant_qemu_post() { - # Create vagrant box - cat <Vagrantfile -Vagrant.configure("2") do |config| - config.vm.provider :libvirt do |libvirt| - libvirt.driver = "kvm" - end -end -EOF - local virtual_size - virtual_size="$(grep -o "^[0-9]*" <<<"${DISK_SIZE}")" - echo '{"format":"qcow2","provider":"libvirt","virtual_size":'"${virtual_size}"'}' >metadata.json - qemu-img convert -f raw -O qcow2 "${1}" box.img - rm "${1}" - - tar -czf "${2}" Vagrantfile metadata.json box.img - rm Vagrantfile metadata.json box.img -} - -function vagrant_virtualbox() { - vagrant_common - arch-chroot "${MOUNT}" /usr/bin/pacman -S --noconfirm virtualbox-guest-utils-nox - arch-chroot "${MOUNT}" /usr/bin/systemctl enable vboxservice -} - -function vagrant_virtualbox_post() { - # Create vagrant box - # VirtualBox-6.1.12 src/VBox/NetworkServices/Dhcpd/Config.cpp line 276 - local mac_address - mac_address="080027$(openssl rand -hex 3 | tr '[:lower:]' '[:upper:]')" - cat <Vagrantfile -Vagrant.configure("2") do |config| - config.vm.base_mac = "${mac_address}" -end -EOF - echo '{"provider":"virtualbox"}' >metadata.json - qemu-img convert -f raw -O vmdk "${1}" "packer-virtualbox.vmdk" - rm "${1}" - - cp "${ORIG_PWD}/box.ovf" . - sed -e "s/MACHINE_UUID/$(uuidgen)/" \ - -e "s/DISK_UUID/$(uuidgen)/" \ - -e "s/DISK_CAPACITY/$(qemu-img info --output=json "packer-virtualbox.vmdk" | jq '."virtual-size"')/" \ - -e "s/UNIX/$(date +%s)/" \ - -e "s/MAC_ADDRESS/${mac_address}/" \ - -i box.ovf - - tar -czf "${2}" Vagrantfile metadata.json packer-virtualbox.vmdk box.ovf - rm Vagrantfile metadata.json packer-virtualbox.vmdk box.ovf -} - # ${1} - Optional build version. If not set, will generate a default based on date. function main() { if [ "$(id -u)" -ne 0 ]; then @@ -340,7 +161,9 @@ function main() { setup_disk bootstrap - postinstall + # shellcheck source=images/base.sh + . "${ORIG_PWD}/images/base.sh" + pre unmount_image local build_version @@ -351,8 +174,13 @@ function main() { else build_version="${1}" fi - create_image "Arch-Linux-x86_64-cloudimg-${build_version}.qcow2" cloud_image cloud_image_post - create_image "Arch-Linux-x86_64-libvirt-${build_version}.box" vagrant_common vagrant_qemu_post - create_image "Arch-Linux-x86_64-virtualbox-${build_version}.box" vagrant_virtualbox vagrant_virtualbox_post + + # shellcheck source=images/common.sh + . "${ORIG_PWD}/images/common.sh" + for image in "${ORIG_PWD}/images/"!(base|common).sh; do + # shellcheck source=/dev/null + . "${image}" + create_image "${IMAGE_NAME}" pre post + done } main "$@" diff --git a/images/base.sh b/images/base.sh new file mode 100644 index 0000000..e6d7074 --- /dev/null +++ b/images/base.sh @@ -0,0 +1,81 @@ +#!/bin/bash + +# Misc "tweaks" done after bootstrapping +function pre() { + # Remove machine-id see: + # https://gitlab.archlinux.org/archlinux/arch-boxes/-/issues/25 + # https://gitlab.archlinux.org/archlinux/arch-boxes/-/issues/117 + rm "${MOUNT}/etc/machine-id" + + arch-chroot "${MOUNT}" /usr/bin/btrfs subvolume create /swap + chattr +C "${MOUNT}/swap" + chmod 0700 "${MOUNT}/swap" + fallocate -l 512M "${MOUNT}/swap/swapfile" + mkswap "${MOUNT}/swap/swapfile" + echo -e "/swap/swapfile none swap defaults 0 0" >>"${MOUNT}/etc/fstab" + + echo "COMPRESSION=\"zstd\"" >>"${MOUNT}/etc/mkinitcpio.conf" + arch-chroot "${MOUNT}" /usr/bin/mkinitcpio -p linux + + sed -i -e 's/^#\(en_US.UTF-8\)/\1/' "${MOUNT}/etc/locale.gen" + arch-chroot "${MOUNT}" /usr/bin/locale-gen + arch-chroot "${MOUNT}" /usr/bin/systemd-firstboot --locale=en_US.UTF-8 --timezone=UTC --hostname=archlinux --keymap=us + ln -sf /run/systemd/resolve/stub-resolv.conf "${MOUNT}/etc/resolv.conf" + + # Setup pacman-init.service for clean pacman keyring initialization + cat <"${MOUNT}/etc/systemd/system/pacman-init.service" +[Unit] +Description=Initializes Pacman keyring +Wants=haveged.service +After=haveged.service +Before=sshd.service cloud-final.service +ConditionFirstBoot=yes + +[Service] +Type=oneshot +RemainAfterExit=yes +ExecStart=/usr/bin/pacman-key --init +ExecStart=/usr/bin/pacman-key --populate archlinux + +[Install] +WantedBy=multi-user.target +EOF + + # Add service for running reflector on first boot + cat <"${MOUNT}/etc/systemd/system/reflector-init.service" +[Unit] +Description=Initializes mirrors for the VM +After=network-online.target +Wants=network-online.target +Before=sshd.service cloud-final.service +ConditionFirstBoot=yes + +[Service] +Type=oneshot +RemainAfterExit=yes +ExecStart=reflector --latest 20 --protocol https --sort rate --save /etc/pacman.d/mirrorlist + +[Install] +WantedBy=multi-user.target +EOF + + # enabling important services + arch-chroot "${MOUNT}" /bin/bash -e <"${MOUNT}/etc/sudoers.d/${NEWUSER}" +Defaults:${NEWUSER} !requiretty +${NEWUSER} ALL=(ALL) NOPASSWD: ALL +EOF + chmod 440 "${MOUNT}/etc/sudoers.d/${NEWUSER}" + + # setup network + cat <"${MOUNT}/etc/systemd/network/eth0.network" +[Match] +Name=eth0 + +[Network] +DHCP=ipv4 +EOF + + # install vagrant ssh key + arch-chroot "${MOUNT}" /bin/bash -e <"${MOUNT}/etc/polkit-1/rules.d/49-nopasswd_global.rules" +polkit.addRule(function(action, subject) { + if (subject.isInGroup("vagrant")) { + return polkit.Result.YES; + } +}); +EOF +} diff --git a/images/vagrant-libvirt.sh b/images/vagrant-libvirt.sh new file mode 100644 index 0000000..be2b56d --- /dev/null +++ b/images/vagrant-libvirt.sh @@ -0,0 +1,28 @@ +#!/bin/bash +# shellcheck disable=SC2034,SC2154 +IMAGE_NAME="Arch-Linux-x86_64-libvirt-${build_version}.box" +PACKAGES=() +SERVICES=() + +function pre() { + vagrant_common +} + +function post() { + # Create vagrant box + cat <Vagrantfile +Vagrant.configure("2") do |config| + config.vm.provider :libvirt do |libvirt| + libvirt.driver = "kvm" + end +end +EOF + local virtual_size + virtual_size="$(grep -o "^[0-9]*" <<<"${DISK_SIZE}")" + echo '{"format":"qcow2","provider":"libvirt","virtual_size":'"${virtual_size}"'}' >metadata.json + qemu-img convert -f raw -O qcow2 "${1}" box.img + rm "${1}" + + tar -czf "${2}" Vagrantfile metadata.json box.img + rm Vagrantfile metadata.json box.img +} diff --git a/images/vagrant-virtualbox.sh b/images/vagrant-virtualbox.sh new file mode 100644 index 0000000..e374be0 --- /dev/null +++ b/images/vagrant-virtualbox.sh @@ -0,0 +1,35 @@ +#!/bin/bash +# shellcheck disable=SC2034,SC2154 +IMAGE_NAME="Arch-Linux-x86_64-virtualbox-${build_version}.box" +PACKAGES=(virtualbox-guest-utils-nox) +SERVICES=(vboxservice) + +function pre() { + vagrant_common +} + +function post() { + # Create vagrant box + # VirtualBox-6.1.12 src/VBox/NetworkServices/Dhcpd/Config.cpp line 276 + local mac_address + mac_address="080027$(openssl rand -hex 3 | tr '[:lower:]' '[:upper:]')" + cat <Vagrantfile +Vagrant.configure("2") do |config| + config.vm.base_mac = "${mac_address}" +end +EOF + echo '{"provider":"virtualbox"}' >metadata.json + qemu-img convert -f raw -O vmdk "${1}" "packer-virtualbox.vmdk" + rm "${1}" + + cp "${ORIG_PWD}/box.ovf" . + sed -e "s/MACHINE_UUID/$(uuidgen)/" \ + -e "s/DISK_UUID/$(uuidgen)/" \ + -e "s/DISK_CAPACITY/$(qemu-img info --output=json "packer-virtualbox.vmdk" | jq '."virtual-size"')/" \ + -e "s/UNIX/$(date +%s)/" \ + -e "s/MAC_ADDRESS/${mac_address}/" \ + -i box.ovf + + tar -czf "${2}" Vagrantfile metadata.json packer-virtualbox.vmdk box.ovf + rm Vagrantfile metadata.json packer-virtualbox.vmdk box.ovf +} -- GitLab