Monday, September 12, 2016

Linux LVM Tutorial Part 1

Here is my scenario: I have two physical SSDs, one with 500GB and another with 250GB. I installed Ubuntu on 250GB disk using default LVM configuration. I am trying to add the disk with 500GB space into LVM.

First, let's check my configuration:
root@desktop:~# fdisk -l /dev/sd*
Disk /dev/sda: 465.8 GiB, 500107862016 bytes, 976773168 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk /dev/sdb: 232.9 GiB, 250059350016 bytes, 488397168 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: ...

Device       Start       End   Sectors   Size Type
/dev/sdb1     2048   1050623   1048576   512M EFI System
/dev/sdb2  1050624   2050047    999424   488M Linux filesystem
/dev/sdb3  2050048 488396799 486346752 231.9G Linux LVM
Disk /dev/sdb1: 512 MiB, 536870912 bytes, 1048576 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0x00000000
Disk /dev/sdb2: 488 MiB, 511705088 bytes, 999424 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk /dev/sdb3: 231.9 GiB, 249009537024 bytes, 486346752 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
fdisk: cannot open /dev/sdc: No medium found

As you can see, my 500GB drive is mapped to /dev/sda and 250GB drive is mapped to /dev/sdb with 3 partitions, the first being EFI system with 512MB of space, the second being Linux filesystem with 488MB, and the last being Linux LVM with 231.9GB of space. 

To allocate the entire 500GB drive to LVM, run
root@desktop:~# fdisk /dev/sda

Welcome to fdisk (util-linux 2.27.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x55bec7d8.

Command (m for help): m

Help:

  DOS (MBR)
   a   toggle a bootable flag
   b   edit nested BSD disklabel
   c   toggle the dos compatibility flag

  Generic
   d   delete a partition
   F   list free unpartitioned space
   l   list known partition types
   n   add a new partition
   p   print the partition table
   t   change a partition type
   v   verify the partition table
   i   print information about a partition

  Misc
   m   print this menu
   u   change display/entry units
   x   extra functionality (experts only)

  Script
   I   load disk layout from sfdisk script file
   O   dump disk layout to sfdisk script file

  Save & Exit
   w   write table to disk and exit
   q   quit without saving changes

  Create a new label
   g   create a new empty GPT partition table
   G   create a new empty SGI (IRIX) partition table
   o   create a new empty DOS partition table
   s   create a new empty Sun partition table


Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-976773167, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-976773167, default 976773167): 

Created a new partition 1 of type 'Linux' and of size 465.8 GiB.

