Shrinking ext4 partition on command line

Trước khi thao tác tới filesystem có thể sẽ ảnh hưởng tới dữ liệu nên  bạn nên backup lại dữ liệu an toàn trước khi thực hiện thay đổi

Do khi cài đặt tôi chỉ chia 1 ổ cứng, sau một thời gian tôi muốn thêm một phân vùng khác nhưng do  tôi đã chia toàn bộ ổ sdb vào sdb1 vì vậy tôi cần giảm dung lượng của sdb1 để tạo thêm phân vùng. các bước thực hiện như sau

Bước 1: Gỡ mount của ổ cần giảm dung lượng

Bước 2: kiểm tra thông tin hiện tại

Bước 3: Shink file system

e2fsck -f /dev/sdb1

Giảm xuống còn 5GB

resize2fs /dev/sdb1 5G

Kết quả được như sau

root@ubuntu:~# e2fsck -f /dev/sdb1
e2fsck 1.46.5 (30-Dec-2021)
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: 111/655360 files (0.0% non-contiguous), 116006/2621440 blocks
root@ubuntu:~# resize2fs /dev/sdb1 5G
resize2fs 1.46.5 (30-Dec-2021)
Resizing the filesystem on /dev/sdb1 to 1310720 (4k) blocks.
The filesystem on /dev/sdb1 is now 1310720 (4k) blocks long.

Bước 4: Thay đổi dung lượng Partition

Chúng để để ý dòng cuối cùng của bước 3 để tính sector cách tính

1310720*4k = 5242880K 

Sử dụng fdisk để xoá phân vùng hiện tại và tạo lại phân vùng với sector  với kích thước ở trên,

fdisk /dev/sdb –> d –> n –> p –> Last sector (nhập vào giá trị trên: +5242880K lưu ý có thêm dấu + và chữ K viết hoa)

sau khi xong ấn lưu lại wr

root@ubuntu:~# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.37.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): d
Selected partition 1
Partition 1 has been deleted.

Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p):

Using default response p.
Partition number (1-4, default 1):
First sector (2048-104857599, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-104857599, default 104857599): +5242880K

Created a new partition 1 of type 'Linux' and of size 5 GiB.
Partition #1 contains a ext4 signature.

Do you want to remove the signature? [Y]es/[N]o: N

Command (m for help): wr

Kết quả sau khi thực hiện xong

Ngoài thực hiện bằng dòng lệnh  bạn có thể sử dụng Gparted có giao diện rất  đơn giản để sử dụng: xem hướng dẫn tại https://quantrihethong.net/huong-dan-su-dung-gparted-quan-ly-phan-vung-linux-chua-bao-gio-de-dang-den-the/