虚拟磁盘的格式有很多,这里不一一列举,简单介绍下linux下的常用格式raw,qcow2
在实际使用过程中可能会碰到这样一种情况,因某种原因导致虚拟机无法启动,但又需要读取虚拟机内的文件,可以通过挂载虚拟磁盘来进行读取,并且可以直接写入。
RAW
KVM官方默认的虚拟磁盘文件格式为RAW。
对于RAW格式可以通过losetup工具来模拟出一个新设备加以挂载。
示例:挂载rhel7-mode.img
A.标准分区
root@jun-live:images#pwd
/var/lib/libvirt/images
root@jun-live:images#qemu-img info rhel7-mode.img
image: rhel7-mode.img
file format:raw
virtual size: 8.0G (8589934592 bytes)
disk size: 3.5G
1.将rhel7-mode.img映射为loop10
root@jun-live:images#losetup /dev/loop10 rhel7-mode.img
root@jun-live:images#ls /dev/mapper/loop10*
ls: cannot access /dev/mapper/loop10*:No such file or directory
此时,内核没有识别到该设备的分区表,和普通硬盘刚分完区后重新加载分区表类似,需要kpartx -a
2.加载设备分区表
root@jun-live:images#kpartx -l rhel7-mode.img
loop18p1: 0 409600 /dev/loop18 2048
loop18p2: 0 16365568 /dev/loop18 411648
loop: device /dev/loop18 still in use, retrying delete
loop deleted : /dev/loop18
通过kpartx -l我们其实就已经可以看到,rhel7-mode.img包含两个分区,并且自动映射在loop18上,但loop18已经被其它镜像所使用,所以报了个资源忙。
root@jun-live:images#kpartx -a /dev/loop10
root@jun-live:images#ls /dev/mapper/loop10*
/dev/mapper/loop10p1@ /dev/mapper/loop10p2@
root@jun-live:images#fdisk -u -l /dev/loop10
Disk /dev/loop10: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders, total 16777216 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
Disk identifier: 0x00067969
Device Boot Start End Blocks Id System
/dev/loop10p1 * 2048 411647 204800 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/loop10p241164816777215 8182784 8e Linux LVM
可以看到,有两个分区(实际上loop10p1为boot分区,loop10p2为LVM)
3.挂载标准分区
root@jun-live:images#mount /dev/mapper/loop10p1 /mnt/iso/
root@jun-live:images#mount|grep /mnt/iso
/dev/mapper/loop10p1 on /mnt/iso typexfs(rw)
root@jun-live:images#ls /mnt/iso/
config-3.10.0-123.el7.x86_64
grub2/
initramfs-0-rescue-ddcdf1804942b72473cc9c4ae83b8ef1.img
initramfs-3.10.0-123.el7.x86_64.img
initramfs-3.10.0-123.el7.x86_64kdump.img
initrd-plymouth.img
symvers-3.10.0-123.el7.x86_64.gz
System.map-3.10.0-123.el7.x86_64
vmlinuz-0-rescue-ddcdf1804942b72473cc9c4ae83b8ef1*
vmlinuz-3.10.0-123.el7.x86_64*
4.测试写入
root@jun-live:images#touch /mnt/iso/test.losetup
root@jun-live:images#ls /mnt/iso/
config-3.10.0-123.el7.x86_64
grub2/
initramfs-0-rescue-ddcdf1804942b72473cc9c4ae83b8ef1.img
initramfs-3.10.0-123.el7.x86_64.img
initramfs-3.10.0-123.el7.x86_64kdump.img
initrd-plymouth.img
symvers-3.10.0-123.el7.x86_64.gz
System.map-3.10.0-123.el7.x86_64
test.losetup
vmlinuz-0-rescue-ddcdf1804942b72473cc9c4ae83b8ef1*
vmlinuz-3.10.0-123.el7.x86_64*
可以直接读写,再来看看LVM
B.RAW---lvm
1.取消设备映射
root@jun-live:images#umount /mnt/iso/
root@jun-live:images#mount /dev/mapper/loop10p2 /mnt/iso/
mount: unknown filesystem type 'LVM2_member'
LVM不能直接挂载,需要作特殊处理。
root@jun-live:images#kpartx -d /dev/loop10
root@jun-live:images#losetup -d /dev/loop10
2.通过偏移量重新映射
fdisk -u 参数指明使用 sector 为单位记录偏移量(offset)而不是 cylinder 为单位
root@jun-live:images#bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, , Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
411648*512
210763776
quit
root@jun-live:images#losetup /dev/loop10 rhel7-mode.img -o 210763776
3.lvm扫描
确认卷组名是否重复,如果有重复需要重命名
root@jun-live:images#pvscan
WARNING: Duplicate VG name vg0: Existing 2gwi76-SA4G-sf4y-B3qd-yIbZ-P7e8-J2U7fP (created here) takes precedence over rJGSsG-3ebw-8c2c-88bM-Am6M-Xycu-WNcQsh
PV /dev/loop10 VG vg0 lvm2 [7.80 GiB / 3.11 GiB free]
PV /dev/sda8 VG vg1 lvm2 [29.80 GiB / 19.80 GiB free]
PV /dev/sda5 VG vg0lvm2 [100.00 GiB / 12.44 GiB free]
Total: 3 [137.61 GiB] / in use: 3 [137.61 GiB] / in no VG: 0 [0 ]
可以看到,该RAW设备里的卷组名也叫vg0,和我本地物理机的卷组重名了,这个时候需要根据卷组的UUID来重命名。
root@jun-live:images#vgs -v
Finding all volume groups
... ...
WARNING: Duplicate VG name vg0: 2gwi76-SA4G-sf4y-B3qd-yIbZ-P7e8-J2U7fP (created here) takes precedence over rJGSsG-3ebw-8c2c-88bM-Am6M-Xycu-WNcQsh
Archiving volume group "vg0" metadata (seqno 4).
Archiving volume group "vg0" metadata (seqno 113).
Creating volume group backup "/etc/lvm/backup/vg0" (seqno 113).
VG Attr Ext #PV #LV #SN VSize VFree VG UUID VProfile
vg0wz--n- 4.00m 1 3 0 7.80g 3.11grJGSsG-3ebw-8c2c-88bM-Am6M-Xycu-WNcQsh
vg0wz--n- 4.00m 1 9 1 100.00g 12.44g 2gwi76-SA4G-sf4y-B3qd-yIbZ-P7e8-J2U7fP
vg1 wz--n- 4.00m 1 1 0 29.80g 19.80g suc7X2-gTB4-2QZd-YS76-hUEs-9wcK-zLQPbz
root@jun-live:images#vgrename rJGSsG-3ebw-8c2c-88bM-Am6M-Xycu-WNcQsh virt_vg0
WARNING: Duplicate VG name vg0: Existing 2gwi76-SA4G-sf4y-B3qd-yIbZ-P7e8-J2U7fP (created here) takes precedence over rJGSsG-3ebw-8c2c-88bM-Am6M-Xycu-WNcQsh
... ...
Volume group "vg0" successfully renamed to "virt_vg0"
4.激活lvm
root@jun-live:images#lvscan
inactive '/dev/virt_vg0/swap' [512.00 MiB] inherit
inactive '/dev/virt_vg0/root' [4.00 GiB] inherit
inactive '/dev/virt_vg0/home' [200.00 MiB] inherit
ACTIVE '/dev/vg1/ubuntu_root' [10.00 GiB] inherit
ACTIVE '/dev/vg0/swap' [1.00 GiB] inherit
ACTIVE '/dev/vg0/ct6_home' [500.00 MiB] inherit
ACTIVE '/dev/vg0/ct6_root' [8.00 GiB] inherit
ACTIVE Original '/dev/vg0/winr2' [40.00 GiB] inherit
ACTIVE Snapshot '/dev/vg0/winr2-snap' [12.00 GiB] inherit
ACTIVE '/dev/vg0/virt' [25.00 GiB] inherit
ACTIVE '/dev/vg0/test' [500.00 MiB] inherit
ACTIVE '/dev/vg0/xfs' [500.00 MiB] inherit
ACTIVE '/dev/vg0/xfs_journal' [100.00 MiB] inherit
root@jun-live:images#vgchange -ay virt_vg0
3 logical volume(s) in volume group "virt_vg0" now active
root@jun-live:images#lvscan|grep virt_vg0
ACTIVE '/dev/virt_vg0/swap' [512.00 MiB] inherit
ACTIVE '/dev/virt_vg0/root' [4.00 GiB] inherit
ACTIVE '/dev/virt_vg0/home' [200.00 MiB] inherit
5.挂载lvm
root@jun-live:images#mount /dev/virt_vg0/root /mnt/iso/
root@jun-live:images#ls /mnt/iso/
bin@ dev/ home/ lib64@ mnt/ proc/ run/ srv/ tmp/ var/
boot/ etc/ lib@ media/ opt/ root/ sbin@ sys/ usr/
root@jun-live:images#mount|grep /mnt/iso
/dev/mapper/virt_vg0-rooton /mnt/iso type xfs (rw)
6.读写完毕后还原
root@jun-live:images#umount /mnt/iso
root@jun-live:images#lvchange -an virt_vg0
root@jun-live:images#kpartx -d /dev/loop10
root@jun-live:images#losetup -d /dev/loop10
补充:
对于还没有分区的RAW虚拟磁盘可以直接以loop格式挂载:
mount -o loop foo.raw /mnt
qcow2
[root@test ~]#lsb_release -a
LSB Version::core-4.1-amd64:core-4.1-noarch
Distributor ID: CentOS
Description: CentOS Linux release 7.0.1406 (Core)
Release:7.0.1406
Codename: Core
[root@test ~]#uname -a
Linux test3.10.0-123.el7.x86_64 #1 SMP Mon Jun 30 12:09:22 UTC x86_64 x86_64 x86_64 GNU/Linux先确认内核是否支持CONFIG_BLK_DEV_NBD模块
[root@test boot]# grep NBD /boot/config-3.10.0-123.el7.x86_64
# CONFIG_BLK_DEV_NBD is not set
发现CentOS 7默认不支持,怎么办,重新编译内核模块
[root@test boot]# yum -y install gcc gcc-c++ make kernel-devel ncurses-develelfutils-libelf-devel
[root@test boot]# cd /usr/src/kernels/3.10.0-123.el7.x86_64/
[root@test 3.10.0-123.el7.x86_64]# cp -f /boot/config-3.10.0-123.el7.x86_64 .config
[root@test 3.10.0-123.el7.x86_64]# vim .config
CONFIG_BLK_DEV_NBD=m
更详细的内核及模块编译方法,请参看:Ubuntu14.04升级内核3.14.25
Device Drivers ---> Block devices ---> Network block device support
编译安装完成后,再次尝试,映射原理同上面的RAW
[root@test boot]#grep NBD /boot/config-3.10.0-123.el7.x86_64
CONFIG_BLK_DEV_NBD=m
或直接编译内核模块
福利:CentOS 其他版本的source rpm这里都可以找到,如kpatch, php等
以CentOS7.2 3.10.0-327为例
mv /usr/src/kernels/$(uname -r) /usr/src/kernels/$(uname -r)-old
rpm -ivh /7.2.1511/os/Source/SPackages/kernel-3.10.0-327.el7.src.rpm
tar -xvf ~/rpmbuild/SOURCES/linux-3.10.0-327.el7.tar.xz
mv linux-3.10.0-327.el7 /usr/src/kernels/$(uname -r)
cd /usr/src/kernels/$(uname -r)
make mrproper
cp -f /usr/src/kernels/$(uname -r)-old/Module.symvers /usr/src/kernels/$(uname -r)
cp -f /boot/config-$(uname -r) /usr/src/kernels/$(uname -r)/.config
make oldconfig
make prepare
make scripts
make CONFIG_BLK_DEV_NBD=m M=drivers/block
cp -f /usr/src/kernels/$(uname -r)/drivers/block/nbd.ko /lib/modules/$(uname -r)/kernel/drivers/block/
depmod -a
modprobe nbd
modinfo nbd
qemu-nbd -b 0.0.0.0 -p 10809 -e 10 -t centos7-s1.qcow2
/usr/libexec/qemu-kvm -enable-kvm -name demo -m 1024 -hda nbd:192.168.130.254:10809 -vnc :19 -snapshot
qemu-nbd -k /tmp/nbd.sock -e 10 -t centos7-s1.qcow2
/usr/libexec/qemu-kvm -enable-kvm -name demo -m 1024 -hda nbd:unix:/tmp/nbd.sock -vnc :19 -snapshot