Accessing the contents of a KVM disk image file

KVM can use either an entire partition to simulate a disk for the guest operating system, or a single file. By default the file type used is a "raw" file compatible with other virtualization systems, like Xen.

In order to mount the image file of the guest OS we need to first map a loop device to it.
The command  losetup is used to associate files with loop devices. First check what the next available loopback device is with the  -f argument which is used to find the next available device:

1
2
[root@server1 ~] losetup -f
/dev/loop7

In this case there are no other loop devices and /dev/loop7 is available. This can then be associated with the VM image file:

1
[root@server1 ~] losetup /dev/loop7 /opt/vm/centos .img

Within a single image file there may be multiple partitions (e.g. /, /boot, etc.). The partition table can be read from the new (loop) device and individual devices for each partition created. This is done with the kpartx command (it creates map devices from device partition tables):

1
2
3
[root@server1 ~] kpartx -av /dev/loop7
add map loop0p1 : 0 7727202 linear /dev/loop7 45
add map loop0p3 : 0 449757 linear /dev/loop7 4756328

This created the following files in /dev/mapper directory:

1
2
3
[root@server1 ~] ls -la /dev/mapper
brw-rw---- 1 root disk 253, 4 2007-11-24 14:56 loop7p1
brw-rw---- 1 root disk 253, 5 2007-11-24 14:56 loop7p3

Now we can mount the files as we would normally do:

1
[root@server1 ~] mount /dev/mapper/loop7p1 /mnt

To revert back execute the following in this order:

1
2
3
4
5
[root@server1 ~] unmount /mnt
[root@server1 ~] kpartx -dv /dev/loop7
del devmap : loop7p1
del devmap : loop7p3
[root@server1 ~] losetup -d /dev/loop7
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值