Detailed description of VM access by Linux Host

Source: Internet
Author: User

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 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.

  1. Easy configuration of Linux dual-nic virtual Routing
  2. How to compile Linux/Unix code in Windows
  3. In the future, it will be a Linux virtual technology.
  4. Choose Linux free Virtual Machine
  5. Teach you the hard drive read/write speed in Linux

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.