title: How to attach a disk image to QEMU containing root filesystem for linux kernel?
body: <p>I am able to launch the linux kernel on QEMU using <code>-kernel</code> and that's pretty straight-forward and simple.</p>
  <p>But now I am attempting something different. I am simulating the combination of UEFI + GRUB + Kernel.</p>
  <p>I have built OVMF and GRUB2 from source and also able to load the kernel image, but having difficulty creating a rootfs disk image and attaching it to qemu so that kernel detects it.</p>
  <p>This is how I prepared the rootfs.img for the kernel :</p>
  <pre><code>#Create an empty ext2 disk image of size 10M
  dd if=/dev/zero of=rootfs.img bs=1M count=10
  mke2fs -j rootfs.img
  
  #Populate the disk
  mkdir /mnt/rootfs
  mount -o loop rootfs.img /mnt/rootfs
  rsync -a busybox/_install/ /mnt/rootfs
  chown -R root:root /mnt/rootfs
  sync
  umount /mnt/rootfs
  </code></pre>
  <p>This is how my GRUB config looks like :</p>
  <pre><code>menuentry &quot;Linux Kernel Boot&quot; {
          set root=(hd0,msdos1)
          linux (hd0,msdos1)/bzImage-x86_64 root=/dev/sdb1 rootwait console=ttyS0,115200n8 console=tty0
  }
  </code></pre>
  <p>This is the command that I use to launch qemu :</p>
  <pre><code>qemu-system-x86_64 -m 2G -drive  if=pflash,format=raw,readonly,file=OVMF_CODE.fd -drive if=pflash,format=raw,file=OVMF_VARS.fd -net none -serial stdio -display none -hda fat:disk -drive file=rootfs.img,index=1
  </code></pre>
  <p>The FAT disk that I have attached as <code>hda</code> contains my GRUB and kernel image. I want a separate disk for rootfs and do not want to use this FAT for that purpose.</p>
  <p>The setup boots till kernel but kernel is not able to mount the disk <code>sdb1</code> and hence does not see the <code>init</code> in rootfs and hence I get a kernel panic. Below are the kernel logs :</p>
  <pre><code>[    1.030290] NET: Registered protocol family 17
  [    1.033540] ata1.01: configured for MWDMA2
  [    1.042119] Key type dns_resolver registered
  [    1.045150] scsi 0:0:0:0: Direct-Access     ATA      QEMU HARDDISK    2.5+ PQ: 0 ANSI: 5
  [    1.049509] sched_clock: Marking stable (1049235510, 0)-&gt;(1378961625, -329726115)
  [    1.058429] registered taskstats version 1
  [    1.060917] Loading compiled-in X.509 certificates
  [    1.066242] sd 0:0:0:0: [sda] 1032192 512-byte logical blocks: (528 MB/504 MiB)
  [    1.070386] sd 0:0:0:0: Attached scsi generic sg0 type 0
  [    1.074945] sd 0:0:0:0: [sda] Write Protect is off
  [    1.079585] scsi 0:0:1:0: Direct-Access     ATA      QEMU HARDDISK    2.5+ PQ: 0 ANSI: 5
  [    1.082483] sd 0:0:0:0: [sda] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA
  [    1.090820]   Magic number: 0:346:692
  [    1.094970] sd 0:0:1:0: [sdb] 20480 512-byte logical blocks: (10.5 MB/10.0 MiB)
  [    1.097740] sd 0:0:1:0: [sdb] Write Protect is off
  [    1.101097] sd 0:0:1:0: Attached scsi generic sg1 type 0
  [    1.108436] console [netcon0] enabled
  [    1.110947] netconsole: network logging started
  [    1.114830] sd 0:0:1:0: [sdb] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA
  [    1.122345] scsi 1:0:0:0: CD-ROM            QEMU     QEMU DVD-ROM     2.5+ PQ: 0 ANSI: 5
  [    1.138010]  sda: sda1
  [    1.142638] sr 1:0:0:0: [sr0] scsi3-mmc drive: 4x/4x cd/rw xa/form2 tray
  [    1.145513] cdrom: Uniform CD-ROM driver Revision: 3.20
  [    1.149749] cfg80211: Loading compiled-in X.509 certificates for regulatory database
  [    1.164130] sd 0:0:1:0: [sdb] Attached SCSI disk
  [    1.169575] sr 1:0:0:0: Attached scsi generic sg2 type 5
  [    1.174262] sd 0:0:0:0: [sda] Attached SCSI disk
  [    1.190198] cfg80211: Loaded X.509 cert 'sforshee: 00b28ddf47aef9cea7'
  [    1.194508] platform regulatory.0: Direct firmware load for regulatory.db failed with error -2
  [    1.197606] cfg80211: failed to load regulatory.db
  [    1.200837] ALSA device list:
  [    1.203626]   No soundcards found.
  [    1.531285] input: ImExPS/2 Generic Explorer Mouse as /devices/platform/i8042/serio1/input/input3
  [    1.537979] md: Waiting for all devices to be available before autodetect
  [    1.541357] md: If you don't use raid, use raid=noautodetect
  [    1.548186] md: Autodetecting RAID arrays.
  [    1.551815] md: autorun ...
  [    1.554889] md: ... autorun DONE.
  [    1.559995] VFS: Cannot open root device &quot;sdb1&quot; or unknown-block(8,17): error -6
  [    1.563196] Please append a correct &quot;root=&quot; boot option; here are the available partitions:
  [    1.566295] 0800          516096 sda
  [    1.566326]  driver: sd
  [    1.572371]   0801          516064 sda1 be1afdfa-01
  [    1.572397]
  [    1.577560] 0810           10240 sdb
  [    1.577573]  driver: sd
  [    1.582573] 0b00         1048575 sr0
  [    1.582585]  driver: sr
  [    1.587948] Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(8,17)
  [    1.590831] CPU: 0 PID: 1 Comm: swapper/0 Not tainted 4.15.3+ #2
  [    1.593384] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 0.0.0 02/06/2015
  [    1.596327] Call Trace:
  [    1.600043]  dump_stack+0x46/0x59
  [    1.602665]  panic+0xca/0x218
  [    1.604991]  ? printk+0x3e/0x46
  [    1.607197]  mount_block_root+0x174/0x223
  [    1.609463]  ? set_debug_rodata+0xc/0xc
  [    1.611851]  mount_root+0xfb/0x104
  [    1.614159]  prepare_namespace+0x130/0x166
  [    1.616415]  kernel_init_freeable+0x1c5/0x1d7
  [    1.618676]  ? rest_init+0xb0/0xb0
  [    1.621326]  kernel_init+0x5/0xf0
  [    1.623768]  ret_from_fork+0x35/0x40
  [    1.627035] Kernel Offset: 0x24800000 from 0xffffffff81000000 (relocation range: 0xffffffff80000000-0xffffffffbfffffff)
  [    1.632240] ---[ end Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(8,17) 
  </code></pre>
  <p>Can someone please guide the proper way to attach the disk in qemu which can solve this issue, or if I need to change something in the GRUB config?</p>
  <p><strong>EDIT</strong></p>
  <p>I created the disk image with a partition table this time as per the advice given by @stark.</p>
  <pre><code>$ dd if=/dev/zero of=rootfs.img bs=1M count=10
  
  #Using fdisk, created a partition of type Linux (83). This is how it looks now
  $ fdisk -l rootfs.img
  Disk rootfs.img: 10 MiB, 10485760 bytes, 20480 sectors
  Units: sectors of 1 * 512 = 512 bytes
  Sector size (logical/physical): 512 bytes / 512 bytes
  I/O size (minimum/optimal): 512 bytes / 512 bytes
  Disklabel type: dos
  Disk identifier: 0xc08ab9b0
  
  Device      Boot Start   End Sectors Size Id Type
  rootfs.img1       2048 20479   18432   9M 83 Linux
  
  $ sudo losetup -o 1048576 --sizelimit 10485248 /dev/loop23 rootfs.img
  $ sudo mkfs.ext2 /dev/loop23
  $ sudo mount /dev/loop23 /mnt/rootfs
  $ sudo cp -vrf busybox/_install/* /mnt/rootfs/
  $ sudo umount /mnt/rootfs
  $ sudo losetup -d /dev/loop23
  </code></pre>
  <p>Now again I launch the kernel with the same command as above, but this time the kernel just hangs :</p>
  <pre><code>[    1.593046] EXT4-fs (sdb1): couldn't mount as ext3 due to feature incompatibilities
  [    1.599792] EXT4-fs (sdb1): mounting ext2 file system using the ext4 subsystem
  [    1.614366] EXT4-fs (sdb1): mounted filesystem without journal. Opts: (null)
  [    1.618589] VFS: Mounted root (ext2 filesystem) readonly on device 8:17.
  [    1.626643] devtmpfs: mounted
  [    1.665158] Freeing unused kernel memory: 1216K
  [    1.667934] Write protecting the kernel read-only data: 18432k
  [    1.670772] tsc: Refined TSC clocksource calibration: 2207.976 MHz
  [    1.673349] clocksource: tsc: mask: 0xffffffffffffffff max_cycles: 0x1fd3a19a709, max_idle_ns: 440795259673 ns
  [    1.681441] Freeing unused kernel memory: 2004K
  [    1.693107] Freeing unused kernel memory: 1080K
  [    1.836603] modprobe (1041) used greatest stack depth: 14072 bytes left
  [    1.877586] rcS (1037) used greatest stack depth: 13984 bytes left
  [    2.683414] clocksource: Switched to clocksource ts
  [  333.025228] kworker/dying (178) used greatest stack depth: 13944 bytes left
  </code></pre>
  <p>No output beyond this. I have created <code>etc/init.d/rcS</code> in the rootfs.img which has below content :</p>
  <pre><code>#!/bin/sh
  mount -t sysfs none /sys
  mount -t configfs none /config
  mount -t devtmpfs none /dev
  
  echo &quot;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++&quot;
  echo &quot;+++++++++++++++++++++++++++++++ HELLOO ++++++++++++++++++++++++++++++&quot;
  echo &quot;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++&quot;
  </code></pre>
  <p><strong>EDIT2</strong></p>
  <p>I removed <code>console=tty0</code> from the GRUB config and the kernel booted to shell. But I am not sure why it caused hang in this virtual environment?</p>
  <p>I am thinking that since <code>tty0</code> represent current display and since <code>-display none</code> is set for qemu, this mixup creates some conflict and hence this issue...</p>
  <p>But please someone give a correct and concrete conclusion on this issue so that this thread becomes useful to someone in future.</p>