SUSE Conversations


Mount/access Files Residing on Xen Virtual Machines

variia

By: variia

August 30, 2007 12:33 pm

Reads:1448

Comments:0

Rating:0

This tip explains how to access files residing on Xen virtual machines. This can be a life saver when for instance the VM is not bootable and you need to restore or edit some files quickly to fix it.

The solution can vary depending upon the complexity of your setup therefore what I am presenting here is based on native Linux partitions respect to the domU.

Problem:

Need access to the file system, perhaps backup or restore files residing on Xen virtual machines from the dom0 host.

Solution:

  1. Block device (physical partition) users:Use kpartx which creates device maps from partitions.

Example:

The block device can be anything such as LVM chunk, EVMS cluster managed volume or a simple native partition respect to the dom0.

Ensure you have multipath-tools installed:

dom0:~ # rpm -q multipath-tools || yast2 -i multipath-tools

Check the partitioning on the block device:

dom0:~ # fdisk -l /dev/evms/san2/vm3
Disk /dev/evms/san2/vm3: 5368 MB, 5368708096 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

              Device Boot      Start         End      Blocks   Id  System
/dev/evms/san2/vm3p1   *           1           9       72261   83  Linux
/dev/evms/san2/vm3p2              10         652     5164897+   f  W95 Ext'd (LBA)
/dev/evms/san2/vm3p5              10          75      530113+  82  Linux swap / Solaris
/dev/evms/san2/vm3p6              76         493     3357553+  83  Linux
/dev/evms/san2/vm3p7             494         652     1277136   83  Linux

Create maps from the block device first:

dom0:~ # kpartx -a /dev/evms/san2/vm3

Maps are here:

dom0:~ # ls -l /dev/mapper
total 0
lrwxrwxrwx 1 root root      16 Aug 26 16:28 control -> ../device-mapper
brw------- 1 root root 253,  1 Aug 26 16:28 mpath1
brw------- 1 root root 253,  0 Aug 26 16:28 mpath2
brw------- 1 root root 253, 13 Aug 29 08:55 san2|vm3p1
brw------- 1 root root 253, 14 Aug 29 08:55 san2|vm3p2
brw------- 1 root root 253, 15 Aug 29 08:55 san2|vm3p5
brw------- 1 root root 253, 16 Aug 29 08:55 san2|vm3p6
brw------- 1 root root 253, 17 Aug 29 08:55 san2|vm3p7
The names look a bit strange but that's fine. Mount the individual partitions now:
dom0:~ # mount -o rw /dev/mapper/san2\|vm3p6 /mnt

I mounted the root partition with write option first under /mnt:

dom0:~ # ls -l /mnt
total 96
dr-xr-xr-x  3 root root  4096 Aug 28 10:12 automount
drwxr-xr-x  2 root root  4096 Aug 25 16:51 bin
drwxr-xr-x  2 root root  4096 Aug 25 16:45 boot
drwxr-xr-x  5 root root  4096 Aug 25 16:45 dev
drwxr-xr-x 69 root root  8192 Aug 29 08:53 etc
drwxr-xr-x  2 root root  4096 May  4 01:43 home
drwxr-xr-x 11 root root  4096 Aug 25 17:10 lib
drwx------  2 root root 16384 Aug 25 16:45 lost+found
drwxr-xr-x  2 root root  4096 May  4 01:43 media
drwxr-xr-x  3 root root  4096 Aug 28 15:08 mnt
drwxr-xr-x  4 root root  4096 Aug 25 16:49 opt
drwxr-xr-x  2 root root  4096 Aug 25 16:45 proc
drwx------ 10 root root  4096 Aug 28 14:56 root
drwxr-xr-x  3 root root  8192 Aug 25 16:52 sbin
drwxr-xr-x  4 root root  4096 Aug 25 16:45 srv
-rw-r--r--  1 root root     0 Aug 29 08:53 success
drwxr-xr-x  3 root root  4096 Aug 25 16:45 sys
drwxrwxrwt  6 root root  4096 Aug 29 08:49 tmp
drwxr-xr-x 12 root root  4096 Aug 25 16:50 usr
drwxr-xr-x  3 root root  4096 Aug 25 16:54 var

Do the remaining 2 partitions too:

dom0:~ # mount -o rw /dev/mapper/san2\|vm3p1 /mnt/boot
dom0:~ # mount -o rw /dev/mapper/san2\|vm3p7 /mnt/var

Now you can restore an entire system or just make an offline full backup, edit files, etc. When you finished unmount all partitions then remove the device maps too. At completion ensure they are all gone before you start the VM up:

dom0:~ # umount /mnt/var
dom0:~ # umount /mnt/boot
dom0:~ # umount /mnt
dom0:~ # kpartx -d /dev/evms/san2/vm3 

dom0:~ # ls -l /dev/mapper
total 0
lrwxrwxrwx 1 root root     16 Aug 26 16:28 control > ../device-mapper
brw------- 1 root root 253, 1 Aug 26 16:28 mpath1
brw------- 1 root root 253, 0 Aug 26 16:28 mpath2
  1. For users using file image disks:

The solution is very similar but we need to ?mount? the file image through loop device for being able to access its partitions first (note: loop0 may not be the first available loop device on your system):

dom0:~ # cd /etc/xen/images
dom0:/etc/xen/images # losetup /dev/loop0 vm1.img 
dom0:/etc/xen/images # losetup -a
/dev/loop0: [6806]:318859 (vm1.img)

dom0:/etc/xen/images # fdisk -l /dev/loop0 
Disk /dev/loop0: 1638 MB, 1638400000 bytes 
255 heads, 63 sectors/track, 199 cylinders 
Units = cylinders of 16065 * 512 = 8225280 bytes 
      Device Boot      Start         End      Blocks   Id  System 
/dev/loop0p1   *           1           9       72261   83  Linux 
/dev/loop0p2              10         199     1526175    5  Extended 
/dev/loop0p5              10          26      136521   82  Linux swap / Solaris 
/dev/loop0p6              27         151     1004031   83  Linux 
/dev/loop0p7             152         199      385528+  83  Linux 

dom0:/etc/xen/images # kpartx -a /dev/loop0 
dom0:/etc/xen/images # ls -l /dev/mapper/ 
total 0 
lrwxrwxrwx 1 root root      16 Sep 12 15:38 control -> ../device-mapper 
brw------- 1 root root 253, 15 Sep 30 13:19 loop0p1 
brw------- 1 root root 253, 16 Sep 30 13:19 loop0p2 
brw------- 1 root root 253, 17 Sep 30 13:19 loop0p5 
brw------- 1 root root 253, 18 Sep 30 13:19 loop0p6 
brw------- 1 root root 253, 19 Sep 30 13:19 loop0p7

Now you can mount, access, backup, restore, even format any of the partitions of the file image in the same fashion as I presented above:

dom0:/etc/xen/images # mount /dev/mapper/loop0p6 /mnt
dom0:/etc/xen/images # mount /dev/mapper/loop0p1 /mnt/boot
dom0:/etc/xen/images # mount /dev/mapper/loop0p7 /mnt/var
dom0:/etc/xen/images # mount 
-snip-
/dev/mapper/loop0p6 on /mnt type ext3 (rw) 
/dev/mapper/loop0p1 on /mnt/boot type ext2 (rw) 
/dev/mapper/loop0p7 on /mnt/var type ext3 (rw)

There’s another way of doing this:

dom0:/etc/xen/images # lomount -diskimage vm1.img -partition 1  /mnt
dom0:/etc/xen/images # mount 
-snip-
/var/lib/xen/images/vm1.img on /mnt type ext2 (rw,loop=/dev/loop0,offset=32256)

It’s sort of quicker as it doesn’t need the loop device to be set first and kpartx utility for mapping, just mounts the partition to the specified location. However it works for native primary partitions only. If you used extended partitions or perhaps LVM inside the file image you need to calculate the ?offset? first then mount it:

dom0:/etc/xen/images # fdisk -l -u vm1.img 
You must set cylinders. 
You can do this from the extra functions menu. 
Disk vm1.img: 0 MB, 0 bytes 
255 heads, 63 sectors/track, 0 cylinders, total 0 sectors 
Units = sectors of 1 * 512 = 512 bytes 
  Device Boot      Start         End      Blocks   Id  System 
vm1.img1   *          63      144584       72261   83  Linux 
vm1.img2          144585     3196934     1526175    5  Extended 
vm1.img5          144648      417689      136521   83  Linux 
vm1.img6          417753     2425814     1004031   83  Linux 
vm1.img7         2425878     3196934      385528+  83  Linux

Find the “start” position of your desired partition then multiply it with 512 (as displayed by fdisk) to get the right offset number:

417753*512=213889536

It’s the root partition, I want to mount that:

dom0:/etc/xen/images # mount -o loop,offset=213889536 vm1.img /mnt
dom0:/etc/xen/images # mount 
-snip-
/var/lib/xen/images/vm1.img on /mnt type ext3 (rw,loop=/dev/loop0,offset=213889536)

This tip is derived from another coolsolution article regarding LVM images located here:
http://www.novell.com/coolsolutions/tip/19710.html

Environment:

  • SLES/SLED
  • multipath tools:dom0:~ # rpm -qf /sbin/kpartx

    multipath-tools-0.4.7-34.23

  • xen tools:dom0:~ # rpm -qf /usr/bin/lomount

    xen-tools-3.0.4_13138-0.47

VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)

Tags: , ,
Categories: Enterprise Linux, Technical Solutions, Virtualization

Disclaimer: As with everything else at SUSE Conversations, this content is definitely not supported by SUSE (so don't even think of calling Support if you try something and it blows up).  It was contributed by a community member and is published "as is." It seems to have worked for at least one person, and might work for you. But please be sure to test, test, test before you do anything drastic with it.

Comment

RSS