Penguin
Note: You are viewing an old revision of this page. View the current version.

Setting up LVM under Linux on a single drive

You will want to make sure Multi device support (RAID and LVM) is enabled and LVM support is compiled into your kernel. You could build it as a module and load it from your initrd, but you don't really need to go to the effort.

Scenario

You have a 36gb disc (this might be a RAID logical volume already, but the system sees it as one disc with 36gb on it). You want to install partitions for /usr, /var etc (smart) but you're not sure what sizes they need to be and you will want to be able to resize them in future.

Solution

At install time, create a small (<100mb) /boot, swap, and a reasonable (4-6gb) / partition. Leave the rest of the space unpartitioned.

Make sure LVM is compiled into your kernel, and you have the LVM tools installed. Debian 3 (woody): install lvm10. Debian Sarge/Sid: install lvm2.

Gentoo has lvm (1.01) and lvm2. You may also need the device-mapper patch for 2.4.x kernels, available at ftp://sources.redhat.com/pub/dm/. From the LVM2 README.Debian
LVM2 requires the device-mapper kernel module (dm-mod). This is available as a kernel patch for 2.4 (included as standard in current Debian 2.4 kernels), and is distributed with linux 2.5 and above.

Create a PV1? partition on your drive

  1. fdisk /dev/sda

Set the type of this partition (which, in my case, is primary partition 4 and eats the rest of my disc) to 8e - Linux LVM.

Prepare the partition

First, run vgscan
  1. vgscan

vgscan -- reading all physical volumes (this may take a while...) vgscan -- "/etc/lvmtab" and "/etc/lvmtab.d" successfully created vgscan -- WARNING: This program does not do a VGDA backup of your volume group4?

This creates a volume group descriptor area (VGDA) at the start of the disks.

  1. pvcreate /dev/sda4

pvcreate -- physical volume "/dev/sda4" successfully created

Create a volume group (VG)2?

This volume group can contain multiple discs, but in this case it only uses one.

  1. vgcreate lvmarray /dev/sda4

vgcreate -- INFO: using default physical extent size 4 MB vgcreate -- INFO: maximum logical volume size is 255.99 Gigabyte vgcreate -- doing automatic backup of volume group "lvmarray" vgcreate -- volume group "lvmarray" successfully created and activated

  1. vgdisplay

Confirm that the VG size is the right amount for the size of the partition.

Note that if you use devfs, you'll need to use the full devfs pathname, not the /dev/sda4 symlink. EG:, use /dev/scsi/host0/bus0/target0/lun0/part4 instead of /dev/sda4

Create some LVs 3?

  1. lvcreate -L20G -nhome lvmarray

lvcreate -- doing automatic backup of "lvmarray" lvcreate -- logical volume "/dev/lvmarray/home" successfully created

  1. lvcreate -L5G -nvar lvmarray

lvcreate -- doing automatic backup of "lvmarray" lvcreate -- logical volume "/dev/lvmarray/var" successfully created

  1. lvcreate -L5G -nusr lvmarray

lvcreate -- only 793 free physical extents in volume group "lvmarray"

  1. lvcreate -L793 -nusr lvmarray

lvcreate -- rounding size up to physical extent boundary lvcreate -- doing automatic backup of "lvmarray" lvcreate -- logical volume "/dev/lvmarray/usr" successfully created

Create filesystems on your LVs

  1. for i in home var usr; do mke2fs -j /dev/lvmarray/$i; done

(Now would be a good time to learn about label based mounting!)

Mount your LVs

mount /dev/lvmarray/home /home

You will of course want to move everything from /home first, and add them to fstab(5) etc... To move your partitions, you'll probably want to be in runlevel 1.

Resizing logical volumes

Oops! Just created 20g /home, 5gb /var and 860mb /usr. So lets take 4gb from home and add it to usr.

  1. umount /home
  2. e2fsadm -L-4G /dev/lvmarray/home
  3. mount /home
  4. umount /usr
  5. e2fsadm -L+4G /dev/lvmarray/usr
  6. mount /usr

e2fsadm is a utility that comes with LVM that lets you automatically fsck, resize FS and then resize LV.

  1. e2fsadm -L+1G /foo
is equivalent to the two commands
  1. lvextend -L+1G /foo
  2. resize2fs /foo

You need to have e2fsprogs installed for this to work (but you probably needed it to make the FS to start with.) When the time comes to learn to resize LVM partitions, there'll be more. Until then, see http://tldp.org/HOWTO/LVM-HOWTO

LVM under Debian Sarge

If you are upgrading from a 2.4 to a 2.6 kernel and you have an LVM1 partition, you must install the 'lvm2' package.

There are two version of LVM:

  1. LVM1 (lvm10 in woody/sarge) work only with linux-2.4.
  2. LVM2 (lvm2, only in sarge) work with both linux-2.4 and 2.6, and is backward compatible with LVM1.

Terminology

1? PV: Physical Volume; a disc or a partition on a disc. 2? VG: Volume Group; a group of one-or-more physical volumes across which you get a "virtual disk", a space to create logical volumes in. 3? LV: Logical Volume; something you eventually create an FS on. 4? To create a VGDA (Volume Group Descriptor Area) Backup use !vgcfgbackup(8)? regularly