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
|