Friday, July 27, 2007

LVM on Linux

I feel stupid for saying this but I played around with LVM on Ubuntu Linux for the first time today. I need to get out more. The benefits to LVM are flexibility. The major downside is critical failure of a physical volume will screw all logical volumes in that pool. Ubuntu Linux has LVM almost completely automated; just choose use the whole disk with LVM and it creates a non-LVM boot partition then a physical volume with the rest. Easy. Adding another physical disk was easy:

root@homer:~# fdisk -l

There will be a message similar to this one, "Disk /dev/sdb doesn't contain a valid partition table", if the disk is brand new. If not, the partition table might require some modifications.

root@homer:~# fdisk /dev/sdb

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

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)

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

Calling ioctl() to re-read partition table.
Syncing disks.

The pvdisplay command can be used to determine the volume group name (VG Name) and display physical volumes. Now to create the physical volume, extend the volume group to include the new physical volue, then create a logical volume within the group.

root@homer:~# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created

root@homer:~# vgextend Ubuntu /dev/sdb1
Volume group "Ubuntu" successfully extended

root@homer:~# lvcreate --name share_01 --size 1016M Ubuntu
Logical volume "share_01" created

Once created, put the ext3 filesystem on the new logical volume

root@homer:~# mkfs.ext3 /dev/Ubuntu/share_01
mke2fs 1.38 (30-Jun-2005)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
130048 inodes, 260096 blocks
13004 blocks (5.00%) reserved for the super user
First data block=0
8 block groups
32768 blocks per group, 32768 fragments per group
16256 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376

Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 31 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

Create a mount point then modify the fstab so this new volume mounts.

mkdir /share
root@homer:~# cp /etc/fstab /etc/fstab_20072707
root@homer:~# vi /etc/fstab

Note the backup of fstab made! Reboot the machine and the new logical volume should be available on the mount point

Adding another drive to the pool was just as easy:

root@homer:~# fdisk /dev/sdc

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

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)

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

Calling ioctl() to re-read partition table.
Syncing disks.
root@homer:~# pvcreate /dev/sdc1
Physical volume "/dev/sdc1" successfully created
root@homer:~# vgextend Ubuntu /dev/sdc1
Volume group "Ubuntu" successfully extended

To increase the size of the shared volume:

root@homer:~# umount /share
root@homer:~# lvextend -L2032M /dev/Ubuntu/share_01
Extending logical volume share_01 to 1.98 GB
Logical volume home_01 successfully resized
root@homer:~# e2fsck -f /dev/Ubuntu/share_01
e2fsck 1.38 (30-Jun-2005)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/Ubuntu/share_01: 11/130048 files (0.0% non-contiguous), 8197/260096 blocks
root@homer:~# resize2fs /dev/Ubuntu/share_01
resize2fs 1.38 (30-Jun-2005)
Resizing the filesystem on /dev/Ubuntu/share_01 to 520192 (4k) blocks.
The filesystem on /dev/Ubuntu/share_01 is now 520192 blocks long.

root@homer:~# mount /dev/Ubuntu/share_01 /share_01

Too easy... Here are some links that were very helpful:

No comments: