25 May 2008

(easy) LVM on Gentoo

LVM (Logical Volume Manager) is a great piece of software which allow you to deal with Logical Volumes. Using LVM, you are able to extend/reduce your filesystems which is pretty handy when you need more space.


Let's get the easy way to do that.






Obs.: My post about LVM on ubuntu have a lot of info, so I'll paste the important info about LVM here


What is LVM?


LVM (Logical Volume Manager) is a great piece of software which allow you to deal with Logical Volumes. Using LVM, you are able to extend/reduce your filesystems which is pretty handy when you need more space.


A lot of distributions support out of the box in installer. Unfortunately, Hardy Heron doesn't offer this on the Desktop Install CD.






What can I do with LVM?


* Resize volume groups online by absorbing new physical volumes (PV) or ejecting existing ones.
* Resize logical volumes online by concatenating extents onto them or truncating extents from them.
* Create read-only snapshots of logical volumes (LVM1).
* Create read-write snapshots of logical volumes (LVM2).
* Stripe whole or parts of logical volumes across multiple PVs, in a fashion similar to RAID0.
* Mirror whole or parts of logical volumes, in a fashion similar to RAID1.
* Move online logical volumes between PVs.
* Split or merge volume groups in situ (as long as no logical volumes span the split). This can be useful when migrating whole logical volumes to or from offline storage.


What can't I do with LVM?


* LVM does not provide parity-based redundancy across LVs, as with RAID4, RAID5 or RAID6. This functionality is instead provided by Linux metadisks, which can be used as LVM physical volumes.
* Converting regular filesystems into LVM filesystems online (you also can copy the stuff to a logical volume).


Implementation


LVM keeps a metadata header at the start of every PV, each of which is uniquely identified by a UUID. Each PV's header is a complete copy of the entire volume group's layout, including the UUIDs of all other PV, the UUIDs of all logical volumes and an allocation map of PEs to LEs. This simplifies data recovery in the event of PV loss.


In the 2.6-series Linux kernels, the LVM is implemented in terms of the device mapper, a simple block-level scheme for creating virtual block devices and mapping their contents onto other block devices. This minimizes the amount of relatively hard-to-debug kernel code needed to implement the LVM. It also allows its I/O redirection services to be shared with other volume managers (such as EVMS). Any LVM-specific code is pushed out into its user-space tools, which merely manipulate these mappings and reconstruct their state from on-disk metadata upon each invocation.


To bring a volume group online, the "vgchange" tool:


1. Searches for PVs in all available block devices.
2. Parses the metadata header in each PV found.
3. Computes the layouts of all visible volume groups.
4. Loops over each logical volume in the volume group to be brought online and:
1. Checks if the logical volume to be brought online has all its PVs visible.
2. Creates a new, empty device mapping.
3. Maps it (with the "linear" target) onto the data areas of the PVs the logical volume belongs to.


To move an online logical volume between PVs, the "pvmove" tool:


1. Creates a new, empty device mapping for the destination.
2. Applies the "mirror" target to the original and destination maps. The kernel will start the mirror in "degraded" mode and begin copying data from the original to the destination to bring it into sync.
3. Replaces the original mapping with the destination when the mirror comes into sync, then destroys the original.


These device mapper operations take place transparently, without applications or filesystems being aware that their underlying storage is moving.


How can I learn more?


http://en.wikipedia.org/wiki/Lvm
http://tldp.org/HOWTO/LVM-HOWTO
http://sourceware.org/lvm2/
http://www.gentoo.org/doc/en/lvm2.xml


Requirements


* Gentoo 2007.0 livecd/livedvd or any other distribution livecd able to load lvm modules and applications.


Preparing


1) Start dm-mod (if not started yet)


modprobe dm-mod


3) Redetect disks and/or start lvm stuff.


vgscan
vgchange -a -y



Preparing Disks


Let's create a boot partition outside of LVM and all the rest for LVM (120Mb for /boot in 2*40Gb hardisks).



$ sudo fdisk /dev/sda



The number of cylinders for this disk is set to 4865.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)



Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-4865, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1033-9729, default 9729): +120M



Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (17-4865, default 17):
Using default value 17
Last cylinder or +size or +sizeM or +sizeK (17-4865, default 4865):
Using default value 4865



Command (m for help):t
Partition number (1-4): 2
Hex code (type L to list codes): 8e



Command (m for help):w
The partition table has been altered!



Calling ioctl() to re-read partition table.


WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.



$ sudo fdisk /dev/sdb


The number of cylinders for this disk is set to 4998.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)



Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-4998, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-4998, default 4998):
Using default value 4998



Command (m for help):t
Partition number (1-4): 2
Hex code (type L to list codes): 8e



Formating Partitions


1) Format /boot partition
# mke2fs /dev/sda1


2) Create physical volumes for LVM


# pvcreate /dev/sda2
# pvcreate /dev/sdb1



3) Create volume group (I'll use rootvg, like AIX)...


# vgcreate rootvg /dev/sda2


4) ...then add the second physical volume


# vgextend rootvg /dev/sdb1


5) Create the logical volumes.


# lvcreate -n rootlv -L 15G rootvg
# lvcreate -n varlv -L 5G rootvg
# lvcreate -n homelv -L 30G rootvg
# lvcreate -n usrlv -L 15G rootvg



6) Now you're able to *see* this logical volumes.


# mount /dev/rootvg/rootvg-rootlv /mnt
# mount /dev/rootvg/rootvg-usrlv /mnt/usr
# mount /dev/rootvg/rootvg-varlv /mnt/var
# mount /dev/rootvg/rootvg-homelv /mnt/home
# mount -t proc proc /mnt/proc
# mount -o bind /dev /mnt/dev



You know the rest... and don't forget to put LVM support on kernel.

1 comment:

  1. Nice guide. I've always thought about trying this just to be able to resize partitions easy. Thanks!

    ReplyDelete