本文主要记录一下在Linux命令行环境中如何对新磁盘进行分区并格式化,以及对已有分区如何进行扩容。

使用的分区工具是fdisk,格式化工具为mkfs,验证的环境为Ubuntu24.04虚拟机。

使用parted工具扩容已有分区,e2fsck检查文件系统是否被破坏,resize2fs扩容文件系统以适应分区大小。

创建新分区

第一步:VMware给虚拟机中新增一块硬盘

VMware给Ubuntu24.04增加一块新硬盘

添加完硬盘之后重启虚拟机。

第二步:sudo权限下查看硬盘情况

root@ubuntu24:/# fdisk -l
Disk /dev/sdb: 20 GiB, 21474836480 bytes, 41943040 sectors # 这里可以看到新增一个设备/dev/sdb,并且该设备下没有分区
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


Disk /dev/sda: 50 GiB, 53687091200 bytes, 104857600 sectors # 原有的设备,并且设备下有两个分区/dev/sda1 /dev/sda2
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: gpt
Disk identifier: 589F299F-630C-414B-BF99-1E3BE5773E78

Device     Start       End   Sectors Size Type
/dev/sda1   2048      4095      2048   1M BIOS boot
/dev/sda2   4096 104855551 104851456  50G Linux filesystem

第三步:对/dev/sdb设备进行分区

root@ubuntu24:/# 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.

Device does not contain a recognized partition table.
Created a new DOS (MBR) disklabel with disk identifier 0xca261806.

Command (m for help): p # 打印当前分区表
Disk /dev/sdb: 20 GiB, 21474836480 bytes, 41943040 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: 0xca261806

Command (m for help): m # 打印操作菜单

Help:

  DOS (MBR)
   a   toggle a bootable flag
   b   edit nested BSD disklabel
   c   toggle the dos compatibility flag

  Generic
   d   delete a partition
   F   list free unpartitioned space
   l   list known partition types
   n   add a new partition
   p   print the partition table
   t   change a partition type
   v   verify the partition table
   i   print information about a partition

  Misc
   m   print this menu
   u   change display/entry units
   x   extra functionality (experts only)

  Script
   I   load disk layout from sfdisk script file
   O   dump disk layout to sfdisk script file

  Save & Exit
   w   write table to disk and exit # 将分区表数据写入磁盘并退出
   q   quit without saving changes

  Create a new label
   g   create a new empty GPT partition table
   G   create a new empty SGI (IRIX) partition table
   o   create a new empty MBR (DOS) partition table
   s   create a new empty Sun partition table


Command (m for help): n # 创建分区表
Partition type
   p   primary (0 primary, 0 extended, 4 free) # 主分区
   e   extended (container for logical partitions) # 扩容分区
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-41943039, default 2048) : # 起始扇区为2048,主要是为了给UEFI引导系统代码留出空间
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-41943039, default 41943039): +10G # 分区大小为10G

Created a new partition 1 of type 'Linux' and of size 10 GiB. # 创建新分区成功

Command (m for help): w # 写入分区表数据到磁盘
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

root@ubuntu24:/# fdisk -l # 查看分区结果
Disk /dev/sdb: 20 GiB, 21474836480 bytes, 41943040 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: 0xca261806

Device     Boot Start      End  Sectors Size Id Type
/dev/sdb1        2048 20973567 20971520  10G 83 Linux # 分区成功


Disk /dev/sda: 50 GiB, 53687091200 bytes, 104857600 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: gpt
Disk identifier: 589F299F-630C-414B-BF99-1E3BE5773E78

Device     Start       End   Sectors Size Type
/dev/sda1   2048      4095      2048   1M BIOS boot
/dev/sda2   4096 104855551 104851456  50G Linux filesystem

第四步:格式化分区,即写入文件系统数据

root@ubuntu24:/# mkfs.ext4 /dev/sdb1
mke2fs 1.47.0 (5-Feb-2023)
Creating filesystem with 2621440 4k blocks and 655360 inodes
Filesystem UUID: 18f6dee7-996b-4b12-a9b9-580b4d44c301
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

第五步:挂载文件系统

root@ubuntu24:/# mkdir test
root@ubuntu24:/# mount -t ext4 /dev/sdb1 test/
root@ubuntu24:/# cd test/
root@ubuntu24:/test# ls
lost+found
root@ubuntu24:/test# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdb1       9.8G   24K  9.3G   1% /test

挂在命令使用方法:mount -t type device mountpoint -o options

扩容分区

扩容分区使用的是parted命令,fdisk命令需要删除重建新分区才可以。

