M01: Introduction to Operating Systems
TU1: Installing, configuring and exploiting a computer system
ASIX1
Practical Exercise 12: Logical Volume Management 4-4-17

Practical Exercise 12:  Logical Volume Management

GENERAL CONDITIONS
1- Deadline: 9-04-2017.
2- Send your report as a PDF file attached to an e-mail with the following specifications:
     a) E-mail address:
cf(at)collados.org or jordi.binefa(at)fje.edu depending on who your teacher is.
     b) File Name:

        b.1)
ASIX1 (Catalan): asix1_surname_name_m01tu01pr12.pdf
        b2.) DAW1 (English): daw1_surname_name_m01tu01pr12.pdf     
     c)
Subject:
         c.1)
ASIX1 (Catalan): asix1_surname_name_m01tu01pr12
         c.2)
DAW1 (English): daw1_surname_name_m01tu01pr12
3- Make this report individually.
4- Left, right, top and bottom margins: 2cm.
5- Character format: a) Font: Arial, b) Size: 10, c) Questions typeface: Bold, d) Answers typeface: Regular.

1- DOCUMENTATION

1.1.- What is LVM.?. Concepts 

LVM stands for Logical Volume Management. It is a system of managing logical volumes, or filesystems, that is much more advanced and flexible than the traditional method of partitioning a disk into one or more segments and formatting that partition with a filesystem. LVM is a abstraction layer between your operating system and physical hard drives. Due to this abstraction layer, the operating system does not see physical hard drives and partitions any longer  and instead,  the operating system  sees  logical hard drives and partitions made of a combination of physical hard drives and partitions.

There are 3 concepts that LVM manages:

a) Physical Volume: Physical Volumes correspond to physical disks or partitions. They are physical block devices such as /dev/sda, /dev/sdb, /dev/sdc1....

b)
Volume Group:  It is a named collection of physical  volumes.  You can name Volume Groups however you like but it's highly recommended to provide  your volume groups with unique names  as far as it is possible. A way to achieve this is to use your host names as a basis. For instance, on a machine called "websrv", you would name a single volume group "vg-websrv", or two volume groups “vg1-websrv” and “vg2-websrv”. A volume  group is  the LVM equivalent of a disk.

c) Logical Volume: Logical volumes are the LVM equivalent of partitions: they hold a filesystem. Unlike partitions though, logical volumes get names rather than numbers, they can span across multiple disks, and do not have to be physically contiguous.

With LVM, disks and partitions can be abstracted to contain multiple disks and partitions into one device. Your operating systems will never know the difference because LVM will only show the OS the volume groups (disks) and logical volumes (partitions) that you have set up.

1.2.-Advantages and Recommendations
 
One of the biggest advantages LVM has is that most operations can be done on the fly, while the system is running. Most operations that you can do with gparted require that the partitions you are trying to manipulate are not in use at the time, so you have to boot from the livecd to perform them.

With gparted you can expand and shrink partitions, but only if they are not in use. LVM can expand a partition while it is mounted, if the filesystem used on it also supports that ( like the usual ext3/4 ). When expanding a partition, gparted can only expand it into adjacent free space, but LVM can use free space anywhere in the volume group.

Because volume groups and logical volumes aren’t physically tied to a hard drive, it makes it easy to dynamically resize, move  and create disks and partitions.

With LVM, if your system is running out of disk space, you can just add another disk and extend the logical volume on the fly.

LVM  allows you to create snapshots of an  Logical Volume at any moment, even while the system is running. A snapshot is the state of a system at a particular point in time, in other words, a read-only copy of the data set frozen at a point in time.

If you need extended features like live snapshots, easy expansion or want to combine multiple hard drives into a single pool of storage then, you should work with LVM.

1.3.- Commands. Steps required to configure LVM

In ordet to manage LVM there are a set the command line tools available. Most of the commands in LVM are very similar to each other. Each valid command is preceded by one of the following:

The physical volume commands are for adding or removing hard drives in volume groups. Volume group commands are for changing what abstracted set of physical partitions are presented to your operating in logical volumes. Logical volume commands will present the volume groups as partitions so that your operating system can use the designated space.

The steps required to configure LVM include:

1st  step --> Create physical volumes (pvcreate)
2nd step --> Create  volume groups (vgcreate)
3rd  step --> Create logical volumes (lvcreate)
4th  step -->  Build filesystems  (mkfs)
5th  step --> Mount  filesystems (mount)