Command (m for help): t
Selected partition 1
Partition type (type L to list all types): L

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden or  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx         
 5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data    
 6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility   
 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi ea  Rufus alignment
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         eb  BeOS fs        
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ee  GPT            
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        ef  EFI (FAT-12/16/
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f0  Linux/PA-RISC b
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f1  SpeedStor      
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f4  SpeedStor      
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      f2  DOS secondary  
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fb  VMware VMFS    
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fc  VMware VMKCORE 
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fd  Linux raid auto
1c  Hidden W95 FAT3 75  PC/IX           bc  Acronis FAT32 L fe  LANstep        
1e  Hidden W95 FAT1 80  Old Minix       be  Solaris boot    ff  BBT            
Partition type (type L to list all types): 8e
Changed type of partition 'Linux' to 'Linux LVM'.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

Now, let's verify that /dev/sda has been formatted to type LVM:
root@desktop:~# fdisk -l /dev/sda
Disk /dev/sda: 465.8 GiB, 500107862016 bytes, 976773168 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: ...

Device     Boot Start       End   Sectors   Size Id Type
/dev/sda1        2048 976773167 976771120 465.8G 8e Linux LVM

Perfect. Let's now add this physical drive to LVM physical volume:
root@desktop:~# pvcreate /dev/sda1
  Physical volume "/dev/sda1" successfully created

Again, let's verify:
root@desktop:~# pvdisplay 
  --- Physical volume ---
  PV Name               /dev/sdb3
  VG Name               ubuntu-vg
  PV Size               231.91 GiB / not usable 2.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              59368
  Free PE               4075
  Allocated PE          55293
  PV UUID               ...
   
  "/dev/sda1" is a new physical volume of "465.76 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sda1
  VG Name               
  PV Size               465.76 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               ...

You can see that /dev/sdb3 is not fully allocated, as there is some free space. Let's examine logical volume:
root@desktop:~# lvdisplay
  --- Logical volume ---
  LV Path                /dev/ubuntu-vg/root
  LV Name                root
  VG Name                ubuntu-vg
  LV UUID                ...
  LV Write Access        read/write
  LV Creation host, time ubuntu, 2016-09-12 21:40:26 +0900
  LV Status              available
  # open                 1
  LV Size                215.99 GiB
  Current LE             55293
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:0

As you can see, there is only one logical volume, namely /dev/ubuntu-vg/root and its space is only 216GB out of total 232GB of /dev/sdb3. To enlare the volume of this logical volume, I run
root@desktop:~# lvextend /dev/ubuntu-vg/root /dev/sdb3
  Size of logical volume ubuntu-vg/root changed from 215.99 GiB (55293 extents) to 231.91 GiB (59368 extents).
  Logical volume root successfully resized.

Let's verify:
root@desktop:~# lvdisplay
  --- Logical volume ---
  LV Path                /dev/ubuntu-vg/root
  LV Name                root
  VG Name                ubuntu-vg
  LV UUID                ...
  LV Write Access        read/write
  LV Creation host, time ubuntu, 2016-09-12 21:40:26 +0900
  LV Status              available
  # open                 1
  LV Size                231.91 GiB
  Current LE             59368
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:0

Viola! I have enlarged the logical volume /dev/ubuntu-vg/root to use the entire /dev/sdb3 space.

Next, I will create three logical volumes from group volume home:
root@desktop:~# lvcreate --name vm --size 100GB home 
  Logical volume "vm" created.
root@desktop:~# lvcreate --name home --size 200GB home
  Logical volume "home" created.
root@desktop:~# lvcreate --name backup -l 100%FREE home
  Logical volume "backup" created.

The first command creates logical volume named vm with size 100GB from volume group home.
The second command creates logical volume named home with size 200GB from volume group home.
The third last command creates logical volume named backup with the remaining space of group volume home.

After all these steps, my final logical volume info is given below:
root@desktop:~# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/ubuntu-vg/root
  LV Name                root
  VG Name                ubuntu-vg
  LV UUID                ...
  LV Write Access        read/write
  LV Creation host, time ubuntu, 2016-09-12 21:40:26 +0900
  LV Status              available
  # open                 1
  LV Size                231.91 GiB
  Current LE             59368
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:0
   
  --- Logical volume ---
  LV Path                /dev/home/vm
  LV Name                vm
  VG Name                home
  LV UUID                ...
  LV Write Access        read/write
  LV Creation host, time desktop, 2016-09-12 23:14:54 +0900
  LV Status              available
  # open                 0
  LV Size                100.00 GiB
  Current LE             25600
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:1
   
  --- Logical volume ---
  LV Path                /dev/home/home
  LV Name                home
  VG Name                home
  LV UUID                ...
  LV Write Access        read/write
  LV Creation host, time desktop, 2016-09-12 23:15:11 +0900
  LV Status              available
  # open                 0
  LV Size                200.00 GiB
  Current LE             51200
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:2
   
  --- Logical volume ---
  LV Path                /dev/home/backup
  LV Name                backup
  VG Name                home
  LV UUID                ...
  LV Write Access        read/write
  LV Creation host, time desktop, 2016-09-12 23:16:11 +0900
  LV Status              available
  # open                 0
  LV Size                165.76 GiB
  Current LE             42434
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:3


For Linux system to take effect on resize of the partition, you will need to run the following:
root@desktop:~# resize2fs logical_volume_path

where logical_volume_path is /dev/home/home or /dev/ubuntu-vg/root, etc.

No comments:

Post a Comment