Previous Next

Linux - Resizing Partitions - January 2002

In January, we prepared the computer club computer to add a second Linux distribution - Debian. That computer contains two 20 Gb hard drives, one loaded with Microsoft Windows software and one loaded with Linux software. When we installed SuSE, we used the entire 20 Gb Linux hard drive for SuSE. But to add Debian we needed another partition - we had unused disk space, but we needed to split a partition.

At the meeting, I did not know how to split a partition with Linux and still keep the installed software intact. Luckily, our club sysop knew how to install and use Partition Magic under Windows. Partition Magic worked, it recognized the ext2 partitions on the Linux hard drive., it defragmented the Linux files and split the partition. We checked this by rebooting to SuSE Linux; SuSE still worked. But I wondered if there was a way to split a partition using Linux software. There is a way, but it has advantages and disadvantages with respect to Windows. Here s how:

There are four important Linux commands: df, e2fsck, resize2fs and fdisk. e2fsck is equivalent to Windows scandisk; it repairs broken links and detects orphaned files. resize2fs moves files closer together. fdisk overwrites a partition table. One shortcoming of Linux with respect to Windows software is that you must work on an unmounted filesystem. I have written more about that, later in this article.

Here is an example of resizing a Linux partition. First mount the filesystem and check to see how full it is with the df (disk free) command:

# df
Filesystem 1k-blocks Used Available Use% Mounted
/dev/hda3 3976792 2552504 1222272    68% /
/dev/hdb3 2960492 1879680  930420    67% /suse

In this example I will split the /dev/hdb3 partition. The command shows that it is 67% full. Unmount the filesystem.

When partitioned, the drive should be split along a cylinder boundary. Run the fdisk program and list the partition table:

# fdisk /dev/hdb
Command (m for help): p
Disk /dev/hdb: 128 heads 63 sectors 
  784 cylinders
Units = cylinders of 8064 * 512 bytes
Device Boot Start End  Blocks Id System
/dev/hdb1    1      5  20128+ 83 Linux
/dev/hdb2    6     38  133056 82 Linux swap
/dev/hdb3   39    784 3007872 83 Linux
Command (m for help): q

This command shows that the /dev/hdb3 partition used 746 cylinders (from 39-784). The filesystem will be resized to 522 cylinders (~70%). Now the math gets a little tricky. Both the df and the fdisk programs listed a number of blocks in terms of 1Kb blocks. But the resize2fs and e2fsck programs work in 4Kb blocks.

Run the e2fsck program to clean up any broken links and orphaned files:

# e2fsck -f /dev/hdb3
e2fsck 1.25 (20-Sep-2001)
Pass 1: Checking inodes, blocks, and sizes
Special (device/socket/fifo) inode 262923 
  has non-zero size. Fix? yes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/hdb3: *** FILE SYSTEM WAS MODIFIED ***
/dev/hdb3: 82381/376096 files (0.0% non-
  contiguous), 481765/751968 blocks

Notice that the number of blocks is listed as 751968 instead of 3007872; the software has switched from 1Kb blocks to 4Kb blocks. One advantage of the Linux software e2fsck over Windows scandisk is that it is much faster.

The resize2fs command requires a target size in 4Kb blocks. Run the resize2fs program with a target size of 526176:

# resize2fs -p /dev/hdb3 526176
resize2fs 1.25 (20-Sep-2001)
Begin pass 2 (max = 150244)
Relocating blocks         XXXXXXXXXXXXXXXX
Begin pass 3 (max = 23)
Scanning inode table      XXXXXXXXXXXXXXXX
Begin pass 4 (max = 4570)
Updating inode references XXXXXXXXXXXXXXXX
The filesystem on /dev/hdb3 is now 526176 blocks long.

resize2fs has moved the files closer together and made the filesystem smaller. Now the filesystem is smaller than the partition. Again, the Linux resize2fs is faster than Windows defrag.

Run fdisk, delete the partition, create a new partition that matches the new filesystem size and write the new partition table:
# fdisk /dev/hdb
Command (m for help): p
Disk /dev/hdb: 128 heads, 63 sectors, 784 cylinders
Units = cylinders of 8064 * 512 bytes
Device Boot Start End Blocks Id System
/dev/hdb1 1 5 20128+ 83 Linux
/dev/hdb2 6 38 133056 82 Linux swap
/dev/hdb3 39 784 3007872 83 Linux
Command (m for help): d
Partition number (1-4): 3
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (39-784, default 39):
Using default value 39
Last cylinder or +size or +sizeM or +sizeK (39-784, default 784): 560
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.

Run fdisk to create a new partition from the freed up space.
Device Boot Start End Blocks Id System
/dev/hdb1 1 5 20128+ 83 Linux
/dev/hdb2 6 38 133056 82 Linux swap
/dev/hdb3 39 560 2104704 83 Linux
/dev/hdb4 561 784 903168 83 Linux

The filesystem is now smaller and still works. In comparison with Windows software, Linux is faster, but more difficult to use. You can also enlarge a partition into unused disk space by running fdisk and then resize2fs.

Online Linux Book - There is an amazing online Linux book at rute.sourceforge.net This book can be purchased from retailers, but you can browse the entire contents online! Read it online to brush up on Linux. I also recommend Running Linux by O Reilly.

Previous Next