挂载虚拟机磁盘文件

挂载虚拟机磁盘文件

我们想在宿主机上直接查看vm的虚拟机磁盘文件,并读取里面的内容,需要怎么做呢?

方式一:使用loop 挂载

通过fdisk -lu 查看镜像中每个分区的偏移量,然后通过loop挂载

[root@centos images]# cd /var/lib/libvirt/images/
[root@centos images]# fdisk -lu ubuntu.raw 
You must set cylinders.
You can do this from the extra functions menu.

Disk ubuntu.raw: 0 MB, 0 bytes
255 heads, 63 sectors/track, 0 cylinders, total 0 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: 0x000ccae5

     Device Boot      Start         End      Blocks   Id  System
ubuntu.raw1   *        2048    38862847    19430400   83  Linux
Partition 1 has different physical/logical endings:
     phys=(1023, 254, 63) logical=(2419, 25, 38)
ubuntu.raw2        38864894    40957951     1046529    5  Extended
Partition 2 has different physical/logical beginnings (non-Linux?):
     phys=(1023, 254, 63) logical=(2419, 58, 6)
Partition 2 has different physical/logical endings:
     phys=(1023, 254, 63) logical=(2549, 131, 14)
ubuntu.raw5        38864896    40957951     1046528   82  Linux swap / Solaris
[root@centos images]# echo $((2048*512))
1048576
[root@centos images]# mount -o loop,offset=1048576 ubuntu.raw /image/
[root@centos images]# umount /image/

方式二:使用kpartx挂载

基础平台要求是linux环境。
需要用到的工具: losetup, kpartx, mount

##步骤
# 获取可以挂载的环回设备:
[root@virtserver ~]# losetup -f
/dev/loop0
# 进行块设备挂载:
losetup /dev/loop0 abc.img
# 执行块设备的分区映射:
kpartx -a /dev/loop0
# 查看映射分区入口:
ls /dev/mapper/ 
# 通过fdisk -l查看磁盘情况。找到需要挂载的数据分区。
# mount分区:
mount /dev/mapper/vg_lbrhel-lv_root /mnt/  

移除挂载

去挂载步骤:
1.umount
2.kpartx -d
3.losetup -d
原文地址:https://www.cnblogs.com/yanling-coder/p/11887492.html