linux lvm command examples

logical volume management example 03

In this example we are going to go through the basic procedure for adding a disk to LVM. We will then add a second larger disk and move the data from the smaller disk to the larger disk. Two disks have been added into our Virtual environment and can be seen from the "fdisk -l" command below.


# fdisk -l

Disk /dev/sdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000


Disk /dev/sdc: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Initially we are only going to use the smaller disk "/dev/sdb" (2147 MB).

Create a Partition using fdisk

First we need to create a partition for use with LVM. To start the interactive dialogue we issue the command "fdisk /dev/sdb".

We then select "n" for a new partition, then "p" for primary. Next we accept the default partition number and the default size values. We select "t" to set our system partition type to LVM. Option "8e" is selected as this is for LVM. Once complete, we write our changes by selecting "w".


[root@centos ~]# fdisk /dev/sdb

Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x55112c08.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-261, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-261, default 261):
Using default value 261

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

Create a Physical Volume (PV)

Our next step is to create a Physical Volume. To accomplish this we use the "pvcreate" command.


# pvcreate /dev/sdb1
  Writing physical volume data to disk "/dev/sdb1"
  Physical volume "/dev/sdb1" successfully created

Create a Volume Group (VG)

To create a Volume Group we use the command "vgcreate".


# vgcreate vg01 /dev/sdb1
  Volume group "vg01" successfully created

Check our PV and VG

We can display our changes by issuing the "pvs" and "vgs" commands:


# pvs
  PV         VG          Fmt  Attr PSize PFree
  /dev/sda2  vg_centos lvm2 a--  7.51g    0
  /dev/sdb1  vg01        lvm2 a--  2.00g 2.00g

# vgs
  VG          #PV #LV #SN Attr   VSize VFree
  vg01          1   0   0 wz--n- 2.00g 2.00g
  vg_centos   1   2   0 wz--n- 7.51g    0

From the above we can see that "/dev/sdb1" is now associated with the Volume Group "vg01". We can also see that only "1" PV is associated with the Volume Group.

Create a Logical Volume with all available space

The command for creating a Logical Volume is "lvcreate". In our example we give our Logical volume the name "lvdata01" and specify that we will be using 100% of available space from the Volume Group "vg01".


# lvcreate -n lvdata01 -l 100%VG vg01
  Logical volume "lvdata01" created

Checking the PV and LV

If we now issue the commands "lvs" and "vgs" we can check the current state of our configuration:


# lvs
  LV       VG          Attr     LSize Pool Origin Data%  Move Log Copy%  Convert
  lvdata01 vg01        -wi-a--- 2.00g
  lv_root  vg_centos -wi-ao-- 6.04g
  lv_swap  vg_centos -wi-ao-- 1.47g
# vgs
  VG          #PV #LV #SN Attr   VSize VFree
  vg01          1   1   0 wz--n- 2.00g    0
  vg_centos   1   2   0 wz--n- 7.51g    0

From the above we can see that our Logical Volume is 2GB in size and that it is associated with the Volume Group "vg01".

Create a Filesystem

To create a filesystem we will use the "mkfs.ext3" command. This will create a filesystem of type "ext3". You may use a different filesystem type (mkfs.ext4).


# mkfs.ext3 /dev/vg01/lvdata01
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
130816 inodes, 523264 blocks
26163 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=536870912
16 block groups
32768 blocks per group, 32768 fragments per group
8176 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912

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

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

Create a mount point for our filesystem

In this example we will create a directory called "/var/myspace". This will become our mount point. The command used for creating a directory is "mkdir". The command used for mounting our filesystem is "mount".


# mkdir /var/myspace

# mount /dev/vg01/lvdata01 /var/myspace

Once we have mounted our filesystem we can check this easily by issuing the command "df -h".


# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_centos-lv_root
                      6.0G  4.7G  1.1G  82% /
tmpfs                 250M     0  250M   0% /dev/shm
/dev/sda1             485M   38M  422M   9% /boot
/dev/mapper/vg01-lvdata01
                      2.0G   36M  1.9G   2% /var/myspace

We can now see from the above output that our filesystem has been mounted and has "1.9GB" of available space.

Add Entries into Mount Table

For a filesystem to be mounted automatically at boot time, it needs to have an entry defined in what is known as the "mount table". For our filesystem to be automatically mounted, we need to add the following line to the file "/etc/fstab":


