Logical Volume Manager

From ALSwiki
Jump to: navigation, search

Overview

Logical Volume Management provides a lot of flexibility in managing the file system. Partitions can be resized at any time. There's also a snapshot feature.

Here's a simple overview, based on the diagram in http://tldp.org/HOWTO/LVM-HOWTO/ (originally supplied by Erik Bågfors on the linux-lvm mailing list).

ext2   xfs   xfs     Filesystems
  |     |     |
usrlv varlv rootlv   Logical Volumes (LVs)
   \   |    /
    \  |   /
     diskvg          Volume Group (VG)
      /  \
     /    \
   sdb1  sdc1        Physical Volumes (PVs) on partitions or whole disks
  • File systems sit on top of Logical Volumes (LVs) or "abstract partitions".
  • LVs are carved out of Volume Groups, an abstraction used to organize a set of Physical Volumes (PVs).
  • PVs are typically disks, or disk-like things (e.g. a Software RAID).

The setup will touch on all four of the levels in this diagram.

Installation

Install the LVM package

 $ sudo apt-get install lvm2

Create the LVM Partition

Make it the size of the entire second drive. Check with

 $ df

The list will include the 2 disks, one (usually sda1) holding the system and the other (usually sdb1), depending on what it had on it, other stuff.

 Filesystem           1K-blocks      Used Available Use% Mounted on
 /dev/sda1             73917408   1016796  69145824   2% /
 varrun                  517704       760    516944   1% /var/run
 varlock                 517704         4    517700   1% /var/lock
 udev                    517704        76    517628   1% /dev
 devshm                  517704         0    517704   0% /dev/shm
 /dev/sdb1             other stuff...

Start fdisk.

 $ sudo fdisk /dev/sdb

Use p to see the current partitions.

 Command (m for help): p
 Disk /dev/sdb: 500.1 GB, 500107862016 bytes
 255 heads, 63 sectors/track, 60801 cylinders
 Units = cylinders of 16065 * 512 = 8225280 bytes
 Disk identifier: 0x3eef1cc1
   
 Device Boot      Start         End      Blocks     Id  System
 /dev/sdb1            1       60801   488384001   8e  Linux swap / Solaris

Create a new LVM partition that spans the whole disk.

 Command (m for help): n
 Command action
   e   extended
   p   primary partition (1-4)
 p
 Partition number (1-4): 1
 First cylinder (1-621, default 1):<ENTER>
 Using default value 1
 Last cylinder or +size or +sizeM or +sizeK (1-60801, default 60801): <ENTER> Or the max number if it's not the default

Assign the partition type as LVM.

 Command (m for help): t
 Partition number (1-4): 1
 Hex code (type L to list codes): 8E The code for LVM is 8E
 Changed system type of partition 1 to 8E (Linux LVM)

Use p again to see the new partition.

 Command (m for help): p
 Disk /dev/sdb: 500.1 GB, 500107862016 bytes
 255 heads, 63 sectors/track, 60801 cylinders
 Units = cylinders of 16065 * 512 = 8225280 bytes
 Disk identifier: 0x3eef1cc1
 
 Device Boot      Start         End      Blocks   Id  System
 /dev/sdb1            1       60801   488384001   8e  Linux LVM

Write the new partition table.

 Command (m for help): w
 The partition table has been altered!
 Calling ioctl() to re-read partition table.
 Syncing disks.

Check that it worked.

 $ sudo sfdisk -l /dev/sdb
 Disk /dev/sdb: 60801 cylinders, 255 heads, 63 sectors/track
 Units = cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0
 
 Device Boot Start        End   #cyls    #blocks   Id  System
 /dev/sdb1          0+  60800   60801- 488384001   8e  Linux LVM
 /dev/sdb2          0       -       0          0    0  Empty
 /dev/sdb3          0       -       0          0    0  Empty
 /dev/sdb4          0       -       0          0    0  Empty

A partitioning howto with more detail is at: http://tldp.org/HOWTO/Partition/

Set up the Logical Volume Structure

I wanted to set up a single volume group for the entire 500G logical volume partition, called "system". From that, I wanted to allocate 150G as a 50G logical volume for /home and a 100G logical volume for a samba partition, /srv.

