磁盘分区

点击查看

初步了解磁盘分区

硬盘的分区主要分为基本分区(Primary Partition)和扩展分区(Extension Partition)两种,基本分区和扩展分区的数目之和不能大于4个。且基本分区可以马上被使用但不能再分区。扩展分区必须在进行分区后才能使用,也就是说它必须还要进行二次分区。那么由扩展分区再分下去的是什么呢?那就是逻辑分区(Logical Partition),并且逻辑分区没有数量上的限制
img
在Linux中,每一个硬件设备都映射到一个系统的文件,对于硬盘、光驱等IDE或SCSI设备也不例外。Linux对各种IDE设备分配了一个由hd前缀组成的文件;而对于各种SCSI设备,则分配了一个由sd前缀组成的文件。对于IDE硬盘,驱动器标识符为hdx~,其中hd表明分区所在设备的类型,这里是指IDE硬盘。x为盘号(a为基本盘,b为基本从属盘,c为辅助主盘,d为辅助从属盘), ~代表分区,前4个分区用数字1~4表示,它们是主分区或扩展分区,从5开始就是逻辑分区。例,hda3表示为第一个IDE硬盘上的第三个主分区或扩展分区,hdb2表示为第二个IDE硬盘上的第二个主分区或扩展分区。对于SCSI硬盘则标识为sdx~, SCSI硬盘是用sd来表示分区所在设备的类型的,其余则和IDE硬盘的表示方法一样,这里不再多说。例如,第一个IDE设备,Linux就定义为hda;第二个IDE设备就定义为hdb;下面以此类推。而SCSI设备就应该是sda、sdb、sdc等

分区数量

要进行分区就必须针对每一个硬件设备进行操作,这就有可能是一块IDE硬盘或是一块SCSI硬盘。对于每一个硬盘(IDE或SCSI)设备,Linux分配了一个1~16的序列号码,这就代表了这块硬盘上面的分区号码。例如,第一个IDE硬盘的第一个分区,在Linux下面映射的就是hda1,第二个分区就称作hda2。对于SCSI硬盘则是sda1、sdb1等,
在Linux中规定,每一个硬盘设备最多能由4个主分区(其中包含扩展分区)构成,任何一个扩展分区都要占用一个主分区号码,也就是在一个硬盘中,主分区和扩展分区一共最多是4个。主分区的作用就是供计算机进行操作系统启动,因此每一个操作系统的启动,或者称作引导程序,都应该存放在主分区上。这就是主分区和扩展分区及逻辑分区的最大区别。在指定安装引导Linux的Bootloader的时候,都要指定在主分区上,这就是最好的例证。Linux规定了主分区(或者扩展分区)占用1~16号码中的前4个号码。以第一个IDE硬盘为例说明,主分区(或者扩展分区)占用了hda1、hda2、hda3、hda4,而逻辑分区占用了hda5到hda16等12个号码。因此,Linux下面每一个硬盘总共最多有16个分区。

分区类型

Linux分区类型有ext2,ext3,ext4,xfs,BtrFS,GlusterFS等格式

用命令fdisk创建一个基本分区(/dev/sdb1)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
[root@linlink ~]# fdisk /dev/sdb               //指定/dev/sdb分区
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): n //n回车、创建一个新分区
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p //选择分区类型为(p)基本分区、e是扩展分区、l是逻辑分区(逻辑分区建立在扩展分区上)
Partition number (1-4, default 1): 1 //选择号默认1
First sector (2048-41943039, default 2048): //选择起始扇区、有需要自己研究去
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +5G //给分区分配大小
Partition 1 of type Linux and of size 5 GiB is set
Command (m for help): p //这里的p是打印分区表(/dev/sdb)
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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 label type: dos
Disk identifier: 0xc8546a0b

Device Boot Start End Blocks Id System
/dev/sdb1 2048 10487807 5242880 83 Linux

Command (m for help): w //w是保存退出
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@linlink ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 199G 0 part
├─centos-root 253:0 0 50G 0 lvm /
├─centos-swap 253:1 0 3.9G 0 lvm [SWAP]
└─centos-home 253:2 0 145.1G 0 lvm /home
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 5G 0 part
sr0 11:0 1 4.2G 0 rom

