Изменение разделов ext4 в Linux и fdisk и resize2fs

Иногда возникает необходимость увеличить или уменьшить размер раздела на накопителе на Linux без удобных программ с GUI интерфейсом

Увеличение раздела и файловой системы

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

Размонтировать раздел

[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

Удалить раздел

[root@foo1 ~]# fdisk /dev/sda
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/sda: 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.

Создать новый раздел большего объёма

[root@foo1 ~]# fdisk /dev/sda
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/sda: 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/sda2 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.

Выполнить fsck на файловой системе

[root@foo1 ~]# e2fsck -f /dev/sda2
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/sda2: 16/130560 files (0.0% non-contiguous), 426988/522080 blocks

5. Resize your filesystem with resize2fs

[root@foo1 ~]# resize2fs /dev/sda2
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/sda2 to 1044192 (1k) blocks.
The filesystem on /dev/sda2 is now 1044192 blocks long.

6. Re-mount extended partition

[root@foo1 ~]# mount /dev/sda2 /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/sda2 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/sda2 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/sda2
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/sda2: 15/261120 files (0.0% non-contiguous), 363953/1044192 blocks

 3. Resize the filesystem with resize2fs

[root@foo1 ~]# resize2fs /dev/sda2 400M
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/sda2 to 409600 (1k) blocks.
The filesystem on /dev/sda2 is now 409600 blocks long.

4. Delete the partition

[root@foo1 ~]# fdisk /dev/sda
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/sda: 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/sda
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/sda: 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/sda2 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/sda2 /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/sda2 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.

Добавить комментарий