一、申请新硬盘
1. 申请添加硬盘
2. 成功后需要重启机器,才能看到硬盘
二、使用命令fdisk -l 确定新硬盘
sudo fdisk -l
回显:
磁盘 /dev/sdc:214.7 GB, 214748364800 字节,419430400 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节置文件系统格
磁盘 /dev/sda:53.7 GB, 53687091200 字节,104857600 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000a860e
设备 Boot Start End Blocks Id System
/dev/sda1 * 2048 616447 307200 83 Linux
/dev/sda2 616448 104857599 52120576 8e Linux LVM
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
磁盘 /dev/sdb:161.1 GB, 161061273600 字节,314572800 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:gpt
Disk identifier: 7CB95C4D-482B-4572-94E0-76B6321D8E51
# Start End Size Type Name
1 34 314572766 150G Microsoft basic primary
磁盘 /dev/mapper/Vol-root:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘 /dev/mapper/Vol-swap:2147 MB, 2147483648 字节,4194304 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘 /dev/mapper/Vol-home:12.6 GB, 12561940480 字节,24535040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘 /dev/mapper/Vol-tmp:10.7 GB, 10737418240 字节,20971520 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘 /dev/mapper/Vol-var:166.4 GB, 166429982720 字节,325058560 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
对比发现第一块硬盘/dev/sdc
是新加的
磁盘 /dev/sdc:214.7 GB, 214748364800 字节,419430400 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
三、使用fdisk对/dev/sdc分区
fdisk /dev/sdc
回显:
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
Device does not contain a recognized partition table
使用磁盘标识符 0xcb30a938 创建新的 DOS 磁盘标签。
命令(输入 m 获取帮助):
输入m查看常用指令及意义:
命令操作
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)
输入n,在当前硬盘中新建一个分区:
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p):
p是主分区,e是扩展分区,输入p,创建一个主分区:
分区号 (1-4,默认 1):
硬盘全新,直接输入回车,创建分区一:
起始 扇区 (2048-419430399,默认为 2048):
回车,使用默认起始扇区
Last 扇区, +扇区 or +size{K,M,G} (2048-419430399,默认为 419430399):
回车,使用默认终止扇区
将使用默认值 419430399
分区 1 已设置为 Linux 类型,大小设为 200 GiB
此时的更改还在内存中,输入p确认分区数据是否正确:
磁盘 /dev/sdc:214.7 GB, 214748364800 字节,419430400 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0xcb30a938
设备 Boot Start End Blocks Id System
/dev/sdc1 2048 419430399 209714176 83 Linux
确认没有问题后输入w,改动落盘:
The partition table has been altered!
Calling ioctl() to re-read partition table.
正在同步磁盘。
此时在硬盘/dev/sdc上创建了分区sdc1,
四、将硬盘修改同步到系统内核
partprobe /dev/sdc
五、查看物理卷、逻辑卷
pvdisplay查看物理卷:
--- Physical volume ---
PV Name /dev/sda2
VG Name Vol
PV Size <49.71 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 12724
Free PE 0
Allocated PE 12724
PV UUID jGxa54-5Z5P-6TEz-YF2V-3dHP-fNCG-31v9rD
--- Physical volume ---
PV Name /dev/sdb1
VG Name Vol
PV Size <150.00 GiB / not usable <3.97 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 38399
Free PE 256
Allocated PE 38143
PV UUID qZ4NuA-vZof-zAAN-N9rN-VK6K-z0yc-0gNZGs
lvdiskplay查看逻辑卷
--- Logical volume ---
LV Path /dev/Vol/home
LV Name home
VG Name Vol
LV UUID N18K2u-LOSG-3HRy-mUAA-eOQA-7mgi-F1Ko9F
LV Write Access read/write
LV Creation host, time localhost, 2022-08-09 01:07:38 +0800
LV Status available
# open 1
LV Size <11.70 GiB
Current LE 2995
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2
--- Logical volume ---
LV Path /dev/Vol/tmp
LV Name tmp
VG Name Vol
LV UUID lIaK7n-GCcr-49G2-qTIY-hwLH-ZpRv-pqKl2I
LV Write Access read/write
LV Creation host, time localhost, 2022-08-09 01:07:38 +0800
LV Status available
# open 1
LV Size 10.00 GiB
Current LE 2560
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:3
--- Logical volume ---
LV Path /dev/Vol/var
LV Name var
VG Name Vol
LV UUID Ap6gbh-luh5-5pcj-WehD-Bx8w-9HD7-Xj04MR
LV Write Access read/write
LV Creation host, time localhost, 2022-08-09 01:07:38 +0800
LV Status available
# open 1
LV Size 155.00 GiB
Current LE 39680
Segments 3
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:4
--- Logical volume ---
LV Path /dev/Vol/root
LV Name root
VG Name Vol
LV UUID 4IfOQk-31cm-Fcx1-eanG-jeBf-lw0R-2yVTXM
LV Write Access read/write
LV Creation host, time localhost, 2022-08-09 01:07:38 +0800
LV Status available
# open 1
LV Size 20.00 GiB
Current LE 5120
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
--- Logical volume ---
LV Path /dev/Vol/swap
LV Name swap
VG Name Vol
LV UUID 77QaRH-sadM-yEii-HX5f-TvOT-64PY-EKJpXw
LV Write Access read/write
LV Creation host, time localhost, 2022-08-09 01:07:38 +0800
LV Status available
# open 2
LV Size 2.00 GiB
Current LE 512
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1
我们的目标是使用新硬盘对/dev/Vol/var逻辑卷进行扩容:
--- Logical volume ---
LV Path /dev/Vol/var
LV Name var
VG Name Vol
LV UUID Ap6gbh-luh5-5pcj-WehD-Bx8w-9HD7-Xj04MR
LV Write Access read/write
LV Creation host, time localhost, 2022-08-09 01:07:38 +0800
LV Status available
# open 1
LV Size 155.00 GiB
Current LE 39680
Segments 3
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:4
使用pvcreate /dev/sdc1创建物理卷:
Physical volume "/dev/sdc1" successfully created.
使用pvdisplay查看发现多了/dev/sdc1这个物理卷
--- Physical volume ---
PV Name /dev/sda2
VG Name Vol
PV Size <49.71 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 12724
Free PE 0
Allocated PE 12724
PV UUID jGxa54-5Z5P-6TEz-YF2V-3dHP-fNCG-31v9rD
--- Physical volume ---
PV Name /dev/sdb1
VG Name Vol
PV Size <150.00 GiB / not usable <3.97 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 38399
Free PE 256
Allocated PE 38143
PV UUID qZ4NuA-vZof-zAAN-N9rN-VK6K-z0yc-0gNZGs
"/dev/sdc1" is a new physical volume of "<200.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdc1
VG Name
PV Size <200.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID jloLZ4-TS9y-O5c8-TIQ9-yeCj-cecT-jwN6no
六、将sdc硬盘的分区sdc1上创建的物理卷加入卷组
使用vgdisplay查看卷组
--- Volume group ---
VG Name Vol
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 8
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 5
Open LV 5
Max PV 0
Cur PV 2
Act PV 2
VG Size <199.70 GiB
PE Size 4.00 MiB
Total PE 51123
Alloc PE / Size 50867 / <198.70 GiB
Free PE / Size 256 / 1.00 GiB
VG UUID J0mnNe-UHlF-jIcJ-1LEk-edeo-1tqJ-sN2ubw
使用命令 vgextend Vol /dev/sdc1 添加物理卷/dev/sdc1到卷组 Vol中
(Vol是卷组的名称,该命令添加一个或多个物理卷到卷组,用来扩容卷组中的逻辑卷)
Volume group "Vol" successfully extended
再使用vgdisplay查看卷组
--- Volume group ---
VG Name Vol
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 9
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 5
Open LV 5
Max PV 0
Cur PV 3
Act PV 3
VG Size <399.70 GiB
PE Size 4.00 MiB
Total PE 102322
Alloc PE / Size 50867 / <198.70 GiB
Free PE / Size 51455 / <201.00 GiB
VG UUID J0mnNe-UHlF-jIcJ-1LEk-edeo-1tqJ-sN2ubw
发现变动行:
Free PE / Size 51455 / <201.00 GiB
七、扩展逻辑卷,扩展xfs分区大小
使用命令lvextend -l +100%FREE /dev/Vol/var扩展逻辑卷大小
* -l 选项表示后面的参数是要扩展的逻辑卷的大小。
* +100%FREE 指定要将逻辑卷扩展到空闲空间的最大百分比。这里的 100%FREE 表示使用所有可用的空闲空间来扩展逻辑卷。
* /dev/Vol/var 是lvdisplay中的LV Path
Size of logical volume Vol/var changed from 155.00 GiB (39680 extents) to <356.00 GiB (91135 extents).
Logical volume Vol/var successfully resized.
使用sudo xfs_growfs /dev/Vol/var对xfs分区扩展空间
meta-data=/dev/mapper/Vol-var isize=512 agcount=104, agsize=393216 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=40632320, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 40632320 to 93322240
最后使用df命令查看硬盘分区,发现/var分区已经扩容了
/dev/mapper/Vol-root 20G 1.3G 19G 7% /
devtmpfs 7.8G 0 7.8G 0% /dev
tmpfs 7.8G 0 7.8G 0% /dev/shm
tmpfs 7.8G 8.5M 7.8G 1% /run
tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup
/dev/sda1 297M 125M 172M 43% /boot
/dev/mapper/Vol-var 356G 300M 356G 1% /var
/dev/mapper/Vol-tmp 10G 33M 10G 1% /tmp
/dev/mapper/Vol-home 12G 33M 12G 1% /home