Running QEMU with a root file system directory instead of disk image

10,948

Solution 1

If your rootfs is not too big you can use an initd. Use the -initrd option to qemu and provide a (compressed) cpio file with the rootfs. So if you have a tar file you must unpack it and create a cpio instead. You must use -H newc format for cpio. Example;

mkdir /tmp/rootfs
cd /tmp/rootfs
tar xf /path/to/rootfs.tar
find | cpio --quiet -o -H newc | bzip2 -c > /tmp/rootfs.cpio

A nice thing is that you don't need root or sudo for this.

Solution 2

You can write your filesystem in a USB drive, then attach that with QEMU virtual machine.

Than mount that as your root filesystem with kernel options at GRUB

  1. Activate boot menu in virtualmachine
  2. Attach the external drive in which your filesystem including kernel is present.
  3. If your filesystem has kernel and grub it's all fine. Else attach any linux live CD also with the virtual machine. Edit the GRUB command line and use your kernel and root filesystem.

Say your pendrive is /dev/sdB (considering you've attached a hard disk image also and that is /dev/sdA, anyway you can find it) your grub command shall look like

kernel (hd1,gpt0)/location_of-kernel root=/dev/sdB
initramfs mention_if_you_have
boot

Replace gpt0 with proper partition and /dev/sdB with proper letter live /dev/sdb1 or /dev/sdb (in case the USB has no partition)

Share:
10,948

Related videos on Youtube

e.jahandar
Author by

e.jahandar

Updated on September 18, 2022

Comments

  • e.jahandar
    e.jahandar over 1 year

    In order to test the root file system i need to run QEMU with created disk image as root file system, the QEMU accepts -kernel which can be used for specifying kernel directly without deploying it inside root file system, however i need similar feature for root file system to specify path of extracted root file system instead of creating a disk image and use it with -hda or -usbdisk options.

    is it possible at all with QEMU alone?! Or with third-party tools (like virtual disk image emulator)

    My goal is to test a tar archive of root file system without creating disk image

    • jpf
      jpf about 6 years
      Maybe you could try to setup the rootfs as a p9 shared folder, then mount the shared folder as rootfs in your initramfs?
    • firo
      firo about 6 years
      Try to boot with NFS.
    • Mio Rin
      Mio Rin over 5 years
      Is there a reason why chroot or systemd-nspawn (or other containerization) can not be used for this?
    • Darren Ng
      Darren Ng over 2 years
      consider 9p? - 9p - 9p_fstab - virtfs_kvm - 9p_vs_nfs