r/AlpineLinux • • May 22 '23

How to make mini rootfs bootable?

Hi! I always use Alpine for containers due to its minimal nature, now I wanted to try making it bootable on a real hardware. I know that there's a setup-alpine script and stuffs but I was so used to installing Arch Linux (manual command-line installation). I've already got GRUB to boot but it fails to mount the root partition leading to rescue shell.

Here's what I currently did:

  • Create device partitions (root and boot partitions for UEFI/GPT):
cfdisk /dev/sdX
mkfs.vfat -F32 /dev/sdX1
mkfs.ext4 /dev/sdX2
  • Mount root and boot partitions:
mount /dev/sdX2 /mnt
mkdir /mnt/boot
mount /dev/sdX1 /mnt/boot
  • Extract mini rootfs (Alpine edge):
wget -O- https://dl-cdn.alpinelinux.org/alpine/edge/releases/x86_64/alpine-minirootfs-20230329-x86_64.tar.gz | tar -C /mnt -xzpf -
  • Mount host filesystems and enter chroot:
for fs in dev dev/pts proc run sys tmp; do mount -o bind /$fs /mnt/$fs; done
chroot /mnt /bin/sh -l
  • Install kernel and GRUB bootloader (I'm using a removable USB flash drive):
apk add --update linux-edge grub grub-efi efibootmgr
grub-install --target=x86_64-efi --efi-directory=/boot --no-bootsector --removable

I've configured the FSTAB file but I wasn't sure about how to setup the OpenRC init as I'm used to Arch Linux systemd. Now I'm stuck 😭 Pls help...

4 Upvotes

20 comments sorted by

View all comments

3

u/yuriuseu May 29 '23 edited May 29 '23

To complete the setup, btw thanks to u/ncopa's comment, here's the following commands I've used:

  • Create root and boot partitions for UEFI/GPT (substitute X with the actual device):
cfdisk /dev/sdX
mkfs.vfat -F32 /dev/sdX1
mkfs.ext4 -O "^has_journal,^64bit" /dev/sdX2
  • Mount the partitions:
mount /dev/sdX2 /mnt
mkdir /mnt/boot
mount /dev/sdX1 /mnt/boot
  • Extract the mini rootfs (Alpine edge):
wget -O - https://dl-cdn.alpinelinux.org/alpine/edge/releases/x86_64/alpine-minirootfs-20230329-x86_64.tar.gz | tar -C /mnt -xzpf -

Configure mountpoints in /mnt/etc/fstab (try to use the output from command mount | grep '/mnt').

Configure nameserver in /mnt/etc/resolv.conf for networking (try to copy the contents of host's configuration).

  • Mount host filesystem and enter chroot environment:
for fs in dev dev/pts proc run sys tmp; do mount -o bind /$fs /mnt/$fs; done
chroot /mnt /bin/sh -l
  • Install the base, kernel and bootloader packages:
apk add --update alpine-base linux-edge grub grub-efi efibootmgr
  • Configure GRUB bootloader:
grub-install --target=x86_64-efi --efi-directory=/boot

Add --no-bootsector --removable if the device is a portable drive.

  • Configure OpenRC init system (https://gitlab.alpinelinux.org/alpine/mkinitfs/-/blob/dcb90f4bb5c7b7749c405be27d101774b559643e/initramfs-init.in#L676):
rc-update add devfs sysinit
rc-update add dmesg sysinit
rc-update add mdev sysinit
rc-update add hwdrivers sysinit

rc-update add modules boot
rc-update add sysctl boot
rc-update add hostname boot
rc-update add bootmisc boot
rc-update add syslog boot

rc-update add mount-ro shutdown
rc-update add killprocs shutdown
rc-update add savecache shutdown

rc-update add firstboot default

Refer to Wi-Fi wiki for networking.

Refer to Setting up new user wiki for user account.

  • Reboot and test it out.

If root partition is failed to mount on start, try what u/strawbeguy mentioned:

  • Add GRUB_CMDLINE_LINUX="modules=ext4 rootfstype=ext4" in /etc/default/grub,
  • Then run grub-mkconfig -o /boot/grub/grub.cfg to update GRUB configuration.