Running disk images in QEMU


Notes how to run in QEMU different images. Its cool to run raspi images with qemu, that allows quickly test what is inside shell, what kind of packages are missing and if needed test your software.

Runing Raspberry Pi images

Download image

Download raspi debian kernel

wget -c

Download image

wget -c

QEMU is using their own file image format lets concert img to qcow2

qemu-img convert -f raw -O qcow2 2017-08-16-raspbian-stretch-lite.img raspbian-stretch-lite.qcow

Set network on local machine tun0 that will used by qemu to

brctl addbr tun0
brctl addif tun0 enp0s25

Start qemu with raspi4 image

qemu-system-arm \
-kernel ./kernel-qemu-4.4.34-jessie \
-append "root=/dev/sda2 panic=1 rootfstype=ext4 rw" \
-hda raspbian-stretch-lite.qcow \
-cpu arm1176 -m 256 \
-M versatilepb \
-no-reboot \
-serial stdio \
-nic user,hostfwd=tcp::10022-:22

Login to raspi prompt

Username: pi
Password: raspberry

Getting cursor back


Create Image Custom Image

Be careful its your responsibility what you type in terminal.

Create Image

Create image 128MB in size, with 2 partitions ext2 and ext4, install grub2.

dd if=/dev/zero of=disk.img bs=1048576 count=128
parted --script disk.img mklabel msdos mkpart p ext2 1 64 mkpart p ext4 64 128 set 1 boot on 

kpartx -a disk.img

mkfs.ext2 /dev/mapper/loop0p1
mkfs.ext4 /dev/mapper/loop0p2

mkdir /mnt/disk0
mount /dev/mapper/loop0p1 /mnt/disk0/
grub-install --target=i386-pc --recheck --no-floppy --root-directory=/mnt/disk0 --modules="biosdisk part_msdos ext2 configfile normal multiboot" /dev/loop0

nano /mnt/disk0/boot/grub/grub.cfg
umount /mnt/disk0
kpartx -d disk.img

Installing GRUB2

Installing grub

grub-install --target=i386-pc --recheck --no-floppy --root-directory=/mnt/disk0 --modules="biosdisk part_msdos ext2 configfile normal multiboot" /dev/loop0

After grub installation create config with some minimal entries, and point to kernel that was copied to partition. Kernel for test could taken from /boot directory. How to compile kernel its different story.

## Begin /boot/grub/grub.cfg
set default=1
set timeout=5

probe -u $root --set=rootuuid
set imgdevpath="/dev/disk/by-uuid/$rootuuid"

menuentry "GNU/Linux, vmlinux" {
        linux   /vmlinux root=/dev/sdb2 ro

Not sure why it works with sdb2 but not with sda1.


Now image is ready to be launched from qemu. Use any kernel image you like, this will boot tills stage where kernel will ask to set "init=" parameter.

Booting image from local filesystem, all kernel output goes to shell.

qemu-system-x86_64 \
    -kernel diskimages/bzImage \
    -hda disk.img \
    -append "root=/dev/sdb2 console=ttyS0" \
    -serial stdio -display none \
    -m 128

Booting just from img file

qemu-system-x86_64 \
    -drive format=raw,file=disk.img 
    -m 128

If shell fails with this output. Then everything booting succeed.

"---[ end Kernel panic - not syncing: No working init found.  Try passing init= option to kernel. See
Linux Documentation/admin-guide/init.rst for guidance. ]---"

Next step is to figure out what to do with this info, maybe make your own distro? Create some cool ass crypto drive? Or just have fun.

Resize image

When space runs out on the virtual machine, provided images can be resized Here is example how to resize image to bigger size and still be able to run it on qemu.

Main step that is may harm is fdisk, save disk layout with

fdisk -l of=raspbian-stretch-lite-20gb.img

and use it for later reference when partition will be resized.

dd if=2017-08-16-raspbian-stretch-lite.img of=raspbian-stretch-lite-20gb.img seek=0 conv=notrunc
fdisk -l raspbian-stretch-lite-20gb.img
#use here fdisk to resize partiions
sudo kpartx -a raspbian-stretch-lite-20gb.img
sudo kpartx -d raspbian-stretch-lite-20gb.img
sudo e2fsck /dev/mapper/loop0p2
resize2fs /dev/mapper/loop0p2

References to other articles