Shrinking a partition
In the following example /dev/sdb is an unmounted ext4 formatted 16GB SD-card with two partitions. All of the available space on the card is partitioned but only less than 3GB of data is on it.The card shall be repartitioned so that there is, except the boot partition, only one partition with 4GB.
Anyone who owns a Raspberry- Pi will be familiar with this partitioning scheme...
The method described here will work with nearly all types of drives. A prerequisite remains: The filesystem used must support shrinking.
Let's have a look at the current partitioning scheme:
root@NC10SA:~# e2fsck -f /dev/sdb2
e2fsck 1.42 (29-Nov-2011)
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
/dev/sdb2: 77205/321920 files (0.2% non-contiguous), 593647/1310720 blocks
Check the filesystem contained on the partition for errors.
root@NC10SA:~# e2fsck -f /dev/sdb2
e2fsck 1.42 (29-Nov-2011)
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
/dev/sdb2: 77205/321920 files (0.2% non-contiguous), 593647/1310720 blocks
Because of the fact that there are less than 3GB of data in this partition let's shrink the filesystem on it to 3 GB.
root@NC10SA:~# resize2fs /dev/sdb2 3G
resize2fs 1.42 (29-Nov-2011)
Resizing the filesystem on /dev/sdb2 to 786432 (4k) blocks.
The filesystem on /dev/sdb2 is now 786432 blocks long.
The filesystem now has a size of 3GB.
The next step is to shrink the partition.
To achieve this the partition is deleted and recreated by using the same start sector. Only the end is set to +4GB. With this all data contained on the drive is kept unmodified.
The next step is to shrink the partition.
To achieve this the partition is deleted and recreated by using the same start sector. Only the end is set to +4GB. With this all data contained on the drive is kept unmodified.
root@NC10SA:~# fdisk /dev/sdb
Command (m for help): p
Disk /dev/sdb: 15.9 GB, 15931539456 bytes
64 heads, 32 sectors/track, 15193 cylinders, total 31116288 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: 0x000981cb
Device Boot Start End Blocks Id System
/dev/sdb1 8192 122879 57344 c W95 FAT32 (LBA)
/dev/sdb2 122880 31116287 15496704 83 Linux
Command (m for help): d
Partition number (1-4): 2
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): p
Partition number (1-4, default 2): 2
First sector (2048-31116287, default 2048): 122880
Last sector, +sectors or +size{K,M,G} (122880-31116287, default 31116287): +4G
Command (m for help): p
Disk /dev/sdb: 15.9 GB, 15931539456 bytes
64 heads, 32 sectors/track, 15193 cylinders, total 31116288 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: 0x000981cb
Device Boot Start End Blocks Id System
/dev/sdb1 8192 122879 57344 c W95 FAT32 (LBA)
/dev/sdb2 122880 8511487 4194304 83 Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
Now the partition is 4GB in size.
The last step needed is to grow the filesystem to the desired size of 4GB.
The last step needed is to grow the filesystem to the desired size of 4GB.
root@NC10SA:~# e2fsck -f /dev/sdb2
e2fsck 1.42 (29-Nov-2011)
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
/dev/sdb2: 77205/193152 files (0.2% non-contiguous), 585219/786432 blocks
root@NC10SA:~# resize2fs /dev/sdb2
resize2fs 1.42 (29-Nov-2011)
Resizing the filesystem on /dev/sdb2 to 1048576 (4k) blocks.
The filesystem on /dev/sdb2 is now 1048576 blocks long.
After mounting the partition you can see that it now is 4GB.
root@NC10SA:~# mount /dev/sdb2 /mnt/
root@NC10SA:~# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 146G 23G 116G 17% /
udev 489M 4.0K 489M 1% /dev
tmpfs 199M 840K 198M 1% /run
none 5.0M 0 5.0M 0% /run/lock
none 496M 152K 496M 1% /run/shm
/dev/sdb2 4.0G 2.2G 1.6G 58% /mnt