LVMify the disk

From FnordWiki
Jump to navigation Jump to search

LVM? What's this all about, anyway?

LVM Linux's Logical Volume Manager. Wikipedia describes the Linux LVM here. Basically, it allows you to take a pool of one or more hard drives and create one or more virtual hard drives out of piecs of the real drives. These can then be resized at run-time, allowing the admin to grow (or sometimes) shrink filesystems while the machine is up and running. This page covers the moving of a hard-partitioned Seagate DockStar running Debian to an LVM-based setup.

Before LVM-ification

Got a backup? You could bork your machine pretty hard if something bad happens. You've been warned.

Here's how the DockStar's USB flash drive looks now with regard to filesystem free space and partitioning:

root@canopus:~# fdisk -l -u /dev/sda

Disk /dev/sda: 4000 MB, 4000317440 bytes
64 heads, 32 sectors/track, 3815 cylinders, total 7813120 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: 0x9a198ead

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1050623      524288   83  Linux
/dev/sda2         1050624     2099199      524288   83  Linux
/dev/sda3         2099200     7813119     2856960   8e  Linux LVM
root@canopus:~# df
Filesystem           1K-blocks      Used Available Use% Mounted on
rootfs                  516040    311876    177952  64% /
none                     60492        36     60456   1% /dev
/dev/sda1               516040    311876    177952  64% /
tmpfs                    62864         0     62864   0% /lib/init/rw
tmpfs                    62864         0     62864   0% /dev/shm
tmpfs                    62864         0     62864   0% /tmp
root@canopus:~# cat /etc/fstab
# /etc/fstab: static file system information.
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
/dev/root      /               ext2    noatime,errors=remount-ro 0 1
/dev/sda2      none            swap    sw                0       0
tmpfs          /tmp            tmpfs   defaults          0       0
root@canopus:~#

The current situation

As you can probably tell, the only filesystem is on /dev/sda1 and /dev/sda2 is used for swap space. /dev/sda3 is not used for anything at this point.

The desired end state

I'd like to end up with /dev/sda1 used purely for system configuration and needed-for-boot files. Swap space, /var, /home, /tmp, and /usr will all live in LVM managed block devices. /dev/sda2 will be converted from swap space to a working backup copy of the current root filesystem and used for repairing the real system should the sysadmin suffer a severe bout of brain-damage and break the thing really bad.

Based on lots of use of du, I believe 512Mbytes for swap, 64Mbytes for /home, 128Mbytes for /tmp, 192Mbytes for /usr, and 192Mbytes for /var will be a good target configuration.

Creating an LVM physical volume

Some data structures need to be placed on a physical device that LVM uses. This is done with the pvcreate program. /dev/sda3 is where we'll put the LVM managed logical volumes, so pvcreate is invoked so (in test mode first, to be sure our command line is sane):

root@canopus:~# pvcreate --test --verbose --metadatatype 2 --pvmetadatacopies 2 --zero y /dev/sda3
  Test mode: Metadata will NOT be updated.
    Set up physical volume for "/dev/sda3" with 5713920 available sectors
    Zeroing start of device /dev/sda3
  Physical volume "/dev/sda3" successfully created
    Test mode: Wiping internal cache
    Wiping internal VG cache
root@canopus:~# pvcreate --verbose --metadatatype 2 --pvmetadatacopies 2 --zero y /dev/sda3
    Set up physical volume for "/dev/sda3" with 5713920 available sectors
    Zeroing start of device /dev/sda3
  Physical volume "/dev/sda3" successfully created
root@canopus:~# 

Creating a volume group

Now that there is a physical volume, we can create a volume group. We're specifying that this volume group will not be clustered, is an LVM2 volume group (the tools support the older LVM1 format, too), a physical extent size of 64Mibibytes (this is the size of the pieces that go into any given logical volume. Since the backing store for this volume group is about 3Gibibytes in size, a 64Mibibyte gives us 43 pieces of that to play with), a volume group name of vg00, and that /dev/sda3 is our physical backing device. Again, test mode is used to ensure sanity of command line before really creating the volume group.

root@canopus:~# vgcreate --test --verbose --clustered n --metadatatype 2 --physicalextentsize 64M vg00 /dev/sda3
  Test mode: Metadata will NOT be updated.
    Wiping cache of LVM-capable devices
    Wiping cache of LVM-capable devices
    Adding physical volume '/dev/sda3' to volume group 'vg00'
    Test mode: Skipping archiving of volume group.
    Test mode: Skipping volume group backup.
  Volume group "vg00" successfully created
    Test mode: Wiping internal cache
    Wiping internal VG cache
root@canopus:~# vgcreate --verbose --clustered n --metadatatype 2 --physicalextentsize 64M vg00 /dev/sda3
    Wiping cache of LVM-capable devices
    Wiping cache of LVM-capable devices
    Adding physical volume '/dev/sda3' to volume group 'vg00'
    Creating directory "/etc/lvm/archive"
    Archiving volume group "vg00" metadata (seqno 0).
    Creating directory "/etc/lvm/backup"
    Creating volume group backup "/etc/lvm/backup/vg00" (seqno 1).
  Volume group "vg00" successfully created
root@canopus:~#

Making /dev/sda2 available

/dev/sda2 is currently used as swap space. It's the same size as /dev/sda1. I want to deactivate the swap space here and make a copy of the root filesystem. First, though, there should be a different swap space make available.

Making our first logical volume: /dev/vg00/swap

Creating a logical volume is done with the lvcreate command. You want to decide a few things before creating one: how big should it be? What name should it have? Which volume group does it belong to?

Here's what I did to make /dev/vg00/swap:

root@canopus:~# lvcreate --test --verbose --size 512M --name swap --zero y vg00
  Test mode: Metadata will NOT be updated.
    Setting logging type to disk
    Finding volume group "vg00"
    Test mode: Skipping archiving of volume group.
    Creating logical volume swap
    Test mode: Skipping volume group backup.
    Found volume group "vg00"
  Aborting. Failed to activate new LV to wipe the start of it.
    Found volume group "vg00"
  Unable to deactivate failed new LV. Manual intervention required.
    Test mode: Wiping internal cache
    Wiping internal VG cache
root@canopus:~# lvcreate --verbose --size 512M --name swap --zero y vg00
    Setting logging type to disk
    Finding volume group "vg00"
    Archiving volume group "vg00" metadata (seqno 5).
    Creating logical volume swap
    Creating volume group backup "/etc/lvm/backup/vg00" (seqno 6).
    Found volume group "vg00"
    Creating vg00-swap
    Loading vg00-swap table (254:0)
    Resuming vg00-swap (254:0)
    Clearing start of logical volume "swap"
    Creating volume group backup "/etc/lvm/backup/vg00" (seqno 6).
  Logical volume "swap" created
root@canopus:~#

Reconfiguring swap location

After this logical volume has been created, we'll put the swap space signature on it using mkswap, activate it with swapon, deactivate the old one (swapoff), and update /etc/fstab to save the changes across a reboot:

root@canopus:~# mkswap /dev/vg00/swap
mkswap: /dev/vg00/swap: warning: don't erase bootbits sectors
        on whole disk. Use -f to force.
Setting up swapspace version 1, size = 524284 KiB
no label, UUID=76b70850-c3ab-4512-b55b-0545f2690130
root@canopus:~# swapon /dev/vg00/swap
root@canopus:~# swapoff /dev/sda2
root@canopus:~# sed -e'/^\/dev\/sda2/s/^/# /' /etc/fstab > /tmp/fstab
root@canopus:~# echo '/dev/vg00/swap    none            swap    sw              0       0' >> /tmp/fstab
root@canopus:~# install -o root -g root -m 644 /tmp/fstab /etc/fstab
root@canopus:~# cat /etc/fstab
# /etc/fstab: static file system information.
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
/dev/root      /               ext2    noatime,errors=remount-ro 0 1
# /dev/sda2      none            swap    sw                0       0
tmpfs          /tmp            tmpfs   defaults          0       0
/dev/vg00/swap    none            swap    sw              0       0
root@canopus:~#

Just to be sure /dev/sda2 is not being used any more as swap space, have a look at /proc/swaps:

root@canopus:~# cat /proc/swaps 
Filename                                Type            Size    Used    Priority
/dev/dm-0                               partition       524280  0       -1
root@canopus:~#