How to Increase Linux VM ext4 File System

If you have ever created a Linux VM guest and made the ext4 root disk too small, there is a pretty simple way to increase the size of the disk. The following will outline those steps.  Sections in blue are what you would type:

  • Edit the settings in vSphere for the VM

    1. increase the size of the disk
    2. add another disk just for linux swap (same size as memory).  I like to separate the swap partition from the root.  In VMware, this is very easy and makes this process very simple
    3. reboot VM.
  • On the VM do the following:

    1. Delete all the partitions on /dev/sda (1,2,5) and recreate just the root primary partition /dev/sda1
    2. create swap disk using mkswap command
    3. edit /etc/fstab and chage UUID of swap disk
    4. reboot server
    5. after reboot, resize root partition /dev/sda1
    6. confirm your work

Delete and Recreate

root@freeradius2:~# fdisk /dev/sda

Command (m for help): p

Disk /dev/sda: 21.5 GB, 21474836480 bytes
64 heads, 2 sectors/track, 327680 cylinders, total 41943040 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 identifier: 0x00000900

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 31457279 15727616 83 Linux
/dev/sda2 31459326 33552383 1046529 5 Extended
/dev/sda5 31459328 33552383 1046528 82 Linux swap / Solaris

 Delete All Partitions

Command (m for help): d
Partition number (1-5): 1

Command (m for help): d
Partition number (1-5): 2

Command (m for help): d
Partition number (1-5): 5

Command (m for help): p

Disk /dev/sda: 21.5 GB, 21474836480 bytes
64 heads, 2 sectors/track, 327680 cylinders, total 41943040 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 identifier: 0x00000900

Device Boot Start End Blocks Id System

Create New  root Partion, use same first sector as previous partition

Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
Using default value 1
First sector (2048-41943039, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039):
Using default value 41943039

Command (m for help): p

Disk /dev/sda: 21.5 GB, 21474836480 bytes
64 heads, 2 sectors/track, 327680 cylinders, total 41943040 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 identifier: 0x00000900

Device Boot Start End Blocks Id System
/dev/sda1 2048 41943039 20970496 83 Linux

Make Partition Bootable

Command (m for help): a
Partition number (1-4): 1

Command (m for help): p

Disk /dev/sda: 21.5 GB, 21474836480 bytes
64 heads, 2 sectors/track, 327680 cylinders, total 41943040 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 identifier: 0x00000900

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 41943039 20970496 83 Linux

Write Label to Disk

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@freeradius2:~#

Make Swap Disk, note the UUID which we will add to fstab

root@freeradius2:~# mkswap -f /dev/sdb
Setting up swapspace version 1, size = 4194300 KiB
no label, UUID=57938015-46bc-469b-b4bb-f41b4d2f5a30

Edit /etc/fstab, change UUID of swap

root@freeradius2:/etc# vi fstab

# /etc/fstab: static file system information.
#
# Use ‘blkid -o value -s UUID’ to print the universally unique identifier
# for a device; this may be used with UUID= as a more robust way to name
# devices that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
proc            /proc           proc    nodev,noexec,nosuid 0       0
# / was on /dev/sda1 during installation
UUID=46d6653b-7686-43f3-95ce-ecf3571b578e /               ext4    errors=remount-ro 0       1
# swap was on /dev/sda5 during installation
UUID=57938015-46bc-469b-b4bb-f41b4d2f5a30 none            swap    sw              0       0
/dev/fd0        /media/floppy0  auto    rw,user,noauto,exec,utf8 0       0

Reboot Server

root@freeradius2:~# reboot

After Reboot, Resize root 

root@freeradius2:~# resize2fs /dev/sda1
resize2fs 1.42 (29-Nov-2011)
Filesystem at /dev/sda1 is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 2
Performing an on-line resize of /dev/sda1 to 5242624 (4k) blocks.
The filesystem on /dev/sda1 is now 5242624 blocks long.

 Now Check Your Work

root@freeradius2:~# df -h

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        20G  4.7G   15G  25% /
udev            2.0G  4.0K  2.0G   1% /dev
tmpfs           792M  536K  791M   1% /run
none            5.0M     0  5.0M   0% /run/lock
none            2.0G     0  2.0G   0% /run/shm

 

root@freeradius2:~# swapon -s

Filename                                Type            Size    Used    Priority
/dev/sdb                                partition       4194300 0       -1

Permanent link to this article: https://daherlabs.mywire.org/wordpress/?p=52

1 comment

  1. thanks for the great post. it reminds me that i have to bring more structure into my blogging. your blog is very interesting. please let me know how to go for your rss blog. lista de email lista de email lista de email lista de email lista de email

Leave a Reply

Your email address will not be published.