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

Practical Exercise 12:  Logical Volume Management

GENERAL CONDITIONS
1- Deadline: 24-03-2019.
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 

a) Basic ideas:
1- LVM stands for Logical Volume Management. LVM is a system for mapping physical block devices (hard drives, usb memory,..)  onto higher-level virtual block devices.
2- On computers working with VLM,  the operating system works with logical volumes.
3- Logical volumes are  combinations of multiple physical hard drives and/or physical partitions.
4- On computers working with LVM, filesystems are installed on a logical volumes.
5- On computers working with LVM, we mount filesysterm installed on a logical volume.
6- LVM shows to your operating system logical volumes and it "hides" physical hard drives and physical partitions that are part of a logical volume.

b) Some Advantges
1- Most operations can be done on the fly, while the system is running . It means that you can dynamically expand, shrink, move and create volumes. For instance: 
    * You can create a small logical volume and  resize it dynnamically as it get filled up.
    * 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.
2- Adding and replacing disks without service disruption (in combination with hot swaping)
3- Snapshopts of logical volumes 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.
4- Use any number of disks as one big disk.
5- Filesystem encrytion
6- Caching of frequently used data.
7- Logical volumes can span across multiple disks, and do not have to be physically contiguous.
8- Online operation such as resize , move, creates disks, or move data onto newer drives, or taking old drives out of service.
9- Reallocate data (databases, user data, etc...).

c) LVM concepts and definitions

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

2- 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 hard disk. It' s a kind of "logical hard drive".

3- 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.

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:

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 --> Initialize a block divice to be used as a physical volume (pvcreate)
2nd step --> Create  volume groups (vgcreate)
3rd step (no mandatory) -->  Add additional physical volumes to an exisiting volume group (vgextend)
4th  step --> Create logical volumes (lvcreate)
5th  step -->  Build filesystems  (mkfs)
6th  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 
(no mandatory) -->  Reduce a volume grroup or in other words, remove physical volumes from a volume group (vgreduce)
4th  step -->  Remove  a volume group (vgremove)
5th  step -->  Remove physical voumes if a device is no longer required for use by LVM (pvremove)

In order to display properties or attributes of:

a) Logical volumes -->  Run lvdisplay
b) Volume groups --> Run vgdisplay
c) Physical volumes --> Run pvdisplay


1.4.- Installation

All tools to manage a LVM volume are available in lvm2 package ==> Run: sudo  aptitude  install  lvm2


1.5.- Creating 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.- Creating volume groups

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

1.7.- Creating logical volume

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

1.8.- Formatting a logical volume

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

1.9.- Mounting a logical volume

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

References
a)
https://wiki.debian.org/LVM
b)
https://wiki.ubuntu.com/Lvm
c)
https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/4/html/Cluster_Logical_Volume_Manager/LVM_CLI.html

2- PRACTICAL EXERCISE

PART I

1-
Shutdown your virtual machine. Make the following changes on your virtual machine:
    a) Select Storage --> Controller: SATA -->  "Port Count". Change Port Count to 4 (It means that you can plug 4 SATA disks into your virtual machine).
    b) Select disk1 --> Attributes. Tick the Hot-pluggable option.

    c) Select disk2 --> Attributes. Tick the Hot-pluggable option.
2
- Run  fdisk and
     a)
Create a msdos partition table on /dev/sdb and /dev/sdc
     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.
3- Install and start lvm2.

4
-
Declare /dev/sdb1 and /dev/sdc1 as LVM physical volumes. Show information about both LVM physycal volumes.
5- Create a volume group called vg01 made of  /dev/sdb1 and /dev/sdc1. Show information about the LVM volume group.
6- Create a 25GiB logical volume called lv1 in the volume group  previously created.  Show information about the LVM logical volume.
7-
Create a 10GiB 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 lv2. Extend lv2 to 13GiB. In order to extend lv2, read the following link: Growing Logical volumes (1st example). Format and mount again lv2.
Check that lv2 has been mounted  and its new size.
12-
Umount lv1. Shrink lv1 to 21GiB. In order to shrink lv1, read the following link: Shrinking Logical volumes. Format and mount again lv1. Check that lv1 has been mounted  and its new size.
13-
Umount lv2. Add 4GiB to lv2. In order to add size to lv2, read the following link: Growing Logical volumes (2nd example) . Format and mount again lv2. Check that lv2 has been mounted  and its new size.
14-
Unmount lv1 and lv2. Check that lv1 and lv2 have been unmounted.
15- Remove the logical  lv1 and   lv2. Check that lv1 and  lv2 have been removed.
16- Remove the volume group vg01. Check that the volume group has been removed.
17- Remove the physical volumes /dev/sdb1 and /dev/sdc1. Check that both physical volumes have been successfully wiped.
18- Change partitions type to 83 - Linux. Check it.

PART II
1-
Run  fdisk and:
     a) Create one single primary partition for the full size of the SATA1 and SATA2 hard drives.
     b) Change /dev/sdb1 and /dev/sdc1  partition type to 8e - Linux LVM.

2- Create a new logical volumen lv02:
    a)
Declare /dev/sdb1 and /dev/sdc1 as LVM physical volumes.
    b)
Create a volume group called vg02 made of  /dev/sdb1 and /dev/sdc1.
    c)
Create a logical volume called lv02 that fills up the total size of vg02. Check that lv02 has been properly created.
    HELP!!!!!: In order to create a logical volume that fills up the total size of a volume group, you should use --extents   100%VG 
    d) Install an ext4 filesystem on lv02.
    e) Mount lv02 on /mnt/lv02.
Check that lv02 has been properly mounted.
3- Do not poweroff your computer and:
    a) Add a new hard drive into your system with the following features: VDI, dinamically allocated, 40GiB size, called disk3.
    b) Check that the new hard drive is  attached to SATA3 and that the "Hot-pluggable"  attribute has been checked.
4- Run lsscsi. Check that the new hard drive is listed in your system.
5-
Run  fdisk and:
     a) Create one single primary partition for the full size of the SATA3 hard drive.
     b) Change /dev/sdd1 partition type to 8e - Linux LVM.
6- Declare /dev/sdd1 as a LVM physical volume. Add /dev/sdd1 to the volume group vg02.
Show information about the LVM volume group. Read the following link: Adding Physical Volumes to a Volume Group
7- Extends lv02 to fill up the total size of vg02. Read the following link: 
Growing Logical Volumes (3rd example) . Check the new lv02 size.
8- Extend the filesystem installed on lv02. Read  Resizing an Ext4 filesystem or  Extending a Linux File System after Resizing the Volume (AWS). Check the new size of the filesystem installed on lv02 using df.