LoginSignup
21
20

More than 3 years have passed since last update.

QEMU 4.0.0 で RaspberryPi Zero (Stretch/armv6l) のハードウェアエミュレーション環境を作成する

Last updated at Posted at 2019-10-12

1.Introduction

一部界隈では需要があるネタのようですので、今回はStretch環境で書きます。 前回は Raspbian Buster でエミュレーション環境を構築しました。 今回は Raspbian Stretch でパーティション拡張を含めた環境構築手順を残します。 1時間ほどで環境構築できます。 QEMUのビルドを除けば15分ほどで環境構築できます。 なお、作業実施済みのimgファイルはコチラからダウンロード可能です。 https://drive.google.com/open?id=14pjBRkTSKrRuFiUxtLm5WvHNIp1FxISg

手順を構築しておきながらなんですが、RaspberryPi Zero の実機を所有していないため、我ながら使いみちが謎です。
Terminal - b920405@ubuntu: ~-qemu-system-pizero_017.png

2.Environment

  • Ubuntu 18.04
  • QEMU 4.0.0
  • Raspbian Stretch (armv6l)

3.Procedure

作業ディレクトリの作成
$ cd ~
$ mkdir qemu-system-pizero;cd qemu-system-pizero
QEMU4.0.0のビルドとインストール
$ wget https://download.qemu.org/qemu-4.0.0.tar.xz
$ tar xvJf qemu-4.0.0.tar.xz
$ cd qemu-4.0.0
$ ./configure
$ make -j8
$ sudo make install
$ sudo ldconfig
$ qemu-system-arm --version

QEMU emulator version 4.0.0
Copyright (c) 2003-2019 Fabrice Bellard and the QEMU Project developers

$ cd ..
OSイメージとカーネルとデバイスツリーファイルのダウンロード
### Download 2019-04-08-raspbian-stretch-lite
$ curl -sc /tmp/cookie "https://drive.google.com/uc?export=download&id=1OuQ9VroFqm97ZHlSyit3X-7vBhmESDD_" > /dev/null
$ CODE="$(awk '/_warning_/ {print $NF}' /tmp/cookie)"
$ curl -Lb /tmp/cookie "https://drive.google.com/uc?export=download&confirm=${CODE}&id=1OuQ9VroFqm97ZHlSyit3X-7vBhmESDD_" -o 2019-04-08-raspbian-stretch-lite.zip
$ unzip 2019-04-08-raspbian-stretch-lite.zip
$ rm 2019-04-08-raspbian-stretch-lite.zip

### Download kernel-qemu-4.14.79-stretch
$ curl -sc /tmp/cookie "https://drive.google.com/uc?export=download&id=1NrpsWObfed9RaOwfvD6_It_K7XYvsVyL" > /dev/null
$ CODE="$(awk '/_warning_/ {print $NF}' /tmp/cookie)"
$ curl -Lb /tmp/cookie "https://drive.google.com/uc?export=download&confirm=${CODE}&id=1NrpsWObfed9RaOwfvD6_It_K7XYvsVyL" -o kernel-qemu-4.14.79-stretch

### Download versatile-pb.dtb
$ curl -sc /tmp/cookie "https://drive.google.com/uc?export=download&id=1Po7T1DfIbRgEMTs8YItB0q8GHN8qrf8I" > /dev/null
$ CODE="$(awk '/_warning_/ {print $NF}' /tmp/cookie)"
$ curl -Lb /tmp/cookie "https://drive.google.com/uc?export=download&confirm=${CODE}&id=1Po7T1DfIbRgEMTs8YItB0q8GHN8qrf8I" -o versatile-pb.dtb
OSイメージの拡張
$ sudo qemu-img resize 2019-04-08-raspbian-stretch-lite.img +15G
Raspbian_Stretch_の仮想環境起動
### Launch Raspbian Stretch
$ qemu-system-arm \
  -kernel kernel-qemu-4.14.79-stretch \
  -dtb versatile-pb.dtb \
  -m 256 -M versatilepb -cpu arm1176 \
  -serial stdio \
  -append "rw console=ttyAMA0 root=/dev/sda2 rootfstype=ext4 loglevel=8 rootwait fsck.repair=yes memtest=1" \
  -drive file=2019-04-08-raspbian-stretch-lite.img,format=raw \
  -no-reboot
