Qemu - 运行Gentoo Rootfs
- 平台: ubuntu-22.04-desktop-amd64.
- 对象: gentoo rootfs、qemu.
- 文件: linux-6.1.26.tar.xz、stage3-arm64-systemd-20230528T234658Z.tar.xz.
本例通过qemu运行gentoo rootfs for arm64。
1.安装依赖
$ sudo apt install qemu-user-static qemu-system-aarch64 systemd-container gcc-aarch64-linux-gnu
2.下载文件
linux-6.1.26.tar.xz
https://mirrors.edge.kernel.org/pub/linux/kernel/v6.x/linux-6.1.26.tar.xz
stage3-arm64-systemd-20230528T234658Z.tar.xz
3.编译内核
$ make defconfig ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu-
$ make -j8 ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu-
4.创建文件系统
$ dd if=/dev/zero of=gentoo-base.ext4 bs=4096 count=512K
$ mkfs.ext4 gentoo-base.ext4
$ sudo mount -t ext4 gentoo-base.ext4 /mnt
$ sudo tar -xf stage3-arm64-systemd-20230528T234658Z.tar.xz -C /mnt
$ sudo cp /usr/bin/qemu-aarch64-static /mnt/usr/bin/
$ sudo cp /etc/resolv.conf /mnt/etc/resolv.conf
$ sudo vim /mnt/etc/portage/repos.conf
在repos.conf中添加如下内容:
[DEFAULT]
main-repo = gentoo
[gentoo]
location = /usr/portage
sync-type = rsync
sync-uri = rsync://rsync.mirrors.ustc.edu.cn/gentoo-portage/
auto-sync = yes
进入chroot环境:
$ sudo systemd-nspawn -D /mnt/
chroot环境中做如下配置:
# 修改root密码,3种不同字符, 11~72长度字符.
>$ passwd
>$ exit
安装内核模块:
$ cd linux-6.1.26/
$ sudo make modules_install INSTALL_MOD_PATH=/mnt ARCH=arm64
$ cd ..
卸载gentoo-base.ext4:
$ sudo umount /mnt
5.运行
$ touch start-qemu.sh
$ chmod +x start-qemu.sh
$ vim start-qemu.sh
填入如下内容:
#!/bin/sh
qemu-system-aarch64 \
-M virt \
-cpu cortex-a53 \
-nographic \
-smp 1 \
-m 512M \
-kernel linux-6.1.26/arch/arm64/boot/Image \
-append "rootwait root=/dev/vda rw console=ttyAMA0" \
-netdev user,id=eth0 \
-device virtio-net-device,netdev=eth0 \
-drive file=gentoo-base.ext4,if=none,format=raw,id=hd0 \
-device virtio-blk-device,drive=hd0
账号:root,密码:上面自己执行passwd设置的。顺利的话,就可以正常使用了。