Qemu仿真----(9)运行Debian Rootfs

平台: ubuntu-22.04-desktop-amd64.
对象: debian rootfs、qemu.
文件: linux-6.1.26.tar.xz

本例通过qemu运行debian rootfs for arm64。

1.安装依赖

$ sudo apt install binfmt-support qemu-user-static qemu-system-aarch64 gcc-aarch64-linux-gnu debootstrap

2.下载文件

linux-6.1.26.tar.xz

[https://mirrors.edge.kernel.org/pub/linux/kernel/v6.x/linux-6.1.26.tar.xz]

debian rootfs

$ mkdir rootfs
$ sudo debootstrap --arch arm64 --variant=minbase --include=whiptail,ca-certificates,tzdata buster rootfs https://mirrors.ustc.edu.cn/debian/

参数说明:

minbase: 包含必要的包和apt包管理器;

buildd: 包含编译工具包;

fakechroot: 包含不用root权限的包;

scratchbox: 包含scratchbox(交叉编译工具链)相关包;

除了使用debootstrap,还可以改成qemu-debootstrap,后者调用前者,参数相同,只是后者会多执行一些步骤。

另外,制作ubuntu rootfs与制作debian rootfs类似,下载ubuntu rootfs如下(除此之外的以下其它步骤通用):

$ sudo debootstrap --arch arm64 --variant=minbase --include=whiptail,ca-certificates,tzdata jammy rootfs https://mirrors.ustc.edu.cn/ubuntu-ports/

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=debian10-base.ext4 bs=1M count=1024
$ mkfs.ext4 debian10-base.ext4
$ sudo mount -t ext4 debian10-base.ext4 /mnt/
$ sudo cp -raf rootfs/* /mnt/
$ sudo cp /usr/bin/qemu-aarch64-static /mnt/usr/bin/
$ sudo cp /etc/resolv.conf /mnt/etc/
$ sudo mount -t proc /proc /mnt/proc
$ sudo mount -t sysfs /sys /mnt/sys
$ sudo mount -o bind /dev /mnt/dev
$ sudo mount -o bind /dev/pts /mnt/dev/pts

安装内核模块:

cd linux-6.1.26/
sudo make modules_install ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- INSTALL_MOD_PATH=/mnt
cd ..

5.chroot

$ sudo chroot /mnt

进入chroot环境:

$ apt update
$ apt install systemd sudo rsyslog udev
$ useradd -s '/bin/bash' -m -G adm,sudo hellouser
$ passwd hellouser
$ echo "debian10" > /etc/hostname
$ cp /lib/systemd/system/serial-getty\@.service /lib/systemd/system/serial-getty\@ttyAMA0.service
$ systemctl enable serial-getty\@ttyAMA0.service
$ exit

然后执行:

$ sudo vim /mnt/lib/systemd/system/serial-getty@ttyAMA0.service

其中[serial-getty\@ttyAMA0.service]做如下修改:

--- lib/systemd/system/serial-getty@.service
+++ lib/systemd/system/serial-getty@ttyAMA0.service
@@ -11,8 +11,8 @@
 Description=Serial Getty on %I
 Documentation=man:agetty(8) man:systemd-getty-generator(8)
 Documentation=http://0pointer.de/blog/projects/serial-console.html
-BindsTo=dev-%i.device
-After=dev-%i.device systemd-user-sessions.service plymouth-quit-wait.service getty-pre.target
+#BindsTo=dev-%i.device
+#After=dev-%i.device systemd-user-sessions.service plymouth-quit-wait.service getty-pre.target
 After=rc-local.service
 
 # If additional gettys are spawned during boot then we should make

卸载:

$ sudo umount /mnt/proc
$ sudo umount /mnt/sys
$ sudo umount /mnt/dev/pts
$ sudo umount /mnt/dev
$ sudo umount /mnt

6.运行

$ touch start-qemu.sh
$ chmod +x start-qemu.sh

start-qemu.sh添加如下内容:

#!/bin/sh

qemu-system-aarch64 \
    -M virt \
    -cpu cortex-a53 \
    -nographic \
    -smp 1 \
    -m 1024M \
    -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=debian10-base.ext4,if=none,format=raw,id=hd0 \
    -device virtio-blk-device,drive=hd0

执行start-qemu.sh,顺利的话,就可以进入ubuntu base了,可以直接apt安装需要的软件。

posted @ 2023-05-27 20:05  this毛豆  阅读(646)  评论(0编辑  收藏  举报