Extend a disk

Tutorial to extend a disk or partition in Linux.

Show partition table

Before we can expand a disk, we need to determine what type of disk or partition we have.

To do this I suggest displaying the partition table:

sudo fdisk -l /dev/sda

Disk /dev/sda: 33 GiB, 35433480192 bytes, 69206016 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: 0x0baa69a2

Device     Boot   Start      End  Sectors  Size Id Type
/dev/sda1  *       2048   999423   997376  487M 83 Linux
/dev/sda2       1001470 67106815 66105346 31.5G  5 Extended
/dev/sda5       1001472 67106815 66105344 31.5G 8e Linux LVM

To display all disks, use the following command: sudo fdisk -l

Extend partitions

Before we can resize the volume, we need to resize the partition. In the example, there is a partition of type "extended" (/dev/sda2) and it contains a partition of type "logical lvm" (/dev/sda5). We need to resize them both.

sudo parted /dev/sda

GNU Parted 3.2
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print
Disk /dev/sda: 35.4GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type      File system  Flags
 1      1049kB  512MB   511MB   primary   ext2         boot
 2      513MB   34.4GB  33.8GB  extended
 5      513MB   34.4GB  33.8GB  logical                lvm

(parted) resizepart 2 100%
(parted) resizepart 5 100%
(parted) quit

Extend an LVM volume

You need to start by expanding the physical volume.

sudo pvresize /dev/sda5

View the space available in the volume.

sudo vgs

  VG      #PV #LV #SN Attr   VSize  VFree
  demo-vg   1   2   0 wz--n- 32.52g 1.05g

The name of the volume (VG) is demo-vg.

View logical volumes.

sudo lvs

  LV     VG      Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root   demo-vg -wi-ao---- 27.48g
  swap_1 demo-vg -wi-ao----  4.00g

You can then expand the logical volume.

sudo lvresize --size +1G --resizefs /dev/demo-vg/root #This command will increase the partition by 1 Gb.
sudo lvresize --extents +100%FREE --resizefs /dev/demo-vg/root #Uses all of the free space available in the physical volume.

Resize a disk without LVM

The following command resizes a disk without LVM to the size of the partition. (In case the root disk is sda1)

sudo resize2fs /dev/sda1

Other suggestions

Commandes Linux utiles

CentOS Linux Ubuntu

Liste de commandes Linux utile tous les jours.

Création d'une crontab

CentOS Linux Ubuntu

Tutoriel de création d'une tâche planifier sous Linux via l'utilisation de crontab.

Extensionner un disque

CentOS Linux Ubuntu

Tutoriel pour extensionner un disque ou partition sous Linux.

Copyright 2017-2020 © webO3 - all rights reserved