Qemu - h-sendai/RaspberryPi GitHub Wiki

RaspbianをIntel CPU上で動かす方法

qemuを使ってIntel CPU上でRaspbianを動かす方法。

参考にしたページ

Raspbianを新しくしたのとホスト側がCentOS 7以外はここに 書かれている方法で起動できる。

CentOS 7での準備

CentOS 8用qemu RPMパッケージがなかったのでCentOS 7で テスト。CentOS 7ならEPELにqemu-system-armパッケージがある。

centos7# yum install epel-release
centos7# yum --enablerepo=epel install qemu-system-arm

Raspbianとqemu用カーネル、Device Treeの用意

http://ftp.jaist.ac.jp/pub/raspberrypi/raspbian_lite/images/ から zipファイルをダウンロード。zipを展開。

centos7% mkdir raspbian
centos7% cd raspbian
centos7% cp /どこか/2020-02-13-raspbian-buster-lite.img .
centos7% cd ..
centos7% git clone https://github.com/dhruvvyas90/qemu-rpi-kernel

コマンド行が長くなるのでrunというシェルスクリプトを 作る。

#!/bin/sh

qemu-system-arm \
-kernel qemu-rpi-kernel/kernel-qemu-4.19.50-buster \
-cpu arm1176 \
-M versatilepb \
-dtb qemu-rpi-kernel/versatile-pb-buster.dtb \
-m 256 \
-no-reboot \
-nographic \
-append 'root=/dev/sda2 panic=1' \
-drive file=raspbian/2020-05-27-raspios-buster-lite-armhf.img,index=0,media=disk,format=raw \
-net nic -net user,hostfwd=tcp::5022-:22

QEMUを終了させるにはguest OS内でpowerdownで終了させるか、 C-a xを押してQEMUを終了させる。

下のように-nographicではない-serial stdioで起動するとC-cを押すと guest OSに伝わるかわりに、QEMU自体が終了する(あまりうれしくない)。

#!/bin/sh

qemu-system-arm \
-kernel qemu-rpi-kernel/kernel-qemu-4.19.50-buster \
-cpu arm1176 \
-M versatilepb \
-dtb qemu-rpi-kernel/versatile-pb-buster.dtb \
-m 256 \
-no-reboot \
-serial stdio \
-append 'root=/dev/sda2 panic=1' \
-hda raspbian/2020-02-13-raspbian-buster-lite.img \
-net nic -net user,hostfwd=tcp::5022-:22

-driveのかわりに -hda raspbian/2020-02-13-raspbian-buster-lite.img とすることも可能だが、これを使うと

WARNING: Image format was not specified for 'raspbian/2020-05-27-raspios-buster-lite-armhf.img' and probing guessed raw.

Automatically detecting the format is dangerous for raw images, write operations on block 0 will be restricted. Specify the 'raw' format explicitly to remove the restrictions.

という警告がでる。

これで次のようなディレクトリ構造になる。

|-- qemu-rpi-kernel
|   |-- README.md
|   |-- kernel-qemu-3.10.25-wheezy
|   |-- kernel-qemu-4.14.79-stretch
|   |-- kernel-qemu-4.19.50-buster
|   |-- kernel-qemu-4.4.34-jessie
|   |-- tools
|   |   |-- README.md
|   |   |-- build-kernel-qemu
|   |   |-- build-kernel-qemu.conf-4.14.79
|   |   |-- config_file
|   |   |-- config_file_3.10.25
|   |   |-- config_file_4.19.50
|   |   |-- config_file_4.4.34
|   |   |-- config_ip_tables
|   |   |-- config_loopdev
|   |   |-- config_webcam
|   |   |-- linux-arm-4.4.34.patch
|   |   |-- linux-arm.patch
|   |   `-- qemu_choose_vm.sh
|   `-- versatile-pb.dtb
|-- raspbian
|   `-- 2020-02-13-raspbian-buster-lite.img
`-- run

./runで走らせるとrunを走らせた端末にRaspbianの出力がでる。 ログインプロンプトがでたらユーザー名、パスワードを入力する。 ログイン後、

systemctl start ssh

でsshdを起動するとホスト側からssh -p 5022 127.0.0.1でアクセス 可能になる。

qemu built-in DHCPによりIPアドレスが取得でき、qemuの IPマスカレードによりホスト側ネットワークを経由して外の サイトとアクセスができる。ただしTCP, UDPのみが実装されており、 ICMPは実装されていないのでpingは使えない。 (参考) https://wiki.archlinux.org/index.php/QEMU#User-mode_networking

ディスクの拡張

ダウンロードしたraspbian liteイメージのままだとディスク領域が小さい ので上記参考にしたページの手順でディスク領域を拡張する。

centos7% qemu-img resize -f raw 2020-02-13-raspbian-buster-lite.img +4G
qemuでraspbianを起動後
root@raspberrypi:/home/pi# fdisk /dev/sda
Command (m for help): p
Disk /dev/sda: 5.7 GiB, 6144655360 bytes, 12001280 sectors
Disk model: QEMU HARDDISK
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: 0x738a4d67

Device     Boot  Start     End Sectors  Size Id Type
/dev/sda1         8192  532479  524288  256M  c W95 FAT32 (LBA)
/dev/sda2       532480 3612671 3080192  1.5G 83 Linux
(/dev/sda2のStartの値をメモする)
Command (m for help): d (dコマンドで2番目のパーティションをいったん削除)
Partition number (1,2, default 2): (リターンキーのみでよい)
Partition 2 has been deleted.

Command (m for help): n (primary 2を作る)
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2):
First sector (2048-12001279, default 2048): 532480 (メモしておいたsda2のStartの値を入力する)
Last sector, +/-sectors or +/-size{K,M,G,T,P} (532480-12001279, default 12001279): (リターンのみでよい)

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

Do you want to remove the signature? [Y]es/[N]o: n (ext4シグネチャは消さないようにする)

このあとraspbianを再起動しresizeコマンドを実行する:

root@raspberrypi:/home/pi# resize2fs /dev/sda2

この方法でディスクイメージを大きくしたあと、起動して apt update; apt upgradeでアップデートすることができる。

ToDo

  • Raspbian入出力がでている端末でC-cを叩くとqemu自体が終了する(Raspbian側に C-cが伝わらない)。-> -serial stdioのかわりに-nographicを使うことで解決した。