[笔记] wsl2 下使用 qemu/grub 模拟系统启动(多分区)

首先创建一块虚拟硬盘:

dd bs=512 count=204800 if=/dev/zero of=hd.img

然后使用 losetup -f 将其关联为本地回环设备

losetup -f hd.img
# 关联 hd.img 到空闲回环设备上
losetup -a
/dev/loop0: [2080]:409 (/root/code/demo05/hd.img)
# 查看刚刚关联到了哪里,这里关联到了 /dev/loop0 回环设备文件上

使用系统工具 fdisk 为虚拟硬盘创建分区系统,这里选择使用 GPT 分区系统,并创建了两块分区.

这里需要注意不要配置为 MBR 分区系统,会导致系统无法启动,grub-install 目前没找到如何设置支持 MBR 分区系统.

> fdisk hd.img
ghimi-surface# fdisk hd.img

Welcome to fdisk (util-linux 2.37.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x7b76f30f.

Command (m for help): g # 创建分区系统为 gpt 格式
Created a new GPT disklabel (GUID: AC9ECA30-457D-1742-B3B7-75E228F7F59B).

Command (m for help): n # 新建分区
Partition number (1-128, default 1): # 分区号
First sector (2048-204766, default 2048): # 起始扇区
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-204766, default 204766): +50M
# 结束扇区
Created a new partition 1 of type 'Linux filesystem' and of size 50 MiB.

Command (m for help): n # 新建分区
Partition number (2-128, default 2): # 分区号
First sector (104448-204766, default 104448): # 起始扇区
Last sector, +/-sectors or +/-size{K,M,G,T,P} (104448-204766, default 204766):
# 结束扇区
Created a new partition 2 of type 'Linux filesystem' and of size 49 MiB.

Command (m for help): t # 修改分区类型
Partition number (1,2, default 2): 1 
Partition type or alias (type L to list all): 4 # 设置分区类型为 BIOS boot 类型

Changed type of partition 'Linux filesystem' to 'BIOS boot'.

Command (m for help): p # 打印当前分区结果
Disk hd.img: 100 MiB, 104857600 bytes, 204800 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: gpt
Disk identifier: AC9ECA30-457D-1742-B3B7-75E228F7F59B

Device      Start    End Sectors Size Type
hd.img1      2048 104447  102400  50M BIOS boot # 注意这里类型为 BIOS boot 类型
hd.img2    104448 204766  100319  49M Linux filesystem

Command (m for help): w # 同步分区结果到虚拟硬盘文件中
The partition table has been altered.

在创建完成后通过 kpartx -av 识别分区信息:

kpartx -av /dev/loop0
add map loop0p1 (252:0): 0 32768 linear 7:0 2048
add map loop0p2 (252:1): 0 67584 linear 7:0 34816
# -a 添加映射到系统设备文件中
# -v 详细打印映射结果

然后在 /dev/mapper 目录下可以看到刚刚识别的分区信息

ls -l /dev/mapper/loop*
brw------- 1 root root 252, 0 Dec 11 22:56 /dev/mapper/loop0p1
brw------- 1 root root 252, 1 Dec 11 22:56 /dev/mapper/loop0p2
# 或者通过 kpartx 查看
> kpartx -l /dev/loop0
loop0p1 : 0 102400 /dev/loop0 2048
loop0p2 : 0 100319 /dev/loop0 104448

现在我们可以为这两个分区进行格式化.

> mkfs.ext4 /dev/mapper/loop0p1
mke2fs 1.46.5 (30-Dec-2021)
Discarding device blocks: done
Creating filesystem with 8448 4k blocks and 8448 inodes

Allocating group tables: done
Writing inode tables: done
Creating journal (1024 blocks): done
Writing superblocks and filesystem accounting information: done

接下来挂载启动分区 /dev/mapper/loop0p1 ,为其安装 grub:

> mount /dev/mapper/loop0p1 /mnt

为其安装 grub:

> grub-install --boot-directory=/mnt/boot --no-floppy --target i386-pc --force /dev/loop0
# --boot-directory 指定启动目录,未来内核存放的位置
# --no-floppy 不设置软盘
# --force 发生警告不停止安装
# /dev/loop0 注意这里指定的是磁盘而不是分区
# --target i386-pc 指定安装目标虚拟机类型,这里设置了 x86 格式,没有选择 efi格式
# grub-install --boot-directory=hdisk/boot --no-floppy /dev/loop0
Installing for i386-pc platform.
Installation finished. No error reported.

接下来就可以通过 qemu 启动这块虚拟硬盘了:

qemu-system-x86_64 -drive file=hd.img,media=disk,format=raw,index=0
# drive 指定设备
# file=hd.img 指定虚拟磁盘文件
# media=disk 指定设备类型为硬盘
# format=raw 指定虚拟磁盘格式为 raw 格式,其他还有 vhdi,vdx,qcow2等格式
# index=0 指定设备索引号(总线中的识别顺序)为0
# 也可以通过指定选项 -hda 启动
# --nographic 指定不通过窗口启动
qemu-system-x86_64 -hda hd.img

目前启动后报 unknown filesystem ,还在排查是什么原因导致的,如果有大神欢迎评论区答疑解惑.
[笔记] wsl2 下使用 qemu/grub 模拟系统启动(多分区)_第1张图片

qemu-system-x86_64 --nographic 退出方式

先按下 Ctrl + A 然后按下 X.

操作回滚

umount hdisk
# 移除子分区关联映射
> kpartx -dv /dev/loop0
del devmap : loop0p1
del devmap : loop0p2
# 移除回环设备关联映射
losetup -D

参考资料

  • The GRUB2 Bootloader – Installation and Configuration

你可能感兴趣的:(数据库,linux,ffmpeg)