1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
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
```bash
wget -c https://github.com/dhruvvyas90/qemu-rpi-kernel/raw/master/kernel-qemu-4.4.34-jessie
```
Download image
```bash
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
```bash
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
```bash
brctl addbr tun0
brctl addif tun0 enp0s25
```
Start qemu with raspi4 image
```bash
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
```bash
Username: pi
Password: raspberry
```
Getting cursor back
```bash
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.
```bash
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
```bash
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
```bash
fdisk -l of=raspbian-stretch-lite-20gb.img
```
and use it for later reference when partition will be resized.
```bash
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)
|