Monday, January 09, 2012

Extending Disk space on Your Linux Computer

*************************************************************************************
Extending Disk space on Your Linux Computer using LVM2.
*************************************************************************************

Prerequisite: This tutorial covers adding disk space to your linux computer. First it is assumed that the new hard drive was physically added to your system.

As root perform the following:

[root]# fdisk /dev/sdb
Command (m for help): m (Enter the letter "m" to get list of commands)
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)

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

Command (m for help): p

Disk /dev/sdb: 240 heads, 63 sectors, 2654 cylinders
Units = cylinders of 15120 * 512 bytes

Device Boot Start End Blocks Id System
/dev/sdb1 1 2654 20064208+ 5 Linux

Command (m for help): w (Write and save partition table)

Format the new volume using the mkfs command:

[root]# mkfs -t ext3 /dev/sdb1
mke2fs 1.27 (8-Mar-2002)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
2508352 inodes, 5016052 blocks
250802 blocks (5.00%) reserved for the super user
First data block=0
154 block groups
32768 blocks per group, 32768 fragments per group
16288 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000

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

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


The pvdisplay allows you to see the attributes of one or more physical volumes like size, physical extent size, space used for the volume group descriptor area and so on.

[root]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name vg_lvm
PV Size 19.51 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 4994
Free PE 0
Allocated PE 4994
PV UUID uZOPl7-Jg0a-QAbV-K3NU-402p-Ia7i-eUTe81


Adding physical volumes to a volume group
Use 'vgextend' to add an initialized physical volume to an existing volume group (vg_lvm).

[root]# vgextend vg_lvm /dev/sdb1

[root]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name vg_lvm
PV Size 19.51 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 4994
Free PE 0
Allocated PE 4994
PV UUID uZOPl7-Jg0a-QAbV-K3NU-402p-Ia7i-eUTe81

--- Physical volume ---
PV Name /dev/sdb1
VG Name vg_lvm
PV Size 19.99 GiB / not usable 1.43 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 5118
Free PE 126
Allocated PE 4992
PV UUID up8sB5-3lSg-Elwc-xoLj-xn3N-cqc2-KoQ6cY

Extending a logical volume

To extend a logical volume you simply tell the lvextend command how much you want to increase the size. You can specify how much to grow the volume, or how large you want it to grow to:

[root]# lvextend -L20G /dev/mapper/vg_lvm-lv_root
lvextend -- extending logical volume "/dev/mapper/vg_lvm-lv_root" to 20 GB
lvextend -- doing automatic backup of volume group "vg_lvm"
lvextend -- logical volume "/dev/mapper/vg_lvm-lv_root" successfully extended

This will extend /dev/mapper/vg_lvm-lv_root to 20 Gigabytes.

[root]# lvextend -L+13G /dev/mapper/vg_lvm-lv_root
lvextend -- extending logical volume "/dev/mapper/vg_lvm-lv_root" to 33 GB
lvextend -- doing automatic backup of volume group "vg_lvm"
lvextend -- logical volume "/dev/mapper/vg_lvm-lv_root" successfully extended

will add another 13 GB to /dev/mapper/vg_lvm-lv_root.

After you have extended the logical volume it is necessary to increase the file system size to match. how you do this depends on the file system you are using.

By default, most file system resizing tools will increase the size of the file system to be the size of the underlying logical volume so you don't need to worry about specifying the same size for each of the two commands.

Unless you have patched your kernel with the ext2online patch it is necessary to unmount the file system before resizing it. (It seems that the online resizing patch is rather dangerous, so use at your own risk)

[root]# resize2fs /dev/mapper/vg_lvm-lv_root

[root]# lvdisplay
--- Logical volume ---
LV Name /dev/vg_lvm/lv_root
VG Name vg_lvm
LV UUID ZzRWTt-M8QA-Awke-2t6c-Mf7i-0CUe-Xe2O4H
LV Write Access read/write
LV Status available
# open 1
LV Size 33.13 GiB
Current LE 8482
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0

*************************************************************************************
Extending Swap on an LVM2 Logical Volume
*************************************************************************************

To extend an LVM2 swap logical volume (assuming /dev/vg_lvm/lv_swap is the volume you want to extend) from 6GB to 11 GB:

Disable swapping for the associated logical volume:
[root]# swapoff -v /dev/vg_lvm/lv_swap

Resize the LVM2 logical volume by 5 GB:
[root]# lvm lvresize /dev/vg_lvm/lv_swap -L +5G

Format the new swap space:
[root]# mkswap /dev/vg_lvm/lv_swap

Enable the extended logical volume:
[root]# swapon -va

Test that the logical volume has been extended properly:
[root]# free
total used free shared buffers cached
Mem: 3924924 229652 3695272 0 9372 67056
-/+ buffers/cache: 153224 3771700
Swap: 11300856 0 11300856

[root]# lvdisplay /dev/vg_lvm/lv_swap
--- Logical volume ---
LV Name /dev/vg_lvm/lv_swap
VG Name vg_lvm
LV UUID ewhNre-qYYE-iY0W-HtQE-m5jk-x7s4-f8UPUb
LV Write Access read/write
LV Status available
# open 1
LV Size 10.78 GiB
Current LE 2759
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:1

No comments: