None

Extending root fs on linux vm


How to extend the root filesystem of linux vm running on Virtualbox.

By Kostas Koutsogiannopoulos

As an example we are using a CentOS 7 default installation. The system is installed on a single 8GB virtual hard drive with lvm and we need to resize it to 10GB. We could add a new -extra- virtual disk to our system and then extend the filesystem on the new disk. For this example we will resize the existing virtual disk.

Extending the virtual disk (vdi)

On Virtualbox you can extend only "dynamically allocated" virtual disks. If you have "fixed size" you can only clone it to dynamically allocated with the command:

# VBoxManage clonehd MY_DISK.vdi MY_NEW_DISK.vdi --variant Standard

...and then extend it to the new size running:

# VBoxManage modifyhd MY_NEW_DISK.vdi --resize <new_size in megabytes>

In our example we just run:

# VBoxManage modifyhd CENTOS.vdi --resize 10240
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

Unfortunatery the virtual machine needs to be down. This is a limitation of the current version of Virtualbox. Using other virtualization software we could complete the whole procedure live.

After the disk resize you can restart the Virtualbox UI and confirm the new size.

Before:

After:

Then you can boot your virtual machine.

Option 1: Create a new partition

With fdisk you can create a new primary partition on unallocated space.

The command sequence is:

# fdisk /dev/sda

[p] to print the current partitions:

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    16777215     7875584   8e  Linux LVM

[n] and continue with the defaults to create the new partitiοn.

Then in fdisk change the [t]ype of your new partition to Hex code '8e' (Linux LVM).

Then enter [p] for confirmation:

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    16777215     7875584   8e  Linux LVM
/dev/sda3        16777216    20971519     2097152   8e  Linux LVM

[w] to save the changes, then reboot or run partprobe to live probe the kernel about the new partition table.

After boot create a new physical volume on the new partition and confirm:

# pvs
  PV         VG     Fmt  Attr PSize PFree 
  /dev/sda2  centos lvm2 a--  7.51g 40.00m

# pvcreate /dev/sda3
  Physical volume "/dev/sda3" successfully created

# pvs
  PV         VG     Fmt  Attr PSize PFree 
  /dev/sda2  centos lvm2 a--  7.51g 40.00m
  /dev/sda3         lvm2 ---  2.00g  2.00g

Then add your new physical volume to your volume group and confirm:

# vgs
  VG     #PV #LV #SN Attr   VSize VFree 
  centos   1   2   0 wz--n- 7.51g 40.00m

# vgextend centos /dev/sda3
  Volume group "centos" successfully extended

# vgs
  VG     #PV #LV #SN Attr   VSize VFree
  centos   2   2   0 wz--n- 9.50g 2.04g

Then extend your lvm and confirm:

# lvs
  LV   VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root centos -wi-ao----   6.67g                                                    
  swap centos -wi-ao---- 820.00m

# lvextend -l +100%FREE /dev/mapper/centos-root
  Size of logical volume centos/root changed from 6.67 GiB (1707 extents) to 8.70 GiB (2228 extents).
  Logical volume root successfully resized.

* You can also use the "-r" option on lvextend to automatically trigger a filesystem extension. We do it separately here to be more analytical.

# lvs
  LV   VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root centos -wi-ao----   8.70g                                                    
  swap centos -wi-ao---- 820.00m

The last thing you have to do is resize of the filesystem (xfs in our case):

# xfs_growfs /root
meta-data=/dev/mapper/centos-root isize=256    agcount=4, agsize=436992 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=1747968, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 1747968 to 2281472

You can now use the extra space.

Option 2: Recreate the last partition

This is the only option in cases that you already have reached the maximum number of partitions on the hard drive.
With fdisk you can delete the last partition and create a new one of the same type (but different size) without loosing any data.

So after virtual disk resize we have this situation on fdisk:

# fdisk /dev/sda

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    16777215     7875584   8e  Linux LVM

We can delete the second partition (/dev/sda2) and re-create it with the same type (8e). So after [d]elete, [n]ew and [t]ype commands we have the new partition table:

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    20971519     9972736   8e  Linux LVM

Now we can [w]rite the changes and reboot the system.

Afte boot run pvscan > pvresize > pvscan to resize yout physical volume and confirm the changes:

# pvscan
  PV /dev/sda2   VG centos   lvm2 [7.51 GiB / 40.00 MiB free]
  Total: 1 [7.51 GiB] / in use: 1 [7.51 GiB] / in no VG: 0 [0   ]

# pvresize /dev/sda2
  Physical volume "/dev/sda2" changed
  1 physical volume(s) resized / 0 physical volume(s) not resized

# pvscan
  PV /dev/sda2   VG centos   lvm2 [9.51 GiB / 2.04 GiB free]
  Total: 1 [9.51 GiB] / in use: 1 [9.51 GiB] / in no VG: 0 [0   ]

Then proceed with extension of your lvm and filesystem:

# lvextend -l +100%FREE /dev/mapper/centos-root
  Size of logical volume centos/root changed from 6.67 GiB (1707 extents) to 8.71 GiB (2229 extents).
  Logical volume root successfully resized.

# xfs_growfs /root
meta-data=/dev/mapper/centos-root isize=256    agcount=4, agsize=436992 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=1747968, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 1747968 to 2282496

You can now use the extra space.

 


View epilis's profile on LinkedIn Visit us on facebook X epilis rss feed: Latest articles