Expanding Partitions with LVM
The Logical Volume Manager (LVM) enables you to resize your partitions without having to modify the partition tables on your hard disk. This is most useful when you find yourself running out of space on a filesystem and want to expand into a new disk partition versus migrating all or a part of the filesystem to a new disk.
LVM Terms
Before you run through the LVM tutorial, you need to become familiar with some LVM concepts:
Physical Volume: A physical volume (PV) is another name for a regular physical disk partition that is used or will be used by LVM. Volume Group: Any number of physical volumes (PVs) on different disk drives can be lumped together into a volume group (VG). Under LVM, volume groups are analogous to a virtual disk drive. Logical Volumes: Volume groups must then be subdivided into logical volumes. Each logical volume can be individually formatted as if it were a regular Linux partition. A logical volume is, therefore, like a virtual partition on your virtual disk drive. This may seem complicated, but it allows you to create new virtual partitions with sizes you can change from groups of real disk partitions whose sizes you probably cannot change. Another advantage of LVM is that this can all be done without disturbing other partitions on your hard disks. Physical Extent: Real disk partitions are divided into chunks of data called physical extents (PEs) when you add them to a logical volume. PEs are important as you usually have to specify the size of your volume group not in gigabytes, but as a number of physical extents.
Make sure you understand these terms fully as they will be used repeatedly in many of the following sections. Let's go!
Configuring LVM Devices
It is probably best to learn about the features of LVM through a scenario. Suppose a small company needs to expand disk capacity, but there isn't the budget to purchase an adequately sized hard drive. The /home filesystem, which resides on /dev/hde5, has become too full. You just added a new hard drive /dev/hdf with 50% of the capacity of /dev/hde5 into which you want to expand /home. The device /dev/hdf has a single partition named /dev/hdf1 into which /dev/hde5 will be merged. Take a look at the required steps.
Back up Your Data
Use the tar command or some other method to backup your data in /home. The LVM process will destroy the data on all physical volumes.
Unmount Your /home Filesystem
As /home stores most users' data, you'll need to do some preparatory work before unmounting the filesystem:
1. | Use the who command to see who's logged in. If other users are present, send a message with the wall command informing them that the system is about to shutdown.
[root@bigboy tmp]# who
root pts/0 Nov 6 14:46 (192-168-1-242.my-web-site.org)
bob pts/0 Nov 6 12:01 (192-168-1-248.my-web-site.org)
bunny pts/0 Nov 6 16:25 (192-168-1-250.my-web-site.org)
[root@bigboy tmp]# wall The system is shutting down now!
Broadcast message from root (pts/0) (Sun Nov 7 15:04:27 2004):
The system is shutting down now!
[root@bigboy tmp]#
| 2. | Log into the VGA console, and enter single-user mode:
[root@bigboy tmp]# init 1
| 3. | Unmount the filesystem:
sh-2.05b# umount /home
|
Now we're ready to start modifying the partitions, which is covered next.
Determine the Partition Types
You have to change each LVM partition used to be of type 8e (Linux LVM). You can test this with the fdisk -l command. Here is an example using /dev/hde that shows your target partitions are of the incorrect type:
sh-2.05b# fdisk -l /dev/hde
Disk /dev/hde: 4311 MB, 4311982080 bytes
16 heads, 63 sectors/track, 8355 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Device Boot Start End Blocks Id System
/dev/hde1 1 4088 2060320+ fd Linux raid
autodetect
/dev/hde2 4089 5713 819000 83 Linux
/dev/hde3 5714 6607 450576 83 Linux
/dev/hde4 6608 8355 880992 5 Extended
/dev/hde5 6608 7500 450040+ 83 Linux
sh-2.05b#
Start fdisk
You can change the partition type using fdisk with the disk name as its argument. Use it to modify both partitions /dev/hde5 and /dev/hdf1. The fdisk examples that follow are for /dev/hde5; repeat them for /dev/hdf1.
sh-2.05b# fdisk /dev/hde
The number of cylinders for this disk is set to 8355.
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):
Set the ID Type to 8e
You now need to set the partition types to the LVM value of 8e. Partitions /dev/hde5 and /dev/hdf1 are the fifth and sixth partitions on disk /dev/hde. Modify their type using the t command, and then specify the partition number and type code. You can also use the L command to get a full listing of ID types in case you forget.
Command (m for help): t
Partition number (1-6): 5
Hex code (type L to list codes): 8e
Changed system type of partition 5 to 8e (Linux LVM)
Command (m for help): t
Partition number (1-6): 6
Hex code (type L to list codes): 8e
Changed system type of partition 6 to 8e (Linux LVM)
Command (m for help):
Make Sure the Change Occurred
Use the p command to get the new proposed partition table:
Command (m for help): p
Disk /dev/hde: 4311 MB, 4311982080 bytes
16 heads, 63 sectors/track, 8355 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Device Boot Start End Blocks Id System
/dev/hde1 1 4088 2060320+ fd Linux raid
autodetect
/dev/hde2 4089 5713 819000 83 Linux
/dev/hde3 5714 6607 450576 83 Linux
/dev/hde4 6608 8355 880992 5 Extended
/dev/hde5 6608 7500 450040+ 8e Linux LVM
Command (m for help):
Save the Partition Changes
Use the w command to permanently save the changes to disk /dev/hde:
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.
[root@bigboy updates]#
The error above will occur if any of the other partitions on the disk is mounted. This shouldn't be grave as you are already in single user mode in which most of the system's processes that would be accessing the partition have been shutdown.
Define Each Physical Volume
After modifying the partition tables of /dev/hde and /dev/hdf, initialize the target partitions with the pvcreate command. This wipes out all the data on them in preparation for the next step. If you haven't backed up your data yet, do it now!
sh-2.05b# pvcreate /dev/hde5
pvcreate -- physical volume "/dev/hde5" successfully created
sh-2.05b# pvcreate /dev/hdf1
pvcreate -- physical volume "/dev/hdf1" successfully created
sh-2.05b#
Run VGscan
The next step is to make Linux scan for any new LVM disk partitions and automatically create the LVM configuration files in the /etc directory. To do this, use the vgscan command:
sh-2.05b# vgscan
vgscan -- reading all physical volumes (this may take a while...)
sh-2.05b#
Create a Volume Group for the PVs
Use the vgcreate command to combine the two physical volumes into a single unit called a volume group. The LVM software effectively tricks the operating system into thinking the volume group is a new hard disk. In the example, the volume group is called lvm-hde.
sh-2.05b# vgcreate lvm-hde /dev/hdf1 /dev/hde5
Volume group "lvm-hde" successfully created
sh-2.05b#
Therefore, the vgcreate syntax uses the name of the volume group as the first argument followed by the partitions that it will be comprised of as all subsequent arguments.
Create a Logical Volume from the Volume Group
Now you're ready to partition the volume group into logical volumes with the lvcreate command. Like hard disks, which are divided into blocks of data, logical volumes are divided into units called physical extents (PEs).
You'll have to know the number of available PEs before creating the logical volume. This is done with the vgdisplay command using the new lvm-hde volume group as the argument:
sh-2.05b# vgdisplay lvm-hde
--- Volume group ---
VG Name lvm-hde
VG Access read/write
VG Status available/resizable
VG # 0
MAX LV 256
Cur LV 0
Open LV 0
MAX LV Size 255.99 GB
Max PV 256
Cur PV 2
Act PV 2
VG Size 848 MB
PE Size 4 MB
Total PE 212
Alloc PE / Size 0 / 0
Free PE / Size 212 / 848 MB
VG UUID W7bgLB-lAFW-wtKi-wZET-jDJF-8VYD-snUaSZ
sh-2.05b#
As you can see, 212 PEs are available as free. You can now use all 212 of them to create a logical volume named lvm0 from volume group lvm-hde.
sh-2.05b# lvcreate -l 212 lvm-hde -n lvm0
Logical volume "lvm0" created
sh-2.05b#
Format the Logical Volume
After the logical volume is created, you can format it as if it were a regular partition. In this case, use the -t switch to specify to the mkfs formatting program that you want a type ext3 partition:
sh-2.05b# mkfs -t ext3 /dev/lvm-hde/lvm0
mke2fs 1.32 (09-Nov-2002)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
108640 inodes, 217088 blocks
10854 blocks (5.00%) reserved for the super user
First data block=0
7 block groups
32768 blocks per group, 32768 fragments per group
15520 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 38 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
sh-2.05b#
Create a Mount Point
When you formatted the /dev/hde5 partition, you lost the /home directory. Now you have to recreate /home on which you'll later mount your new logical volume:
sh-2.05b# mkdir /home
Update the /etc/fstab File
The /etc/fstab file lists all the partitions that need to be automatically mounted when the system boots. This snippet configures the newly labeled partition to be mounted on the /home mount point:
/dev/lvm-hde/lvm0 /home ext3 defaults 1 2
The /dev/hde5 and /dev/hdf1 partitions are replaced by the combined /lvm0 logical volume. You, therefore, don't want the old partitions to be mounted again. Make sure that any reference to them in this file has either been commented with a # character at the beginning of each line or deleted entirely.
#/dev/hde5 /data1 ext3 defaults 1 2
#/dev/hdf1 /data2 ext3 defaults 1 2
Mount the Volume
The mount -a command reads the /etc/fstab file and mounts all the devices that haven't been mounted already. After mounting, test the volume by listing its directory contents. It should just contain the lost+found directory.
sh-2.05b# mount -a
sh-2.05b# ls /home
lost+found
sh-2.05b#
Restore Your Data
You can now restore your backed up data to /home.
Get Out of Single User Mode
Return to your original run state by using either the init 3 or init 5 commands. The exit command will make you return to your default runlevel.
 |