/dev/vg01/lvdata01      /var/myspace            ext3    defaults        0 0

Once the line has been added you can test the syntax by issuing the command "mount -a". Any entries in the "/etc/fstab" will be mounted.

Create a large file

For this exercise we are going to create a single file of "1GB" in size. One of the easiest ways to create a large file is to use the "dd" command:


# dd if=/dev/zero of=/var/myspace/big_file.img bs=1G count=1

# ls -lh big*
-rw-r--r--. 1 root root 1.0G Apr 12 08:50 big_file.img

We now have a file called "big_file.img" of "1GB" in size. After creating this file, we can now see from the following "df -h" command that we have used "56%" of our available space.


# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_centos-lv_root
                      6.0G  4.7G  1.1G  82% /
tmpfs                 250M     0  250M   0% /dev/shm
/dev/sda1             485M   38M  422M   9% /boot
/dev/mapper/vg01-lvdata01
                      2.0G  1.1G  850M  56% /var/myspace

Adding another Disk

In our exercise we are now going to add an additional disk to increase the amount of space available. The process will be the same as the first disk. Before we add our next disk we will "unmount" our filesystem with the "umount" command:


# umount /var/myspace

The disk we are going to add is "/dev/sdc". We can view this again from the command "fdisk -l".


# fdisk -l /dev/sdc
Disk /dev/sdc: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Prepare our new disk for use with LVM


# fdisk /dev/sdc
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xdd63f450.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-522, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-522, default 522):
Using default value 522

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

Create a new Physical Volume for new Disk

To create the PV, we will use the "pvcreate" command:


# pvcreate /dev/sdc1
  Writing physical volume data to disk "/dev/sdc1"
  Physical volume "/dev/sdc1" successfully created

Add new Disk to Existing Volume Group

We now issue the "vgextend" command to add "/dev/sdc1" to our existing Volume Group "vg01".


# vgextend vg01 /dev/sdc1
  Volume group "vg01" successfully extended

pvmove command - move data from one disk to another

Now we are going to move data from /dev/sdb1 to our larger area /dev/sdc1 using the "pvmove" command:


# pvmove /dev/sdb1 /dev/sdc1
  /dev/sdb1: Moved: 0.4%
  /dev/sdb1: Moved: 16.8%
  /dev/sdb1: Moved: 34.6%
  /dev/sdb1: Moved: 51.1%
  /dev/sdb1: Moved: 75.0%
  /dev/sdb1: Moved: 100.0%

Remove /dev/sdb1 from Volume Group

Our next step in the exercise is to remove "/dev/sdb1" from the Volume Group "vg01" using the "vgreduce" command:


# vgreduce vg01 /dev/sdb1
  Removed "/dev/sdb1" from volume group "vg01"

Remove a Physical Volume

Our next step is to remove /dev/sdb1 from LVM with the "pvremove" command:


# pvremove /dev/sdb1
  Labels on physical volume "/dev/sdb1" successfully wiped

Extend Logical Volume and Filesystem

To use our new added space we need to extend our Logical Volume "/dev/vg01/lvdata01" and filesystem. The command for this task is "lvextend". Notice, we will use the "-r" option to automatically resize our filesystem:


# lvextend /dev/vg01/lvdata01 /dev/sdc1 -r
fsck from util-linux-ng 2.17.2
/dev/mapper/vg01-lvdata01: 12/130816 files (0.0% non-contiguous), 279590/523264 blocks
  Extending logical volume lvdata01 to 4.00 GiB
  Logical volume lvdata01 successfully resized
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/mapper/vg01-lvdata01 to 1047552 (4k) blocks.
The filesystem on /dev/mapper/vg01-lvdata01 is now 1047552 blocks long.

And finally.... Mount our filesystem again


# mount /dev/vg01/lvdata01 /var/myspace

# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_centos-lv_root
                      6.0G  4.7G  1.1G  82% /
tmpfs                 250M     0  250M   0% /dev/shm
/dev/sda1             485M   38M  422M   9% /boot
/dev/mapper/vg01-lvdata01
                      4.0G  1.1G  2.7G  28% /var/myspace

Now this time when we issue the "df -h" command, we can see that we have now only utilized "28%" of our available space. Our Volume Group is now "4GB" in size. And we have our "big_file.img":


# ls -l /var/myspace
total 1049620
-rw-r--r--. 1 root root 1073741824 Apr 12 08:50 big_file.img