Skip to content

Instantly share code, notes, and snippets.

@hideojoho
Last active April 3, 2023 07:28
Show Gist options
  • Save hideojoho/372128762cdc44a8ea92632b606b520d to your computer and use it in GitHub Desktop.
Save hideojoho/372128762cdc44a8ea92632b606b520d to your computer and use it in GitHub Desktop.
How to increase a VM's disk space

How to increase a VM's disk space

Environments

  • Vagrant
  • Virtualbox
  • CentOS 7

Note

The following steps are based on a Vagrant VM box which does NOT use LVM for a root partition, which seems to be the case for the official CentOS box.

Install the vagrant-disksize plugin

$ vagrant plugin install vagrant-disksize

Edit Vagrantfile

Add the following line to Vagrantfile

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  config.vm.box = "centos-VAGRANTSLASH-7"
  config.disksize.size = '250GB' # <- Add this line to increase from 40GB to 250GB, for instance

Change partition data

Restart the VM and run the following commands.

[vagrant@localhost ~]$ sudo su -
[root@localhost ~]# fdisk -l /dev/sda

Disk /dev/sda: 268.4 GB, 268435456000 bytes, 524288000 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 label type: dos
Disk identifier: 0x0009ef88

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048    83886079    41942016   83  Linux
[root@localhost ~]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).

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: 268.4 GB, 268435456000 bytes, 524288000 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 label type: dos
Disk identifier: 0x0009ef88

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048    83886079    41942016   83  Linux

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (2-4, default 2): 2
First sector (83886080-524287999, default 83886080): 
Using default value 83886080
Last sector, +sectors or +size{K,M,G} (83886080-524287999, default 524287999): 
Using default value 524287999
Partition 2 of type Linux and of size 210 GiB is set

Command (m for help): p

Disk /dev/sda: 268.4 GB, 268435456000 bytes, 524288000 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 label type: dos
Disk identifier: 0x0009ef88

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048    83886079    41942016   83  Linux
/dev/sda2        83886080   524287999   220200960   83  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@localhost ~]# exit
logout
[vagrant@localhost ~]$ exit
logout
Connection to 127.0.0.1 closed.

Restart VM and format and mount the new partition

  • New partition: /dev/sda1
  • Mount point: /mariadb-data
$ vagrant reload
$ vagrant ssh
[vagrant@localhost ~]$ sudo su -
[root@localhost ~]# mkfs -t xfs -f /dev/sda2
meta-data=/dev/sda2              isize=512    agcount=4, agsize=13762560 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=55050240, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=26880, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@localhost ~]# mkdir /mariadb-data
[root@localhost ~]# mount -t xfs -o defaults /dev/sda2 /mariadb-data
[root@localhost ~]# df -H
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        43G  5.1G   38G  12% /
devtmpfs        4.1G     0  4.1G   0% /dev
tmpfs           4.2G     0  4.2G   0% /dev/shm
tmpfs           4.2G  9.0M  4.1G   1% /run
tmpfs           4.2G     0  4.2G   0% /sys/fs/cgroup
tmpfs           821M     0  821M   0% /run/user/1000
/dev/sda2       226G   34M  226G   1% /mariadb-data
[root@localhost ~]# blkid
/dev/sda1: UUID="8ac075e3-1124-4bb6-bef7-a6811bf8b870" TYPE="xfs" 
/dev/sda2: UUID="547a98b3-59a1-4748-90ac-fba19cd6524c" TYPE="xfs" 
[root@localhost ~]# vi /etc/fstab
UUID=547a98b3-59a1-4748-90ac-fba19cd6524c /mariadb-data         xfs     defaults
        0 0 # <- Add this line
[root@localhost ~]# exit
logout
[vagrant@localhost ~]$ exit
logout
Connection to 127.0.0.1 closed.
$ vagrant reload
$ vagrant ssh
[vagrant@localhost ~]$ df -H
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        43G  5.1G   38G  12% /
devtmpfs        4.1G     0  4.1G   0% /dev
tmpfs           4.2G     0  4.2G   0% /dev/shm
tmpfs           4.2G  9.0M  4.1G   1% /run
tmpfs           4.2G     0  4.2G   0% /sys/fs/cgroup
/dev/sda2       226G   34M  226G   1% /mariadb-data
tmpfs           821M     0  821M   0% /run/user/1000

Now you have additional 226GB in a new partition (/mariadb-data). Note that you need to specify a right UUID in /etc/fstab which you can obtain from blkid command.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment