diff options
| -rwxr-xr-x | build-arch-gce | 50 |
1 files changed, 23 insertions, 27 deletions
diff --git a/build-arch-gce b/build-arch-gce index c2be75c..ac32289 100755 --- a/build-arch-gce +++ b/build-arch-gce @@ -23,7 +23,7 @@ fi # Setup cleanup trap to remove all temporary data. cleanup() { - echo "- Cleaning up." + echo '- Cleaning up.' [[ ${mount_dir:-} ]] && umount "$mount_dir" [[ ${loop_dev:-} ]] && losetup --detach "$loop_dev" [[ ${work_dir:-} ]] && rm -r "$work_dir" @@ -31,12 +31,12 @@ cleanup() { } trap cleanup EXIT -echo "- Creating an empty raw disk image." +echo '- Creating an empty raw disk image.' work_dir=$(mktemp --directory --tmpdir="$PWD" build-arch-gce.XXX) disk_raw=$work_dir/disk.raw truncate --size=10G -- "$disk_raw" -echo "- Setting up a loop device and partitioning the image." +echo '- Setting up a loop device and partitioning the image.' loop_dev=$(losetup --find --partscan --show -- "$disk_raw") sfdisk --quiet -- "$loop_dev" <<-'EOF' label:gpt @@ -44,16 +44,16 @@ sfdisk --quiet -- "$loop_dev" <<-'EOF' type=4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709,name=root EOF -echo "- Formatting the root partition." +echo '- Formatting the root partition.' root_dev=${loop_dev}p2 mkfs.ext4 -q -L root -- "$root_dev" -echo "- Mounting the root partition." +echo '- Mounting the root partition.' mount_dir=$work_dir/disk.mnt mkdir -- "$mount_dir" mount -- "$root_dev" "$mount_dir" -echo "- Installing Arch Linux." +echo '- Installing Arch Linux.' append_gce_repo() { gawk -i inplace ' /^\[gce\]$/ { found = 1 } { print } @@ -67,10 +67,10 @@ append_gce_repo() { cp /etc/pacman.conf "$work_dir" append_gce_repo "$work_dir/pacman.conf" pacstrap -G -M -C "$work_dir/pacman.conf" -- "$mount_dir" \ - base linux grub dhclient openssh sudo google-compute-engine growpart e2fsprogs + base linux grub e2fsprogs dhclient openssh sudo google-compute-engine growpart append_gce_repo "$mount_dir/etc/pacman.conf" -echo "- Configuring fstab." +echo '- Configuring fstab.' root_uuid=$(lsblk --noheadings --raw --output UUID -- "$root_dev") { printf '# LABEL=%s\n' root @@ -80,38 +80,38 @@ root_uuid=$(lsblk --noheadings --raw --output UUID -- "$root_dev") printf '\n\n' } >> "$mount_dir/etc/fstab" -echo "- Running additional setup in chroot." +echo '- Running additional setup in chroot.' arch-chroot -- "$mount_dir" /bin/bash -s -- "$loop_dev" <<-'EOS' set -eEuo pipefail trap 'echo "Error: \`$BASH_COMMAND\` exited with status $?"' ERR - echo "-- Configuring time." + echo '-- Configuring time.' ln -sf /usr/share/zoneinfo/UTC /etc/localtime gawk -i assert -i inplace ' /^#NTP=/ { $0 = "NTP=metadata.google.internal"; ++f } { print } END { assert(f == 1, "f == 1") }' /etc/systemd/timesyncd.conf - systemctl --quiet enable systemd-timesyncd + systemctl --quiet enable systemd-timesyncd.service - echo "-- Configuring locale." + echo '-- Configuring locale.' gawk -i assert -i inplace ' /^#en_US\.UTF-8 UTF-8\s*$/ { $0 = substr($0, 2); ++f } { print } END { assert(f == 1, "f == 1") }' /etc/locale.gen locale-gen echo 'LANG=en_US.UTF-8' > /etc/locale.conf - echo "-- Configuring journald." + echo '-- Configuring journald.' gawk -i assert -i inplace ' /^#ForwardToConsole=/ { $0 = "ForwardToConsole=yes"; ++f } { print } END { assert(f == 1, "f == 1") }' /etc/systemd/journald.conf - echo "-- Configuring ssh." + echo '-- Configuring ssh.' gawk -i assert -i inplace ' /^#PasswordAuthentication / { $0 = "PasswordAuthentication no"; ++f1 } /^#PermitRootLogin / { $0 = "PermitRootLogin no"; ++f2 } { print } END { assert(f1 * f2 == 1, "f == 1") }' /etc/ssh/sshd_config - systemctl --quiet enable sshd + systemctl --quiet enable sshd.service - echo "-- Configuring pacman." + echo '-- Configuring pacman.' curl --silent --show-error -o /etc/pacman.d/mirrorlist \ 'https://www.archlinux.org/mirrorlist/?country=all&ip_version=4&use_mirror_status=on' gawk -i assert -i inplace ' @@ -131,16 +131,12 @@ arch-chroot -- "$mount_dir" /bin/bash -s -- "$loop_dev" <<-'EOS' [Install] WantedBy=multi-user.target EOF - systemctl --quiet enable pacman-init + systemctl --quiet enable pacman-init.service - echo "-- Enabling other services." - systemctl --quiet enable \ - dhclient@eth0 growpartfs@- \ - google-accounts-daemon google-clock-skew-daemon google-instance-setup \ - google-network-daemon google-shutdown-scripts google-startup-scripts \ - google-oslogin-cache.timer + echo '-- Enabling other services.' + systemctl --quiet enable dhclient@eth0.service growpartfs@-.service - echo "-- Configuring initcpio." + echo '-- Configuring initcpio.' gawk -i assert -i inplace ' /^MODULES=/ { $0 = "MODULES=(virtio_pci virtio_scsi sd_mod ext4)"; ++f1 } /^BINARIES=/ { $0 = "BINARIES=(fsck fsck.ext4)"; ++f2 } @@ -153,7 +149,7 @@ arch-chroot -- "$mount_dir" /bin/bash -s -- "$loop_dev" <<-'EOS' rm /boot/initramfs-linux-fallback.img mkinitcpio --nocolor --preset linux - echo "-- Configuring grub." + echo '-- Configuring grub.' grub-install --target=i386-pc -- "$1" cat <<-'EOF' > /etc/default/grub # GRUB boot loader configuration @@ -165,13 +161,13 @@ arch-chroot -- "$mount_dir" /bin/bash -s -- "$loop_dev" <<-'EOS' grub-mkconfig -o /boot/grub/grub.cfg EOS -echo "- Cleaning up and finalizing the image." +echo '- Cleaning up and finalizing the image.' > "$mount_dir/etc/machine-id" rm -- "$mount_dir/var/log/pacman.log" umount -- "$mount_dir" unset mount_dir -echo "- Building the compressed image." +echo '- Building the compressed image.' disk_tar="arch-v$(date --utc +%Y%m%d).tar.gz" tar --sparse -czf "$work_dir/$disk_tar" --directory="$work_dir" disk.raw mv -- "$work_dir/$disk_tar" . |