Create the Physical Volume (PV)

 $ sudo pvcreate /dev/sdb1
 Physical volume "/dev/sdb1" successfully created

Check if it's there.

 $ sudo pvs
 PV         VG     Fmt  Attr PSize   PFree
 /dev/sdb1  system lvm2 a-   465.76G 465.76G

Create the Volume Group (VG)

I named my group "system".

 $ sudo vgcreate system /dev/sdb1

Check if it's there.

 $ sudo vgs
 VG     #PV #LV #SN Attr   VSize   VFree
 system   1   2   0 wz--n- 465.76G 465.76G

Create the LVM Partitions

Since these are going to be mounted as /home and /srv, use those names.

The first time I tried this, I got an error. A little newsgroup searching found a bug report. Before the first time you use lvcreate, you need to issue

 $ sudo modprobe dm_mod

Then proceed with the volume creation.

 $ sudo lvcreate --size 50G --name home
 $ sudo lvcreate --size 100G --name srv

Check that they are there.

 $ sudo lvs
 LV   VG     Attr   LSize   Origin Snap%  Move Log Copy%
 home system -wi-ao  50.00G
 srv  system -wi-ao 100.00G

More detail on LVM is at http://tldp.org/HOWTO/LVM-HOWTO/

Create the file systems

Create a file system on each logical volume

I chose ext3 as the file type. There are other choices, but this one is well supported, tired and true. There's a good discussion of the consideration in "Beginning Ubuntu Server Administration", by Sander van Vugt, ISBN 978-1-59059-923-5.

The logical volumes are in /dev/mapper and named using the pattern <lvgroup>-<lvname>. The home logical volume is at /dev/mapper/system-home.

Use mkfs to create the file systems.

 $ mkfs.ext3 /dev/mapper/system-home
 mke2fs 1.27 (30-Jun-2005)
 Filesystem label=
 OS type: Linux
   ...
 Writing inode tables: done
 Creating journal (1024 blocks): done
 Writing superblocks and filesystem accounting information: done
 
 This filesystem will be automatically checked every 35 mounts or
 180 days, whichever comes first.  Use tune2fs -c or -i to override.

Repeat for the second LV.

 $ mkfs.ext3 /dev/mapper/system-srv

The filesysestem is complete, and ready to use. It only needs to be mounted.

Mounting home and srv

The srv volume is easy since it exists as an empty directory.

 $ sudo mount -t ext3 /dev/mapper/system-srv /srv

If it doesn't, create it:

 $ sudo mkdir /srv

This is slightly more complicated for /home because I already have it mounted and it has a few files in it. There's a tutorial on moving your home directory that covers the issue of file copying at: http://ubuntu.wordpress.com/2006/01/29/move-home-to-its-own-partition/

Make a temporary directory to copy the old /home files into.

 $ sudo mkdir /mnt/newhome

Mount it.

 $ sudo mount -t ext3 /dev/mapper/system-home /mnt/newhome

Copy the files from the old home directory into it. Then unmount it and move the original home out of the way.

 $ sudo umount /mnt/newhome
 $ sudo mv /home /oldhome

Create a new /home directory and mount it to the system-home logical volume.

 $ sudo mkdir /home
 $ sudo mount -t ext3 /dev/mapper/system-home /home

All of the files should be there. Clean up:

 $ sudo remdir /mnt/newhome
 $ sudo remdir /oldhome

Set them to mount automatically

Use your favorite editor and add the following lines to /etc/fstab

 /dev/mapper/system-home        /home  ext3    defaults 0       0
 /dev/mapper/system-srv         /srv   ext3    defaults 0       0

Test it

Reboot and check to see if the mounts came back. (By this time, I'd added a few more files to /srv.)

 $ df
 Filesystem             1K-blocks      Used Available Use% Mounted on
 /dev/sda1               73917408   1016776  69145844   2% /
 varrun                    517704       760    516944   1% /var/run
 varlock                   517704         4    517700   1% /var/lock
 udev                      517704        76    517628   1% /dev
 devshm                    517704         0    517704   0% /dev/shm
 /dev/mapper/system-home 51606140    184316  48800384   1% /home
 /dev/mapper/system-srv 103212320   5512444  92456996   6% /srv