Skip to content

Instantly share code, notes, and snippets.

@macdaliot
Forked from stefanozanella/cnxsoft.md
Created July 26, 2018 12:31
Show Gist options
  • Select an option

  • Save macdaliot/377b7ad63aed2db36a93ea1f1eba3e31 to your computer and use it in GitHub Desktop.

Select an option

Save macdaliot/377b7ad63aed2db36a93ea1f1eba3e31 to your computer and use it in GitHub Desktop.
Emulate a Raspberry Pi with Qemu+ KVM

Running with custom Raspbian installation

Prepare installer image

yum install makebootfat dosfstools

dd if=/dev/zero of=rpi_installer.img bs=1M count=2048
losetup /dev/loop0 rpi_installer.img
kpartx -av /dev/loop0
fdisk /dev/loop0
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-261, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-261, default 261): 
Using default value 261

Command (m for help): a
Partition number (1-4): 1

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 22: Invalid argument.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
kpartx -av /dev/loop0
mkfs.vfat /dev/mapper/loop0p1
mkdir /tmp/x
mount -o loop rpi_installer.img /tmp/x
cd /tmp/x
unzip /root/rpi_installer_08-19-12.zip
cd && umount /tmp/x
losetup -d /dev/loop0

qemu-system-arm -kernel kernel-qemu -cpu arm1176 -m 256 -M versatilepb -no-reboot -serial stdio -append "root=/dev/sda1 panic=1" -hda rpi_installer.img -net nic -net user -vnc :1 -net tap,ifname=vnet1,script=no,downscript=no

VM doesn't boot for lack of init script.

Running with offical Raspberry Pi Raspbian image

Reference: http://xecdesign.com/qemu-emulating-raspberry-pi-the-easy-way/#more-45

Install guest machine

virt-install --name=owen --ram=1024 --vcpus=1   --cdrom=/media/CentOS-6.3-x86_64-minimal.iso  --os-type=linux --os-variant=rhel6   --disk pool=vmstorage,size=20 --network bridge=br0,model=e1000   --video=vga --vnc --connect qemu:///system

Installation options: (copy params from post and adapt) /dev/vda1 500 /boot /dev/vda2 19979 os root 17960 /root ext4 swap 2016 - swap (copy default tasks from post)

Prepare environment

curl -O http://ftp.snt.utwente.nl/pub/software/rpi/images/raspbian/2012-12-16-wheezy-raspbian/2012-12-16-wheezy-raspbian.zip
curl -O http://xecdesign.com/downloads/linux-qemu/kernel-qemu

yum install unzip
unzip  2012-12-16-wheezy-raspbian.zip
yum install http://www.opencomputingsolutions.com/pub/rpms/over/centos/6/x86_64/qemu-1.1.0-1.x86_64.rpm http://www.opencomputingsolutions.com/pub/rpms/over/centos/6/x86_64/qemu-arm-1.1.0-1.x86_64.rpm

Launch Raspberry Pi Emulation

qemu-system-arm -kernel kernel-qemu -cpu arm1176 -m 256 -M versatilepb -no-reboot -serial stdio -append "root=/dev/sda2 panic=1" -hda 2012-12-16-wheezy-raspbian.img -net nic -net user -vnc :0 -net tap,ifname=vnet0,script=no,downscript=no

Configurator:

  • change_locale -> en_GB.UTF-8
  • change_timezone -> Europe/Rome
  • ssh -> Enable
  • boot_behaviour -> Boot straight to desktop? Yes

Setup bridge on host VM:

vi /etc/sysconfig/network-script/ifcfg-virbr0
DEVICE=virbr0
TYPE=Bridge
IPADDR=172.16.32.6
NETMASK=255.255.255.0
GATEWAY=172.16.32.254
ONBOOT=yes
BOOTPROTO=static
IPV6INIT=no
IPV6_AUTOCONF=no
STP=no
vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE="eth0"
BOOTPROTO="none"
HWADDR="52:54:00:70:79:B3"
NM_CONTROLLED="no"
ONBOOT="yes"
TYPE="Ethernet"
UUID="e2d7181d-be2d-496f-aa2f-d4c49832c393"
BRIDGE="virbr0"
PROMISC="yes"

Add raspberry interface to bridge:

brctl addif virbr0 vnet0
ip link set vnet0 up

Configure address on RPi interface:

sudo vi /etc/network/interfaces
iface eth0 inet static
address 172.16.32.7
netmask 255.255.255.0
gateway 172.16.32.254
sudo vi /etc/resolv.conf
nameserver 8.8.8.8

Update packages and install puppet:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install puppet

Configure server:

sudo vi /etc/puppet/puppet.conf
...
[agent]
server = puppet.derecom.it

First run:

sudo puppet agent --test --verbose

On the master:

puppet ca sign raspberrypi

Booting with UBoot for Raspberry

curl -O http://people.freebsd.org/~gonzo/arm/rpi/raspberry-pi-uboot-20120621.tar.gz

yum install makebootfat dosfstools

dd if=/dev/zero of=uboot.img bs=1M count=2048
losetup /dev/loop1 uboot.img
kpartx -av /dev/loop1
fdisk /dev/loop1
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-261, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-261, default 261): 
Using default value 261

Command (m for help): a
Partition number (1-4): 1

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 22: Invalid argument.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
kpartx -av /dev/loop1
mkfs.vfat /dev/mapper/loop1p1
mkdir /tmp/x
mount -o loop /dev/mapper/loop1p1 /tmp/x
cd /tmp/x
tar zxvf /root/raspberry-pi-uboot-20120621.tar.gz
cd && umount /tmp/x
losetup -d /dev/loop0

qemu-system-arm -kernel kernel-qemu -cpu arm1176 -m 256 -M versatilepb -no-reboot -serial stdio -append "root=/dev/sda1 panic=1" -hda uboot.img -net nic -net user -vnc :1 -net tap,ifname=vnet1,script=no,downscript=no

Same failure as with installer...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment