Home > Applications & Tools, Virtualization > How to Resize Linux VM Disks and Prepare Your Linux VM Templates

How to Resize Linux VM Disks and Prepare Your Linux VM Templates

November 13th, 2017 Leave a comment Go to comments

One of great benefits o using virtual machines is that we can increase the disk sizes whenever needed with a few clicks. In the VMware case, you can open the vSphere Client or the Web Client, and locate the virtual machine. Make sure you don’t have any snapshots otherwise you will not be able to change the disk size. Open the “Edit Setting” and select a disk you want to expand. From there, you can set a new size. There are plenty of coverage on this from VMware and other bloggers.

The other common use case is VM cloning. You have a VM template with small disk size and then clone it to a VM with a bigger size you will actually use. With the VM GUI, you can easily do this either during the cloning, or after cloning.

Time to learn how to "Google" and manage your VMware and clouds in a fast and secure

HTML5 App

The above is actually the easy part. Believe it or not, you won’t be able to use the extra space allocated unless you continue with the following steps within the Linux guest OS itself or other similar steps. These steps are not related to hypervisors, so they work in VMs provisioned with Hyper-V, Xen, or KVM.

The following commands assume the VM has a three disk partitions but the primary disk is the one that holds your data. We’ll resize the original disk from 32GB to 40Gb. The steps include removal all the three partitions and creation a new one that takes all the available space on the disk.

One thing you will realize is that you will be better off with one partition per disk in your VM templates in the future. That will make the disk and filesystem resizing a lot easier than otherwise. In that case, you can simply delete once and do the rest the same way as follows.

$ sudo 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.
 
 
Command (m for help): p
Disk /dev/sda: 40 GiB, 42949672960 bytes, 83886080 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: 0x4a373ec2
 
Device     Boot    Start      End  Sectors Size Id Type
/dev/sda1  *        2048 58720255 58718208  28G 83 Linux
/dev/sda2       58722302 67106815  8384514   4G  5 Extended
/dev/sda5       58722304 67106815  8384512   4G 82 Linux swap / Solaris
 
ommand (m for help): d
Partition number (1,2,5, default 5):
 
Partition 5 has been deleted.
 
Command (m for help): d
Partition number (1,2, default 2):
 
Partition 2 has been deleted.
 
Command (m for help): d
Selected partition 1
Partition 1 has been deleted.
 
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-83886079, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-83886079, default 83886079):
 
Created a new partition 1 of type 'Linux' and of size 40 GiB.
 
Command (m for help): w
 
 
$ sudo fdisk -l
Disk /dev/sda: 40 GiB, 42949672960 bytes, 83886080 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: 0x4a373ec2
 
Device     Boot Start      End  Sectors Size Id Type
/dev/sda1        2048 83886079 83884032  40G 83 Linux
 
$ sudo partprobe
Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
Error: Invalid partition table - recursive partition on /dev/sr0.
 
$ resize2fs /dev/sda1
resize2fs 1.42.13 (17-May-2015)
open: Permission denied while opening /dev/sda1
gridstore@ubuntu16:~$ sudo resize2fs /dev/sda1
resize2fs 1.42.13 (17-May-2015)
Filesystem at /dev/sda1 is mounted on /; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 3
The filesystem on /dev/sda1 is now 10485504 (4k) blocks long.
 
$ sudo df -l
Filesystem     1K-blocks    Used Available Use% Mounted on
udev             2004344       0   2004344   0% /dev
tmpfs             404652    5756    398896   2% /run
/dev/sda1       41152832 1272628  37892604   4% /
tmpfs            2023260       0   2023260   0% /dev/shm
tmpfs               5120       0      5120   0% /run/lock
tmpfs            2023260       0   2023260   0% /sys/fs/cgroup
tmpfs             404652       0    404652   0% /run/user/1000

With the above steps, you can have the disk resized and ready to leverage all the 40GB space. Once again, to prepare your Linux templates you want to have a single partition so that you can easily resize it whenever needed.

For more advanced cases, you want to use lvm which worths its own post for more discussion. We’ll cover it later.

  1. November 13th, 2017 at 00:02 | #1

    [DoubleCloud] How to Resize Linux VM Disks and Prepare Your Linux VM Templates https://t.co/GaDFePxZ8V

  2. November 13th, 2017 at 21:47 | #2

    Thank you for writing this useful article.

  1. No trackbacks yet.