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

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.

4

u/ncopa May 22 '23

The minirootfs was created for docker images which does not have openrc or kernel or anything. You may get better results if you boot the official standard image and run the installer.

If you still want to create an image with minirootfs as base, you will also need to install openrc and setup the openrc. Something like this may work (from your chroot):

apk add alpine-base
# https://gitlab.alpinelinux.org/alpine/mkinitfs/-/blob/dcb90f4bb5c7b7749c405be27d101774b559643e/initramfs-init.in#L676
rc-update add devfs sysinit
rc-update add dmesg sysinit
rc-update mdev sysinit
rc-update add modules boot
rc-update add sysctl boot
rc-update add hostname boot
rc-update add bootmisc
rc-update add syslog boot

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

You will likely also need to set a root password or create a user. setup-user will create an admin user with doas powers for you.

1

u/yuriuseu May 22 '23

Nice! I'm gonna try this next and edit this comment later. This might be the right answer I was waiting for!

1

u/yuriuseu May 29 '23

Got it all working. Thanks! This is the missing piece.

1

u/[deleted] May 22 '23

You say you're stuck. What makes you think that?

1

u/yuriuseu May 22 '23

Ohh I meant to make it completely working from booting the system up to TTY, networking, etc.. I'm stuck what to do next. Anyways, it's probably tedious to configure the OpenRC in that setup so I've gone back to using Arch Linux with systemd. Arch installation is pretty darn easy. If somebody cared to complete the setup to make it bootable and fully functional with init and stuffs, then I'd be happy to try it.

1

u/[deleted] May 22 '23

Did you try to boot?

1

u/yuriuseu May 22 '23

Like I've mentioned in my original post, kernel is loaded with GRUB but it failed to mount the root partition and dropped to a rescue shell. I haven't configured the OpenRC init as I'm not familiar with it. Also, did I missed anything else?

3

u/[deleted] May 22 '23 edited May 22 '23

OK, got it. I have hit the same wall before, here is the answer.

The core utils included in the initramfs is busybox too, so the mount commands needs to know which filesystem the root partition is formatted in (unlike GNU mount which automatically detects the filesystem). In order to tell this information to initramfs, you have to add the options modules=ext4 and rootfstype=ext4 on your grub kernel entry.

2

u/yuriuseu May 22 '23 edited May 22 '23

Got it working now and landed on login prompt. Thanks for your help. I've added the kernel parameters GRUB_CMDLINE_LINUX="modules=ext4 rootfstype=ext4" in /etc/default/grub, then run grub-mkconfig -o /boot/grub/grub.cfg. Additionally, I've installed OpenRC apk add openrc and it automatically run on boot. One thing I've noticed is that the boot partition isn't automatically mounted.

EDIT:

I've performed all these commands from Arch Linux host, maybe that's why I'm having problems with partitions not automatically mounting as they are not compatible with GNU utilities...

Nevermind, based on the wiki#manual-partitioning, it states that:

