CentOS / RHEL : Resize (reduce) non-root EXT3/4 filesystem on non-LVM device (hard disk partition)

Backup all data on filesystem to be resized

Resizing a filesystem and underlying devices is dangerous and potentially destructive if performed incorrectly. Repartitioning devices is similarly destructive and may result in complete data loss. Before proceeding, backup the contents of the filesystem/device to be resized.

Resize (extend) of non-root filesystem On a hard disk partition (non-LVM)

1. Sample setup
In this example, device /dev/sdb (20Gb) contains a single primary partition (/dev/sdb1) of 20Gb with an EXT3/4 filesystem (/data) that spans the entire partition. No free disk space exists between the end of the partition and end of device. Before proceeding, run the following commands – record output for later use.

# df -k /data
Filesystem          1K-blocks      Used Available Use% Mounted on
/dev/sdb1            20635700  10672240   8915224  55% /data
# cat /proc/partitions | grep sdb
   8     16  20971520 sdb
   8     17  20964793 sdb1
# fdisk -l /dev/sdb

Disk /dev/sdb: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sdb1 1 2610 20964793+ 83 Linux

2. Unmount the filesystem to be resized
Unmount the EXT3/4 filesystem to be resized e.g.:

# umount /data

3. Perform a filesystem check
Verify the filesystem type of the filesystem to be resized i.e. whether EXT3, EXT4, etc.

# blkid /dev/sdb1
/dev/sdb1: LABEL="/data" UUID="1fc0bbcd-ba86-40b6-b562-5da90fb0d7af" TYPE="ext3"

Perform a filesystem check of the filesystem ensuring to use the corresponding filesystem check utility (fsck.ext3, fsck.ext4) for the filesystem type e.g.:

# fsck.ext3 -fy /dev/sdb1
e2fsck 1.39 (29-May-2006)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
data: 21/2621440 files (4.8% non-contiguous), 2750333/5241198 blocks

4. Resize (reduce) the filesystem
Use the resize2fs utility to shrink the EXT3/4 filesystem to use less space within the partition. Review the df output recorded earlier to determine how small to shrink the filesystem. Ensure not to reduce the ETX3/4 filesystem smaller than the actual amount of data contained within.

# df -k /data
Filesystem         1K-blocks      Used Available Use% Mounted on
/dev/sdb1           20635700  10672240   8915224  55% /data
# resize2fs /dev/sdb1 11G
resize2fs 1.39 (29-May-2006)
Resizing the filesystem on /dev/sdb1 to 2883584 (4k) blocks.
The filesystem on /dev/sdb1 is now 2883584 blocks long.
Note: when running resize2fs, if no size is specified, the filesystem will be extended to utilise all available/remaining space in the partition.

4. Perform a filesystem check
Perform a filesystem check of the resized EXT3/4 filesystem ensuring to use the corresponding filesystem check utility (fsck.ext3, fsck.ext4) for the filesystem type in use e.g.:

# fsck.ext3 -fy /dev/sdb1
e2fsck 1.39 (29-May-2006)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
data: 21/1441792 files (4.8% non-contiguous), 2712300/2883584 blocks

5. Resize (reduce) the partition
From the filesystem check output above, the filesystem is now 2883584 (* 4Kb) blocks in size i.e.:

# expr 2883584 \* 4096
11811160064 (bytes)

The size of the partition must remain greater than the size of the current filesystem usage within it. Therefore, the partition is resized to a safe value of no less than 12Gb i.e.:

# expr 1024 \* 12
12288 (Mb)

Use the fdisk command to delete then recreate a smaller partition, ensuring to re-use the original partition start block e.g.:

# fdisk /dev/sdb

The number of cylinders for this disk is set to 2610.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): p

Disk /dev/sdb: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sdb1 1 2610 20964793+ 83 Linux

Command (m for help): d
Selected partition 1

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-2610, default 1): [enter]
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-2610, default 2610): +12288M

Command (m for help): p

Disk /dev/sdb: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sdb1 1 1495 12008556 83 Linux

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

Calling ioctl() to re-read partition table.
Syncing disks.

At this point, the disk space between the end of partition and end of the device is available for reuse.

6. Verify partition resize
Verify the system sees the newly resized partition e.g.:

# cat /proc/partitions | grep sdb
   8    16   20971520 sdb
   8    17   12008556 sdb1

If the system fails to detect the new partition sizing, run the partprobe utility against the resized device e.g.:

# partprobe /dev/sdb

7. Perform a filesystem check
Perform a filesystem check of the resized EXT3/4 filesystem ensuring to use the corresponding filesystem check utility (fsck.ext3, fsck.ext4) for the filesystem type in use e.g.:

# fsck.ext3 -fy /dev/sdb1
e2fsck 1.39 (29-May-2006)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
data: 21/1441792 files (4.8% non-contiguous), 2712300/2883584 blocks

8. Mount the resized filesystem
Mount the newly resized EXT3/4 filesystem e.g.:

# mount /data

9. Verify filesystem resize
Review dmesg, messages log, df command output etc. to verify successful filesystem resize e.g.:

# df -k /data
Filesystem          1K-blocks     Used Available Use% Mounted on
/dev/sdb1            11353328 10668192    223764  98% /data
Related Post