Shrinking or expanding an ext3 partition is easy but is not without it’s risks. Before starting, you NEED to take a backup of your data. There’s a strong possibility that it will all disappear and your filesystem will become permenantly broken, as with any disk or filesystem procedure.
Please note:
ns3:~# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda1 9.4G 6.8G 2.2G 77% / tmpfs 443M 0 443M 0% /lib/init/rw udev 10M 92K 10M 1% /dev tmpfs 443M 0 443M 0% /dev/shm /dev/sdb1 20G 9.8G 9.0G 52% /email
In my example, I’m going to resize /dev/sdb1 which is my /email partition. /dev/sdb1 is a partition residing on device /dev/sdb
At this point, we need to look at our filesystem in terms of 4K block size:
ns3:~# df -B 4k Filesystem 4K-blocks Used Available Use% Mounted on /dev/sda1 2458888 1775171 558813 77% / tmpfs 113165 0 113165 0% /lib/init/rw udev 2560 23 2537 1% /dev tmpfs 113165 0 113165 0% /dev/shm /dev/sdb1 5158933 2544177 2352696 52% /email
Now you’ll need to know the size of /dev/sdb1:
ns3:~# fdisk -s /dev/sdb1
20964824
Now before you go ahead, you’ll need to reboot and boot up from a Linux CD that contains support for your hardware and root filesystem, as well as tools such as fdisk, fsck, tune2fs and resize2fs.
Firstly, make sure your filesystem hasn’t been automatically mounted: umount /dev/sdb1
You also need to make sure that your filesystem is clean: fsck -n /dev/sdb1
Now assuming everything so far has been successful, it’s time to begin. Even though resize2fs supports ext3 filesystems, I recommend converting your filesystem to ext2 first: tune2fs -O ^has_journal /dev/sdb1
Now, e2fsck -f /dev/sdb1
At this point, you’ll have a clean ext2 filesystem. I’ll now resize my filesystem to 12GB: resize2fs /dev/sdb1 12G
The output is:
resize2fs 1.38 (30-Jun-2005) Resizing the filesystem on /dev/sda1 to 3145728 (4k) blocks. The filesystem on /dev/sda1 is now 3145728 blocks long.
This number has been calculated as follows: 12GB * 1024 = 12,288MB. 12288MB * 1024 = 12,582,912KB.
12582914KB = 3145728 4K blocks.
Now we’ll use fdisk to delete and recreate the partition.
fdisk /dev/sdb
First enter d then enter 1. This will DELETE your sdb1 partition. This is OK, as we’re not actually deleting any data itself, just the partition metadata. Now we’ll need to create a new partition. To do so, enter n then enter p to specify that we’re creating a primary partition, then 1 to specify partition number 1.
When asked for the first cylinder, enter ‘1′. When asked Last cylinder or +size or +sizeM or +sizeK (1-1247, default 1247): We want the value that we calculated above in KB. I would also add 3% to be safe, although I’m confident that this isn’t strictly necessary: 12,582,912KB * 1.03 = 12,960,399.36. So we’ll enter +12960400K
If we want this to be a bootable partition, we would then enter a then 1. Finally enter p to print the proposed table and confirm that everything is as we want it, then finally w to write.
I’d recommend a reboot, followed by a fsck -n /dev/sdb1
Now we’ll add the journal again, tune2fs -j /dev/sdb1 and finally remount our partition mount /dev/sdb1 /mnt/disk confirming that our size change has been successful with df
Tags: df, ext3, fdisk, fsck, resize, resize2fs, tune2fs