title:Runing disk images in QEMU keywords:linux,qemu,raspi,linux, # Running disk images in QEMU ## Intro 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 https://github.com/dhruvvyas90/qemu-rpi-kernel/raw/master/kernel-qemu-4.4.34-jessie ``` Download image ``` wget -c https://downloads.raspberrypi.org/raspbian_lite/images/raspbian_lite-2017-08-17/2017-08-16-raspbian-stretch-lite.zip unzip 2017-08-16-raspbian-stretch-lite.zip ``` 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 ``` Ctrl+Alt+g ``` ## 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. ```bash ## 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. ### Launch 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. ```bash 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 ```bash qemu-system-x86_64 \ -drive format=raw,file=disk.img -m 128 ``` If shell fails with this output. Then everything booting succeed. ```bash "---[ 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 [01] [http://main.lv/writeup/compile_linux_kernel.md](http://main.lv/writeup/compile_linux_kernel.md) [02] [http://main.lv/writeup/qemu_usage.md](http://main.lv/writeup/qemu_usage.md) ## Links [01] [https://blog.agchapman.com/using-qemu-to-emulate-a-raspberry-pi/](https://blog.agchapman.com/using-qemu-to-emulate-a-raspberry-pi/) [02] [https://github.com/dhruvvyas90/qemu-rpi-kernel.git](https://github.com/dhruvvyas90/qemu-rpi-kernel.git) [03] [https://downloads.raspberrypi.org/raspbian_lite/images/raspbian_lite-2017-08-17/2017-08-16-raspbian-stretch-lite.zip](https://downloads.raspberrypi.org/raspbian_lite/images/raspbian_lite-2017-08-17/2017-08-16-raspbian-stretch-lite.zip) [04] [https://downloads.raspberrypi.org/](https://downloads.raspberrypi.org/) [05] [https://github.com/dhruvvyas90/qemu-rpi-kernel/tree/master/tools](https://github.com/dhruvvyas90/qemu-rpi-kernel/tree/master/tools) [06] [https://en.wikibooks.org/wiki/QEMU/Images](https://en.wikibooks.org/wiki/QEMU/Images) [07] [https://wiki.archlinux.org/index.php/QEMU](https://wiki.archlinux.org/index.php/QEMU) [08] [https://xilinx-wiki.atlassian.net/wiki/spaces/A/pages/18842054/QEMU+-+Zynq-7000](https://xilinx-wiki.atlassian.net/wiki/spaces/A/pages/18842054/QEMU+-+Zynq-7000) [09] [https://linux-tips.com/t/booting-from-an-iso-image-using-qemu/136](https://linux-tips.com/t/booting-from-an-iso-image-using-qemu/136) [10] [https://roscopeco.com/2013/08/12/creating-a-bootable-hard-disk-image-with-grub2/](https://roscopeco.com/2013/08/12/creating-a-bootable-hard-disk-image-with-grub2/) [11] [https://www.centennialsoftwaresolutions.com/post/build-the-linux-kernel-and-busybox-and-run-them-on-qemu](https://www.centennialsoftwaresolutions.com/post/build-the-linux-kernel-and-busybox-and-run-them-on-qemu) [12] [https://en.wikibooks.org/wiki/QEMU/Images](https://en.wikibooks.org/wiki/QEMU/Images) [13] [https://wiki.gentoo.org/wiki/QEMU/Linux_guest](https://wiki.gentoo.org/wiki/QEMU/Linux_guest) [14] [https://www.cs.vu.nl/~herbertb/misc/writingkernels.txt](https://www.cs.vu.nl/~herbertb/misc/writingkernels.txt) [15] [https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/storage_administration_guide/s2-disk-storage-parted-resize-part](https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/storage_administration_guide/s2-disk-storage-parted-resize-part) [16] [https://access.redhat.com/articles/1190213](https://access.redhat.com/articles/1190213)