用命令fdisk创建一个扩展分区(/dev/sdb2)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
[root@linlink ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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 label type: dos
Disk identifier: 0xc8546a0b

Device Boot Start End Blocks Id System
/dev/sdb1 2048 10487807 5242880 83 Linux

Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): e
Partition number (2-4, default 2): 2
First sector (10487808-41943039, default 10487808):
Using default value 10487808
Last sector, +sectors or +size{K,M,G} (10487808-41943039, default 41943039): +5G
Partition 2 of type Extended and of size 5 GiB is set

Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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 label type: dos
Disk identifier: 0xc8546a0b
Device Boot Start End Blocks Id System
/dev/sdb1 2048 10487807 5242880 83 Linux
/dev/sdb2 10487808 20973567 5242880 5 Extended
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@linlink ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 199G 0 part
├─centos-root 253:0 0 50G 0 lvm /
├─centos-swap 253:1 0 3.9G 0 lvm [SWAP]
└─centos-home 253:2 0 145.1G 0 lvm /home
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 5G 0 part
└─sdb2 8:18 0 1K 0 part
sr0 11:0 1 4.2G 0 rom

用命令fdisk创建一个逻辑分区(/dev/sdb5)、注意:需要先创建一个扩展分区

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
[root@linlink ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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 label type: dos
Disk identifier: 0xc8546a0b

Device Boot Start End Blocks Id System
/dev/sdb1 2048 10487807 5242880 83 Linux
/dev/sdb2 10487808 20973567 5242880 5 Extended

Command (m for help): n
Partition type:
p primary (1 primary, 1 extended, 2 free)
l logical (numbered from 5)
Select (default p): l
Adding logical partition 5
First sector (10489856-20973567, default 10489856):
Using default value 10489856
Last sector, +sectors or +size{K,M,G} (10489856-20973567, default 20973567): +3G
Partition 5 of type Linux and of size 3 GiB is set

Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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 label type: dos
Disk identifier: 0xc8546a0b

Device Boot Start End Blocks Id System
/dev/sdb1 2048 10487807 5242880 83 Linux
/dev/sdb2 10487808 20973567 5242880 5 Extended
/dev/sdb5 10489856 16781311 3145728 83 Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@linlink ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 199G 0 part
├─centos-root 253:0 0 50G 0 lvm /
├─centos-swap 253:1 0 3.9G 0 lvm [SWAP]
└─centos-home 253:2 0 145.1G 0 lvm /home
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 5G 0 part
├─sdb2 8:18 0 1K 0 part
└─sdb5 8:21 0 3G 0 part
sr0 11:0 1 4.2G 0 rom

fdisk命令里相关参数

参数 解释
a 切换一个可引导标志
b 编辑BSD磁盘标签
c 切换DOS兼容性标志
d 删除分区
g 创建一个新的空GPT分区表
G 创建一个IRIX (SGI)分区表
l 列出已知分区类型
m 打印菜单
n 添加一个新分区
o 创建一个新的空DOS分区表
p 打印分区表
q 不保存更改就退出
s 创建一个新的空Sun磁盘标签
t 修改分区的系统id
u 改变显示/输入单元
v 验证分区表
w 将表写入磁盘并退出
x 额外的功能(仅限专家)

LVM

点击查看

初步了解LVM

LVM是逻辑盘卷管理(Logical Volume Manager)的简称、最早应用在IBM AIX系统上。它的主要作用是动态分配硬盘分区及调整硬盘分区大小,并且可以让多个分区或者物理硬盘作为一个逻辑卷(相当于一个逻辑硬盘)来使用。这种机制可以让硬盘分区容量划分变得更灵活。通过使用Linux的逻辑卷管理器(Logical Volume Manager, LVM),用户可以在系统运行时动态调整文件系统的大小,把数据从一块硬盘重定位到另一块硬盘,也可以提高I/O操作的性能,以及提供冗余保护,它的快照功能允许用户对逻辑卷进行实时的备份。对一般用户来讲,使用最多的是动态调整文件系统大小的功能。这样,在分区时就不必为如何设置分区的大小而烦恼,只要在硬盘中预留出部分空闲空间,然后根据系统的使用情况,动态调整分区大小。
img
LVM的原理图如图所示。LVM进行逻辑卷的管理时,创建顺序是PV→VG→LV、删除的顺序LV→VG→PV、也就是说,首先创建一个物理卷(对应一个物理硬盘分区或者一个物理硬盘),然后把这些分区或者硬盘加入一个卷组(相当于一个逻辑上的大硬盘),再在这个大硬盘上划分分区LV(逻辑上的分区,也就是逻辑卷),最后把LV逻辑卷格式化以后,就可以像使用一个传统分区那样,把它挂在一个挂载点上,需要的时候,这个逻辑卷可以被动态缩放。

PV

PV(Physical Volume,物理卷):物理卷就是指硬盘,硬盘分区或从逻辑上与硬盘分区具有同样功能的设备(如RAID),是LVM的基本存储逻辑块,处于LVM的最底层,但和基本的物理存储介质(如分区、硬盘等)比较,却包含有与LVM相关的管理参数。当前LVM允许在每个物理卷上保存这个物理卷的0~2份元数据复件。默认为1,保存在设备的开始处;为2时,在设备结束处保存第二份备份。

VG

VG(Volume Group,卷组):可以看成单独的逻辑硬盘,建立在PV之上,是PV的组合。一个卷组中至少要包括一个PV,在卷组建立之后可以动态地添加PV到卷组。

LV

LV(Logical Volume,逻辑卷):相当于物理分区的/dev/sdax。逻辑卷建立在卷组之上,卷组中的未分配空间可以用于建立新的逻辑卷,逻辑卷建立后可以动态地扩展或缩小空间。系统中的多个逻辑卷可以属于同一个卷组,也可以属于不同的多个卷组。

PE

PE(Physical Extent,物理区域):物理区域是物理卷中可用于分配的最小存储单元,物理区域的大小可根据实际情况在建立物理卷时指定。物理区域大小一旦确定将不能更改,同一卷组中的所有物理卷的物理区域大小需要一致。当多个PV组成一个VG时,LVM会在所有PV上做类似格式化的动作,将每个PV切成一块块的空间,这一块块的空间就称为PE,通常是4MB。

LE

LE(Logical Extent,逻辑区域):逻辑区域是逻辑卷中可用于分配的最小存储单元,逻辑区域的大小取决于逻辑卷所在卷组中的物理区域的大小。LE的大小为PE的倍数(通常是1∶1)。

VGDA

VGDA(Volume Group Descriptor Area,卷组描述区域):存在于每个物理卷中,用于描述该物理卷本身、物理卷所属卷组、卷组中的逻辑卷以及逻辑卷中的物理区域的分配等所有的信息,卷组描述区域是在使用pvcreate命令建立物理卷时建立的。

创建LVM类型的分区

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
[root@linlink ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

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
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-41943039, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +5G
Partition 1 of type Linux and of size 5 GiB is set

Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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 label type: dos
Disk identifier: 0xc8546a0b

Device Boot Start End Blocks Id System
/dev/sdb1 2048 10487807 5242880 83 Linux

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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 label type: dos
Disk identifier: 0xc8546a0b

Device Boot Start End Blocks Id System
/dev/sdb1 2048 10487807 5242880 8e Linux LVM

物理卷管理PV

用pvcreate创建物理卷

1
2
[root@linlink ~]# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created.

用pvremove删除物理卷

1
2
[root@linlink ~]# pvremove /dev/sdb1
Labels on physical volume "/dev/sdb1" successfully wiped.

可以通过pvs、pvscan、pvdisplay查看物理卷信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@linlink ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <199.00g 4.00m
[root@linlink ~]# pvscan
PV /dev/sda2 VG centos lvm2 [<199.00 GiB / 4.00 MiB free]
Total: 1 [<199.00 GiB] / in use: 1 [<199.00 GiB] / in no VG: 0 [0 ]
[root@linlink ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name centos
PV Size <199.00 GiB / not usable 3.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 50943
Free PE 1
Allocated PE 50942
PV UUID KrZMQS-uPt0-ikoL-kf0P-I9Gx-PFHw-CZ8GXl

列出已经作为pv的设备

1
2
3
4
5
6
7
8
9
10
11
[root@linlink ~]# lvmdiskscan
/dev/centos/root [ 50.00 GiB]
/dev/sda1 [ 1.00 GiB]
/dev/centos/swap [ <3.88 GiB]
/dev/sda2 [ <199.00 GiB] LVM physical volume
/dev/centos/home [ <145.12 GiB]
/dev/sdb1 [ 5.00 GiB]
3 disks
2 partitions
0 LVM physical volume whole disks
1 LVM physical volume

检查

1
pvscan

卷组管理VG

创建一个卷组

1
2
3
[root@linlink ~]# vgcreate vg1 /dev/sdb1
Physical volume "/dev/sdb1" successfully created.
Volume group "vg1" successfully created

卷组添加一个PV

1
2
[root@linlink ~]# vgextend vg1 /dev/sdb2
Volume group "vg1" successfully extended

卷组添去掉一个PV

1
2
[root@linlink ~]# vgreduce vg1 /dev/sdb2
Removed "/dev/sdb2" from volume group "vg1"

删除卷组

1
2
[root@linlink ~]# vgremove vg1
Volume group "vg1" successfully removed

vgs、vgscan、vgdisplay

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
[root@linlink ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 3 0 wz--n- <199.00g 4.00m
vg1 2 1 0 wz--n- 9.99g 4.99g
[root@linlink ~]# vgscan
Reading volume groups from cache.
Found volume group "centos" using metadata type lvm2
Found volume group "vg1" using metadata type lvm2
[root@linlink ~]# vgdisplay
--- Volume group ---
VG Name centos
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 3
Open LV 3
Max PV 0
Cur PV 1
Act PV 1
VG Size <199.00 GiB
PE Size 4.00 MiB
Total PE 50943
Alloc PE / Size 50942 / 198.99 GiB
Free PE / Size 1 / 4.00 MiB
VG UUID GBOQoR-njkr-HiQ6-Xm8k-iQPu-AFHP-Jb3WHi

--- Volume group ---
VG Name vg1
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 14
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 0
Max PV 0
Cur PV 2
Act PV 2
VG Size 9.99 GiB
PE Size 4.00 MiB
Total PE 2558
Alloc PE / Size 1280 / 5.00 GiB
Free PE / Size 1278 / 4.99 GiB
VG UUID 1l0gl7-9OU3-IbqW-53i5-Sk8t-1fZy-XHi53N

逻辑卷管理LV

lvcreate创建一个lv

1
2
[root@linlink ~]# lvcreate -l +100%FREE vg1 -n lv1
Logical volume "lv1" created. //将vg1卷组的所有空间给到新创建的逻辑卷的lv1中
1
2
[root@linlink ~]# lvcreate -L 5G vg1 -n lv1
Logical volume "lv1" created. //将vg1卷组的5G空间给到新创建的逻辑卷的lv1中

扩展空间lvextend -L

1
lvextend -L <estend_size> <lv_path>
1
2
3
[root@linlink ~]# lvextend -L +2G /dev/vg1/lv1
Size of logical volume vg1/lv1 changed from 5.00 GiB (1280 extents) to 7.00 GiB (1792 extents).
Logical volume vg1/lv1 successfully resized.

删除一个lv

1
2
3
[root@linlink ~]# lvremove /dev/vg1/lv1
Do you really want to remove active logical volume vg1/lv1? [y/n]: y
Logical volume "lv1" successfully removed

lvs、lvscan、lvdisplay查看

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
[root@linlink ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
home centos -wi-ao---- <145.12g
root centos -wi-ao---- 50.00g
swap centos -wi-ao---- <3.88g
lv1 vg1 -wi-a----- 5.00g
[root@linlink ~]# lvscan
ACTIVE '/dev/centos/swap' [<3.88 GiB] inherit
ACTIVE '/dev/centos/home' [<145.12 GiB] inherit
ACTIVE '/dev/centos/root' [50.00 GiB] inherit
ACTIVE '/dev/vg1/lv1' [5.00 GiB] inherit
[root@linlink ~]# lvdisplay
--- Logical volume ---
LV Path /dev/centos/swap
LV Name swap
VG Name centos
LV UUID xQq83K-RJRR-gglN-vHqn-M4zu-fxSw-GYEJcB
LV Write Access read/write
LV Creation host, time 192.168.20.135, 2023-02-04 04:07:36 -0500
LV Status available
# open 2
LV Size <3.88 GiB
Current LE 992
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1

--- Logical volume ---
LV Path /dev/centos/home
LV Name home
VG Name centos
LV UUID j9354I-22je-aOPM-2Gec-iNuL-3Z50-Ly4yCW
LV Write Access read/write
LV Creation host, time 192.168.20.135, 2023-02-04 04:07:36 -0500
LV Status available
# open 1
LV Size <145.12 GiB
Current LE 37150
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2

--- Logical volume ---
LV Path /dev/centos/root
LV Name root
VG Name centos
LV UUID JpAk6n-c373-CKzq-x3v2-PgWI-sxme-ME5AOS
LV Write Access read/write
LV Creation host, time 192.168.20.135, 2023-02-04 04:07:36 -0500
LV Status available
# open 1
LV Size 50.00 GiB
Current LE 12800
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0

--- Logical volume ---
LV Path /dev/vg1/lv1
LV Name lv1
VG Name vg1
LV UUID hwQopV-G3KX-o6VZ-K4f3-QXlz-CEm7-BBdQdz
LV Write Access read/write
LV Creation host, time linlink, 2023-03-10 09:51:20 -0500
LV Status available
# open 0
LV Size 5.00 GiB
Current LE 1280
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:3

扩展根分区

点击查看

fdisk /dev/sdb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
[root@linlink ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

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
Building a new DOS disklabel with disk identifier 0x5166a770.

Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-83886079, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-83886079, default 83886079):
Using default value 83886079
Partition 1 of type Linux and of size 40 GiB is set

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p

Disk /dev/sdb: 42.9 GB, 42949672960 bytes, 83886080 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 label type: dos
Disk identifier: 0x5166a770

Device Boot Start End Blocks Id System
/dev/sdb1 2048 83886079 41942016 8e Linux LVM

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

格式化

1
2
3
4
5
6
7
8
9
10
[root@linlink ~]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=2621376 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=10485504, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=5119, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0

创建pv

1
2
3
4
[root@linlink ~]# pvcreate /dev/sdb1
WARNING: xfs signature detected on /dev/sdb1 at offset 0. Wipe it? [y/n]: y
Wiping xfs signature on /dev/sdb1.
Physical volume "/dev/sdb1" successfully created.

将pv添加到/dev/mapper/centos-root

1
2
[root@linlink ~]# vgextend centos /dev/sdb1
Volume group "centos" successfully extended

查看

1
2
3
4
5
6
7
8
9
10
[root@linlink ~]# df -TH
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 54G 1.1G 53G 2% /
devtmpfs devtmpfs 2.0G 0 2.0G 0% /dev
tmpfs tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs tmpfs 2.0G 13M 2.0G 1% /run
tmpfs tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
/dev/sda1 xfs 1.1G 149M 915M 14% /boot
/dev/mapper/centos-home xfs 156G 34M 156G 1% /home
tmpfs tmpfs 396M 0 396M 0% /run/user/0

扩展逻辑卷

1
2
3
[root@linlink ~]# lvextend -l +100%FREE /dev/mapper/centos-root
Size of logical volume centos/root changed from 50.00 GiB (12800 extents) to 90.00 GiB (23040 extents).
Logical volume centos/root successfully resized.
1
lvextend -L +40G /dev/mapper/centos-root                //L和l的区别

xfs_growfs重新读取

1
2
3
4
5
6
7
8
9
10
11
[root@linlink ~]# xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=3276800 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=13107200, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=6400, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 13107200 to 23592960

再次查看

1
2
3
4
5
6
7
8
9
10
[root@linlink ~]# df -TH
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 97G 1.1G 96G 2% /
devtmpfs devtmpfs 2.0G 0 2.0G 0% /dev
tmpfs tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs tmpfs 2.0G 13M 2.0G 1% /run
tmpfs tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
/dev/sda1 xfs 1.1G 149M 915M 14% /boot
/dev/mapper/centos-home xfs 156G 34M 156G 1% /home
tmpfs tmpfs 396M 0 396M 0% /run/user/0

NFS

点击查看

服务端

安装软件包

1
yum -y install nfs-utils rpcbind

配置相关文件、注:192.168.20.0/24为主机IP段

1
2
mkdir /nfs
echo '/nfs 192.168.20.0/24(rw,sync,no_root_squash)' > /etc/exports
权限 解释
rw: 可读写
ro: 只读,但最终能不能读写,还是与文件系统的rwx 及身份有关
no_root_squash: 当NFS客户端以root管理员访问时,映射为NFS服务器的root管理员
root_squash: 当NFS客户端以root管理员访问时,映射为NFS服务器的匿名(nobody)用户
all_squash: 不论登入NFS 的使用者身份为何,均被映射为匿名用户,通常就是nobody(nfsnobody)
insecure: 允许从客户端过来的非授权访问
sync: 数据会同步写入到内存与硬盘中
async: 数据会先暂存于内存当中,而非直接写入硬盘
anonuid: 指定uid的值,此uid必须存在于/etc/passwd中
anongid: 指定gid的值,此gid必须存在于/etc/group中

启动nfs服务+开机自启

1
systemctl start rpcbind nfs-server && systemctl enable rpcbind nfs-server

检查服务是否存在、注:192.168.20.10为主机ip及nfs服务端ip

1
showmount -e 192.168.20.10

客户端

客户端安装软件包(不用启动)

1
yum -y install nfs-utils rpcbind

客户端检查

1
showmount -e 192.168.20.10

客户端挂载

1
2
mkdir /nfs
mount -t nfs 192.168.20.10:/nfs /nfs

客户端查看挂载

1
df -TH

客户端测试写入

1
2
mkdir /nfs/qnmdnfs
ll /nfs/

HDFS

点击查看

配置需求清单

虚拟机 工作节点1 工作节点2 工作节点3
主机名 node1 node2 node3
CPU 2H 2H 2H
磁盘 60GB 60GB 60GB
内存 2GB 2GB 2GB
网络模式 NAT NAT NAT
IP地址 192.168.20.21 192.168.20.22 192.168.20.23

添加本地解析_所有工作节点执行

1
2
3
4
echo "192.168.20.21 node1
192.168.20.22 node2
192.168.20.23 node3
" >> /etc/hosts

下载jdk1.8和hadoop软件包_所有工作节点执行

1
yum -y install wget
1
wget https://llwp.linlink.xyz/d/cloudcompute/test/jdk-8u331-linux-x64.tar.gz
1
wget https://llwp.linlink.xyz/d/cloudcompute/test/hadoop-2.10.1.tar.gz

解压_所有工作节点执行

1
2
tar xf jdk-8u331-linux-x64.tar.gz -C /usr/local/
tar xf hadoop-2.10.1.tar.gz -C /usr/local/

配置jdk环境和Hadoop环境_所有工作节点执行

1
2
3
4
5
echo 'export JAVA_HOME=/usr/local/jdk1.8.0_331
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin:/usr/local/hadoop-2.10.1/bin/:/usr/local/hadoop-2.10.1/sbin/
' >> /etc/profile

检查jdk环境_所有工作节点执行

1
2
source /etc/profile
java -version

设置免密登录_只在node1执行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@node1 ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:3F/MMmOKhOzcw1F5gkImyLlkMgBTulWKBkkyrZe5Rl8 root@node1
The key's randomart image is:
+---[RSA 2048]----+
|@*.oo o |
|*=Bo + . . |
|oBo+ . . + . |
|oo* .E+ o o o |
|.o o .o S . = + |
| o .o + o + = |
| . o = . . |
| . |
| |
+----[SHA256]-----+
[root@node1 ~]#
1
2
3
4
5
6
7
8
9
[root@node1 ~]# ssh-copy-id node1
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 'node1 (fe80::e71b:1e52:c6a8:3521%ens33)' can't be established.
ECDSA key fingerprint is SHA256:cuzySJJ+Y1b0xwsabngEt/Onknu48EOGb+lxmFxLwsA.
ECDSA key fingerprint is MD5:f9:b6:2c:32:94:58:10:70:88:89:9b:f4:8a:2b:18:fa.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@node1's password: 输入密码
1
2
3
4
5
6
7
8
9
[root@node1 ~]# ssh-copy-id node2
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 'node2 (192.168.20.22)' can't be established.
ECDSA key fingerprint is SHA256:cuzySJJ+Y1b0xwsabngEt/Onknu48EOGb+lxmFxLwsA.
ECDSA key fingerprint is MD5:f9:b6:2c:32:94:58:10:70:88:89:9b:f4:8a:2b:18:fa.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@node2's password: 输入密码
1
2
3
4
5
6
7
8
9
[root@node1 ~]# ssh-copy-id node3
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 'node3 (192.168.20.23)' can't be established.
ECDSA key fingerprint is SHA256:cuzySJJ+Y1b0xwsabngEt/Onknu48EOGb+lxmFxLwsA.
ECDSA key fingerprint is MD5:f9:b6:2c:32:94:58:10:70:88:89:9b:f4:8a:2b:18:fa.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@node3's password: 输入密码

修改配置文件__所有工作节点执行

1
2
sed "25,26d" /usr/local/hadoop-2.10.1/etc/hadoop/hadoop-env.sh  -i
sed -i "24a export JAVA_HOME=/usr/local/jdk1.8.0_331" /usr/local/hadoop-2.10.1/etc/hadoop/hadoop-env.sh
1
sed -i "19a <property>\n\    <name>fs.defaultFS</name>\n\    <value>hdfs://node1:9000</value>\n</property>\n<property>\n\    <name>hadoop.tmp.dir</name>\n\    <value>/opt/hadoop-2.10.1</value>\n</property>" /usr/local/hadoop-2.10.1/etc/hadoop/core-site.xml
1
sed -i "19a <property>\n\    <name>dfs.replication</name>\n\    <value>1</value>\n</property>\n<property>\n\    <name>dfs.namenode.secondary.http-address</name>\n\    <value>node2:50090</value>\n</property>" /usr/local/hadoop-2.10.1/etc/hadoop/hdfs-site.xml
1
echo "node3" > /usr/local/hadoop-2.10.1/etc/hadoop/slaves

初始化+启动_node1执行

1
2
hadoop namenode -format
start-dfs.sh

查看节点状态

1
2
3
[root@node1 ~]# jps
27145 NameNode
27530 Jps
1
2
3
[root@node2 ~]# jps
11333 Jps
11273 SecondaryNameNode
1
2
3
[root@node3 ~]# jps
13621 Jps
13530 DataNode

test一下

1
2
3
4
[root@node1 ~]# hadoop fs -put jdk-8u331-linux-x64.tar.gz /
[root@node1 ~]# hadoop fs -ls /
Found 1 items
-rw-r--r-- 1 root supergroup 148003999 2023-04-05 09:59 /jdk-8u331-linux-x64.tar.gz

GlusterFS

点击查看

Ceph

点击查看

配置需求清单

虚拟机 工作节点1(admin) 工作节点2(osd1) 工作节点3(osd2)
主机名 node1 node2 node3
CPU 2H 2H 2H
磁盘 60GB+20GB 60GB+20GB 60GB+20GB
内存 2GB 2GB 2GB
网络模式 NAT NAT NAT
IP地址 192.168.20.31 192.168.20.32 192.168.20.33

关闭防火墙和selinux_所有工作节点执行

1
systemctl stop firewalld && systemctl disable firewalld
1
setenforce 0

添加本地解析_所有工作节点执行

1
2
3
4
echo "192.168.20.31 node1
192.168.20.32 node2
192.168.20.33 node3
" >> /etc/hosts

配置免密登录_node1执行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
[root@node1 ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:6JofyA259O812g65JO/R5gwDh6fV/GYyj5jL3mMn1wA root@node1
The key's randomart image is:
+---[RSA 2048]----+
| |
| |
| |
| . o oE |
| + + S o. |
| o B * o .. |
| + B B B +o |
| o B ^+Oo . |
| o..+&+B=. |
+----[SHA256]-----+
[root@node1 ~]# ssh-copy-id root@node1
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 'node1 (192.168.20.31)' can't be established.
ECDSA key fingerprint is SHA256:QOg9grT/Owi/HDJRYqkboEZGGOABHPRDTpv99gGYL28.
ECDSA key fingerprint is MD5:d8:bf:8e:c5:89:5e:8c:94:36:f2:e2:20:c9:58:25:71.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@node1's password: 这里输入密码

Number of key(s) added: 1

Now try logging into the machine, with: "ssh 'root@node1'"
and check to make sure that only the key(s) you wanted were added.

[root@node1 ~]# ssh-copy-id root@node2
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 'node2 (192.168.20.32)' can't be established.
ECDSA key fingerprint is SHA256:QOg9grT/Owi/HDJRYqkboEZGGOABHPRDTpv99gGYL28.
ECDSA key fingerprint is MD5:d8:bf:8e:c5:89:5e:8c:94:36:f2:e2:20:c9:58:25:71.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@node2's password: 这里输入密码

Number of key(s) added: 1

Now try logging into the machine, with: "ssh 'root@node2'"
and check to make sure that only the key(s) you wanted were added.

[root@node1 ~]# ssh-copy-id root@node3
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 'node3 (192.168.20.33)' can't be established.
ECDSA key fingerprint is SHA256:QOg9grT/Owi/HDJRYqkboEZGGOABHPRDTpv99gGYL28.
ECDSA key fingerprint is MD5:d8:bf:8e:c5:89:5e:8c:94:36:f2:e2:20:c9:58:25:71.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@node3's password: 这里输入密码

Number of key(s) added: 1

Now try logging into the machine, with: "ssh 'root@node3'"
and check to make sure that only the key(s) you wanted were added.

配置yum源_所有工作节点执行

1
yum -y install wget
1
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
1
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
echo "[ceph]
name=ceph
baseurl=http://mirrors.aliyun.com/ceph/rpm-nautilus/el7/x86_64/
gpgcheck=0
priority=1
[ceph-noarch]
name=cephnoarch
baseurl=http://mirrors.aliyun.com/ceph/rpm-nautilus/el7/noarch/
gpgcheck=0
priority=1
[ceph-source]
name=Ceph source packages
baseurl=http://mirrors.aliyun.com/ceph/rpm-nautilus/el7/SRPMS
enabled=0
gpgcheck=1
type=rpm-md
gpgkey=http://mirrors.aliyun.com/ceph/keys/release.asc
priority=1
" > /etc/yum.repos.d/ceph.repo

开始安装Ceph

node1安装

1
yum -y install ceph-deploy ceph python-setuptools 

node2~3安装

1
yum -y install ceph python-setuptools 

切换工作目录_所有工作节点执行

1
cd /etc/ceph/

node1执行

1
ceph-deploy new node2 node3
1
ceph-deploy mon create-initial
1
ceph-deploy osd create --data /dev/sdb node2
1
ceph-deploy osd create --data /dev/sdb node3
1
ceph-deploy admin node2 node3
1
ceph-deploy mgr create node1 node2 node3

所有工作节点执行

1
chmod +r ceph.client.admin.keyring

创建dashboard_node1执行

1
yum -y install ceph-mgr-dashboard

编辑ceph.conf文件_node1执行

1
2
3
echo "[mon]
mgr initial modules = dashboard
" >> /etc/ceph/ceph.conf

推送配置文件_node1执行

1
ceph-deploy --overwrite-conf config push node1 node2 node3 

重启mgr_node1执行

1
systemctl restart ceph-mgr@node1

使用以下内置命令生成并安装自签名证书_node1执行

1
2
ceph mgr module enable dashboard --force
ceph dashboard create-self-signed-cert

创建具有管理员角色的用户_node1执行

1
echo "000000" > passwd
1
ceph dashboard set-login-credentials admin -i passwd

查看ceph-mgr服务:

1
ceph mgr services

访问https://node1_ip:8443

1
2
3
# 注意:这里一定要加"https://"
账号:admin
密码:000000