1. 为什么我的 Linux 磁盘空间不够用?/ 插入新的磁盘要怎么用?
[注]:第一节基本是一些啰里啰唆的内容,想直接看如何操作,请直接跳转至第二小节🧐
很多人遇到这样的问题,当给一台新的主机安装上 Linux
系统之后,没过多久发现自己的磁盘不够用了,但自己主机的磁盘原本很大应该没那么快就用满才对🤔。
这是由于大多数的同学在安装 Linux
系统的时候都选择的是由系统自动分配磁盘空间,很多时候引导程序仅仅使用某一个物理磁盘,或者使用固定大小(远小于实际磁盘空间)的磁盘空间来布局 Linux
系统仅保证系统的基本运行配置。这样子一来,等到我们正式使用系统的时候会发现,系统的空间比我们实际的空间小的多。
root@linux-im:~# df -hT
Filesystem Type Size Used Avail Use% Mounted on
tmpfs tmpfs 1.6G 1.3M 1.6G 1% /run
/dev/mapper/ubuntu--vg-sys_root ext4 3.0T 267G 2.6T 10% /
tmpfs tmpfs 7.9G 0 7.9G 0% /dev/shm
tmpfs tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/sda2 ext4 2.0G 244M 1.6G 14% /boot
tmpfs tmpfs 1.6G 8.0K 1.6G 1% /run/user/1000
就像下面这样,查看当前系统的磁盘空间基本上就是个 3TB
的样子。再来看看笔者实际上的物理磁盘。
root@linux-im:~# fdisk -l
Disk /dev/loop0: 61.96 MiB, 64970752 bytes, 126896 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 /dev/loop1: 46.96 MiB, 49242112 bytes, 96176 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 /dev/loop2: 79.95 MiB, 83832832 bytes, 163736 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 /dev/loop3: 61.96 MiB, 64966656 bytes, 126888 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 /dev/loop4: 46.95 MiB, 49233920 bytes, 96160 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 /dev/sda: 3 TiB, 3298534883328 bytes, 6442450944 sectors
Disk model: Virtual disk
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: 1207CE68-7484-4012-9AB3-F7782443D93A
Device Start End Sectors Size Type
/dev/sda1 2048 4095 2048 1M BIOS boot
/dev/sda2 4096 4198399 4194304 2G Linux filesystem
/dev/sda3 4198400 6442448895 6438250496 3T Linux filesystem
#========================================================================
Disk /dev/sdb: 10 TiB, 10995116277760 bytes, 21474836480 sectors
Disk model: Virtual disk
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: 0x9e4ba441
#========================================================================
Disk /dev/mapper/ubuntu--vg-sys_root: 3 TiB, 3296383205376 bytes, 6438248448 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
上述系统输出中笔者特意用双横线圈起来的一块物理磁盘,即为系统没有使用的物理磁盘(我这么大的磁盘,你不用??纯纯浪费!),还在想这么大的空间是怎么一下子突然满的,原来是人家系统根本没有用到哦(hh)。
以上描述同样适用于新插入磁盘,当系统新插入磁盘后不会自动为我们分配空间到实际使用的目录中,而是仅作为未分配状态存在。参考 Windows
即就是如下图这样的状态。
2. 扩展磁盘空间
Tips: 如果想要将 /home
目录的空间和 /
根目录的磁盘空间合并,可参考这篇博客《Linux 将 /home 目录与 / 根目录磁盘合并》。
好了,言归正传,下面我们看实操部分。在上文中我们看到有一个未分配的 10TB
大小的磁盘,其设备节点为 /dev/sdb
。接下来我们需要先将其分区(分配为可用空间)。
2.1. 分区
执行 fdisk <设备节点>
,这里需要注意的是若磁盘大于 2TB
,则需要使用 gdisk
命令,二者用法相同。(笔者这里的磁盘已经超过 2TB
,因此使用 gdisk
)。下面是详细的使用过程输出,简单来讲就是:
(1) 执行 gdisk /dev/sdb
,(小于 2TB
大小的磁盘使用 fdisk /dev/sdb
);
(2)敲 p
,将磁盘分区,并且一路回车即可;
(3)敲 w
,保存设置。
root@linux-im:~# gdisk /dev/sdb
GPT fdisk (gdisk) version 1.0.8
Partition table scan:
MBR: MBR only
BSD: not present
APM: not present
GPT: not present
***************************************************************
Found invalid GPT and valid MBR; converting MBR to GPT format
in memory. THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by
typing 'q' if you don't want to convert your MBR partitions
to GPT format!
***************************************************************
Command (? for help): ?
b back up GPT data to a file
c change a partition's name
d delete a partition
i show detailed information on a partition
l list known partition types
n add a new partition
o create a new empty GUID partition table (GPT)
p print the partition table
q quit without saving changes
r recovery and transformation options (experts only)
s sort partitions
t change a partition's type code
v verify disk
w write table to disk and exit
x extra functionality (experts only)
? print this menu
Command (? for help): p
Disk /dev/sdb: 21474836480 sectors, 10.0 TiB
Model: Virtual disk
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): AE9E9686-8757-4A17-B3F5-EFC7CEB9B9E1
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 21474836446
Partitions will be aligned on 2048-sector boundaries
Total free space is 21474836413 sectors (10.0 TiB)
Number Start (sector) End (sector) Size Code Name
Command (? for help): n
Partition number (1-128, default 1):
First sector (34-21474836446, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-21474836446, default = 21474836446) or {+-}size{KMGTP}:
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'
Command (? for help): p
Disk /dev/sdb: 21474836480 sectors, 10.0 TiB
Model: Virtual disk
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): AE9E9686-8757-4A17-B3F5-EFC7CEB9B9E1
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 21474836446
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)
Number Start (sector) End (sector) Size Code Name
1 2048 21474836446 10.0 TiB 8300 Linux filesystem
Command (? for help): w
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!
Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sdb.
The operation has completed successfully.
小知识:
fdisk 与 gdisk 的区别
使用 fdisk 命令的磁盘分区格式为 DOS 分区表格式;
使用 gdisk 命令的磁盘分区格式为 GUID 分区表格式。使用 fdisk 分配大于 2TB 大小的磁盘会弹出这样的提示信息:
DOS partition table format cannot be used on drives for volumes larger than 2199023255040 bytes for 512-byte sectors. Use GUID partition table format (GPT).
2.2. 建立 ext4 文件系统
目前 Linux
系统中常用的文件系统格式为 ext4
,这里将上述分配的磁盘分区创建为 ext4
文件系统格式。
(1) 执行 mkfs -t ext4 /dev/sdb1
root@linux-im:~# mkfs -t ext4 /dev/sdb1
mke2fs 1.46.5 (30-Dec-2021)
Creating filesystem with 2684354299 4k blocks and 335544320 inodes
Filesystem UUID: c996ee7c-7f19-41d6-ae71-60534b3b4ea5
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
102400000, 214990848, 512000000, 550731776, 644972544, 1934917632,
2560000000
Allocating group tables: done
Writing inode tables: done
Creating journal (262144 blocks):
done
Writing superblocks and filesystem accounting information: done
2.3. 查看当前物理卷标
执行 pvdisplay
命令查看系统当前磁盘的物理卷标名称是什么。这里主要看 VG Name
的信息,这里笔者的系统物理卷名为 ubuntu-vg
。如果是 centos8
及以上系统,这里的物理卷标名应该是 cl
,具体按照实际情况来确定。
root@linux-im:~# pvdisplay
--- Physical volume ---
PV Name /dev/sda3
VG Name ubuntu-vg
PV Size <3.00 TiB / not usable 0
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 785919
Free PE 0
Allocated PE 785919
PV UUID 8RysFf-u2YW-18O9-jCSI-Fyfk-ngws-tg7KTP
2.4. 创建物理卷
将建立好文件系统格式的分区创建为可用物理卷。
(1) 执行 pvcreate /dev/sdb1
,创建物理卷;
(2) 执行 vgextend ubuntu-vg /dev/sdb1
,添加物理卷到卷组。(若是 centos8
以上系统则应该是 cl
,具体按照实际情况来确定)
root@linux-im:~# pvcreate /dev/sdb1
WARNING: ext4 signature detected on /dev/sdb1 at offset 1080. Wipe it? [y/n]: y
Wiping ext4 signature on /dev/sdb1.
Physical volume "/dev/sdb1" successfully created.
root@linux-im:~# pvdisplay
--- Physical volume ---
PV Name /dev/sda3
VG Name ubuntu-vg
PV Size <3.00 TiB / not usable 0
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 785919
Free PE 0
Allocated PE 785919
PV UUID 8RysFf-u2YW-18O9-jCSI-Fyfk-ngws-tg7KTP
"/dev/sdb1" is a new physical volume of "<10.00 TiB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size <10.00 TiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID EZffYg-uZY4-Rm4h-B1E3-m7by-nnU8-Y3S1VG
添加到物理卷组
# 查看卷组名称
root@linux-im:~# vgdisplay
--- Volume group ---
VG Name ubuntu-vg
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 2
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 1
Act PV 1
VG Size <3.00 TiB
PE Size 4.00 MiB
Total PE 785919
Alloc PE / Size 785919 / <3.00 TiB
Free PE / Size 0 / 0
VG UUID ewSMf0-ckFt-0QKF-4hUe-2EgC-1CSx-NVNEfe
#添加到卷组
root@linux-im:~# vgextend ubuntu-vg /dev/sdb1
Volume group "ubuntu-vg" successfully extended
root@linux-im:~# vgdisplay
--- Volume group ---
VG Name ubuntu-vg
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 2
Act PV 2
VG Size <13.00 TiB
PE Size 4.00 MiB
Total PE 3407358
Alloc PE / Size 785919 / <3.00 TiB
Free PE / Size 2621439 / <10.00 TiB
VG UUID ewSMf0-ckFt-0QKF-4hUe-2EgC-1CSx-NVNEfe
2.5. 将可用空间扩展到 /
根目录
将上述做好的物理卷分配到根目录空间中。首先要知道自己想要扩展的是哪个挂载点。
(1) 执行 du -hT
,查看根目录挂载的绝对路径;
root@linux-im:~# df -hT
Filesystem Type Size Used Avail Use% Mounted on
tmpfs tmpfs 1.6G 1.3M 1.6G 1% /run
/dev/mapper/ubuntu--vg-sys_root ext4 3.0T 267G 2.6T 10% /
tmpfs tmpfs 7.9G 0 7.9G 0% /dev/shm
tmpfs tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/sda2 ext4 2.0G 244M 1.6G 14% /boot
tmpfs tmpfs 1.6G 8.0K 1.6G 1% /run/user/1000
笔者这里想要扩展的挂载点是 /
根目录,其物理路径为 /dev/mapper/ubuntu--vg-sys_root
,不同系统挂载点不同,根据实际情况修改即可。
(2) 执行 lvresize -L +9.9T /dev/mapper/ubuntu--vg-sys_root
为挂载点增加 9.9TB
磁盘空间;
[注]:这里的 9.9T 为扩展的大小,尤为注意这里的 +,必须添加,如果忘记写这个加号,将会导致严重的后果,虽然也是有办法补救,但很麻烦。
root@linux-im:~# lvresize -L +9.9T /dev/mapper/ubuntu--vg-sys_root
Rounding size to boundary between physical extents: 9.90 TiB.
Size of logical volume ubuntu-vg/sys_root changed from <3.00 TiB (785919 extents) to <12.90 TiB (3381145 extents).
Logical volume ubuntu-vg/sys_root successfully resized.
[注]:其实很难一次性将可用空间全部增加完,添加完之后可以根据估计所剩空间来减少增加内存大小来继续扩展,即这里的 9.9T 可改为 10G,继续执行扩展命令。
2.6. 更新文件系统
到此其实已经将磁盘分配好了,不过还有一步很重要的事情要做,那就是更新系统文件系统,不做这一步将不会看到系统空间的实际增加。
(1) 执行 resize2fs /dev/mapper/ubuntu--vg-sys_root
(centos
系统执行 xfs_growfs
命令)
#同步文件系统
# centos::xfs_growfs /dev/mapper/ubuntu--vg-sys_root
root@linux-im:~# resize2fs /dev/mapper/ubuntu--vg-sys_root
resize2fs 1.46.5 (30-Dec-2021)
Filesystem at /dev/mapper/ubuntu--vg-sys_root is mounted on /; on-line resizing required
old_desc_blocks = 384, new_desc_blocks = 1664
The filesystem on /dev/mapper/ubuntu--vg-sys_root is now 3488769024 (4k) blocks long.
执行 df -hT
,这个时候会发现,自己扩展的空间生效了!!
rootlinux-im:~# df -hT
Filesystem Type Size Used Avail Use% Mounted on
tmpfs tmpfs 1.6G 1.3M 1.6G 1% /run
/dev/mapper/ubuntu--vg-sys_root ext4 13T 267G 12T 3% /
tmpfs tmpfs 7.9G 0 7.9G 0% /dev/shm
tmpfs tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/sda2 ext4 2.0G 244M 1.6G 14% /boot
tmpfs tmpfs 1.6G 8.0K 1.6G 1% /run/user/1000