In today’s article we will demonstrate how you can easily create an LVM partition on your Linux Server. Why LVM? Simply because it allows you to dynamically extend or shrink partitions in real-time!
In an era when Cloud servers and virtualization is greatly used by system admins, the LVM (Logical Volume Manager) allows you to resize Volume Groups (VG) by adding new physical volumes (PV) or deleting existing PVs attached to VGs.
In the end of this article you can find useful LVM commands. Let’s proceed with the steps to create a Linux LVM partition. For the sake of this article tutorial, we assume that you have inserted a new Hard Disk on your server.
Step-by-Step Guide to Create a Linux LVM Partition
- Connect on your server with root user (or other sudo privileged account)
- Type fdisk -l to identify your new hard disk device name. We assume that ours is named /dev/xvdb
- Type fdisk /dev/xvdb
- You are now into the fdisk command prompt, type the following commands in the order specified below:
- type n to create a new disk partition
- type p to create a primary partition
- type 1 to declare it as the first partition of your new Disk
- hit ENTER twice to accept the default start / end cylinders
- type t to change the default Linux partition to LVM partition type
- type L to list all partition types and search for the id for Linux LVM
- type 8e (as per list results from previous step) to change your partition into 8e, i.e. Linux LVM
- type w to write the partition table (basically to apply the changes from all previous steps) and exit fdisk upon completion
- type n to create a new disk partition
- Now it’s time to create a Physical Volume (PV) on our new hard disk: pvcreate /dev/xvdb1
- Create LVM Volume Group (VG) named vg0 with a physical extend size (PE) of 16MB: vgcreate -s 16M vg0 /dev/xvdb1
- To create a Logical Volume (LV) named lvol0 on VG vg0, type: lvcreate -L 400M -n lvol0 vg0
- Let’s format the Logical Volum lvol0 in a RHEL supported file system, e.g. EXT3: mkfs -t ext3 -m 1 -v /dev/mapper/vg0-lvol0
- Create a mount location for our new ext3 file system: mkdir /mnt/vfs
- Last step is to mount the new EXT3 file system to our newly created mount point: mount -t ext4 /dev/mapper/vg0-lvol0 /mnt/vfs
- You can confirm everything has been created correctly, by typing: df -h. An output as follows should appear:
/dev/mapper/vg0-lvol0 385M 12M 373M 3% /mnt/vfs
You are all set! Here is a list of some useful LVM commands to have in hand:
- To display volume group settings (in our case of the VG named vg0) such as physical size (PE size), Volume group name (VG name), maximum logical volumes (Max LV), etc.
vgdisplay vg0
- To list all physical volumes (PV) created for the volume group
pvscan
- To add new physical volumes (PV) to an existing volume group, either through new hard disk or new disk partitions.
vgextend