chamesle-boot (6330B)
1 #!/bin/sh 2 # chamesle-boot [-b limine|grub] [DIR]: makes the system in DIR (/mnt) bootable. writes the boot entries from what is 3 # mounted there, installs the bootloader (limine unless -b grub) into it and puts it on the disk for UEFI, and for BIOS 4 # when the disk has room for it. if /boot is not FAT, it prints an entry for the GRUB of the other system instead. 5 # chamesle-boot 6 # chamesle-boot -b grub /mnt 7 die() { echo "chamesle-boot: $*" >&2; exit 1; } 8 usage() { echo "usage: chamesle-boot [-b limine|grub] [DIR]" >&2; exit 2; } 9 loader=limine 10 export MAG_YES=1 11 while getopts b: o; do 12 case $o in 13 b) loader=$OPTARG;; 14 *) usage;; 15 esac 16 done 17 shift $((OPTIND - 1)) 18 case $loader in limine|grub) ;; *) die "-b is limine or grub";; esac 19 [ $# -le 1 ] || usage 20 [ "$(id -u)" = 0 ] || die "needs root" 21 t=${1:-/mnt} 22 t=${t%/} 23 # / is the running system: mounts and paths go by "/" 24 top=${t:-/} 25 [ -x "$t/sbin/init" ] || die "$t has no system in it, run chamesle-bootstrap first" 26 27 inroot() { if [ -z "$t" ]; then "$@"; else arch-chroot "$t" "$@"; fi; } 28 mnt() { awk -v m="$1" '$2 == m { print $1, $3, $4 }' /proc/mounts | tail -1; } 29 set -- $(mnt "$top") 30 [ $# = 3 ] || die "nothing is mounted on $top" 31 rootdev=$1 rootfs=$2 rootopts=$3 32 set -- $(mnt "$t/boot") 33 bootdev=$1 bootfs=$2 34 35 # the glibc Chamesle is Artix underneath: mkinitcpio makes initramfs-linux.img, and grub is one package 36 glibc= 37 [ -e "$t/usr/lib/libc.so.6" ] && glibc=1 38 kernels= 39 for k in "$t"/boot/vmlinuz-*; do [ -e "$k" ] && kernels="$kernels ${k##*/vmlinuz-}"; done 40 [ -n "$kernels" ] || die "no kernel in $t/boot, install a kernel first (linux-lts)" 41 img() { [ -e "$t/boot/initramfs-$1.img" ] && echo "initramfs-$1.img" || echo "initramfs-$1"; } 42 43 # feature NAME PACKAGE...: puts NAME in the initramfs, with the packages it needs 44 feature() { 45 if [ -n "$glibc" ]; then 46 shift 47 inroot mag install "$@" || die "mag install $* failed" 48 return 49 fi 50 conf=$t/etc/mkinitfs/mkinitfs.conf 51 grep -qE "^features=\"([^\"]* )?$1( |\")" "$conf" && return 52 echo "adding $1 to the initramfs" 53 f=$1 54 shift 55 inroot mag install "$@" || die "mag install $* failed" 56 sed -i "/^features=/s/\"\$/ $f\"/" "$conf" 57 for m in "$t"/lib/modules/*; do inroot mkinitfs "${m##*/}" || die "mkinitfs failed"; done 58 } 59 60 if [ "$rootfs" = zfs ] && [ -n "$glibc" ]; then 61 die "the glibc Chamesle has no ZFS: neither Artix nor Arch carries it" 62 elif [ "$rootfs" = zfs ]; then 63 cmdline="root=ZFS=$rootdev rootfstype=zfs" 64 feature zfs zfs zfs-openrc $(for k in $kernels; do echo zfs-$k; done) 65 for s in zfs-import zfs-mount; do [ -e "$t/etc/runlevels/sysinit/$s" ] || ln -s /etc/init.d/$s "$t/etc/runlevels/sysinit/$s"; done 66 else 67 uuid=$(blkid -s UUID -o value "$rootdev") || die "no UUID for $rootdev" 68 cmdline="root=UUID=$uuid modules=sd-mod,usb-storage,nvme,$rootfs rootfstype=$rootfs" 69 [ -n "$glibc" ] && cmdline="root=UUID=$uuid rw rootfstype=$rootfs" 70 if [ "$rootfs" = btrfs ]; then 71 sub=$(echo "$rootopts" | tr , '\n' | sed -n 's|^subvol=/*||p') 72 [ -n "$sub" ] && cmdline="$cmdline rootflags=subvol=$sub" 73 feature btrfs btrfs-progs 74 fi 75 fi 76 77 if [ "$bootfs" != vfat ]; then 78 [ -n "$bootdev" ] && where="$bootdev" || where="$rootdev" 79 echo "/boot is not FAT, so limine cannot boot it. For the GRUB of the system already on the machine, in its" 80 echo "/etc/grub.d/40_custom, then grub-mkconfig -o /boot/grub/grub.cfg:" 81 echo 82 p=/boot 83 [ -n "$bootdev" ] && p= 84 for k in $kernels; do 85 cat <<G 86 menuentry 'Chamesle ($k)' { 87 search --no-floppy --fs-uuid --set=root $(blkid -s UUID -o value "$where") 88 linux $p/vmlinuz-$k $cmdline quiet 89 initrd $p/$(img $k) 90 } 91 G 92 done 93 exit 0 94 fi 95 96 disk=/dev/$(lsblk -no PKNAME "$bootdev" | head -1) 97 bios=1 98 if [ "$(lsblk -dno PTTYPE "$disk")" = gpt ] && ! lsblk -no PARTTYPE "$disk" | grep -qi 21686148-6449-6e6f-744e-656564454649; then bios=; fi 99 [ -z "$bios" ] && [ ! -d /sys/firmware/efi ] && die "this machine boots by BIOS and $disk is GPT with no BIOS boot partition: add a 1M one in cfdisk (type BIOS boot), or use a dos label" 100 101 if [ $loader = grub ]; then 102 case $(cat "$t/etc/mag/arch" 2>/dev/null) in x86) efi=i386-efi;; *) efi=x86_64-efi;; esac 103 if [ -n "$glibc" ]; then inroot mag install grub >/dev/null || die "mag install grub failed" 104 else inroot mag install grub grub-efi ${bios:+grub-bios} >/dev/null || die "mag install grub failed"; fi 105 inroot grub-install --target=$efi --efi-directory=/boot --boot-directory=/boot --removable --no-nvram >/dev/null || die "grub-install for UEFI failed" 106 echo "UEFI: GRUB in EFI/BOOT on $bootdev" 107 if [ -n "$bios" ]; then 108 inroot grub-install --target=i386-pc --boot-directory=/boot "$disk" >/dev/null || die "grub-install for BIOS failed" 109 echo "BIOS: GRUB is on $disk" 110 else 111 echo "BIOS: $disk is GPT with no BIOS boot partition, so it boots by UEFI only" 112 fi 113 c=$t/boot/grub/grub.cfg 114 [ -e "$c" ] && mv "$c" "$c.old" && echo "the old grub.cfg is grub.cfg.old" 115 { 116 echo "set timeout=3" 117 for k in $kernels; do 118 printf "\nmenuentry 'Chamesle (%s)' {\n\tsearch --no-floppy --fs-uuid --set=root %s\n" "$k" "$(blkid -s UUID -o value "$bootdev")" 119 printf '\tlinux /vmlinuz-%s %s quiet\n\tinitrd /%s\n}\n' "$k" "$cmdline" "$(img $k)" 120 done 121 } > "$c" 122 cat "$c" 123 exit 0 124 fi 125 126 [ -e "$t/usr/share/limine/limine-bios.sys" ] || inroot mag install limine || die "mag install limine failed" 127 128 c=$t/boot/limine.conf 129 [ -e "$c" ] && mv "$c" "$c.old" && echo "the old limine.conf is limine.conf.old" 130 { 131 echo "timeout: 3" 132 for k in $kernels; do 133 printf '\n/Chamesle (%s)\n protocol: linux\n path: boot():/vmlinuz-%s\n' "$k" "$k" 134 printf ' module_path: boot():/%s\n cmdline: %s quiet\n' "$(img $k)" "$cmdline" 135 done 136 } > "$c" 137 cat "$c" 138 echo 139 140 mkdir -p "$t/boot/EFI/BOOT" 141 for e in BOOTX64.EFI BOOTIA32.EFI; do [ -e "$t/usr/share/limine/$e" ] && cp "$t/usr/share/limine/$e" "$t/boot/EFI/BOOT/"; done 142 echo "UEFI: EFI/BOOT on $bootdev" 143 144 cp "$t/usr/share/limine/limine-bios.sys" "$t/boot/" 145 if [ -z "$bios" ]; then 146 echo "BIOS: $disk is GPT with no BIOS boot partition, so it boots by UEFI only" 147 else 148 # on GPT, stage 2 goes in the BIOS boot partition: limine wants its number 149 n=$(lsblk -lno NAME,PARTTYPE "$disk" | awk 'tolower($2) == "21686148-6449-6e6f-744e-656564454649" { print $1 }' | head -n 1 | sed 's/.*[^0-9]//') 150 inroot limine bios-install "$disk" $n || die "limine bios-install $disk $n failed" 151 echo "BIOS: limine is on $disk" 152 fi