Friday, December 27, 2019

Boot Archlinux CD iso from local disk with GRUB

I use btrfs snapshots to backup my system. In order to restore to previous snapshot I had to boot live CD and do the restore. But as live CD is not always at hand, it's better to have an ISO stored locally on your disk and boot directly from it.
I use GRUB2 and archlinux, for which I've created the configuration.



The configuration is fairly simple. To automate it's creation I've prepared custom grub conf, which will generate grub menu entries for me.

File: /etc/grub.d/40_archiso (permissions 755)
#!/bin/sh -e
SUBVOL=/system
ISOBASE=/opt/arch_iso
ISOLIST=$(ls $ISOBASE)

for ISO in $ISOLIST; do
        echo "Adding archlinux $ISO" >&2
        ISOLABEL=$(echo $ISO | sed -e 's/.*-\([0-9]\+\).\([0-9]\+\).*/ARCH_\1\2/')
cat << EOF
menuentry "$ISO" {
        insmod loopback
        set root=(hd0,gpt3)$SUBVOL$ISOBASE

        set iso=\$root/$ISO
        loopback loop \$iso
        linux (loop)/arch/boot/x86_64/vmlinuz archisobasedir=arch archisolabel=$ISOLABEL
        initrd (loop)/arch/boot/intel_ucode.img
        initrd (loop)/arch/boot/x86_64/archiso.img
}
EOF
done
Running grub-mkconfig -o /boot/grub/grub.cfg will recreate grub configuration and add following entry/entries:
### BEGIN /etc/grub.d/40_archiso ###
menuentry "archlinux-2019.12.01-x86_64.iso" {
        insmod loopback
        set root=(hd0,gpt3)/system/opt/arch_iso # system is my subvolume, opt/arch_iso is the ISO download location

        set iso=$root/archlinux-2019.12.01-x86_64.iso
        loopback loop $iso   # mount the iso
        linux (loop)/arch/boot/x86_64/vmlinuz archisobasedir=arch archisolabel=ARCH_201912   # isolabel is generated from the iso name
        initrd (loop)/arch/boot/intel_ucode.img   # NOTE: if you have AMD CPU use amd_ucode,img
        initrd (loop)/arch/boot/x86_64/archiso.img
}
### END /etc/grub.d/40_archiso ###

NOTE: if you store ISO images on btrfs filesystem, don't forget to chattr +C /opt/arch_iso folder and after that copy the ISO image to this folder. To check file attributes do lsattr /opt/arch_iso.
It is important to copy (not move) the ISO!

No comments:

Post a Comment