# Qemu + buildroot 101

## Compile arm kernel and execute it via qemu

```
sudo apt-get install qemu-system-arm
tar -xvzf buildroot-2020.02.3.tar.gz
sudo apt-get install libncurses5-dev libncursesw5-dev

make menuconfig
make list-defconfigs
make qemu_arm_versatile_defconfig

export PATH=$PATH:/home/embeddedcraft/buildroot-2020.02.3/output/host/bin
arm-buildroot-linux-uclibcgnueabi-gcc hello.c -o hello
sudo mount -t ext2 -o rw,loop rootfs.ext2 /mnt/try
sudo cp hello /mnt/try/root/

qemu-system-arm -M versatilepb -kernel vmlinuz-3.2.0-4-versatile -initrd initrd.img-3.2.0-4-versatile -hda debian_wheezy_armel_standard.qcow2 -append "root=/dev/sda1"  -net nic -net user,hostfwd=tcp::7777-:22

tar zcf squashfs-root.tar.gz squashfs-root 
scp -P 7777 ./squashfs-root.tar.gz root@127.0.0.1:/root
```

### Buildroot and QEMU – the quickest recipe for your own Linux

```
get buildroot version from the official website
$ tar -xvzf buildroot-2021.xxx.tar.gz
$ cd buildroot/
$ make qemu_arm_versatile_defconfig
$ make menuconfig
$ qemu-system-arm -M versatilepb -kernel output/images/zImage -dtb output/images/versatile-pb.dtb -drive file=output/images/rootfs.ext2,if=scsi -append "root=/dev/sda console=ttyAMA0,115200" -nographic

source: https://pressreset.net/2013/09/buildroot-and-qemu-the-quickest-recipe-for-your-own-linux/
```

## buildroot essential commands that will safe your life

```
make menuconfig
make HOSTCC=gcc-4.4
make MAKEINFO=true
make -j8
```

## qemu essential commands that will safe you

```
qemu-system-arm -machine help
qemu-system-arm -machine vexpress -cpu help

sudo qemu-system-arm \
    -M vexpress-a9 \
    -kernel ./zImage_arch \
    -dtb ./vexpress-v2p-ca9.dtb \
    --nographic \
    -append &quot;root=/dev/mmcblk0 rw roottype=ext4 console=ttyAMA0&quot; \
    -drive if=sd,driver=raw,cache=writeback,file=./arch_rootfs.ext4 \
    -net nic,macaddr=$macaddr \
    -net tap,vlan=0,ifname=tap0 \
    -snapshot

    
qemu-system-arm -M        vexpress-a9                                                    \
                -cpu      cortex-a9                                                      \
                -m        1024                                                           \
                -nographic                                                               \
                -kernel   $BRIMAGES/zImage                                               \
                -drive    file=$BRIMAGES/rootfs.ext2,index=0,media=disk,format=raw,if=sd \
                -dtb      $BRIMAGES/vexpress-v2p-ca9.dtb                                 \
                -net      nic                                                            \
                -net      user,hostfwd=tcp::2222-:22,hostfwd=tcp::9000-:9000             \
                -append   "rw console=ttyAMA0 console=tty root=/dev/mmcblk0"

                
qemu-system-arm -M versatilepb -kernel vmlinuz-3.2.0-4-versatile -initrd initrd.img-3.2.0-4-versatile -hda debian_wheezy_armel_standard.qcow2 -append "root=/dev/sda1"  -net nic -net user,hostfwd=tcp::7777-:22    

qemu-system-arm -M versatilepb -kernel output/images/zImage -dtb output/images/versatile-pb.dtb -drive file=output/images/rootfs.ext2,if=scsi -append "root=/dev/sda console=ttyAMA0,115200" -nographic

qemu-system-arm -M versatilepb -kernel zImage -dtb versatile-pb.dtb -drive file=rootfs.ext2,if=scsi,format=raw -append "root=/dev/sda console=ttyAMA0,115200" -serial stdio -net nic,model=rtl8139 -net user
```

## Qemu + virtual tap

```
sudo brctl addbr virbr0
sudo ifconfig virbr0 192.168.122.1/24 up

sudo tunctl -t tap0
sudo ifconfig tap0 192.168.122.11/24 up
sudo brctl addif virbr0 tap0

sudo qemu-system-mipsel -M malta -kernel vmlinux-3.2.0-4-4kc-malta -hda debian_wheezy_mipsel_standard.qcow2 -append "root=/dev/sda1 console=tty0" -net nic,vlan=0 -net tap,vlan=0,ifname=tap0 -nographic

ifconfig eth0 192.168.122.12/24 up
```

Another option is portforwarding:

```
sudo qemu-system-mipsel -M malta -kernel vmlinux-3.2.0-4-4kc-malta -hda debian_wheezy_mipsel_standard.qcow2 -append "root=/dev/sda1 console=tty0" -net user,hostfwd=tcp::80-:80,hostfwd=tcp::443-:443,hostfwd=tcp::2222-:22 -net nic -nographic

ssh -p 2222 root@127.0.0.1
scp -r ./data  root@192.168.122.12:/root/
```

{% embed url="<https://bbs.pediy.com/thread-266262.htm>" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gitbook.seguranca-informatica.pt/arm/tools/qemu-101.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
