Qemu仿真----(9)运行Debian Rootfs
<p>平台: ubuntu-22.04-desktop-amd64.<br>对象: debian rootfs、qemu.<br>文件: linux-6.1.26.tar.xz</p><p>本例通过qemu运行debian rootfs for arm64。</p>
<h2>1.安装依赖</h2>
<div class="cnblogs_code">
<pre>$ <span style="color: rgba(0, 0, 255, 1)">sudo</span> apt <span style="color: rgba(0, 0, 255, 1)">install</span> binfmt-support qemu-user-static qemu-system-aarch64 <span style="color: rgba(0, 0, 255, 1)">gcc</span>-aarch64-linux-gnu debootstrap</pre>
</div>
<h2>2.下载文件</h2>
<p>linux-6.1.26.tar.xz</p>
<p></p>
<p>debian rootfs</p>
<div class="cnblogs_code">
<pre>$ <span style="color: rgba(0, 0, 255, 1)">mkdir</span><span style="color: rgba(0, 0, 0, 1)"> rootfs
$ </span><span style="color: rgba(0, 0, 255, 1)">sudo</span> debootstrap --arch arm64 --variant=minbase --include=whiptail,ca-certificates,tzdata buster rootfs https:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">mirrors.ustc.edu.cn/debian/</span></pre>
</div>
<p>参数说明:</p>
<p>minbase: 包含必要的包和apt包管理器;</p>
<p>buildd: 包含编译工具包;</p>
<p>fakechroot: 包含不用root权限的包;</p>
<p>scratchbox: 包含scratchbox(交叉编译工具链)相关包;</p>
<p>除了使用debootstrap,还可以改成qemu-debootstrap,后者调用前者,参数相同,只是后者会多执行一些步骤。</p>
<p>另外,制作ubuntu rootfs与制作debian rootfs类似,下载ubuntu rootfs如下(除此之外的以下其它步骤通用):</p>
<div class="cnblogs_code">
<pre>$ <span style="color: rgba(0, 0, 255, 1)">sudo</span> debootstrap --arch arm64 --variant=minbase --include=whiptail,ca-certificates,tzdata jammy rootfs https:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">mirrors.ustc.edu.cn/ubuntu-ports/</span></pre>
</div>
<h2>3.编译内核</h2>
<div class="cnblogs_code">
<pre>$ <span style="color: rgba(0, 0, 255, 1)">make</span> defconfig ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu-<span style="color: rgba(0, 0, 0, 1)">
$ </span><span style="color: rgba(0, 0, 255, 1)">make</span> -j8 ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu-</pre>
</div>
<h2>4.创建文件系统</h2>
<div class="cnblogs_code">
<pre>$ <span style="color: rgba(0, 0, 255, 1)">dd</span> <span style="color: rgba(0, 0, 255, 1)">if</span>=/dev/zero of=debian10-base.ext4 bs=1M count=<span style="color: rgba(128, 0, 128, 1)">1024</span><span style="color: rgba(0, 0, 0, 1)">
$ mkfs.ext4 debian10</span>-<span style="color: rgba(0, 0, 0, 1)">base.ext4
$ </span><span style="color: rgba(0, 0, 255, 1)">sudo</span> <span style="color: rgba(0, 0, 255, 1)">mount</span> -t ext4 debian10-base.ext4 /mnt/<span style="color: rgba(0, 0, 0, 1)">
$ </span><span style="color: rgba(0, 0, 255, 1)">sudo</span> <span style="color: rgba(0, 0, 255, 1)">cp</span> -raf rootfs<span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> /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</span></pre>
</div>
<p>安装内核模块:</p>
<div class="cnblogs_code">
<pre>cd linux-<span style="color: rgba(128, 0, 128, 1)">6.1</span>.<span style="color: rgba(128, 0, 128, 1)">26</span>/
<span style="color: rgba(0, 0, 255, 1)">sudo</span> <span style="color: rgba(0, 0, 255, 1)">make</span> modules_install ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- INSTALL_MOD_PATH=/<span style="color: rgba(0, 0, 0, 1)">mnt
cd ..</span></pre>
</div>
<h2>5.chroot</h2>
<div class="cnblogs_code">
<pre>$ <span style="color: rgba(0, 0, 255, 1)">sudo</span> <span style="color: rgba(0, 0, 255, 1)">chroot</span> /mnt</pre>
</div>
<p>进入chroot环境:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">$ apt update
$ apt </span><span style="color: rgba(0, 0, 255, 1)">install</span> systemd <span style="color: rgba(0, 0, 255, 1)">sudo</span><span style="color: rgba(0, 0, 0, 1)"> rsyslog udev
$ useradd </span>-s <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">/bin/bash</span><span style="color: rgba(128, 0, 0, 1)">'</span> -m -G adm,<span style="color: rgba(0, 0, 255, 1)">sudo</span><span style="color: rgba(0, 0, 0, 1)"> hellouser
$ </span><span style="color: rgba(0, 0, 255, 1)">passwd</span><span style="color: rgba(0, 0, 0, 1)"> hellouser
$ </span><span style="color: rgba(0, 0, 255, 1)">echo</span> <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">debian10</span><span style="color: rgba(128, 0, 0, 1)">"</span> > /etc/<span style="color: rgba(0, 0, 255, 1)">hostname</span><span style="color: rgba(0, 0, 0, 1)">
$ </span><span style="color: rgba(0, 0, 255, 1)">cp</span> /lib/systemd/system/serial-getty\@.service /lib/systemd/system/serial-<span style="color: rgba(0, 0, 0, 1)">getty\@ttyAMA0.service
$ systemctl enable serial</span>-<span style="color: rgba(0, 0, 0, 1)">getty\@ttyAMA0.service
$ exit</span></pre>
</div>
<p>然后执行:</p>
<div class="cnblogs_code">
<pre>$ <span style="color: rgba(0, 0, 255, 1)">sudo</span> vim /mnt/lib/systemd/system/serial-getty@ttyAMA0.service</pre>
</div>
<p>其中做如下修改:</p>
<div class="cnblogs_code">
<pre>--- lib/systemd/system/serial-<span style="color: rgba(0, 0, 0, 1)">getty@.service
</span>+++ lib/systemd/system/serial-<span style="color: rgba(0, 0, 0, 1)">getty@ttyAMA0.service
@@ </span>-<span style="color: rgba(128, 0, 128, 1)">11</span>,<span style="color: rgba(128, 0, 128, 1)">8</span> +<span style="color: rgba(128, 0, 128, 1)">11</span>,<span style="color: rgba(128, 0, 128, 1)">8</span><span style="color: rgba(0, 0, 0, 1)"> @@
Description</span>=Serial Getty on %<span style="color: rgba(0, 0, 0, 1)">I
Documentation</span>=<span style="color: rgba(0, 0, 255, 1)">man</span>:agetty(<span style="color: rgba(128, 0, 128, 1)">8</span>) <span style="color: rgba(0, 0, 255, 1)">man</span>:systemd-getty-generator(<span style="color: rgba(128, 0, 128, 1)">8</span><span style="color: rgba(0, 0, 0, 1)">)
Documentation</span>=http:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">0pointer.de/blog/projects/serial-console.html</span>
-BindsTo=dev-%<span style="color: rgba(0, 0, 0, 1)">i.device
</span>-After=dev-%i.device systemd-user-sessions.service plymouth-quit-<span style="color: rgba(0, 0, 255, 1)">wait</span>.service getty-<span style="color: rgba(0, 0, 0, 1)">pre.target
</span>+#BindsTo=dev-%<span style="color: rgba(0, 0, 0, 1)">i.device
</span>+#After=dev-%i.device systemd-user-sessions.service plymouth-quit-<span style="color: rgba(0, 0, 255, 1)">wait</span>.service getty-<span style="color: rgba(0, 0, 0, 1)">pre.target
After</span>=rc-<span style="color: rgba(0, 0, 0, 1)">local.service
# If additional gettys are spawned during boot </span><span style="color: rgba(0, 0, 255, 1)">then</span> we should <span style="color: rgba(0, 0, 255, 1)">make</span></pre>
</div>
<p>卸载:</p>
<div class="cnblogs_code">
<pre>$ <span style="color: rgba(0, 0, 255, 1)">sudo</span> <span style="color: rgba(0, 0, 255, 1)">umount</span> /mnt/<span style="color: rgba(0, 0, 0, 1)">proc
$ </span><span style="color: rgba(0, 0, 255, 1)">sudo</span> <span style="color: rgba(0, 0, 255, 1)">umount</span> /mnt/<span style="color: rgba(0, 0, 0, 1)">sys
$ </span><span style="color: rgba(0, 0, 255, 1)">sudo</span> <span style="color: rgba(0, 0, 255, 1)">umount</span> /mnt/dev/<span style="color: rgba(0, 0, 0, 1)">pts
$ </span><span style="color: rgba(0, 0, 255, 1)">sudo</span> <span style="color: rgba(0, 0, 255, 1)">umount</span> /mnt/<span style="color: rgba(0, 0, 0, 1)">dev
$ </span><span style="color: rgba(0, 0, 255, 1)">sudo</span> <span style="color: rgba(0, 0, 255, 1)">umount</span> /mnt</pre>
</div>
<h2>6.运行</h2>
<div class="cnblogs_code">
<pre>$ <span style="color: rgba(0, 0, 255, 1)">touch</span> start-qemu.<span style="color: rgba(0, 0, 255, 1)">sh</span><span style="color: rgba(0, 0, 0, 1)">
$ </span><span style="color: rgba(0, 0, 255, 1)">chmod</span> +x start-qemu.<span style="color: rgba(0, 0, 255, 1)">sh</span></pre>
</div>
<p>start-qemu.sh添加如下内容:</p>
<div class="cnblogs_code">
<pre>#!/bin/<span style="color: rgba(0, 0, 255, 1)">sh</span><span style="color: rgba(0, 0, 0, 1)">
qemu</span>-system-<span style="color: rgba(0, 0, 0, 1)">aarch64 \
</span>-<span style="color: rgba(0, 0, 0, 1)">M virt \
</span>-cpu cortex-<span style="color: rgba(0, 0, 0, 1)">a53 \
</span>-<span style="color: rgba(0, 0, 0, 1)">nographic \
</span>-smp <span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)"> \
</span>-<span style="color: rgba(0, 0, 0, 1)">m 1024M \
</span>-kernel linux-<span style="color: rgba(128, 0, 128, 1)">6.1</span>.<span style="color: rgba(128, 0, 128, 1)">26</span>/arch/arm64/boot/<span style="color: rgba(0, 0, 0, 1)">Image \
</span>-append <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">rootwait root=/dev/vda rw console=ttyAMA0</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)"> \
</span>-netdev user,<span style="color: rgba(0, 0, 255, 1)">id</span>=<span style="color: rgba(0, 0, 0, 1)">eth0 \
</span>-device virtio-net-device,netdev=<span style="color: rgba(0, 0, 0, 1)">eth0 \
</span>-drive <span style="color: rgba(0, 0, 255, 1)">file</span>=debian10-base.ext4,<span style="color: rgba(0, 0, 255, 1)">if</span>=none,format=raw,<span style="color: rgba(0, 0, 255, 1)">id</span>=<span style="color: rgba(0, 0, 0, 1)">hd0 \
</span>-device virtio-blk-device,drive=hd0</pre>
</div>
<p>执行start-qemu.sh,顺利的话,就可以进入ubuntu base了,可以直接apt安装需要的软件。</p><br><br>
来源:https://www.cnblogs.com/phoebus-ma/p/17437257.html
頁:
[1]