如何将磁盘映像附加到包含Linux内核的根文件系统的QEMU?

如何解决如何将磁盘映像附加到包含Linux内核的根文件系统的QEMU?

我能够使用-kernel在QEMU上启动linux内核,这非常简单明了。

但是现在我正在尝试不同的东西。我正在模拟UEFI + GRUB +内核的组合。

我已经从源代码构建了OVMF和GRUB2,还能够加载内核映像,但是很难创建rootfs磁盘映像并将其附加到qemu以便内核可以检测到它。

这是我为内核准备rootfs.img的方法:

#Create an empty ext2 disk image of size 10M
dd if=/dev/zero of=rootfs.img bs=1M count=10
mke2fs -j rootfs.img

#Populate the disk
mkdir /mnt/rootfs
mount -o loop rootfs.img /mnt/rootfs
rsync -a busybox/_install/ /mnt/rootfs
chown -R root:root /mnt/rootfs
sync
umount /mnt/rootfs

这是我的GRUB配置的样子:

menuentry "Linux Kernel Boot" {
        set root=(hd0,msdos1)
        linux (hd0,msdos1)/bzImage-x86_64 root=/dev/sdb1 rootwait console=ttyS0,115200n8 console=tty0
}

这是我用来启动qemu的命令:

qemu-system-x86_64 -m 2G -drive  if=pflash,format=raw,readonly,file=OVMF_CODE.fd -drive if=pflash,file=OVMF_VARS.fd -net none -serial stdio -display none -hda fat:disk -drive file=rootfs.img,index=1

我已附加为hda的FAT磁盘包含我的GRUB和内核映像。我想要一个用于rootfs的单独磁盘,并且不想为此使用FAT。

该安装程序将启动,直到内核启动,但内核无法挂载磁盘sdb1,因此在rootfs中看不到init,因此出现内核恐慌。以下是内核日志:

