major.io words of wisdom from a systems engineer

Mounting an LVM snapshot containing partitions

LVM snapshots can be really handy when you’re trying to take a backup of a running virtual machine. However, mounting the snapshot can be tricky if the logical volume is partitioned.

I have a virtual machine running zoneminder on one of my servers at home and I needed to take a backup of the instance with rdiff-backup. I made a snapshot of the logical volume and attempted to mount it:

[[email protected] ~]# lvcreate -s -n snap -L 5G /dev/vg_i7tiny/vm_zoneminder
  Logical volume "snap" created
[[email protected] ~]# mount /dev/vg_i7tiny/snap /mnt/snap/
mount: wrong fs type, bad option, bad superblock on /dev/mapper/vg_i7tiny-snap,
       missing codepage or helper program, or other error
       In some cases useful info is found in syslog - try
       dmesg | tail or so

Oops. The logical volume has partitions. We will need to mount the volume with an offset so that we can get the right partition. Figuring out the offset can be done fairly easily with fdisk:

[[email protected] ~]# fdisk -l /dev/vg_i7tiny/vm_zoneminder

Disk /dev/vg_i7tiny/vm_zoneminder: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders, total 104857600 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
Disk identifier: 0x0007a1d5

                       Device Boot      Start         End      Blocks   Id  System
/dev/vg_i7tiny/vm_zoneminder1   *        2048     1026047      512000   83  Linux
/dev/vg_i7tiny/vm_zoneminder2         1026048   102825983    50899968   83  Linux
/dev/vg_i7tiny/vm_zoneminder3       102825984   104857599     1015808   82  Linux swap / Solaris

It looks like we have a small boot partition, a big root partition and a swap volume. We want to mount the second volume to copy files from the root filesystem. There are two critical pieces of information here that we need:

  • the sector where the partition starts (the Start column from fdisk)
  • the number of bytes per sector (512 in this case - see the third line of the fdisk output)

Let’s calculate how many bytes we need to skip when we mount the partition and then mount it:

[[email protected] ~]# echo "512 * 1026048" | bc
525336576
[[email protected] ~]# mount -o offset=525336576 /dev/mapper/vg_i7tiny-snap /mnt/snap/
[[email protected] ~]# ls /mnt/snap/
bin  boot  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

The root filesystem from the virtual machine is now mounted and we can copy some files from it. Don’t forget to clean up when you’re finished:

[[email protected] ~]# umount /mnt/snap/
[[email protected] ~]# lvremove -f /dev/vg_i7tiny/snap
  Logical volume "snap" successfully removed

If you need to do this with file-backed virtual machine storage or with a flat file you made with dd/dd_rescue, read my post from 2010 about tackling that similar problem.