美文网首页
qemu 操作

qemu 操作

作者: 河码匠 | 来源:发表于2023-11-20 10:19 被阅读0次

1. qcow2 转 raw qemu-img convert

qemu-img convert -f qcow2 xxx.qcow2 -O raw disk.img

2. 查看镜像信息 qemu-img info

# qemu-img info disk
image: disk
file format: raw
virtual size: 20G (21474836480 bytes)
disk size: 6.4G

3. 挂在虚拟镜像文件 qemu-nbd

这里需要 nbd 内核模块。如果 /dev/ 中没有 nbd0.... 使用 modprobe nbd 载入

  • 挂载镜像 qemu-nbd -c
# qemu-nbd -c /dev/nbd12 /images/test-img/disk

WARNING: Image format was not specified for '/images/test-img/disk' and probing guessed raw.
         Automatically detecting the format is dangerous for raw images, write operations on block 0 will be restricted.
         Specify the 'raw' format explicitly to remove the restrictions.
  • fdisk -l 查看是否挂在成功
root@TEST-01:/images/test-img# fdisk -l

Disk /dev/nbd12: 20 GiB, 21474836480 bytes, 41943040 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: 0x373a3f85

Device       Boot   Start      End  Sectors  Size Id Type
/dev/nbd12p1         2048  3905535  3903488  1.9G 82 Linux swa
  • 使用 partx -a 告诉内核当前磁盘的分区情况。才能使用 mount 挂载
# partx -a /dev/nbd12
  • mount 挂载分区
# mount /dev/nbd12p2 /images/test-img/mount/

# ll mount/

total 116
drwxr-xr-x 24 root root  4096 Sep 25 18:04 ./
drwxr-xr-x  3 root root  4096 Nov 21 10:05 ../
drwxr-xr-x  2 root root  4096 Aug 21  2018 bin/
drwxr-xr-x  3 root root  4096 Aug 21  2018 boot/
drwxr-xr-x  2 root root  4096 Sep 25 18:04 data/
drwxr-xr-x  4 root root  4096 Aug 21  2018 dev/
.......
  • 卸载虚拟镜像

\color{red}{注意}:先 umountqemu-nbd -d。否则 umount 会出问题

# umount /dev/nbd12p2
# qemu-nbd -d /dev/nbd12
/dev/nbd12 disconnected

相关文章

网友评论

      本文标题:qemu 操作

      本文链接:https://www.haomeiwen.com/subject/htsdxrtx.html