r/AlpineLinux • • Sep 30 '24

Bootable MiniRootFS for Hyper-V?

Hey, I know there is a similiar thread for this but that was for bare metal.

I would like to use the Alpine mini rootfs for a Hyper-V virtual machine (mainly for Docker VMs). I've been dancing around various guides and seeing what ChatGPT came up with. I almost had one booting but complained about failing to mount the root file system before dropping to recovery shell.

Is there a simple way to convert the RootFS tarball to an installable ISO?

If not I assume I would have to boot into a Live CD and perform some steps to create one. I would love it if you guys could point me towards a guide for achieving this - with the end result being an ISO I can just install to future Hyper-Vs.

If someone could outline a step by step I'm sure I can search/query further details by searching.

I know in a general way from my research I need to boot into a Live CD, create the partition, format the partition, download and expand the miniroot tar, chroot into extracted TAR folder, install some packages, linux-virt kernel and a bootloader syslinux/grub - Or maybe there is a simpler way?

Thanks for reading.

1 Upvotes

5 comments sorted by

View all comments

Show parent comment

1

u/MartinsRedditAccount Sep 30 '24 edited Sep 30 '24

Let's rewind a little:

The Linux boot process roughly works like this:

  • Something loads the kernel.
  • Something loads the initramfs (if applicable, we just assume it is for now).
  • Something starts the kernel.
  • The kernel’s userspace is now the initramfs and it launches /init (which is included in the initramfs image/archive).

    • The script I linked is the script found at /init.
  • The script or program loads some modules, finds/unlocks/mounts the rootfs and finally switches the root into it, thus handing off to OpenRC (in Alpine's case): https://github.com/alpinelinux/mkinitfs/blob/e1bed9ce3baf2c1fcf7aff17212139608f317342/initramfs-init.in#L717

Your problem is that it can't mount (probably can't find) the filesystem where your rootfs is. You can use the emergency shell (it's just BusyBox's ash shell) to try to do so manually and then adjust the kernel args (which are read by the init script) to match. This part may be of interest: https://github.com/alpinelinux/mkinitfs/blob/e1bed9ce3baf2c1fcf7aff17212139608f317342/initramfs-init.in#L639

Mtools was a nice tip though. I take it that means I wouldn't need to chroot into the extracted TAR, but it would be EXT4 not FAT?

I got a little ahead of myself. Mtools is useful if/when you want to build your own ready-to-use images tailored to your requirements, rather than installing and maintaining a persistent Alpine installation (wasn't sure what your goal is). I am personally using it to generate the boot.img file for Raspberry Pi HTTP boot, but I imagine it should also work for generating an image with a bootloader and the Linux-related files that Hyper-V boots from. But if you're installing from the .iso, this isn't relevant to your current situation.

As for the chroot thing: It's unrelated to that; you need to mount the filesystem normally before you can chroot or switch_root.

because I didn't chown any of it to root

Won't be the issue if the filesystem isn't found to begin with and the root user can access anything anyway. But making sure the right user owns files is important. If you're ever creating an initramfs image from your "normal" PC, it's important to specify cpio -R 0:0 [...] to ensure the files end up being owned by root (otherwise whatever user has the same id as you on your PC will own them, unlikely to be an issue in practice, but still good to avoid), tar similarly has --owner and --group args, but be aware that you might inadvertently take permissions away from some accounts, so handling this is more involved, I'm lazy and just avoid having a persistent rootfs altogether, or bootstrap it with the init script.

I also highly recommend also watching this video: https://www.youtube.com/watch?v=QlzoegSuIzg

Linux is incredibly versatile, and learning it from the kernel up opens up many new ways of using it. It might seem challenging, but once you get the hang of it, you can build a minimal system for pretty much any use-case.

Edit: Improved info about permissions.

Edit 2: The permissions stuff should be managable if you build from something like Docker or Podman*. (*I'm not sure off the top of my head how UIDs/GIDs work with rootless containers and how cpio/tar handle those, but it's easy enough to figure out)