Skip to content

Instantly share code, notes, and snippets.

@initialed85
Last active January 30, 2019 18:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save initialed85/46ef0a1e11c9a5a118def1d845b4f86a to your computer and use it in GitHub Desktop.
Save initialed85/46ef0a1e11c9a5a118def1d845b4f86a to your computer and use it in GitHub Desktop.
Ubuntu 16.04 server for armv7l in QEMU

First off- kudos to https://gist.github.com/takeshixx/686a4b5e057deff7892913bf69bcb85a for the hard work; this Gist is just a spin on that.

Install the prerequisites

sudo apt-get install qemu-system-arm qemu-utils udhcpd

Configure network interface

sudo ip tuntap add dev tap0 mode tap
sudo ip link set up dev tap0
sudo ip addr add 192.168.0.1/24 dev tap0

Set up forwarding/NAT

sudo nano /etc/sysctl.conf
    # uncomment the following line
    net.ipv4.ip_forward=1
    
sudo sysctl -p

sudo iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -o enp0s5 -j MASQUERADE
sudo iptables -A FORWARD -i enp0s5 -o tap0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i tap0 -o enp0s5 -j ACCEPT

Configure udhcpd

sudo nano /etc/default/udhcpd
    # change the following line from "no" to "yes"
    DHCPD_ENABLED="yes"
    
sudo nano /etc/udhcpd.conf
    # blow everything else away and have this
    start		192.168.0.50
    end		192.168.0.99
    interface	tap0
    max_leases	49
    opt	dns	1.1.1.1 8.8.8.8
    option	subnet	255.255.255.0
    opt	router	192.168.0.1
    option	domain	local
    option	lease	864000

sudo service udhcp restart

Get Ubuntu 16.04 netboot stuff

wget http://ports.ubuntu.com/ubuntu-ports/dists/xenial/main/installer-armhf/current/images/generic-lpae/netboot/initrd.gz
wget http://ports.ubuntu.com/ubuntu-ports/dists/xenial/main/installer-armhf/current/images/generic-lpae/netboot/vmlinuz

Make an image file

qemu-img create -f qcow2 ubuntu.img 16G

Start QEMU

qemu-system-arm \
    -kernel vmlinuz \
    -initrd initrd.gz \
    -append "root=/dev/ram" \
    -no-reboot \
    -nographic \
    -m 1024 \
    -M virt \
    -netdev tap,id=mynet0,ifname=tap0,script=no,downscript=no \
    -device e1000,netdev=mynet0,mac=52:55:00:d1:55:01 \
    -hda ubuntu.img

Mount the new image

qemu-img convert -f qcow2 -O raw ubuntu.img ubuntu-raw.img
sudo losetup /dev/loop0 ubuntu-raw.img    
OFFSET=$(($(sudo fdisk -l /dev/loop0 |grep /dev/loop0p1 |awk '{print $3}')*512))
sudo mount -o loop,offset=$OFFSET /dev/loop0 /mnt

Extract the boot stuff

mkdir boot
cp /mnt/initrd.img-4.4.0-38-generic-lpae boot
cp /mnt/vmlinuz-4.4.0-38-generic-lpae boot

Remove stuff that is no longer required

sudo umount /mnt
sudo losetup -d /dev/loop0
rm ubuntu-raw.img

Boot the new image

qemu-system-arm \
    -kernel boot/vmlinuz-4.4.0-139-generic-lpae \
    -initrd boot/initrd.img-4.4.0-139-generic-lpae \
    -append "root=/dev/vda2 rootfstype=ext4" \
    -no-reboot \
    -nographic \
    -m 1024 \
    -M virt \
    -serial mon:stdio \
    -monitor telnet:127.0.0.1:9000,server,nowait \
    -netdev tap,id=mynet0,ifname=tap0,script=no,downscript=no \
    -device e1000,netdev=mynet0,mac=52:55:00:d1:55:01 \
    -drive file=ubuntu.img,if=virtio
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment