qemu 操作

2023-11-20  本文已影响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 /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.
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 /dev/nbd12
# 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
上一篇 下一篇

猜你喜欢

热点阅读