The need to create a new filesystem has been around since the beginning of the first versions of Unix and has not gone away with Linux. It has gotten easier, however, with Logical Volume Management.
This article covers creating a Physical Volume (VG) and a new Volume Group (VG) on a hard drive, and then a Logical Volume in an LVM environment using the Command Line Interface (CLI).
Install hard drive
If there is not enough space on the existing hard drive(s) in the system to add the desired amount of space it may be necessary to add a new hard drive and create the space to add to the Logical Volume.
First install the hard drive and then perform the following steps.
Create Physical Volume from hard drive
Use the dmesg command to determine the drive name in the /dev directory. We will assume for this example that the new hard drive is assigned as /dev/sdb. You should be sure to use the correct device for your computer.
Now create a new Physical Volume (PV) from the new hard drive.
It is not necessary to create a partition of any kind on the new hard drive. This creation of the Physical Volume which will be recognized by the Logical Volume Manager can be performed on a newly installed raw disk or on a Linux partition of type 83. If you are going to use the entire hard drive, creating a partition first does not offer any particular advantages and uses disk space for metadata that could otherwise be used as part of the PV.
Create the Volume Group
Use the following command to create the new Volume Group with the name MyVG01 on the new Physical Volume. You can name the new Volume Group anything you want, but I recommend using a name that has some meaning with respect to the name or ID of the disk on which it is created.
vgcreate MyVG01 /dev/sdb
The above command uses all of the space on the PV for the VG. You can create a new Volume Group consisting of one or more complete Physical Volumes. It is not possible to create a Volume Group from a partial Physical Volume.
Create the Logical Volume
Creating the Logical Volume is the next step. The following command creates a Logical Volume with the name MyVolume and of 5GB size on the Volume Group, MyVG01.
lvcreate -L 5G -n MyVolume MyVG01
You can create a Logical Volume that takes the entire Volume Group with the next command. Just do not specify the size of the Logical Volume to be created.
lvcreate -n MyVolume MyVG01
Multiple Logical Volumes can be created on a single Volume Group.
Create the Filesystem
Now create the EXT4 filesystem on the Logical Volume. In this example the name of the new Logical Volume is MyVolume. Of course you can use any name you choose.
mkfs -t ext4 /dev/mapper/MyVG01-MyVolume
You may wish to add a volume label to the new filesystem. I like to do this as it can assist in determining the use to which each filesystem is put when problems arise and they must be identified in a rescue or reinstallation situation. I name my EXT filesystems with a meaningful name, such as root, home, var, and so on.
e2label /dev/mapper/MyVG01-MyVolume MyVolume
After creating the new filesystem on the Logical Volume, you must create a mount point and add a line to the /etc/fstab file for the new filesystem so that it will be mounted automatically upon every boot.
The two possible lines you might add to /etc/fstab for this mountpoint and Logical Volume are shown below. You should use one or the other, but not both, in your fstab file.
/dev/mapper/MyVG01-MyVolume /MyVolume ext3 defaults 1 2 LABEL=MyVolume /MyVolume ext3 defaults 1 2
You can now mount the new filesystem using one of the two following commands. You can mount this filesystem explicitly using the command below.
Or you can mount any and all currently unmounted filesystems that have entries in the fstab file with the next command.
That completes the creation and mounting of a new filesystem on a new Volume Group.