From b1eb77515d903aeae441217508274a09f8978aca Mon Sep 17 00:00:00 2001 From: FreeArtMan Date: Thu, 23 Apr 2020 20:35:25 +0100 Subject: Added qemu and custom image notes --- md/writeup.md | 1 + md/writeup/running_disk_images_in_qemu.md | 175 ++++++++++++++++++++++++++++++ 2 files changed, 176 insertions(+) create mode 100644 md/writeup/running_disk_images_in_qemu.md diff --git a/md/writeup.md b/md/writeup.md index 6cbca88..94ee3ff 100644 --- a/md/writeup.md +++ b/md/writeup.md @@ -27,6 +27,7 @@ title: Writeup page [Using cgroups](writeup/using_cgroups.md) [Compile static python](writeup/compile_python.md) [Linux hello world in Swift](writeup/linux_hello_world_in_swift.md) +[Running disk images in QEMU](writeup/running_disk_images_in_qemu.md) ## Projects diff --git a/md/writeup/running_disk_images_in_qemu.md b/md/writeup/running_disk_images_in_qemu.md new file mode 100644 index 0000000..d4b2d7d --- /dev/null +++ b/md/writeup/running_disk_images_in_qemu.md @@ -0,0 +1,175 @@ +title:Runing disk images in QEMU +keywords:linux,qemu,raspi,linux, + +# Running RasPi4 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. + +## 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] [/writeup/qemu_usage.md](/writeup/qemu_usage.md) +[07] [https://en.wikibooks.org/wiki/QEMU/Images](https://en.wikibooks.org/wiki/QEMU/Images) +[08] [https://wiki.archlinux.org/index.php/QEMU](https://wiki.archlinux.org/index.php/QEMU) +[09] [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) +[10] [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) +[11] [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/) +[12] [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) +[13] [https://en.wikibooks.org/wiki/QEMU/Images](https://en.wikibooks.org/wiki/QEMU/Images) +[14] [https://wiki.gentoo.org/wiki/QEMU/Linux_guest](https://wiki.gentoo.org/wiki/QEMU/Linux_guest) +[15] [https://www.cs.vu.nl/~herbertb/misc/writingkernels.txt](https://www.cs.vu.nl/~herbertb/misc/writingkernels.txt) -- cgit v1.2.3