Microsoft's Windows may no longer be mainstream. In today's society, Linux is becoming more and more popular. Are you the application of Linux? This article describes in detail how Linux Host accesses the hard disk image of a Virtual Machine (KVM) and plays a certain role in learning how to access the hard disk image of a Virtual Machine (KVM) on a Linux Host.
When using a virtual machine, you often need to access the content of a hard disk image without enabling the virtual machine. For KVM, the simplest method is to use the mount-o loop command to load the image content. However, this method has limits. If the image file has partitions, it will not work.
Of course, you can use the mount-o loop, offset = method to specify an offset to solve this problem. However, you must know the start position of the partition in advance.
After exploration, we finally found that using losetup and kpartx can solve this problem.
The following describes the losetup command. If you execute the following command
Code: ls/dev/loop?
You can see that the/dev directory contains many loop0, loop1, and so on. These are Linux "loop devices ". After the setting, the loop device can imitate the disk devices, such as sda and hda, but the access to the device is actually redirected to a file. To set a loop device, use the losetup command. Run this command with the root permission.
Code: # associate the loop file to the device
Losetup [loop device] File Name
# Remove links between loop devices and files
Losetup [loop device]-d
# Find the next loop device with no associated files
Losetup-f
Losetup-f is particularly useful when writing scripts, because it returns an unused loop device, and then you can save the device name in a variable, for subsequent commands.
So far, the situation is no different from mount-o loop. Although you can immediately mount the loop device that just joined the file, it is equivalent to mount-o loop. However, we have kpartx.
If kpartx is not installed on your system by default, install it first.
Code: sudo apt-get install kpartx
The kpartx command allows the Linux kernel to read a partition table on a device and then generate a device that represents the corresponding partition. Of course, it also requires the root permission to run. Usage:
Code: # list the partitions and names of the refreshed Devices
Kpartx [device]
# Refresh the partition table and generate a device
Kpartx [device]-
# Deleting all partition devices on a specified device
Kpartx [device]-d
For loop devices, the kpartx command can generate partition devices in the format of/dev/mapper/loop0p1.
In this way, with the kpartx command, you can use the mount command to load the partitions in the image file.
In this way, the Linux Host accesses the hard disk image of the Virtual Machine (KVM.
- Easy configuration of Linux dual-nic virtual Routing
- How to compile Linux/Unix code in Windows
- In the future, it will be a Linux virtual technology.
- Choose Linux free Virtual Machine
- Teach you the hard drive read/write speed in Linux