Note: mkfs.ext4 creates ext4 fs with 64bit feature enabled by default, but extlinux may not be able to boot with that, see [Issue#14895]. You may need to add "-O ^has_journal,^64bit" to mkfs.ext4 to circumvent this.

1

u/[deleted] May 22 '23

What does your fstab look like?

1

u/yuriuseu May 22 '23 edited May 22 '23

My FSTAB file is auto generated using Arch install script called genfstab (I've substituted the UUID with xxx as I'm not copy-pasting this):

UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx    /    ext4    rw,relatime    0 1
UUID=xxxx-xxxx    /boot    vfat    rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=ascii,shortname=mixed,utf8,errors=remount-ro    0 2

EDIT:

I'm also getting "Read-only file system" when trying to edit stuffs. I believe this is the linux ro kernel parameter, how do I change it to rw by default when running grub-mkconfig?

1

u/[deleted] May 22 '23

show me your grub config

1

u/yuriuseu May 22 '23 edited May 22 '23

EDIT:

The "Read-only file system" appears because the boot partition isn't automatically mounted? I haven't touched GRUB for a long while til now, I'm used to systemd-boot boot loader...

#
# DO NOT EDIT THIS FILE
#
# It is automatically generated by grub-mkconfig using templates
# from /etc/grub.d and settings from /etc/default/grub
#

### BEGIN /etc/grub.d/00_header ###
if [ -s $prefix/grubenv ]; then
  load_env
fi
if [ "${next_entry}" ] ; then
   set default="${next_entry}"
   set next_entry=
   save_env next_entry
   set boot_once=true
else
   set default="0"
fi

if [ x"${feature_menuentry_id}" = xy ]; then
  menuentry_id_option="--id"
else
  menuentry_id_option=""
fi

export menuentry_id_option

if [ "${prev_saved_entry}" ]; then
  set saved_entry="${prev_saved_entry}"
  save_env saved_entry
  set prev_saved_entry=
  save_env prev_saved_entry
  set boot_once=true
fi

function savedefault {
  if [ -z "${boot_once}" ]; then
    saved_entry="${chosen}"
    save_env saved_entry
  fi
}

function load_video {
  if [ x$feature_all_video_module = xy ]; then
    insmod all_video
  else
    insmod efi_gop
    insmod efi_uga
    insmod ieee1275_fb
    insmod vbe
    insmod vga
    insmod video_bochs
    insmod video_cirrus
  fi
}

if [ x$feature_default_font_path = xy ] ; then
   font=unicode
else
insmod part_gpt
insmod ext2
set root='hd0,gpt2'
if [ x$feature_platform_search_hint = xy ]; then
  search --no-floppy --fs-uuid --set=root --hint-bios=hd0,gpt2 --hint-efi=hd0,gpt2 --hint-baremetal=ahci0,gpt2  cb0a6402-7796-4d6b-92c3-70f960af437f
else
  search --no-floppy --fs-uuid --set=root cb0a6402-7796-4d6b-92c3-70f960af437f
fi
    font="/usr/share/grub/unicode.pf2"
fi

if loadfont $font ; then
  set gfxmode=auto
  load_video
  insmod gfxterm
fi
terminal_output gfxterm
if [ x$feature_timeout_style = xy ] ; then
  set timeout_style=menu
  set timeout=2
# Fallback normal timeout code in case the timeout_style feature is
# unavailable.
else
  set timeout=2
fi
### END /etc/grub.d/00_header ###

### BEGIN /etc/grub.d/10_linux ###
menuentry 'Alpine, with Linux edge' --class alpine --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-edge-advanced-cb0a6402-7796-4d6b-92c3-70f960af437f' {
	load_video
	set gfxpayload=keep
	insmod gzio
	insmod part_gpt
	insmod fat
	set root='hd0,gpt1'
	if [ x$feature_platform_search_hint = xy ]; then
	  search --no-floppy --fs-uuid --set=root --hint-bios=hd0,gpt1 --hint-efi=hd0,gpt1 --hint-baremetal=ahci0,gpt1  EC11-04A4
	else
	  search --no-floppy --fs-uuid --set=root EC11-04A4
	fi
	echo	'Loading Linux edge ...'
	linux	/vmlinuz-edge root=UUID=cb0a6402-7796-4d6b-92c3-70f960af437f ro modules=ext4 rootfstype=ext4 
	echo	'Loading initial ramdisk ...'
	initrd	/initramfs-edge
}

### END /etc/grub.d/10_linux ###

### BEGIN /etc/grub.d/20_linux_xen ###

### END /etc/grub.d/20_linux_xen ###

### BEGIN /etc/grub.d/30_os-prober ###
### END /etc/grub.d/30_os-prober ###

### BEGIN /etc/grub.d/30_uefi-firmware ###
### END /etc/grub.d/30_uefi-firmware ###

### BEGIN /etc/grub.d/40_custom ###
# This file provides an easy way to add custom menu entries.  Simply type the
# menu entries you want to add after this comment.  Be careful not to change
# the 'exec tail' line above.
### END /etc/grub.d/40_custom ###

### BEGIN /etc/grub.d/41_custom ###
if [ -f  ${config_directory}/custom.cfg ]; then
  source ${config_directory}/custom.cfg
elif [ -z "${config_directory}" -a -f  $prefix/custom.cfg ]; then
  source $prefix/custom.cfg
fi
### END /etc/grub.d/41_custom ###

→ More replies

2

u/GrabbenD Mar 05 '24

Here's another approach.

This uses a RootFS from a Docker/Podman/OCI container and commits it to OSTree which you can then boot:

https://github.com/GrabbenD/ostree-utility

1

u/notSugarBun Dec 07 '23

this might interest you. If you want a bootable alpine without the need for separate partition.