将grub2安装到qemu上

蔡楚
2023-12-01

环境:ubuntu14.04 qemu-2.0.0

1.创建磁盘文件
sudo dd if=/dev/zero of=disk.img bs=4096 count=8192

2.将磁盘文件虚拟为循环块设备
sudo losetup /dev/loop1 disk.img

3.对磁盘文件分区
sudo fdisk /dev/loop1
命令执行结果:
命令(输入 m 获取帮助): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
分区号 (1-4,默认为 1):
将使用默认值 1
起始 sector (2048-65535,默认为 2048):
将使用默认值 2048
Last sector, +扇区 or +size{K,M,G} (2048-65535,默认为 65535):
将使用默认值 65535
命令(输入 m 获取帮助): wq
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 22: 无效的参数.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
4.对磁盘文件格式化
sudo mkfs.ext4 /dev/loop1
命令执行结果:
mke2fs 1.42.9 (4-Feb-2014)
Discarding device blocks: 完成
文件系统标签=
OS type: Linux
块大小=1024 (log=0)
分块大小=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
8192 inodes, 32768 blocks
1638 blocks (5.00%) reserved for the super user
第一个数据块=1
Maximum filesystem blocks=33554432
4 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
8193, 24577
Allocating group tables: 完成
正在写入inode表: 完成
Creating journal (4096 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
5.挂载磁盘文件到本地文件系统
sudo mount /dev/loop1 /mnt

6.创建启动目录
sudo mkdir /mnt/boot

7.安装grub2到启动目录
sudo grub-install –boot-directory=/mnt/boot –force dev/loop1
命令执行结果:
Installing for i386-pc platform.
grub-install: warning: File system `ext2’ doesn’t support embedding.
grub-install: warning: 无法嵌入。在此次安装中 GRUB 只能通过使用块列表安装。但是块列表是不可信赖的,不推荐使用。
Installation finished. No error reported.

8.解挂载
sudo umount /mnt

9.解设备虚拟
sudo losetup -d /dev/loop1

10.运行启动qemu
sudo qemu-system-i386 -hda disk.img

 类似资料: