JBY Technologies

Deleting And Creating LVM Logical Volumes On Centos 6

I don't use the /home logical volume that the Centos 6 Minimal Install script creates, so I delete it and either assign all the disk space to root, or create a different logical volume.

Get the names of the logical volumes with a df command:

[root@HOSTNAME ~]# df -H
Filesystem     Size    Used   Available   Use%   Mounted on
/dev/mapper/vg_hostname-lv_root
                      150G    30G    120G         20%       /
tmpfs               4G     .10G     3.90G       1%        /dev/shm
/dev/sda3        .5G     .1G      .4G           20%      /boot
/dev/mapper/vg_hostname-lv_home
                        50G     10G     40G         20%      /home

save any home directories created during the install...

mkdir /tmphome

cd /home

mv * /tmphome

cd /

unmount the logical volume to be deleted

umount /home

put the home directories back in /home...

mv /tmphome/* /home

remove the temporary directory...

rmdir /tmphome

Note: if the previous steps seemed illogical... there is a /home directory on the root filesystem. Previously, a logical volume/filesystem, effectively a disk partition, was mounted on /home. We are unmounting it, and locating the home directories in the /home directory on the root filesystem, instead of on their own partition.

remove /home from /etc/fstab...

note: if you are going to add a different logical volume, skip removing it from fstab. We will just edit it for the new name.

vi /etc/fstab

delete the

/dev/mapper/vg_hostname-lv_home /home ext4 defaults 1 2

line (arrow to the line and hit "dd")

save fstab

:x

make sure you have enough available space in the volume group for what you want to do...

[root@HOSTNAME ~]# vgs
VG                 #P   #LV  #SN  Attr  VSize  VFree
vg_hostname   1      3     0  wz--n-  1.80t 1.50t

delete the logical volume:

lvremove /dev/mapper/vg_hostname-lv_home

Note: it is possible to increase the size of a filesystem when it is mounted, but not to decrease it. This is significant if you need to resize the root filesystem. To decrease the size of the root filesystem, you must boot into single user mode. We are increasing the size of the root filesystem, so we are ok in multi user mode.

increase the root logical volume to 300 GB...

lvextend -L 300G /dev/mapper/vg_hostname-lv_root

lvextend said it worked, but I don't see the new size in a df -- that is because I also need to resize the filesystem. Had I remembered, I could have run...

lvextend -L 300G -r /dev/mapper/vg_hostname-lv_root

where the '-r' flag causes lvextend to automatically resize the filesystem as well.

I can run the resize2fs program to fix it, though...

resize2fs /dev/mapper/vg_hostname-lv_root

which will probably take a few minutes.

if you do a df after resize2fs finishes, you should see 300 GB assigned to the root filesystem.

if we were just deleting /home and giving some extra disk space to root, we are done.

to add a new logical volume of one terabyte called lv_data...

lvcreate -L 1T -n lv_data vg_hostname

to format the new logical volume with an ext4 filesystem...

mkfs.ext4 /dev/mapper/vg_hostname-lv_data

add a directory to mount the new filesystem on...

mkdir /data

revisit /etc/fstab, and change this line...

/dev/mapper/vg_hostname-lv_home /home ext4 defaults 1 2

to this...

/dev/mapper/vg_hostname-lv_data /data ext4 defaults 1 2

when the system is booted, the init scripts read through fstab and try to mount the drives configured therein. You can turn this off by changing "defaults" to "noauto", but that seems undesirable in this case. "noauto" is more often used with remote nfs or Windows filesystems that might be inaccessible at boot time.

make sure you can mount the drive, and validate your fstab entry...

mount /data

afterwards, a df should look something like this...

[root@HOSTNAME ~]# df -H
Filesystem     Size    Used   Available   Use%   Mounted on
/dev/mapper/vg_hostname-lv_root
                      300G    30G    270G         10%       /
tmpfs               4G     .10G     3.90G       1%        /dev/shm
/dev/sda3        .5G     .1G      .4G           20%      /boot
/dev/mapper/vg_hostname-lv_data
                        1.0T     .1T     .9T           10%      /data

Bookmark and Share

Legal Notices