2023-08-13 23:58:13 -05:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
#vim: set ft=bash:
|
|
|
|
|
set -ex
|
|
|
|
|
|
|
|
|
|
port="${1}" # HTTP port that packer is running on
|
2023-08-15 11:23:29 -05:00
|
|
|
host="${2}"
|
2023-08-13 23:58:13 -05:00
|
|
|
|
|
|
|
|
if [ -e /dev/vda ]; then
|
|
|
|
|
disk="/dev/vda"
|
|
|
|
|
elif [ -e /dev/sda ]; then
|
|
|
|
|
disk="/dev/sda"
|
|
|
|
|
else
|
|
|
|
|
exit 1 # Need to teach the script which device to use in this case
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# The sed bit is just to strip out the extra fluff, like comments
|
|
|
|
|
sed -e 's/\s*\([\+0-9a-zA-Z]*\).*/\1/' << EOF | fdisk ${disk}
|
2023-12-30 05:37:32 +00:00
|
|
|
g # create new GPT partition table
|
2023-08-13 23:58:13 -05:00
|
|
|
n # new
|
2023-12-30 05:37:32 +00:00
|
|
|
# default partition number
|
2023-08-13 23:58:13 -05:00
|
|
|
# default start
|
2023-12-30 05:37:32 +00:00
|
|
|
+512M # size
|
|
|
|
|
t # set type
|
|
|
|
|
1 # EFI system partition
|
|
|
|
|
n # second partition
|
|
|
|
|
# default partition number
|
|
|
|
|
# start at first free sector
|
2023-08-13 23:58:13 -05:00
|
|
|
# default to full disk
|
|
|
|
|
p # print, for debugging
|
|
|
|
|
w # write the partition table and exit
|
|
|
|
|
EOF
|
2023-12-30 05:37:32 +00:00
|
|
|
|
|
|
|
|
sleep 10 # Let me at least read it!
|
2023-08-13 23:58:13 -05:00
|
|
|
#t # set partition type
|
|
|
|
|
#1 # on partition 1
|
|
|
|
|
#4 # BIOS boot
|
|
|
|
|
|
|
|
|
|
# Format and mount partitions
|
2023-12-30 05:37:32 +00:00
|
|
|
mkfs.vfat "${disk}1"
|
|
|
|
|
mkfs.ext4 "${disk}2"
|
2023-08-13 23:58:13 -05:00
|
|
|
|
2023-12-30 05:37:32 +00:00
|
|
|
mount "${disk}2" /mnt
|
|
|
|
|
mkdir -p /mnt/boot
|
|
|
|
|
mount "${disk}1" /mnt/boot
|
2023-08-13 23:58:13 -05:00
|
|
|
|
|
|
|
|
# Bootstrap system
|
2023-12-30 05:37:32 +00:00
|
|
|
virt="$(systemd-detect-virt)"
|
2023-08-13 23:58:13 -05:00
|
|
|
nixos-generate-config --root /mnt
|
2023-08-15 11:23:29 -05:00
|
|
|
curl -o /mnt/etc/nixos/configuration.nix "http://${host}:${port}/configuration.nix"
|
2023-11-28 10:30:17 -06:00
|
|
|
curl -o /mnt/etc/nixos/flake.nix "http://${host}:${port}/flake.nix"
|
2023-12-30 05:37:32 +00:00
|
|
|
curl -o /mnt/etc/nixos/guest-agent.nix "http://${host}:${port}/ga-${virt}.nix"
|
2023-08-13 23:58:13 -05:00
|
|
|
sed -i -E -e "s,@BOOT_DEVICE@,${disk}," /mnt/etc/nixos/configuration.nix
|
2023-12-30 05:37:32 +00:00
|
|
|
|
2023-11-28 10:30:17 -06:00
|
|
|
nixos-install --no-root-password --flake "/mnt/etc/nixos/#vagrant"
|
2023-08-13 23:58:13 -05:00
|
|
|
|
|
|
|
|
# Run stuff in the chroot
|
2023-08-15 11:23:29 -05:00
|
|
|
curl -o /mnt/root/in-chroot.sh "http://${host}:${port}/in-chroot.sh"
|
2023-08-13 23:58:13 -05:00
|
|
|
nixos-enter --root /mnt -c 'bash /root/in-chroot.sh'
|
|
|
|
|
rm /mnt/root/in-chroot.sh
|
|
|
|
|
|
2023-12-30 05:37:32 +00:00
|
|
|
umount /mnt/boot
|
|
|
|
|
umount /mnt
|
|
|
|
|
efibootmgr --bootnext "$(efibootmgr | grep Linux | awk '{print $1}' | sed -E 's/[^0-9]//g')"
|
2024-11-18 23:42:28 -06:00
|
|
|
reboot
|