There was a question in my post on “Linux partitioning with fdisk on CentOS 6“. Our reader asked if it is possible to extend an existing linux non LVM partition without loosing it’s data … here is a post on how to resize partition and filesystem with fdisk and resize2fs.
If you want to increase your root partition size you must follow my guide on “Increase Root Partition Size – LVM – CentOS“!
IMPORTANT: In order to resize partition (extend), enough disk space must be available! We can not extend a partition if there are no free sectors/cylinders at the end of the partition to extend!
PLEASE BACKUP YOUR DATA BEFORE RESIZING ANY PARTITION! GeekPeek.Net is not responsible for any data loss!
Let’s Resize Partition and Filesystem with fdisk and resize2fs!
Our system has two disks:
- /dev/sda – 16GB system disk with LVM partitions (root and swap)
- /dev/sdb – 1GB clean disk for tutorial purpose
For the start of this tutorial tutorial we have created one partition on /dev/sdb disk. Partition size (/dev/sdb1) is 500MB. We created an ext4 filesystem on this partition and put some dummy data on it. We will be modifying our partitions with fdisk and filesystem with resize2fs.
For more information on how to create and modify partitions with fdisk read “Linux partitioning with fdisk on CentOS 6“.
Extend Partition and Filesystem
Before we start, let’s check the current disk configuration. Geekpeek mount point is the partition we want to extend. We can see that the partition has 494MB of available space and 401MB is already used by dummy data. We want to extend the partition to 1GB:
[root@foo1 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_foo-LogVol01 13G 4.6G 7.7G 38% / tmpfs 376M 0 376M 0% /dev/shm /dev/sda1 485M 105M 355M 23% /boot /dev/sdb1 494M 402M 67M 86% /geekpeek
1. Unmount the partition
[root@foo1 ~]# umount /geekpeek/ [root@foo1 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_foo-LogVol01 13G 4.6G 7.7G 38% / tmpfs 376M 0 376M 0% /dev/shm /dev/sda1 485M 105M 355M 23% /boot
2. Delete the partition
[root@foo1 ~]# fdisk /dev/sdb 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): d Selected partition 1 Command (m for help): p Disk /dev/sdb: 1073 MB, 1073741824 bytes 255 heads, 63 sectors/track, 130 cylinders, total 2097152 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: 0x2dbb9f13 Device Boot Start End Blocks Id System Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
3. Create a new (larger) partition
[root@foo1 ~]# fdisk /dev/sdb 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): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First sector (2048-2097151, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-2097151, default 2097151): Using default value 2097151 Command (m for help): p Disk /dev/sdb: 1073 MB, 1073741824 bytes 255 heads, 63 sectors/track, 130 cylinders, total 2097152 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: 0x2dbb9f13 Device Boot Start End Blocks Id System /dev/sdb1 2048 2097151 1047552 83 Linux Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table.
We are able to mount the partition at this point, but the filesystem on this partition is only 500MB large. We need to resize the filesystem using resize2fs command in the following steps.
4. Run fsck on your filesystem
[root@foo1 ~]# e2fsck -f /dev/sdb1 e2fsck 1.41.12 (17-May-2010) 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/sdb1: 16/130560 files (0.0% non-contiguous), 426988/522080 blocks
5. Resize your filesystem with resize2fs
[root@foo1 ~]# resize2fs /dev/sdb1 resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/sdb1 to 1044192 (1k) blocks. The filesystem on /dev/sdb1 is now 1044192 blocks long.
6. Re-mount extended partition
[root@foo1 ~]# mount /dev/sdb1 /geekpeek/ [root@foo1 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_foo-LogVol01 13G 4.6G 7.7G 38% / tmpfs 376M 0 376M 0% /dev/shm /dev/sda1 485M 105M 355M 23% /boot /dev/sdb1 988M 402M 536M 43% /geekpeek
Voila! The partition was successfully extended, as we can see all of the data survived! 🙂 Always remember to resize the filesystem with resize2fs.
Reduce a Partition and Filesystem
Reviewing the current disk configuration: Geekpeek mount point is the partition we want to reduce. We can see that the partition has 988MB of available space and 324MB is used by dummy data. We want to reduce the partition to 400MB without loosing data:
[root@foo1 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_foo-LogVol01 13G 4.6G 7.7G 38% / tmpfs 376M 0 376M 0% /dev/shm /dev/sda1 485M 105M 355M 23% /boot /dev/sdb1 988M 324M 614M 35% /geekpeek
1. Unmount the partition
[root@foo1 ~]# umount /geekpeek/ [root@foo1 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_foo-LogVol01 13G 4.6G 7.7G 38% / tmpfs 376M 0 376M 0% /dev/shm /dev/sda1 485M 105M 355M 23% /boot
2. Run fsck on your filesystem
[root@foo1 ~]# e2fsck -f /dev/sdb1 e2fsck 1.41.12 (17-May-2010) 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/sdb1: 15/261120 files (0.0% non-contiguous), 363953/1044192 blocks
3. Resize the filesystem with resize2fs
[root@foo1 ~]# resize2fs /dev/sdb1 400M resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/sdb1 to 409600 (1k) blocks. The filesystem on /dev/sdb1 is now 409600 blocks long.
4. Delete the partition
[root@foo1 ~]# fdisk /dev/sdb 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): d Selected partition 1 Command (m for help): p Disk /dev/sdb: 1073 MB, 1073741824 bytes 255 heads, 63 sectors/track, 130 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: 0x2dbb9f13 Device Boot Start End Blocks Id System Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
4. Create a new (smaller) partition
[root@foo1 ~]# fdisk /dev/sdb 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): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-130, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-130, default 130): +400M Command (m for help): p Disk /dev/sdb: 1073 MB, 1073741824 bytes 255 heads, 63 sectors/track, 130 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: 0x2dbb9f13 Device Boot Start End Blocks Id System /dev/sdb1 1 52 417658+ 83 Linux Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
5. Mount the new partition and filesystem
[root@foo1 ~]# mount /dev/sdb1 /geekpeek/ [root@foo1 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_foo-LogVol01 13G 4.6G 7.7G 38% / tmpfs 376M 0 376M 0% /dev/shm /dev/sda1 485M 105M 355M 23% /boot /dev/sdb1 388M 323M 45M 88% /geekpeek
Voila! The partition was successfully reduced to 400MB, as we can see all of the data survived! 🙂 Always remember to resize the filesystem with resize2fs.