SWAP領域の拡張_100MB->2000MB
$ sudo nano /etc/dphys-swapfile
CONF_SWAPSIZE=2048
パーティション情報の確認_sda2が1.6GBしか確保されていない
$ sudo fdisk -l

Disk /dev/sda: 11.7 GiB, 12540968960 bytes, 24494080 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xc1dc39e5

Device     Boot Start     End Sectors  Size Id Type
/dev/sda1        8192   96042   87851 42.9M  c W95 FAT32 (LBA)
/dev/sda2       98304 3522559 3424256  1.6G 83 Linux

First sector には /dev/sda2 98304 3522559 3424256 1.6G 83 Linux98304 を指定します。

パーティションの再作成
$ sudo fdisk /dev/sda

Command (m for help): d
Partition number (1,2, default 2): 2

Command (m for help): n
Partition type:
p   primary (1 primary, 0 extended, 3 free)
e   extended
Select (default p): p
Partition number (2-4, default 2):
First sector (2048-34979839, default 2048): 98304
Last sector, +sectors or +size{K,M,G,T,P} (98304-34979839, default 34979839):

Created a new partition 2 of type 'Linux' and of size 16.6 GiB.
Partition #2 contains a ext4 signature.

Do you want to remove the signature? [Y]es/[N]o: N

The signature will be removed by a write command.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: Device or resource busy

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).

$ sudo reboot
Raspbian_Stretch_の仮想環境起動
### Launch Raspbian Stretch
$ qemu-system-arm \
  -kernel kernel-qemu-4.14.79-stretch \
  -dtb versatile-pb.dtb \
  -m 256 -M versatilepb -cpu arm1176 \
  -serial stdio \
  -append "rw console=ttyAMA0 root=/dev/sda2 rootfstype=ext4 loglevel=8 rootwait fsck.repair=yes memtest=1" \
  -drive file=2019-04-08-raspbian-stretch-lite.img,format=raw \
  -no-reboot
パーティション情報の確認_sda2が16.6GBに拡張された
$ sudo fdisk -l

Disk /dev/sda: 16.7 GiB, 17909678080 bytes, 34979840 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xc1dc39e5

Device     Boot Start      End  Sectors  Size Id Type
/dev/sda1        8192    96042    87851 42.9M  c W95 FAT32 (LBA)
/dev/sda2       98304 34979839 34881536 16.6G 83 Linux
ディスクの拡張
$ sudo resize2fs /dev/sda2
resize2fs 1.43.4 (31-Jan-2017)
Filesystem at /dev/sda2 is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 2
The filesystem on /dev/sda2 is now 4360192 (4k) blocks long.
拡張状況の確認
$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/root        17G  980M   15G   7% /
devtmpfs        124M     0  124M   0% /dev
tmpfs           124M     0  124M   0% /dev/shm
tmpfs           124M  1.9M  122M   2% /run
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           124M     0  124M   0% /sys/fs/cgroup
/dev/sda1        43M   22M   21M  51% /boot
tmpfs            25M     0   25M   0% /run/user/1000

QEMU_016.png
RAM 256MB, ストレージ 17GB, SWAP 2GB, armv6l 環境の完成です。
Terminal - b920405@ubuntu: ~-qemu-system-pizero_017.png

4.Reference articles

  1. QEMU4.0.0 + Raspbian Buster による RaspberryPi のエミュレーション環境構築(超シンプル手順バージョン)
  2. http://ftp.jaist.ac.jp/pub/raspberrypi/raspbian_lite/images/
  3. https://github.com/dhruvvyas90/qemu-rpi-kernel.git
21
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
20