How To: Increase the Size of Red Hat / CentOS / Fedora VirtualBox guest host … and some LVM usage stuff

Categories How To, Systems

Sometimes you find yourself cheeping out an giving not enough space on you virtualbox host machine. Self-sabotage, masochism – who knows the reasons that rule the decision of not assigning the default 8GBs of space that is ALLOCATED AS NEEDED. Sometimes you give it 32 gigs, and finding that all of a sudden that is not enough… In any case you have the privilege to reverse some mistakes of planing and this is one of them reversible types.
Since software is constructed following the principle of abstraction (a.k.a information hiding, a.k.a you don’t need to know the fundamentals just do your job) you have layers upon layers of software managing storage these days. Each one of them is involved when you want to increase the effective size of your drive, where you can hold files.

Physical media (.vdi) --> Partitions (gparted) --> Logical Volume Mmgt --> File System
           |                      |                    /         \
           |                      |                   /           \
Linux Block Device --> Physical Volumes --> Volume Groups --> Logical Volumes

The problem / initial state:

This is the before size, and my target is the /dev/mapper/cl-root FS.

[[email protected] ~]# df -h
Filesystem           Size  Used Avail Use% Mounted on
/dev/mapper/cl-root  3.5G  2.2G  1.4G  61% /
devtmpfs             235M     0  235M   0% /dev
tmpfs                245M     0  245M   0% /dev/shm
tmpfs                245M  4.4M  241M   2% /run
tmpfs                245M     0  245M   0% /sys/fs/cgroup
/dev/sda1           1014M  182M  833M  18% /boot
tmpfs                 49M     0   49M   0% /run/user/0

The Steps:

Resize the VDI using the VBoxManage utility that comes with VirtualBox:

VBoxManage modifyhd "F:\VMs\Centos7 - Server1\Centos7 - Server1.vdi" --resize 8192

Calculate the size like this: 1024 * GIGS_U_WANT, since the --resize takes megabytes as the parameter. For example: 1024 * 8 = 8192 to get 8 GB. Then, expand the partition w/ GParted, since you will not see any space added from the perspective of the host VM yet:

Then boot up your Red Hat / CentOS / Fedora box and perform a series of fdisk lvextend resize2fs / xfs_growfs commands. I performed this on a live system w/ LVM w/o any problems. The system was not running any web, mail servers, webapps, containers – it was bare. Consider taking the system out of a live pool and backing up the FS before the resize if it so happens that you are using these steps on an important live system.

Step# El Commando Una explicación
0 df -h Check the free disk space in human readable units
1 fdisk -l List partition table(s)
2 fdisk /dev/sd<a-z> Begin fdisk utility in order to change the partitioning information for the disk of interest
3 p print the partition table (remember important information, like names and sizes from the output)
4 d Delete partition …
5 2 … number 2 (which is the /dev/sda2)
6 n Create a new partition …
7 p … make it a primary partition …
8 2 … number 2 (since we deleted number 2, we are now recreating it).
9 x2 “Return” Choose default options for the start and end of the partition sectors.
10 w Write aka confirm the information you entered
11 reboot Reboot the host with the reformmated partition table
12 pvresize /dev/your-PV; Resize physical volumes (why is physical volume resized after the partition info is reformated? – Because of LVM)
13 pvscan List all physical volumes
14 lvextend -l +100%FREE /dev/mapper/your-FS; Add space to a logical volume
15 resize2fs /dev/mapper/your-FS
xfs_growfs /dev/mapper/your-FS
Grow the file system
16 df -h Check the free disk space in human readable units

Steps more in depth:

[[email protected] ~]# fdisk -s /dev/sda2 -l
Disk /dev/sda2: 7515 MB, 7515144192 bytes, 14678016 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

[[email protected] ~]# fdisk -l
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 16777215 7339008 8e Linux LVM

You will be asked for the partition sector size when using the fdisk utility (changing the partition table, steps 2-9 in the table). If your partition needs to span the entire … well … partition (yea, hideous abstractions) you can stick with default options for the beginning and end sectors:
First sector (2099200-16777215, default 2099200):
Using default value 2099200
Last sector, +sectors or +size{K,M,G} (2099200-16777215, default 16777215):
Using default value 16777215
Partition 2 of type Linux and of size 7 GiB is set

Then extend the logical volume (you should see the below result):
[[email protected] ~]# lvextend -l +100%FREE /dev/mapper/cl-root
Size of logical volume cl/root changed from 3.50 GiB (895 extents) to 6.50 GiB (1663 extents).
Logical volume cl/root successfully resized.

Problem w/ resize2fs on Centos 7

You might encounter a problem to resize the filesystem w/ resize2fs utility (I encountered it on Centos 7) whos ugly head looks like this:

[[email protected] ~]# resize2fs /dev/mapper/cl-root
resize2fs 1.42.9 (28-Dec-2013)
resize2fs: Bad magic number in super-block while trying to open /dev/mapper/cl-root
Couldn't find valid filesystem superblock.

… try xfs_growfs like below (let me know if it does not work):
[[email protected] ~]# xfs_growfs /dev/mapper/cl-root
meta-data=/dev/mapper/cl-root isize=512 agcount=4, agsize=229120 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=916480, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
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 916480 to 1702912

Digression on /dev/sdXX

A short excursion (digression) on the /dev/sda2 or /dev/sd&lt;a-z&gt;&lt;Aa-Zz&gt;&lt;1-N&gt; more generally. You will encounter the something like /dev/sdb12 in bigger linux systems. The last one means – the 12th partition of the b drive. The /dev/ is where all the devices are at (*nix as you might know treats almost everything as a file to be read from and / or written to). The /sb... stands for a block device and has become a generic term for all block devices historically … what for la convienencia of course! [1].

Results / solution / final state:

[[email protected] ~]# df -h
/dev/mapper/cl-root 6.5G 2.2G 4.4G 33% /



Leave a Reply