The steps required to remove logical, group and physcal volumes LVM include:

1st  step -->  Umount  filesystems (umount)
2nd step -->  Remove logical volumes (lvremove)
3rd  step -->  Reduce  group volumes (vgreduce)
4th  step -->  Remove  group volumes (vgremove)
5th  step -->  Remove physical voumes (pvremove)


1.4.- Installation

a) All tools to manage a LVM volume are available in lvm2 package ==> Run: sudo  aptitude  install  lvm2
b) In order to start the lvm service the following command should be run: sudo  service  lvm  start

1.5.- Working with physical volumes

a) To declare the /dev/sda2 as a physical volume available for the LVM:   sudo  pvcreate  /dev/sda2
b)
In order to remove the physical volume on  /dev/sda2: sudo  pvremove  /dev/sda2  
c)
To check a physycal volume: sudo  pvdisplay  /dev/sda2

1.6.- Working with volume groups

a) To create a volume group called vg-websrv made of  /dev/sda2 and sdb3:   sudo  vgcreate  vg-websrv /dev/sda2  /dev/sdb3
b)
In order to remove /dev/sda2 from the volume group vg-websrv: sudo  vgreduce  vg-websrv  /dev/sda2
c)
In order to remove all the physical volumes from the volume group vg-websrv: sudo  vgreduce  -a  vg-websrv
d)
In order to remove a volume group called vg-websrv: sudo  vgremove  vg-websrv
e) To check a volume group: sudo  vgdisplay 
vg-websrv

1.7.- Working with volume groups

a) To create a 4GB logical volume called logvol1 in a volume group called vg-websrv:   sudo  lvcreate  -n  logvol1  -L  4g  vg-websrv
b)
In order to remove a
logical volume called logvol1 from a volume group called vg-websrv: sudo  lvremove  /dev/vg-websrv/logvol1
e) To check a logical volume: sudo  lvdisplay  /dev/vg-websrv/logvol1

1.8.- Formatting a logical volume

a) To format a logical volume called logvol1 created in a volume group called vg-websrv: sudo  mkfs  -t  ext4  /dev/vg-websrv/logvol1

1.9.- Mounting a logical volume

a) To mount an Ext4 logical volume called logvol1 created in a volume group called vg-websrv using as a mount point /mnt/lv1: sudo  mount   -t  ext4  /dev/vg-websrv/logvol1  /mnt/lv1

1.10.-  Useful links

a) https://wiki.debian.org/LVM

b) https://wiki.ubuntu.com/Lvm


2- PRACTICAL EXERCISE

0- Run GParted. Select /dev/sdb. Create a msdos partition table.
1- Create a new SATA hard disk with the following characteristics:
    a) Hard drive file type: VDI

    b) Dynamically allocated
    c) Name: deb8
    d) Size: 8GiB
    e)
Attached to  SATA 2

2-
Install and start lvm2.
3- Run  fdisk and
     a) Delete any  partition of the SATA1 and SATA2 hard drives.
     b) Create one single primary partition for the full size of the SATA1 and SATA2 hard drives.
     c) Change the partition type to 8e - Linux LVM.
4-
Declare /dev/sdb1 and /dev/sdc1 as LVM physical volumes. Show information about both LVM physycal volumes.
5- Create a volume group called vg-xxyyzz made of  /dev/sdb1 and /dev/sdc1 (Note: xxyyzz is your host name). Show information about the LVM volume group.
6- Create a 10GB logical volume called lv1 in the volume group  previously created.  Show information about the LVM logical volume.
7-
Create a 5GB logical volume called lv2 in the volume group previously created. Show information about the LVM logical volume.
8-  Format  lv1 and lv2.  The format will be ext4.
9-  Mount  lv1 on /mnt/lv1 (if /mnt/lv1 does not exist then, create it) Check that lv1 has been mounted.
10- Mount  lv2 on /mnt/lv2 (if /mnt/lv2 does not exist then, create it) Check that lv2 has been mounted.
11- Umount lv1 and lv2. Check if lv1 and lv2 have been mounted.
12- Remove the logical  lv1 and   lv2. Check that lv1 and  lv2 have been removed.
13- Remove all the physical volumes from the volume group vg-xxyyzz (Remember: xxyyzz is your host name).
14- Remove the volume group vg-xxyyzz (Remember: xxyyzz is your host name). Check that the volume group has been removed.
15- Remove the physical volumes /dev/sdb1 and /dev/sdc1. Check that both physical volumes have been reduced.