[    1.030290] NET: Registered protocol family 17
[    1.033540] ata1.01: configured for MWDMA2
[    1.042119] Key type dns_resolver registered
[    1.045150] scsi 0:0:0:0: Direct-Access     ATA      QEMU HARDDISK    2.5+ PQ: 0 ANSI: 5
[    1.049509] sched_clock: Marking stable (1049235510,0)->(1378961625,-329726115)
[    1.058429] registered taskstats version 1
[    1.060917] Loading compiled-in X.509 certificates
[    1.066242] sd 0:0:0:0: [sda] 1032192 512-byte logical blocks: (528 MB/504 MiB)
[    1.070386] sd 0:0:0:0: Attached scsi generic sg0 type 0
[    1.074945] sd 0:0:0:0: [sda] Write Protect is off
[    1.079585] scsi 0:0:1:0: Direct-Access     ATA      QEMU HARDDISK    2.5+ PQ: 0 ANSI: 5
[    1.082483] sd 0:0:0:0: [sda] Write cache: enabled,read cache: enabled,doesn't support DPO or FUA
[    1.090820]   Magic number: 0:346:692
[    1.094970] sd 0:0:1:0: [sdb] 20480 512-byte logical blocks: (10.5 MB/10.0 MiB)
[    1.097740] sd 0:0:1:0: [sdb] Write Protect is off
[    1.101097] sd 0:0:1:0: Attached scsi generic sg1 type 0
[    1.108436] console [netcon0] enabled
[    1.110947] netconsole: network logging started
[    1.114830] sd 0:0:1:0: [sdb] Write cache: enabled,doesn't support DPO or FUA
[    1.122345] scsi 1:0:0:0: CD-ROM            QEMU     QEMU DVD-ROM     2.5+ PQ: 0 ANSI: 5
[    1.138010]  sda: sda1
[    1.142638] sr 1:0:0:0: [sr0] scsi3-mmc drive: 4x/4x cd/rw xa/form2 tray
[    1.145513] cdrom: Uniform CD-ROM driver Revision: 3.20
[    1.149749] cfg80211: Loading compiled-in X.509 certificates for regulatory database
[    1.164130] sd 0:0:1:0: [sdb] Attached SCSI disk
[    1.169575] sr 1:0:0:0: Attached scsi generic sg2 type 5
[    1.174262] sd 0:0:0:0: [sda] Attached SCSI disk
[    1.190198] cfg80211: Loaded X.509 cert 'sforshee: 00b28ddf47aef9cea7'
[    1.194508] platform regulatory.0: Direct firmware load for regulatory.db failed with error -2
[    1.197606] cfg80211: failed to load regulatory.db
[    1.200837] ALSA device list:
[    1.203626]   No soundcards found.
[    1.531285] input: ImExPS/2 Generic Explorer Mouse as /devices/platform/i8042/serio1/input/input3
[    1.537979] md: Waiting for all devices to be available before autodetect
[    1.541357] md: If you don't use raid,use raid=noautodetect
[    1.548186] md: Autodetecting RAID arrays.
[    1.551815] md: autorun ...
[    1.554889] md: ... autorun DONE.
[    1.559995] VFS: Cannot open root device "sdb1" or unknown-block(8,17): error -6
[    1.563196] Please append a correct "root=" boot option; here are the available partitions:
[    1.566295] 0800          516096 sda
[    1.566326]  driver: sd
[    1.572371]   0801          516064 sda1 be1afdfa-01
[    1.572397]
[    1.577560] 0810           10240 sdb
[    1.577573]  driver: sd
[    1.582573] 0b00         1048575 sr0
[    1.582585]  driver: sr
[    1.587948] Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(8,17)
[    1.590831] CPU: 0 PID: 1 Comm: swapper/0 Not tainted 4.15.3+ #2
[    1.593384] Hardware name: QEMU Standard PC (i440FX + PIIX,1996),BIOS 0.0.0 02/06/2015
[    1.596327] Call Trace:
[    1.600043]  dump_stack+0x46/0x59
[    1.602665]  panic+0xca/0x218
[    1.604991]  ? printk+0x3e/0x46
[    1.607197]  mount_block_root+0x174/0x223
[    1.609463]  ? set_debug_rodata+0xc/0xc
[    1.611851]  mount_root+0xfb/0x104
[    1.614159]  prepare_namespace+0x130/0x166
[    1.616415]  kernel_init_freeable+0x1c5/0x1d7
[    1.618676]  ? rest_init+0xb0/0xb0
[    1.621326]  kernel_init+0x5/0xf0
[    1.623768]  ret_from_fork+0x35/0x40
[    1.627035] Kernel Offset: 0x24800000 from 0xffffffff81000000 (relocation range: 0xffffffff80000000-0xffffffffbfffffff)
[    1.632240] ---[ end Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(8,17) 

有人可以指导将磁盘连接到qemu中以解决此问题的正确方法,还是我需要在GRUB配置中更改某些内容?

编辑

这次我根据@stark的建议用分区表创建了磁盘映像。

$ dd if=/dev/zero of=rootfs.img bs=1M count=10

#Using fdisk,created a partition of type Linux (83). This is how it looks now
$ fdisk -l rootfs.img
Disk rootfs.img: 10 MiB,10485760 bytes,20480 sectors
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: 0xc08ab9b0

Device      Boot Start   End Sectors Size Id Type
rootfs.img1       2048 20479   18432   9M 83 Linux

$ sudo losetup -o 1048576 --sizelimit 10485248 /dev/loop23 rootfs.img
$ sudo mkfs.ext2 /dev/loop23
$ sudo mount /dev/loop23 /mnt/rootfs
$ sudo cp -vrf busybox/_install/* /mnt/rootfs/
$ sudo umount /mnt/rootfs
$ sudo losetup -d /dev/loop23

现在我再次使用与上面相同的命令启动内核,但是这次内核只是挂起了:

[    1.593046] EXT4-fs (sdb1): couldn't mount as ext3 due to feature incompatibilities
[    1.599792] EXT4-fs (sdb1): mounting ext2 file system using the ext4 subsystem
[    1.614366] EXT4-fs (sdb1): mounted filesystem without journal. Opts: (null)
[    1.618589] VFS: Mounted root (ext2 filesystem) readonly on device 8:17.
[    1.626643] devtmpfs: mounted
[    1.665158] Freeing unused kernel memory: 1216K
[    1.667934] Write protecting the kernel read-only data: 18432k
[    1.670772] tsc: Refined TSC clocksource calibration: 2207.976 MHz
[    1.673349] clocksource: tsc: mask: 0xffffffffffffffff max_cycles: 0x1fd3a19a709,max_idle_ns: 440795259673 ns
[    1.681441] Freeing unused kernel memory: 2004K
[    1.693107] Freeing unused kernel memory: 1080K
[    1.836603] modprobe (1041) used greatest stack depth: 14072 bytes left
[    1.877586] rcS (1037) used greatest stack depth: 13984 bytes left
[    2.683414] clocksource: Switched to clocksource ts
[  333.025228] kworker/dying (178) used greatest stack depth: 13944 bytes left

没有其他输出。我在rootfs.img中创建了etc/init.d/rcS,其内容如下:

#!/bin/sh
mount -t sysfs none /sys
mount -t configfs none /config
mount -t devtmpfs none /dev

echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo "+++++++++++++++++++++++++++++++ HELLOO ++++++++++++++++++++++++++++++"
echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"

EDIT2

我从GRUB配置中删除了console=tty0,并将内核引导到了shell。但是我不确定为什么会导致在此虚拟环境中挂起?

我在想,由于tty0代表当前显示,并且因为-display none设置了qemu,所以这种混淆会产生一些冲突,因此会出现此问题...

但是请有人对此问题给出正确而具体的结论,以便将来该话题对某人有用。

解决方法

您在整个卷(文件)而不是分区上创建了文件系统。如果要在分区上使用它,则必须在文件中写入一个分区表,然后在该分区中创建文件系统。
编辑。不确定grub是否可以在没有GPT或MBR的情况下启动。

要使用此映像进行分区,请创建一个更大的1MB文件,在第一个兆字节上写入GPT分区表,并使用dd将映像复制到其余映像。

dd obs=1M seek=1 rootfs.img newroot.img

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)> insert overwrite table dwd_trade_cart_add_inc > select data.id, > data.user_id, > data.course_id, > date_format(
错误1 hive (edu)> insert into huanhuan values(1,'haoge'); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive> show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 <configuration> <property> <name>yarn.nodemanager.res