lvm to extend a disk

I have a new machine, and it has 1 10g drive wich 8 Gb of disk space.

I want to fill up the space, and then try and create new space, using a spare disk that I have.

Fill up the Disk

dd if=/dev/zero of=/junk bs=1024 count=50240000

And that's the drive 99% full.

Add a New disk

I shut the VM down, add a new 5 Gb Disk, attach it and start it back up.

Machine is now back up, and I check the disks

fdisk -l

Disk /dev/sda: 10 GiB, 10737418240 bytes, 20971520 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: 0x67ec4e5c

Device     Boot Start      End  Sectors Size Id Type
/dev/sda1  *     2048 20969471 20967424  10G 8e Linux LVM


Disk /dev/sdb: 10 GiB, 10737418240 bytes, 20971520 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

I have a sdb disk ... unused.

We need to create a Partition

using fdisk /dev/sdb

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): 1
First sector (2048-20971519, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-20971519, default 20971519): 1{g}
Last sector, +sectors or +size{K,M,G,T,P} (2048-20971519, default 20971519): +1gb

Created a new partition 1 of type 'Linux' and of size 954 MiB.

The key Strokes are

  • n
    • new
  • p
    • primary
  • 1
    • Partition Number
    • start from
  • +1gb
    • size in Gb

Then w to write this command

Create a Physical Volume

pvcreate /dev/sdb1

I get the output

 Physical volume "/dev/sdb1" successfully created.

Check the Name of the Volume Group

In the df command you can see the volume name, You also can see it in the vgdisplay command.

vgdisplay | grep -i Name
  VG Name               lvmt-vg

Now Extend the name with the new Physical-Group

vgextend lvmt-vg /dev/sdb1
  Volume group "lvmt-vg" successfully extended

Check the Amount of space in the Physical Volumes

pvscan
  PV /dev/sda1   VG lvmt-vg         lvm2 [10.00 GiB / 36.00 MiB free]
  PV /dev/sdb1   VG lvmt-vg         lvm2 [952.00 MiB / 952.00 MiB free]
  Total: 2 [10.93 GiB] / in use: 2 [10.93 GiB] / in no VG: 0 [0   ]

We can see that we have spare space in the Physical Volume - but it is not available in the OS yet.

df


Filesystem                 Size  Used Avail Use% Mounted on
udev                       469M     0  469M   0% /dev
tmpfs                       99M  3.2M   96M   4% /run
/dev/mapper/lvmt--vg-root  8.8G  8.3G   92M  99% /

Resize the Volume

Use the logical Volume name, we add space from the physical-device.

lvextend -L+500m /dev/mapper/lvmt--vg-root /dev/sdb1
  Size of logical volume lvmt-vg/root changed from 8.96 GiB (2295 extents) to 9.45 GiB (2420 extents).
  Logical volume lvmt-vg/root successfully resized.

The extract space is still not see in the machine.

resizefs

We now resizefs to grown the space.

resize2fs /dev/mapper/1-sys

resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/1-sys is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 2
Performing an on-line resize of /dev/mapper/1-sys to 4718592 (4k) blocks.
The filesystem on /dev/mapper/1-sys is now 4718592 blocks long.

At ths poiint the disk drive has been extended and the Os can finally see it and use it.