Wednesday, August 12, 2015

Extending a root filesystem in Linux without LVM

Extending a root filesystem in Linux without LVM

Overview

One of the great things about working with virtual environments is that if you start to run out of space in a virtual machine, it is usually trivial to extend your disk. I’m going to take the example of a Linux guest under VMware Workstation though this should apply to most hypervisors and flavors of Linux.
If your VM is not using LVM, the steps you’ll perform are:
  1. Extend the size of the physical disk
  2. Extend the size of the partition on the physical disk
  3. Do an online resize of the filesystem to use the new space
There is a caveat that your root filesystem (or the one you’re extending) must be the last one on disk so it can grow contiguously into the free space. If your filesystem is very old, created with an older build of mkfs.ext2, it may not be configured for online resize (missing -O resize_inode flag – more recent versions of mkfs.ext2 automatically reserve space for block group descriptor tables, older versions ~2007/8 may not) and thus these steps won’t work.
If your guest VM is critical in any way, shape or form, I strongly recommend cloning it to keep a backup in case something goes wrong during the filesystem resize process.

Online resize of a root filesystem without LVM

Since you don’t have LVM, your only option is to increase the size of the hard disk and then expand the partition containing the filesystem you wish to expand. The contraints mentioned above apply. The filesystem to resize MUST be the last one on the disk and you can not have more than 3 partitions on the disk already. Confirm that you meet these requirements:
[root@temeria ~]# parted
GNU Parted 2.1
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sda: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number  Start   End     Size    Type     File system  Flags 
1      1049kB  316MB   315MB   primary  ext4         boot 
2      316MB   17.2GB  16.9GB  primary  ext4
As you can see here, partition 2 is the last one on disk which meets our criteria.
If you find that you have a swap partition (#3) after the root filesystem, there is a workaround.
Exit parted and run “swapoff -a” to turn off all swap. Then go back into parted and delete the swap partition, so if it was number 3, you’d run parted and type “rm 3”. After this, edit /etc/fstab and remove the line pointing to the swap partition so your system doesn’t try to mount it at the next boot. Once you’ve done this,  you should be able to continue with the rest of this process.
You will then end up without a swap partition at the end of this process.
We can confirm that this corresponds with the root filesystem by taking a look at our mounted filesystems:
(type “q” to exit out of parted first)
[root@temeria ~]# df -k
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda2             16274852   2196088  13252040  15% /
tmpfs                  1025580        88   1025492   1% /dev/shm
/dev/sda1               297485     32659    249466  12% /boot

Increase disk allocation in the hypervisor

The first thing you need to do is increase the size of your existing disk.
Increase physical disk allocation for the virtual machine in the hypervisor
Increase physical disk allocation for the virtual machine in the hypervisor
In VMware Workstation or the vSphere client, navigate to the virtual machine you’re working with, edit it’s settings and under the hardware tab, click on the Hard Disk. On the right side, you’ll see a button labelled Utilities. Click that and click on expand. You can now enter a new size for the disk. Once this is done, you’ll get a popup notifying you of the success.
The next step is to now consume the extra space in the virtual machine by increasing the partition size and then the filesystem size.
Power on the VM and log in to the shell. While is it possible to do this via GUI, I prefer to use the shell for its more universal accessibility.
[root@temeria ~]# parted
GNU Parted 2.1
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sda: 25.8GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number  Start   End     Size    Type     File system  Flags 
1      1049kB  316MB   315MB   primary  ext4         boot 
2      316MB   17.2GB  16.9GB  primary  ext4
You can see the disk size has increased now.

Increase the partition size

You can’t resize a mounted filesystem with parted and resize2fs won’t resize the underlying partition. The workaround is a bit tricky and you have to be careful to keep the start cylinder the same when doing this. What you do is, in fdisk you delete the partition and recreate it with a larger size making sure you keep the start location (cylinder) the same. The example below illustrates this:
[root@temeria ~]# fdisk /dev/sda
WARNING: DOS-compatible mode is deprecated. It’s strongly recommended to switch off the mode (command ‘c’) and change display units to sectors (command ‘u’).
Command (m for help): p
Disk /dev/sda: 25.8 GB, 25769803776 bytes
255 heads, 63 sectors/track, 3133 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00073409
Device Boot Start End Blocks Id System
/dev/sda1 * 1 39 307200 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 39 2097 16534528 83 Linux
Command (m for help): d
Partition number (1-4): 2
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (39-3133, default 39):
Using default value 39
Last cylinder, +cylinders or +size{K,M,G} (39-3133, default 3133):
Using default value 3133
Command (m for help): p
Disk /dev/sda: 25.8 GB, 25769803776 bytes
255 heads, 63 sectors/track, 3133 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00073409
Device Boot Start End Blocks Id System
/dev/sda1 * 1 39 307200 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 39 3133 24857598+ 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.
Reboot the system to ensure the partition table is reread.
Resize the filesystem
This is perhaps the simplest step.
Simply execute the resize2fs command with your partition as an argument.
[root@temeria ~]# resize2fs /dev/sda2
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/sda2 is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 2
Performing an on-line resize of /dev/sda2 to 6214399 (4k) blocks.
The filesystem on /dev/sda2 is now 6214399 blocks long.
Check to see confirm the resize operation. You can use df -k or df -h (human friendly output)
[root@temeria ~]# df -k
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda2             24469104   2196340  21035568  10% /
tmpfs                  1025580        88   1025492   1% /dev/shm
/dev/sda1               297485     32659    249466  12% /boot

[root@temeria ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2              24G  2.1G   21G  10% /
tmpfs                1002M   88K 1002M   1% /dev/shm
/dev/sda1             291M   32M  244M  12% /boot
And that is it, you’re done!


No comments:

Post a Comment

  How to Change Instance Type & Security Group of EC2 in AWS By David Taylor Updated April 29, 2023 EC2 stands for Elastic Compute Cloud...