The Linux Logical Volume Management system provides an excellent way to change the sizes of your disks and manage disk space allocated to various tasks.
I will use the following tasks and the above diagram as a way to document how Logical Volumes work.
- Create a new directory on a hard disk, the old way using fstab.
- Create a new directory using logical volumes, but no flexibility.
- Create a flexible configuration for my home dir
- Expand the directory created
Task 1 Create a new directory on a hard disk, the old way using fstab.
This is the example on the bottom of the image above. Make sure that you know the correct device. (Device assignments may change whenever you re-boot.) To make sure,unplug it and run lsblk and blkid (run as root). Now plug in the device and rerun lsblk and blkid. You can compare the before and after to see the device. Create a partition if you want. Use fdisk to partition. Now Make a file system using mkfs.ext4 /dev/sde or /dev/sde1
Label the HD
e2label /dev/sde new_stuff
make sure it has a file system on it
create the mount point for it
mount /dev/sde /home/me/new_stuff
Does the file “not_mounted.txt” show? If so you failed to mount the new disk
If it is gone, you are ok
Add to fstab:
/dev/sde /home/ne/new_stuff ext4 defaults 0 0
Task 2. Create a new directory using logical volumes, but no flexibility.
Make sure you know what dev you are working on. (See above.) Determine size and write it all down.
Create a partition using fdisk if you want.
Make a file system on it
Now umount it, use e2label to label it something that you will recognize.
Now you are going to create the logical volume we see n the above image starting with /dev/sdd.
Check our work
Add the physical volume to the volume group
vgcreate vg_new_proj /dev/sdd
Check our work
Create Logical Volume
lvcreate -n code_new -L 100% vg_new_proj
Instead of 100% you could specify an exact amount, ie 100G
Check our work
OK? Let’s mount it.
touch /home/me/new_proj/not mounted.txt
mount /dev/vg_new_proj/code_new /home/me/new_proj
add to fstab
/dev/vg_new_proj/code_new /home/me/new_proj ext4 defaults 0 0
OK. Let’s do task 3 Create a flexible configuration for my home dir
Modern Linux distributions put /home on a logical volume by default. If you are really trying to expand /home, use task 4 as your template. If you want to create or expand a subdirectory of your personal directory, this is not a perfect example, but you may be able to understand the process from this example.
Partition new disk using fdisk
Make file system on it
e2label /dev/sdb “NewHome”
lgcreate vg_curr_proj /dev/sdb /dev/sdc
lvcreate -n home -L 100 G vg_curr_proj
Ok let’s go to task 4 Expand the directory created
assuming we just created vg home and want to expand it a bit. This gives 100 G additional space.
lvextend -n /dev/vg_curr_proj/home -L 100 G