22 May 2008

LVM on Ubuntu Hardy Heron

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/


Requirements


* Ubuntu install LiveCD 8.04.
* Internet connection or lvm2 deb package.
* Free Disk Space


Preparing


1) Install LVM2 Package


sudo apt-get install lvm2


2) Start dm-mod


sudo modprobe dm-mod


3) Redetect disks


sudo partprobe


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



$ sudo partprobe


Formating Partitions


1) Format /boot partition
$ sudo mke2fs /dev/sda1


2) Create physical volumes for LVM


$ sudo pvcreate /dev/sda2
$ sudo pvcreate /dev/sdb1



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


$ sudo vgcreate rootvg /dev/sda2


4) ...then add the second physical volume


$ sudo vgextend rootvg /dev/sdb1


5) Create the logical volumes.


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



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


7) After installation ends, don't reboot yet. Mount your logical volumes, proc and dev.


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



8) Copy your resolv.conf to filesystem.


$ sudo cp /etc/resolv.conf /mnt/etc/


9) Chroot


$ sudo su -
# chroot /mnt/ /bin/bash
# source /etc/profile



10) And now, install lvm2 and re-install grub


# apt-get install lvm2
# grub-install /dev/sda



11) Now you can reboot.

9 comments:

  1. [...] My post about LVM on ubuntu have a lot of info, so I’ll paste the important info about LVM [...]

    ReplyDelete
  2. Eu sou um spammer viado e adoro fazer spam em comentarios de blogs:

    meu email: jstall@panic.com
    meu hostname: 189-10-174-97.smace701.dsl.brasiltelecom.net.br

    ReplyDelete
  3. [...] your filesystems which is pretty handy when you need more space. A lot of distributions suppohttp://menelkir.wordpress.com/2008/05/22/lvm-on-ubuntu-hardy-heron/SIP application server runs on Linux Linux Devices?? May 28, 2008 ?? Nortel announced a [...]

    ReplyDelete
  4. [...] your filesystems which is pretty handy when you need more space. A lot of distributions suppohttp://menelkir.wordpress.com/2008/05/22/lvm-on-ubuntu-hardy-heron/Cool Solutions: Manually Partitioning Your Hard Drive with fdiskJul 20, 2007 ... linux-1reo: fdisk [...]

    ReplyDelete
  5. João Jerónimo3.2.09

    I don't understand the problem of having the boot partition inside LVM, since Grub knows how to read kernels from LVM logical volumes.
    AFAIK, the only real big problem of having a LVM-based system is about having the root (/) on LVM, since the device mapper has to be configured before mounting the root, and this requires an ugly initrd.

    ReplyDelete
  6. Its good to have /boot off lvm and using ext2, because if something got worse, you'll be able to boot and fixit without a livecd.

    ReplyDelete
  7. yo, menelkir.wordpress.com great name for site)))
    ------------------------
    my blog: http://dewat.ru/

    ReplyDelete
  8. Hello,
    One basic question, do all above steps requires pre-installed system or you can do all the settings for fresh install??

    ReplyDelete