ext4硬盘扩容(仅供说明,不用于生产)
Ubuntu24上的ext4
不适合多分区扩容,仅适合测试说明
# 清除磁盘签名
rambo@ub5:~$ sudo wipefs -a /dev/sdb
注:-a选项会清除所有类型的文件系统签名,包括GPT/MBR分区表、文件系统签名(如ext4、NTFS等)以及其他可能存在的元数据
rambo@ub5:~$ sudo fdisk -l /dev/sdb
[sudo] password for rambo:
Disk /dev/sdb: 10 GiB, 10737418240 bytes, 20971520 sectors
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
rambo@ub5:~$ sudo fdisk /dev/sdb
Welcome to fdisk (util-linux 2.39.3).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
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-20971519, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-20971519, default 20971519): +2G
Created a new partition 1 of type 'Linux' and of size 2 GiB.
Partition #1 contains a ext4 signature.
Do you want to remove the signature? [Y]es/[N]o: Y # 移除旧签名
The signature will be removed by a write command.
rambo@ub5:~$ sudo mkfs.ext4 /dev/sdb1
rambo@ub5:~$ sudo mkdir /mnt/usb1
rambo@ub5:~$ sudo mount /dev/sdb1 /mnt/usb1/
rambo@ub5:~$ df -Th /mnt/usb1/
Filesystem Type Size Used Avail Use% Mounted on
/dev/sdb1 ext4 2.0G 24K 1.8G 1% /mnt/usb1
# 制作临时数据
rambo@ub5:~$ sudo cp -r /usr/lib/ /mnt/usb1/
rambo@ub5:~$ df -Th /mnt/usb1/
Filesystem Type Size Used Avail Use% Mounted on
/dev/sdb1 ext4 2.0G 1.4G 451M 76% /mnt/usb1
# 扩容
rambo@ub5:~$ sudo umount /mnt/usb1/ # 先卸载
rambo@ub5:~$ sudo fdisk /dev/sdb
Welcome to fdisk (util-linux 2.39.3).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
This disk is currently in use - repartitioning is probably a bad idea.
It's recommended to umount all file systems, and swapoff all swap
partitions on this disk.
Command (m for help): p # 查看现在的分区
Disk /dev/sdb: 10 GiB, 10737418240 bytes, 20971520 sectors
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x364187b2
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 4196351 4194304 2G 83 Linux
Command (m for help): d # 删除现在的分区
Selected partition 1 # 因为只有一个分区,所以这里是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-20971519, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-20971519, default 20971519): +4G # 扩展成4G
Created a new partition 1 of type 'Linux' and of size 4 GiB.
Partition #1 contains a ext4 signature.
Do you want to remove the signature? [Y]es/[N]o: Y # 删除旧的签名,会产生新的签名
The signature will be removed by a write command.
Command (m for help): p # 查看当前的新分区
Disk /dev/sdb: 10 GiB, 10737418240 bytes, 20971520 sectors
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x364187b2
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 8390655 8388608 4G 83 Linux
Command (m for help): w # 保存当前的分区
The partition table has been altered.
Syncing disks.
rambo@ub5:~$ sudo partprobe # 刷新分区表
# 检查文件系统是否有错误
rambo@ub5:~$ sudo e2fsck -f -y /dev/sdb1
# 扩展文件系统的大小
rambo@ub5:~$ sudo resize2fs /dev/sdb1
# 重新挂载
rambo@ub5:~$ sudo mount /dev/sdb1 /mnt/usb1/
rambo@ub5:~$ df -Th /mnt/usb1/
Filesystem Type Size Used Avail Use% Mounted on
/dev/sdb1 ext4 3.9G 1.4G 2.4G 37% /mnt/usb1