Tag Archives: partitions

New volume group and LVM for snapshots


To backup a large MySQL database on a regular basis, I am going to use a method of taking snapshots of the database files using LVM in linux. The volume I will be using to create the logical volume is a RAID 1 (mirrored) array, as I created in the mdadm creating a new array guide.

Creating the volume group with lvm

The array we are using as the basis of the volume group is /dev/md0. Firstly we need to initialise the RAID array (you can use raw block devices or other partitions) for use as a physical volume. This is simple:

$ pvcreate /dev/md0
  Physical volume "/dev/md0" successfully created

The volume can now be used to make a volume group (I am calling this volume group vgssd, to state that it’s the SSD group):

$ vgcreate vgssd /dev/md0
  Volume group "vgssd" successfully created

Show some information about the newly created volume group

$ vgdisplay vgssd
  --- Volume group ---
  VG Name               vgssd
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               439.87 GiB
  PE Size               4.00 MiB
  Total PE              112607
  Alloc PE / Size       0 / 0   
  Free  PE / Size       112607 / 439.87 GiB
  VG UUID               ae5CoA-J0v4-eo0j-GyJd-DGvl-t7bF-zQd1wV

This shows we’ve got around the 440GB free for creating logical volumes on this device. We won’t be using it all, as we need to allow room for snapshots.

Creating the logical volume with lvm

It’s up to you to decide how to create and use your partitions. Remember that using LVM allows you to resize (extend and shrink) your volumes, as well as add more physical hard drives to the system. We have created a block device that now is mirrored over 2 SSD harddrives. I will be setting up a 250GB partition for use with mysql data, and allowing loads of space for snapshots, and extending it going forward.

$ lvcreate -L250G -n mysql_data vgssd
  Logical volume "mysql_data" created

I am going to mount this at: /var/mysql_data, and format it to be an ext4 filesystem, and make sure that access times are not recorded on mounting in fstab (noatime)

$ mkdir /var/mysql_data
$ mkfs.ext4 /dev/vgssd/mysql_data
mke2fs 1.42.9 (4-Feb-2014)
Discarding device blocks: done                            
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
16384000 inodes, 65536000 blocks
3276800 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
2000 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
	4096000, 7962624, 11239424, 20480000, 23887872

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

And the line added to /etc/fstab is as follows:

/dev/vgssd/mysql_data /var/mysql_data ext4 defaults,noatime 0 0

So that shows how to use lvm to create a partition that can be easily extended, backed up, etc.


Mounting partitions contained in an LVM


When creating a virtual machine with an LVM partition, it will create partitions within that LVM partition. Mounting those ‘sub’ partitions requires an extra step to access them. I used this when I was cloning virtual machines so that I could change the hostname of the copied host before booting it up.

A tool called kpartx is your friend here. It allows the system to see partitions within a LVM partition, so they can be mounted. In this guide we will be accessing partitions created as part of my mogilefs playground virtual machines. Below are the partitions that we are trying to access:

$ sudo fdisk -l /dev/vhosts/mogile2

Disk /dev/vhosts/mogile2: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders, total 10485760 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: 0x000b30be

Device Boot Start End Blocks Id System
/dev/vhosts/mogile2p1 * 2048 9437183 4717568 83 Linux
/dev/vhosts/mogile2p2 9439230 10483711 522241 5 Extended
/dev/vhosts/mogile2p5 9439232 10483711 522240 82 Linux swap / Solaris

NOTE: Make sure that the virtual machine is NOT running before mounting these partitions

So let’s try and mount one of these partitions (/dev/vhosts/mogile2p1):

$ sudo mount /dev/vhosts/mogile2p1 /mnt/temp
mount: special device /dev/vhosts/mogile2p1 does not exist

So the following will help map these partitions to the host OS so they can be mounted. If you haven’t got kpartx installed, then on debian/ubuntu systems install with sudo apt-get install kpartx

kpartx guide

To allow the host system to see the partitions for mounting, we run the following:

$ sudo kpartx -a /dev/vhosts/mogile2

We can then mount with

$ sudo mount /dev/mapper/vhosts-mogile2p1 /mnt/temp

And then use it as we normally would. In this case, I used it to edit the /mnt/temp/etc/hostname and /mnt/temp/etc/hosts file

Finally un mount it:
$ sudo umount /mnt/temp

And if you are done with it, and cleaned up, then remove it from the partition tables:

$ sudo kpartx -d /dev/vhosts/mogile2