Bobcares

Kpartx guest disk recovery & more

by | Nov 18, 2021

Kpartx guest disk recovery made easy with Bobcares by your side.

At Bobcares, we have a solution to every query as a part of our Server Management Service.

Let’s take a look at how our skilled Support Team recently helped a customer retrieve their data from their hard disk.

Whats is kpartx?

Recovering data from the hard disk is not an impossible task as long as you have access to the right tools and possess the right skills. Fortunately, for one of our customers who wanted to recover their data from their hard disk, our Support Team was up to the task.

Kpartx, derived from util-linux’ party can read partition tables on a specific device. It can also be used to create device maps over detected partition segments. Furthermore, kpartx is called from hotplug when device maps are created or deleted.

How to use kpartx for guest disk recovery ?

In this scenario, kpartx can be used to mount the guest file system as the loop system which is then accessible. Each guest storage image lays claim to a partition table embedded in the file.

Our Support Techs suggest using libguestfs and guestfish packages for access and advanced modification of the guest file systems.

Remember to ensure that guests are offline before reading their files unless you want to wind up with data loss or damage.

  1. First, install kpartx package with the following command:
     # yum install kpartx
  2. Then, list the partition device mappings that are attached to file-based storage image with the following command:
    # kpartx -l /var/lib/libvirt/images/guest.img 
    loop0p1 : 0 409600 /dev/loop0 63 
    loop0p2 : 0 10064717 /dev/loop0 409663

    Here, guest represents a Linux guest. We would also like to point out that the first partition is the boot partition and the second partition is an EXT3 that contains the root partition.

  3. After that, we will add the partition mappings to the specified devices in /dev/mapper with the following command:
     # kpartx -a /var/lib/libvirt/images/guest.img

    We will verify that the partition mapping was successful by checking for new devices in the /dev/mapper/ directory with this command:

    # ls /dev/mapper/ loop0p1 loop0p2

    The image mappings will follow the loopXpY naming format.

  4. Next, we will mount the loop device to a directory as seen below:
     # mkdir /mnt/guest 
    # mount /dev/mapper/loop0p1 /mnt/guest -o loop,ro

    In the above example, the directory is first created before mounting the loop device.

  5. Then, we can opt to read or copy the files available for reading in the /mnt/guest directory.
  6. After that, we will unmount the loop device to allow the guest to reuse the guest image.
    # umount /mnt/tmp
  7. Finally, we will disconnect the image file from the partition mappings with this command:
     # kpartx -d /var/lib/libvirt/images/guest.img

Kpartx guest disk recovery from guest LVM volumes

If you are trying to read data on LVM volumes on virtual storage images, our Support Engineers suggest these steps:

  1. First, add the guest.img partition mappings to the recognized devices in the /dev/mapper/ directory as seen below:
    # kpartx -a /var/lib/libvirt/images/guest.img
  2. Then, use the vgscan command to find new volume groups as seen below:
     # vgscan
    Reading all physical volumes. 
    This may take a while... 
    Found volume group "VolGroup00" using metadata type lvm2
    
  3. Next, we will activate the volume group on the partition with the following command:
    # vgchange -ay VolGroup00 2
    logical volumes in volume group VolGroup00 now active
  4. After that, we will display information about the new volumes with lvs command.
  5. Then, mount volume in the directory. For instance:
    # mount /dev/VolGroup00/LogVol00 /mnt/guestboot
  6. Now, you can read or copy the files in the /mnt/guestboot directory.
  7. After that, unmount the device to allow the guest to reuse the guest image. For instance:
    # umount /mnt/
  8. Then, disconnect the volume group as seen below:
    # vgchange -an VolGroup00
  9. Finally, disconnect the partition mappings and the image file with this command:
    # kpartx -d /var/lib/libvirt/images/guest.img
    

[Need additional assistance? Give us a call today.]

Conclusion

In brief, we learned how to retrieve data from a hard disk by using kpartx. The Support Engineers demonstrated how to access data on LVM volumes on virtual storage images as well.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.