2023-11-08 05:07:45 +00:00
|
|
|
#!/hint/bash -euE
|
2024-01-25 21:00:47 +00:00
|
|
|
# Copyright (C) 2023-2024 Umorpha Systems
|
2023-11-08 05:07:45 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
|
|
|
|
load_module "$(dirname -- "${BASH_SOURCE[0]}")/base-net.sh"
|
|
|
|
|
|
|
|
packages+=(
|
|
|
|
# Things to put in the initramfs:
|
|
|
|
#bash # base
|
|
|
|
#coreutils # base
|
|
|
|
#grep # base
|
|
|
|
#systemd # base
|
|
|
|
#util-linux # base
|
|
|
|
arch-install-scripts
|
|
|
|
dosfstools
|
|
|
|
e2fsprogs
|
|
|
|
lvm2
|
|
|
|
pv
|
|
|
|
|
|
|
|
# Other
|
2023-11-10 18:09:51 +00:00
|
|
|
jq
|
2023-11-08 05:07:45 +00:00
|
|
|
libisoburn
|
2023-11-10 06:39:29 +00:00
|
|
|
mkinitcpio-netconf
|
2023-11-10 18:09:51 +00:00
|
|
|
syslinux
|
2023-11-08 05:07:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
pre_install+=(20:bootstrap:pre_install)
|
|
|
|
bootstrap:pre_install() {
|
|
|
|
local arg_mountpoint=$1
|
|
|
|
|
2023-12-07 05:06:07 +00:00
|
|
|
# The `keymap` mkinitcpio hook looks at locale.conf to
|
2023-12-07 05:19:27 +00:00
|
|
|
# determine whether to enable Unicode. This is important so
|
|
|
|
# that the output of `systemd-repart` is readable.
|
2023-12-07 05:06:07 +00:00
|
|
|
cat >"${arg_mountpoint}/etc/locale.conf" <<-EOF
|
|
|
|
LANG=en_US.UTF-8
|
|
|
|
EOF
|
|
|
|
|
2023-11-08 05:07:45 +00:00
|
|
|
install -Dm644 /dev/stdin "$arg_mountpoint/etc/mkinitcpio.conf.d/umorpha-bootstrap.conf" <<'EOF'
|
|
|
|
#!/hint/bash
|
|
|
|
# Copyright (C) 2023 Umorpha Systems
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
|
2023-12-08 03:55:03 +00:00
|
|
|
# Remove 'kms' and 'fsck' from HOOKS.
|
2023-11-11 03:53:50 +00:00
|
|
|
for ((i=0; i<${#HOOKS[@]}; i++)); do
|
2023-12-08 03:55:03 +00:00
|
|
|
if [[ ${HOOKS[i]} == kms ]] || [[ ${HOOKS[i]} == fsck ]]; then
|
2023-11-11 03:53:50 +00:00
|
|
|
HOOKS=("${HOOKS[@]:0:i}" "${HOOKS[@]:i+1}")
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2023-11-08 05:07:45 +00:00
|
|
|
# Insert 'umorpha-bootstrap' and 'lvm2' into HOOKS.
|
|
|
|
for ((i=0; i<${#HOOKS[@]}; i++)); do
|
|
|
|
if [[ ${HOOKS[i]} == filesystems ]]; then
|
2023-11-10 18:09:51 +00:00
|
|
|
HOOKS=("${HOOKS[@]:0:i}" umorpha-bootstrap lvm2 "${HOOKS[@]:i}" )
|
2023-11-08 05:07:45 +00:00
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
EOF
|
|
|
|
|
|
|
|
install -Dm644 /dev/stdin "$arg_mountpoint/usr/lib/initcpio/install/umorpha-bootstrap" <<'EOF'
|
|
|
|
#!/hint/bash
|
|
|
|
# Copyright (C) 2023 Umorpha Systems
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
|
|
|
|
build() {
|
|
|
|
add_module cdrom
|
|
|
|
add_binary eject
|
2023-11-10 18:09:51 +00:00
|
|
|
|
|
|
|
add_checked_modules /drivers/net/
|
|
|
|
add_binary ip
|
|
|
|
add_binary /usr/lib/initcpio/ipconfig /sbin/ipconfig
|
2023-11-10 06:39:29 +00:00
|
|
|
add_binary curl
|
2023-11-10 18:09:51 +00:00
|
|
|
add_file /etc/ssl/certs/ca-certificates.crt
|
|
|
|
add_binary jq
|
2024-01-22 21:48:37 +00:00
|
|
|
add_binary systemd-sysusers
|
2023-11-10 18:09:51 +00:00
|
|
|
|
2023-11-08 05:07:45 +00:00
|
|
|
add_runscript
|
|
|
|
|
|
|
|
add_binary umorpha-install
|
2023-12-06 23:46:11 +00:00
|
|
|
add_binary umorpha-mount
|
2023-11-08 05:07:45 +00:00
|
|
|
add_binary arch-chroot
|
|
|
|
add_binary bash
|
|
|
|
add_binary chroot
|
|
|
|
add_binary readlink
|
|
|
|
add_binary pv
|
|
|
|
add_binary systemd-repart
|
|
|
|
add_binary mkfs.ext4
|
|
|
|
add_binary mkfs.vfat
|
2023-12-05 15:58:44 +00:00
|
|
|
add_binary lvcreate
|
2023-12-08 03:55:03 +00:00
|
|
|
add_binary lvresize
|
2023-11-08 05:07:45 +00:00
|
|
|
add_binary pvcreate
|
2023-12-05 15:58:44 +00:00
|
|
|
add_binary pvs
|
2023-11-08 05:07:45 +00:00
|
|
|
add_binary vgchange
|
|
|
|
add_binary vgcreate
|
2023-11-09 08:20:55 +00:00
|
|
|
add_binary vgremove
|
2023-12-05 15:58:44 +00:00
|
|
|
add_binary vgs
|
2023-11-08 05:07:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
help() {
|
|
|
|
cat <<HELPEOF
|
|
|
|
This hook mounts the CD and runs 'umorpha-install' on the rootfs.img
|
|
|
|
found there.
|
|
|
|
|
|
|
|
Boot parameters:
|
|
|
|
- iso: the ISO disk device
|
|
|
|
HELPEOF
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
|
|
|
|
install -Dm644 /dev/stdin "$arg_mountpoint/usr/lib/initcpio/hooks/umorpha-bootstrap" <<'EOF'
|
2023-11-09 22:42:46 +00:00
|
|
|
#!/hint/ash
|
2023-11-08 05:07:45 +00:00
|
|
|
# Copyright (C) 2023 Umorpha Systems
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
|
|
|
|
# args: errmsg
|
|
|
|
umorpha_emergency_shell() {
|
|
|
|
run_hookfunctions 'run_emergencyhook' 'emergency hook' $EMERGENCYHOOKS
|
|
|
|
err "$*"
|
|
|
|
echo "You are now being dropped into an emergency shell."
|
|
|
|
launch_interactive_shell
|
|
|
|
msg "Trying to continue (this will most likely fail) ..."
|
|
|
|
}
|
|
|
|
|
|
|
|
# args: /path/to/newroot
|
|
|
|
bootstrap_mount_handler() {
|
|
|
|
local newroot="${1}"
|
|
|
|
|
|
|
|
local isodev
|
|
|
|
if ! isodev=$(resolve_device "$iso"); then
|
|
|
|
umorpha_emergency_shell "Failed to resolve device '$iso'"
|
|
|
|
fi
|
|
|
|
case "$iso" in
|
|
|
|
'UUID='* | 'LABEL='* | 'PARTUUID='* | 'PARTLABEL='*) : ;;
|
|
|
|
*) iso="$isodev" ;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
if ! mount --mkdir -o ro "$iso" /mnt/iso; then
|
|
|
|
umorpha_emergency_shell "Failed to mount iso '$iso'"
|
|
|
|
fi
|
|
|
|
|
2023-11-10 18:09:51 +00:00
|
|
|
msg "- [umorpha-install] ------------------------------------------------------------"
|
2023-11-08 05:07:45 +00:00
|
|
|
ln -s /tmp /var/tmp
|
|
|
|
mkdir /dev/pts /dev/shm
|
2023-11-13 04:52:10 +00:00
|
|
|
if ! umorpha-install bios /mnt/iso/rootfs.img /dev/vda; then
|
2023-11-08 05:07:45 +00:00
|
|
|
umorpha_emergency_shell "Failed to install rootfs to /dev/vda"
|
|
|
|
fi
|
|
|
|
eject /mnt/iso
|
2023-11-10 06:39:29 +00:00
|
|
|
|
2023-11-10 18:09:51 +00:00
|
|
|
msg "- [netconf] --------------------------------------------------------------------"
|
2023-11-11 03:53:50 +00:00
|
|
|
out=$(ipconfig ip=dhcp 2>&1)
|
2023-11-10 18:09:51 +00:00
|
|
|
echo "$out"
|
2023-11-11 03:53:50 +00:00
|
|
|
echo "$out" | grep -o 'dns[0-9]*\s*:\s*\S*' | sed 's/.*:/nameserver /' >/etc/resolv.conf
|
2023-11-10 18:09:51 +00:00
|
|
|
|
|
|
|
msg "- [cloud-init] -----------------------------------------------------------------"
|
|
|
|
if ! curl --fail-with-body -H 'Metadata-Token: cloudinit' http://169.254.169.254/v1.json >/vultr-v1.json; then
|
|
|
|
umorpha_emergency_shell "Failed to download '/vultr-v1.json'"
|
|
|
|
fi
|
|
|
|
if ! jq -r '.["user-data"]' </vultr-v1.json >/cloud-init-user-data; then
|
|
|
|
umorpha_emergency_shell "Failed to parse '/vultr-v1.json'"
|
|
|
|
fi
|
|
|
|
if [ "$(head -c2 /cloud-init-user-data)" != '#!' ]; then
|
|
|
|
echo ":: /cloud-init-user-data does not look like a script, skipping"
|
|
|
|
else
|
|
|
|
chmod 755 /cloud-init-user-data
|
|
|
|
if ! /cloud-init-user-data; then
|
2023-12-08 03:55:03 +00:00
|
|
|
umorpha_emergency_shell "Failed to run '/cloud-init-user-data'"
|
2023-11-10 18:09:51 +00:00
|
|
|
fi
|
2024-02-12 22:01:44 +00:00
|
|
|
echo
|
2023-11-10 18:09:51 +00:00
|
|
|
fi
|
|
|
|
|
2023-12-08 03:55:03 +00:00
|
|
|
# Right now, in the successful case, this won't run. The
|
2024-02-12 22:01:44 +00:00
|
|
|
# /cloud-init-user-data probably called the Vultr API to detach the
|
2023-12-08 03:55:03 +00:00
|
|
|
# ISO, which halts the VM before the HTTP response comes back.
|
|
|
|
# So that `curl` call never gets to finish, and this never gets to run.
|
2024-02-12 22:01:44 +00:00
|
|
|
#
|
|
|
|
# 2024-02-12: Looks like iso/detach no longer halts the VM?
|
2023-11-10 06:39:29 +00:00
|
|
|
|
2023-11-10 18:09:51 +00:00
|
|
|
msg "- [reboot] ---------------------------------------------------------------------"
|
2023-11-08 05:07:45 +00:00
|
|
|
reboot -f
|
|
|
|
}
|
|
|
|
|
|
|
|
run_hook() {
|
|
|
|
export mount_handler='bootstrap_mount_handler'
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
|
2023-12-06 23:46:11 +00:00
|
|
|
install -Dm755 "$(dirname -- "${BASH_SOURCE[0]}")/../bin/umorpha-install" "$arg_mountpoint/usr/bin/umorpha-install"
|
|
|
|
install -Dm755 "$(dirname -- "${BASH_SOURCE[0]}")/../bin/umorpha-mount" "$arg_mountpoint/usr/bin/umorpha-mount"
|
2023-11-08 05:07:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
post_install+=(20:bootstrap:post_install)
|
|
|
|
bootstrap:post_install() {
|
|
|
|
local arg_mountpoint=$1
|
|
|
|
|
|
|
|
chmod 644 -- "$arg_mountpoint"/boot/initramfs*.img
|
|
|
|
rm -rf "$arg_mountpoint"/boot/syslinux
|
|
|
|
|
|
|
|
mkdir -p -- "$arg_mountpoint/boot/isolinux"
|
|
|
|
ln -sr -t "$arg_mountpoint/boot/isolinux" -- \
|
|
|
|
"$arg_mountpoint/usr/lib/syslinux/bios/isolinux.bin" \
|
|
|
|
"$arg_mountpoint/usr/lib/syslinux/bios/ldlinux.c32"
|
|
|
|
install -Dm644 /dev/stdin "$arg_mountpoint/boot/isolinux/syslinux.cfg" <<-EOF
|
|
|
|
DEFAULT boot
|
|
|
|
LABEL boot
|
2024-01-24 06:33:42 +00:00
|
|
|
LINUX /vmlinuz-linux-libre
|
|
|
|
INITRD /initramfs-linux-libre.img
|
2023-11-08 05:07:45 +00:00
|
|
|
EOF
|
|
|
|
}
|