Qemu仿真----(8)运行Ubuntu Base

平台: ubuntu-22.04-desktop-amd64.
对象: ubuntu base、qemu.
文件: ubuntu-base-22.04.2-base-arm64.tar.gz、linux-6.1.26.tar.xz

Ubuntu Base不是一个超小的内存运行嵌入式发行版;它是Ubuntu的最小实现,本例通过qemu运行ubuntu-base.

1.安装依赖

$ sudo apt install binfmt-support qemu-user-static qemu-system-aarch64 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]

ubuntu-base-22.04.2-base-arm64.tar.gz

[http://cdimage.ubuntu.com/ubuntu-base/releases/22.04.2/release/ubuntu-base-22.04.2-base-arm64.tar.gz]

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=ubuntu22-base.ext4 bs=1M count=1024
$ mkfs.ext4 ubuntu22-base.ext4
$ sudo mount -t ext4 ubuntu22-base.ext4 /mnt/
$ sudo tar -xf ubuntu-base-22.04.2-base-arm64.tar.gz -C /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 resolvconf udev vim
$ useradd -s '/bin/bash' -m -G adm,sudo hellouser
$ passwd hellouser
$ echo "ubuntu22" > /etc/hostname
$ cp /lib/systemd/system/serial-getty\@.service /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

然后执行:

$ systemctl enable serial-getty\@ttyAMA0.service
$ exit

退出chroot之后,执行:

$ 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
$ 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=ubuntu22-base.ext4,if=none,format=raw,id=hd0 \
    -device virtio-blk-device,drive=hd0

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

posted @ 2023-05-26 23:39  this毛豆  阅读(459)  评论(2编辑  收藏  举报