使用parted命令扩容分区没有验证数据是否会丢失,因此需要提前备份数据

第一步:查看磁盘剩余空间

使用lsblk命令可以查看磁盘设备剩余空间:

root@ubuntu24:/# lsblk
NAME   MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda      8:0    0  50G  0 disk
├─sda1   8:1    0   1M  0 part
└─sda2   8:2    0  50G  0 part /
sdb      8:16   0  20G  0 disk # /dev/sdb磁盘还有10G的剩余空间
└─sdb1   8:17   0  10G  0 part

第二步:扩容分区/dev/sdb1

再次强调一点,使用parted扩容分区不保证数据的完整性。

root@ubuntu24:/# parted /dev/sdb
GNU Parted 3.6
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print # 打印当前分区情况
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  10.7GB  10.7GB  primary  ext4

(parted) help # 查看命令
  align-check TYPE N                       check partition N for TYPE(min|opt) alignment
  help [COMMAND]                           print general help, or help on COMMAND
  mklabel,mktable LABEL-TYPE               create a new disklabel (partition table)
  mkpart PART-TYPE [FS-TYPE] START END     make a partition
  name NUMBER NAME                         name partition NUMBER as NAME
  print [devices|free|list,all]            display the partition table, or available devices, or free space, or all found partitions
  quit                                     exit program
  rescue START END                         rescue a lost partition near START and END
  resizepart NUMBER END                    resize partition NUMBER # 可以根据分区编号修改分区大小
  rm NUMBER                                delete partition NUMBER
  select DEVICE                            choose the device to edit
  disk_set FLAG STATE                      change the FLAG on selected device
  disk_toggle [FLAG]                       toggle the state of FLAG on selected device
  set NUMBER FLAG STATE                    change the FLAG on partition NUMBER
  toggle [NUMBER [FLAG]]                   toggle the state of FLAG on partition NUMBER
  type NUMBER TYPE-ID or TYPE-UUID         type set TYPE-ID or TYPE-UUID of partition NUMBER
  unit UNIT                                set the default unit to UNIT
  version                                  display the version number and copyright information of GNU Parted
(parted) resizepart 1 # 修改编号为1的分区
End?  [10.7GB]? 15 # 从10.7G扩容到15MB,这是因为没有带单位
Warning: Shrinking a partition can cause data loss, are you sure you want to continue?
Yes/No? Yes
(parted)
(parted) print # 再次打印查看扩容结果
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  15.0MB  14.0MB  primary  ext4 # 已经扩容到15MB

(parted) resizepart 1 # 再次扩容分区
End?  [15.0MB]? 15GiB # 扩容到15G,这次携带单位
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  16.1GB  16.1GB  primary  ext4 # 这里显示的数据不准确

(parted) resizepart 1 20GiB
Error: The location 20GiB is outside of the device /dev/sdb.
(parted) resizepart 1 100% # 这次一步到位将剩余空间占满
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  21.5GB  21.5GB  primary  ext4

(parted) quit
Information: You may need to update /etc/fstab.

第三步:查看扩容分区结果


root@ubuntu24:/# lsblk
NAME   MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda      8:0    0  50G  0 disk
├─sda1   8:1    0   1M  0 part
└─sda2   8:2    0  50G  0 part /
sdb      8:16   0  20G  0 disk
└─sdb1   8:17   0  20G  0 part

第四步:分区变大,修复文件系统

使用e2fsck检查修改分区大小是否破坏了文件系统。

使用resize2fs修改文件系统大小以使用分区大小。

root@ubuntu24:/# e2fsck -f /dev/sdb1
e2fsck 1.47.0 (5-Feb-2023)
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: 11/655360 files (0.0% non-contiguous), 66753/2621440 blocks
root@ubuntu24:/# resize2fs /dev/sdb1
resize2fs 1.47.0 (5-Feb-2023)
Resizing the filesystem on /dev/sdb1 to 5242624 (4k) blocks.
The filesystem on /dev/sdb1 is now 5242624 (4k) blocks long.

第五步:重新挂载文件系统

root@ubuntu24:/# mount -t ext4 /dev/sdb1 test/
root@ubuntu24:/# df -h
Filesystem      Size  Used Avail Use% Mounted on
tmpfs           790M  1.3M  789M   1% /run
/dev/sda2        49G   20G   28G  42% /
tmpfs           3.9G     0  3.9G   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           3.9G     0  3.9G   0% /run/qemu
tmpfs           790M   12K  790M   1% /run/user/1000
/dev/sdb1        20G   24K   19G   1% /test
root@ubuntu24:/# cd /test/
root@ubuntu24:/test# ls
lost+found