Commit 8bff2545 authored by Kristian Klausen's avatar Kristian Klausen 🎉
Browse files

Try to improve readability by moving everything into functions

... and use readonly, local and adding a few more comments.
parent 89ae66b5
......@@ -4,13 +4,16 @@
# 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
MIRROR="https://mirror.pkgbuild.com"
readonly MIRROR="https://mirror.pkgbuild.com"
ORIG_PWD="${PWD}"
OUTPUT="${PWD}/output"
mkdir -p "tmp" "${OUTPUT}"
TMPDIR="$(mktemp --directory --tmpdir="${PWD}/tmp")"
cd "${TMPDIR}"
function init() {
readonly ORIG_PWD="${PWD}"
readonly OUTPUT="${PWD}/output"
readonly TMPDIR="$(mktemp --dry-run --directory --tmpdir="${PWD}/tmp")"
mkdir -p "${OUTPUT}" "${TMPDIR}"
cd "${TMPDIR}"
}
# Do some cleanup when the script exits
function cleanup() {
......@@ -69,8 +72,8 @@ function start_qemu() {
# Wait for a specific string from qemu
function expect() {
length="${#1}"
i=0
local length="${#1}"
local i=0
# We can't use ex: grep as we could end blocking forever, if the string isn't followed by a newline
while true; do
# read should never exit with a non-zero exit code,
......@@ -92,41 +95,49 @@ function send() {
echo -en "${1}" >guest.in
}
prepare_boot
start_qemu
expect "archiso login:"
send "root\n"
expect "# "
send "bash\n"
expect "# "
send "trap \"shutdown now\" ERR\n"
expect "# "
send "mkdir /mnt/arch-boxes && mount -t 9p -o trans=virtio host /mnt/arch-boxes -oversion=9p2000.L\n"
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.sh,http} .\n"
expect "# "
send "mkdir pkg && mount --bind pkg /var/cache/pacman/pkg\n"
expect "# "
# Wait for pacman-init
send "until systemctl is-active pacman-init; do sleep 1; done\n"
expect "# "
send "pacman -Sy --noconfirm qemu-headless jq\n"
expect "# "
send "bash -x ./build.sh\n"
expect "# "
send "cp -r --preserve=mode,timestamps output /mnt/arch-boxes/tmp/$(basename "${TMPDIR}")/\n"
expect "# "
mv output/* "${OUTPUT}/"
send "shutdown now\n"
wait
function main() {
init
prepare_boot
start_qemu
# Login
expect "archiso login:"
send "root\n"
expect "# "
# Switch to bash and shutdown on error
send "bash\n"
expect "# "
send "trap \"shutdown now\" ERR\n"
expect "# "
# Prepare environment
send "mkdir /mnt/arch-boxes && mount -t 9p -o trans=virtio host /mnt/arch-boxes -oversion=9p2000.L\n"
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.sh,http} .\n"
expect "# "
send "mkdir pkg && mount --bind pkg /var/cache/pacman/pkg\n"
expect "# "
# Wait for pacman-init
send "until systemctl is-active pacman-init; do sleep 1; done\n"
expect "# "
# Install required packages
send "pacman -Sy --noconfirm qemu-headless jq\n"
expect "# "
## Start build and copy output to local disk
send "bash -x ./build.sh\n"
expect "# "
send "cp -r --preserve=mode,timestamps output /mnt/arch-boxes/tmp/$(basename "${TMPDIR}")/\n"
expect "# "
mv output/* "${OUTPUT}/"
# Shutdown the VM
send "shutdown now\n"
wait
}
main
......@@ -4,27 +4,24 @@
# 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
DISK_SIZE="2G"
IMAGE="image.img"
readonly DISK_SIZE="2G"
readonly IMAGE="image.img"
# shellcheck disable=SC2016
MIRROR='https://mirror.pkgbuild.com/$repo/os/$arch'
readonly MIRROR='https://mirror.pkgbuild.com/$repo/os/$arch'
if [ "$(id -u)" -ne 0 ]; then
echo "root is required"
exit 1
fi
ORIG_PWD="${PWD}"
OUTPUT="${PWD}/output"
mkdir -p "tmp" "${OUTPUT}"
if [ -n "${SUDO_UID:-}" ]; then
chown "${SUDO_UID}:${SUDO_GID}" "tmp" "${OUTPUT}"
fi
TMPDIR="$(mktemp --directory --tmpdir="${PWD}/tmp")"
cd "${TMPDIR}"
function init() {
readonly ORIG_PWD="${PWD}"
readonly OUTPUT="${PWD}/output"
readonly TMPDIR="$(mktemp --dry-run --directory --tmpdir="${PWD}/tmp")"
mkdir -p "${OUTPUT}" "${TMPDIR}"
if [ -n "${SUDO_UID:-}" ]; then
chown "${SUDO_UID}:${SUDO_GID}" "${OUTPUT}" "${TMPDIR}"
fi
cd "${TMPDIR}"
MOUNT="${PWD}/mount"
mkdir "${MOUNT}"
readonly MOUNT="${PWD}/mount"
mkdir "${MOUNT}"
}
# Do some cleanup when the script exits
function cleanup() {
......@@ -170,8 +167,9 @@ Vagrant.configure("2") do |config|
end
end
EOF
VIRTUAL_SIZE="$(grep -o "^[0-9]*" <<<"${DISK_SIZE}")"
echo '{"format":"qcow2","provider":"libvirt","virtual_size":'"${VIRTUAL_SIZE}"'}' >metadata.json
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}"
......@@ -188,10 +186,11 @@ function vagrant_virtualbox() {
function vagrant_virtualbox_post() {
# Create vagrant box
# VirtualBox-6.1.12 src/VBox/NetworkServices/Dhcpd/Config.cpp line 276
MAC_ADDRESS="080027$(openssl rand -hex 3 | tr '[:lower:]' '[:upper:]')"
local mac_address
mac_address="080027$(openssl rand -hex 3 | tr '[:lower:]' '[:upper:]')"
cat <<EOF >Vagrantfile
Vagrant.configure("2") do |config|
config.vm.base_mac = "${MAC_ADDRESS}"
config.vm.base_mac = "${mac_address}"
end
EOF
echo '{"provider":"virtualbox"}' >metadata.json
......@@ -203,23 +202,31 @@ EOF
-e "s/DISK_UUID/$(uuidgen)/" \
-e "s/DISK_CAPACITY/$(qemu-img info --output=json "box/packer-virtualbox.vmdk" | jq '."virtual-size"')/" \
-e "s/UNIX/$(date +%s)/" \
-e "s/MAC_ADDRESS/${MAC_ADDRESS}/" \
-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
}
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
function main() {
if [ "$(id -u)" -ne 0 ]; then
echo "root is required"
exit 1
fi
if [ -z "${BUILD_DATE:-}" ]; then
BUILD_DATE="$(date -I)"
fi
create_image "cloud-img.img" "Arch-Linux-x86_64-cloudimg-${BUILD_DATE}.qcow2" cloud_image cloud_image_post
create_image "vagrant-qemu.img" "Arch-Linux-x86_64-libvirt-${BUILD_DATE}.box" vagrant_qemu vagrant_qemu_post
create_image "vagrant-virtualbox.img" "Arch-Linux-x86_64-virtualbox-${BUILD_DATE}.box" vagrant_qemu vagrant_virtualbox_post
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
if [ -z "${BUILD_DATE:-}" ]; then
BUILD_DATE="$(date -I)"
fi
create_image "cloud-img.img" "Arch-Linux-x86_64-cloudimg-${BUILD_DATE}.qcow2" cloud_image cloud_image_post
create_image "vagrant-qemu.img" "Arch-Linux-x86_64-libvirt-${BUILD_DATE}.box" vagrant_qemu vagrant_qemu_post
create_image "vagrant-virtualbox.img" "Arch-Linux-x86_64-virtualbox-${BUILD_DATE}.box" vagrant_qemu vagrant_virtualbox_post
}
main
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment