Sometimes you need to get/change/analyse some files/data/logs from the virtual machine which running under lvm.

We are going to mount the disk under Proxmox hypervisor, which is running debian as a base OS.

Imagine, we have situation like that:

lvs | grep vm
> > vm-100-disk-1 pve Vwi-aotz-- 32.00g data 75.02 
> > vm-100-disk-2 pve Vwi-aotz-- 32.00g data 71.63

On vm-100-disk-1 we have the data which we need, but its under lvm control, so its not so easy to mount that volume simply using mount command, because we will get this error message:

mount /dev/mapper/pve-vm--100--disk--1 /mnt/vm100/
> > mount: wrong fs type, bad option, bad superblock on /dev/mapper/pve-vm--100--disk--1,
> >  missing codepage or helper program, or other error
> > 
> > In some cases useful info is found in syslog - try
> >  dmesg | tail or so.

To get around this problem, we will need to create a loop device where our vm disk will be bound to:

losetup /dev/loop0 /dev/pve/vm-100-disk-1
fdisk -l /dev/loop0
> > Disk /dev/loop0: 32 GiB, 34359738368 bytes, 67108864 sectors
> > Units: sectors of 1 * 512 = 512 bytes
> > Sector size (logical/physical): 512 bytes / 512 bytes
> > I/O size (minimum/optimal): 512 bytes / 512 bytes
> > Disklabel type: dos
> > Disk identifier: 0xe9174e2b
> > 
> > Device Boot Start End Sectors Size Id Type
> > /dev/loop0p1 * 2048 67108863 67106816 32G 8e Linux LVM
partx -v --add /dev/loop0 
> > partition: none, disk: /dev/loop0, lower: 0, upper: 0
> > /dev/loop0: partition table type 'gpt' detected
> > range recount: max partno=3, lower=0, upper=0
> > /dev/loop0: partition #1 added
> > /dev/loop0: partition #2 added
> > /dev/loop0: partition #3 added

Now, scan the volume groups and check if you have new vgs there

vgscan
> >  Reading all physical volumes. This may take a while...
> >  Found volume group "vmvol" using metadata type lvm2
> >  Found volume group "pve" using metadata type lvm2
> > 
vgs
> >  VG #PV #LV #SN Attr VSize VFree 
> >  vmvol 1 0 0 wz--n- 32.00g 32.00g
> >  pve 2 6 1 wz--n- 557.49g 235.75g

Yes, we have a new vmvol volume group here, which we actually need. Ok, then activate that group so you can mount it afterwards:

vgchange -ay vmvol
> >3 logical volume(s) in volume group "vmvol" now active

Ok, the volume group is now active. All volume groups are located under /dev/mapper/. So we just mount it (i.e.: under /mnt/vmvol (path should exist)) like:

mount /dev/mapper/vmvol-root /mnt/vmvol/
df -hT /mnt/vmvol
> > Filesystem            Type  Size  Used Avail Use% Mounted on
> > /dev/mapper/vmvol-root ext4  9.8G  5.1G  4.3G  55% /mnt/vmvol

cd /mnt/vmvol/; ls -l
> > total 136
> > drwxr-xr-x   2 root root  4096 Feb 12 17:02 bin
> > drwxr-xr-x   3 root root  4096 Feb 18 14:22 boot
> > drwxr-xr-x  10 root root 36864 Aug 29  2008 dev
> > drwxr-xr-x 158 root root 12288 Mar  4 14:28 etc
> > lrwxrwxrwx   1 root root    19 Feb 12 17:13 home -> /mnt/glusterfs/home
> > lrwxrwxrwx   1 root root    29 Feb 12 16:55 initrd.img -> boot/initrd.img-4.9.0-8-amd64
> > lrwxrwxrwx   1 root root    29 Feb 12 16:55 initrd.img.old -> boot/initrd.img-4.9.0-8-amd64
> > drwxr-xr-x  13 root root  4096 Feb 12 17:00 lib
> > drwxr-xr-x   2 root root  4096 Feb 18 14:22 lib64
> > drwx------   2 root root 16384 Feb 12 16:53 lost+found
> > drwxr-xr-x   2 root root  4096 Feb 12 16:54 media
> > drwxr-xr-x   3 root root  4096 Feb 12 17:13 mnt
> > drwxr-xr-x   2 root root  4096 Feb 12 16:54 opt
> > drwxr-xr-x   2 root root  4096 Jan 22 14:47 proc
> > drwx------   5 root root  4096 Mar  8 14:47 root
> > drwxr-xr-x  18 root root  4096 Feb 12 17:02 run
> > drwxr-xr-x   2 root root  4096 Feb 18 14:22 sbin
> > drwxr-xr-x   2 root root  4096 Feb 12 17:13 srv
> > drwxr-xr-x   2 root root  4096 Jan 22 14:47 sys
> > drwxrwxrwt  12 root root  4096 Mar 11 12:19 tmp
> > drwxr-xr-x  11 root root  4096 Feb 12 16:59 usr
> > drwxr-xr-x  14 root root  4096 Feb 12 17:16 var
> > lrwxrwxrwx   1 root root    26 Feb 12 16:55 vmlinuz -> boot/vmlinuz-4.9.0-8-amd64
> > lrwxrwxrwx   1 root root    26 Feb 12 16:55 vmlinuz.old -> boot/vmlinuz-4.9.0-8-amd64

Un-mount logical volume, deactivate volume group and disable unneeded logical volumes under LVM table.

get out of mounted directory
cd /
umount directory
umount /mnt/vmvol
deactivate volume group
vgchange -an vmvol
> > 0 logical volume(s) in volume group "vmvol" now active
tell kernel to deactivate partitions from that disk
partx --del /dev/loop0
remove loop device
losetup -D /dev/loop0

Next Post

Blog Comments powered by Disqus.