小知识!
1.硬盘的是主要接口
IDE ——> SATA 1代/2代/3代
SCSI ——> SAS
PCIE ssd固态硬盘,直接接在pci总线上,速度非常的快,但是不适合热插拔,服务器中不常见
FC 光纤接口的硬盘
2. kernel 对不同接口的硬盘命名
OS IDE(并口) SATA(串口) SCSI
RHEL5 /dev/hda /dev/sda /dev/sda
RHEL6 /dev/sda /dev/sda /dev/sda
RHEL7 /dev/sda /dev/sda /dev/sda
KVM /dev/vda(半虚拟化驱动)
/dev/sda(全虚拟化驱动)
3. 物理服务器磁盘识别
HP服务器
/dev/cciss/c0d0
/dev/cciss/c0d0p1 //c0第一个控制器, d0第一块磁盘, p1分区1
/dev/cciss/c0d0p2 //c0第一个控制器, d0第一块磁盘, p2分区2
除惠普服务器之外,其他厂家的服务器都是/dev/sda的形式
3.从工作原理上对于硬盘的分类:
机械硬盘:又称之为磁盘
盘片:磁盘上面有一摞盘片
磁道:盘片上一圈一圈的是磁道
扇区:磁道被划分之后是扇区,默认是512B(字节)
柱面:半径相同的盘片组成一个柱子一样的体,叫做柱面。
硬盘的转速:1400r/min 7200r/min 10000r/min 15000r/min
固态硬盘:没有盘片,里面都是芯片和电子,目前的在dell r730的服务器上有应用。
4.磁盘的尺寸(英寸)
3.5
2.5
1.8
5.存储的连接方式
本地存储:本地硬盘
外部存储:通过相应的线,如scsi线连接到服务器中
网络存储:通过以太网或者是FC网络进行连接的存储
硬盘接口分类
硬盘分为固态硬盘(SSD)和机械硬盘(HDD);SSD采用闪存颗粒来存储,HDD采用磁性碟片来存储
分区方式
| ###### 硬盘大小
| 2TB以下 | 2TB以上 |
| --- | --- | --- |
| ###### 分区软件
| fdisk
gdisk | gdisk |
| ###### 分区表
| MSDOS(MBR) | GPT |
磁盘容量单位换算
计算机硬盘存储容量通常使用
字节(B,Byte)、
千字节(KB,KiloByte)、
兆字节(MB,MegaByte)、
吉字节(GB,Gigabyte)、
太字节(TB,TeraByte)、
拍字节(PB,PetaByte)、
艾字节(EB,ExaByte)、
泽字节(ZB,ZettaByte)、
尧字节 (YB,YottaByte)、
BB(BrontoByte)、
NB(NonaByte)、
DB(DoggaByte)等来衡量。
计算方式:
1DB=1024NB;
1NB=1024BB,
1BB=1024YB;
1YB=1024ZB;
1ZB=1024EB;
1EB=1024PB;
1PB=1024TB;
1TB=1024GB;
1GB=1024MB;
1MB=1024KB;
1KB=1024B
分区限制
MSDOS(MBR)分区的限制:
主分区;扩展分区;逻辑分区
只支持4个主分区,总分区数不能超过15个
只支持2TB以下的硬盘 |
GPT分区的限制:
| 128个分区 |
注意:从MBR转到GPT,或从GPT转换到MBR会导致数据全部丢失!
分区管理
:::info
内容摘要:
分区工具fdisk
分区工具parted
:::
MSDOS(MBR)分区
示例:
1. 添加一块硬盘:
[root@localhost ~]# ls /dev/sd* ----查看内核识别的磁盘
/dev/sda /dev/sda1 /dev/sda2 /dev/sdb
[root@localhost ~]# fdisk -l ---- 查看系统中磁盘容量
Disk /dev/sda: 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: 0x000a1d85
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 41943039 19921920 8e Linux LVM
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
[root@localhost ~]# lsblk --- 查看分区信息
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─centos-root 253:0 0 17G 0 lvm /
└─centos-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 2G 0 part
├─sdb2 8:18 0 1K 0 part
├─sdb5 8:21 0 3G 0 part
└─sdb6 8:22 0 5G 0 part
sr0 11:0 1 4.3G 0 rom
2. 分区
主分区
扩展分区
逻辑分区
[root@localhost ~]# 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 0x1c354e10.
Command (m for help): m
Command action
---m : 查看全部可用的参数
---p : 查看打印分区信息
---n : 添加新的分区
---d : 删除某个分区信息
---w : 保存并退出
---q : 不保存直接退出
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: 0x4caace32
Device Boot Start End Blocks Id System
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 ---- 默认1分区id
First sector (2048-41943039, default 2048): ---- 系统默认保留2048起始扇区
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +2G --分区大小
Partition 1 of type Linux and of size 2 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: 0x4caace32
Device Boot Start End Blocks Id System
/dev/sdb1 2048 4196351 2097152 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 # 默认分区id
First sector (4196352-41943039, default 4196352):
Using default value 4196352
Last sector, +sectors or +size{K,M,G} (4196352-41943039, default 41943039): --- 什么不写表示剩余所有空间全部分配
Using default value 41943039
Partition 2 of type Extended and of size 18 GiB is set
Command (m for help):
Command (m for help): n
Partition type:
p primary (1 primary, 1 extended, 2 free)
l logical (numbered from 5)
Select (default p):
Using default response p
Partition number (3,4, default 3):
No free sectors available # 没有空间分配
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 (4198400-41943039, default 4198400):
Using default value 4198400
Last sector, +sectors or +size{K,M,G} (4198400-41943039, default 41943039): +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: 0x4caace32
Device Boot Start End Blocks Id System
/dev/sdb1 2048 4196351 2097152 83 Linux
/dev/sdb2 4196352 41943039 18873344 5 Extended
/dev/sdb5 4198400 10489855 3145728 83 Linux
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 6
First sector (10491904-41943039, default 10491904):
Using default value 10491904
Last sector, +sectors or +size{K,M,G} (10491904-41943039, default 41943039): +5G
Partition 6 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: 0x4caace32
Device Boot Start End Blocks Id System
/dev/sdb1 2048 4196351 2097152 83 Linux
/dev/sdb2 4196352 41943039 18873344 5 Extended
/dev/sdb5 4198400 10489855 3145728 83 Linux
/dev/sdb6 10491904 20977663 5242880 83 Linux
Command (m for help): w # 保存修改并退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~] fdisk -l /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: 0x4caace32
Device Boot Start End Blocks Id System
/dev/sdb1 2048 4196351 2097152 83 Linux
/dev/sdb2 4196352 41943039 18873344 5 Extended
/dev/sdb5 4198400 10489855 3145728 83 Linux
/dev/sdb6 10491904 20977663 5242880 83 Linux
3. 使用分区
需要对分区进行格式化
ext2 、 ext3 、 ext4 、xfs
[root@localhost ~]mkfs.ext4 /dev/sdb6 # ext4 格式化分区
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
40 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
[root@localhost ~]# mkfs.xfs /dev/sdb5 ----- xfs 格式化
meta-data=/dev/sdb5 isize=512 agcount=4, agsize=196608 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=786432, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
4. 挂载使用磁盘
[root@localhost ~]# mount /dev/sdb6 /mnt/
[root@localhost ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 17G 2.0G 16G 12% /
devtmpfs devtmpfs 475M 0 475M 0% /dev
tmpfs tmpfs 487M 0 487M 0% /dev/shm
tmpfs tmpfs 487M 7.7M 479M 2% /run
tmpfs tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda1 xfs 1014M 133M 882M 14% /boot
tmpfs tmpfs 98M 0 98M 0% /run/user/0
/dev/sdb6 ext4 4.8G 2.1G 2.6G 45% /mnt
[root@localhost ~]# ls /mnt/
aa lost+found
[root@localhost ~]# dd if=/dev/zero of=./aa bs=1G count=1
5. 删除分区
[root@localhost ~]# 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): d # 删除分区
Partition number (1,2,5,6, default 6): 6 # 删除id6的分区
Partition 6 is deleted
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: 0x4caace32
Device Boot Start End Blocks Id System
/dev/sdb1 2048 4196351 2097152 83 Linux
/dev/sdb2 4196352 41943039 18873344 5 Extended
/dev/sdb5 4198400 10489855 3145728 83 Linux
GPT分区
[root@localhost /]# parted /dev/sdb ---分区命令
GNU Parted 3.1
使用 /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) # mklabel # 选择分区表类型
新的磁盘标签类型? # gpt
(parted) # print # 打印分区表信息
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 2147MB
Sector size (logical/physical): 512B/512B
Partition Table: # gpt
Disk Flags:
Number Start End Size File system Name 标志
(parted) # mkpart # 创建分区表
分区名称? []? # sdb1 # 分区名称
文件系统类型? [ext2]?# xfs # 文件类型
起始点? 1 # 起始位置
结束点? 500M # 结束位置
(parted)# print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 2147MB
Sector size (logical/physical): 512B/512B
Partition Table:# gpt
Disk Flags:
Number Start End Size File system Name 标志
1 1049kB 500MB 499MB xfs sdb1
(parted)# mkpart
分区名称? []?# sdb2
文件系统类型? [ext2]?# xfs
起始点? 500M
结束点? 1000M
(parted)# print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 2147MB
Sector size (logical/physical): 512B/512B
Partition Table:# gpt
Disk Flags:
Number Start End Size File system Name 标志
1 1049kB 500MB 499MB xfs sdb1
2 500MB 1000MB 500MB sdb2
mkfs.xfs -f /dev/sdb1 ---格式化
mount /dev/sdb1 /mnt/
umount /dev/sdb1
删除
[root@localhost /]# parted /dev/sdb
GNU Parted 3.1
使用 /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(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|NUMBER] display the partition table, available devices, free space,
all found partitions, or a particular partition
quit exit program
rescue START END rescue a lost partition near START and END
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
unit UNIT set the default unit to UNIT
version display the version number and copyright information of GNU
Parted
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 2147MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name 标志
1 1049kB 500MB 499MB xfs sdb1
2 500MB 1000MB 500MB sdb2
(parted) rm 2
(parted) rm 1
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 2147MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name 标志
(parted)
[root@localhost /]# ls /dev/sdb*
/dev/sdb
由GPT分区改回msdos 分区格式
[root@localhost /]# parted /dev/sdb
GNU Parted 3.1
使用 /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel
新的磁盘标签类型? msdos # msdos 分区格式
警告: The existing disk label on /dev/sdb will be destroyed and all data on this disk will
be lost. Do you want to continue?
是/Yes/否/No? yes
(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 标志
(parted) quit
LVM管理
Logical Volume Manager(逻辑卷管理)的简写
LVM概念:
LVM是Linux环境中对磁盘分区进行管理的一种机制,是建立在硬盘和分区之上、文件系统之下的一个逻辑层,直白一点的翻译就是"逻辑滚动管理员",也就是指"逻辑卷管理"。
作用是可提高磁盘分区管理的灵活性。#使用LVM主要是方便管理、增加了系统的扩展性。
并且可以跨分区,多个硬盘组合。
Lvm基本组成:
1.物理卷(PV)
PV (Physical Volume)实体滚动条
PV 是LVM最基本的物理组成部分,利用fdisk命令把实际的"partition转化成8e"的系统格式,然后利用指令pvcreate把partition变成能够利用的physical volume
2.卷组(VG)
VG (Volume Group)滚动条群组
VG,说的简单些就如我们使用的磁盘,是可以进行分区的,对于LVM上面的文件系统而言,"VG就是一个大磁盘"。文件系统时看不到LVM下面的组成部分的,它只管使用,而LVM则负责管理下面的组成储存块
3.逻辑卷(LV)
LV (Logical Volume)逻辑滚动条
LV就是VG切成的类似于分区的东西啦,我们可以用"格式化,挂载"使用啦,但是LV是VG切割成的,而VG又是有数量众多的PE组成,所以"LV的大小取决于LV所包含的PE的数量的多少"
4.物理块(PE)
PE (Physical Extend)实体延伸区块
PE是LVM最小的存储单位,一般为4MB大小,它是构成VG的基本单位,PE对于VG来说就如block对于分区来讲,它是存储数据的单元。而pe的大小并不是固定的,而是可以变化的,所以"pe决定了LVM的灵活性,即能扩增,也能缩减"
LVM特点:
#传统分区的缺点:
传统的文件系统是基于分区的,一个文件系统对应一个分区。这种方式比较直观,但不易改变:
1.不同的分区相对独立,无相互联系,空间不能充分利用;
2.当一个分区空间已满时,无法对其扩充,只能采用重新分区/建立文件系统,非常麻烦;或把分区中的数据移到另一个更大的分区中。
#当采用LVM时:
1.硬盘的多个分区由LVM统一为卷组管理,可以方便的加入或移走分区以扩大或减小卷组的可用容量,充分利用硬盘空间;
2.文件系统建立在LVM上,可以跨分区,方便使用;
3.当系统空间不足而加入新的硬盘时,不必把用户的数据从原硬盘迁移到新硬盘,而只须把新的分区加入卷组并扩充逻辑卷即可。
区别:
基本分区(MBR|GPT) ----> Filesystem(制作文件系统类型) ----> mount(挂载)
逻辑卷LVM ----> Filesystem(制作文件系统类型) ----> mount(挂载)
LVM的制作
第一步,拥有基本分区
做成物理卷: pv ------- sdb1、sdb2 打pv的标记。
加入卷组: vg-------- 卷组里面包含:sdb1 sdb2,建立在PV之上。
逻辑卷 lv ------- 逻辑卷是从卷组里面拿空间出来做成的逻辑卷,在起个名,建立在VG之上
制作文件系统------mkfs.xfs lvm路径
挂载 ------mount 使用lvm
#制作lvm的时候可以是一整块硬盘也可以是硬盘分好的区并且是还没有用的。必须是没有做文件系统的
LVM创建命令
|
pv(物理卷) vg(卷组) lv(逻辑卷)
创建 pvcreate vgcreate lvcreate
扫描 pvscan vgscan lvscan
显示详细信息 pvdisplay vgdisplay lvdisplay
显示 pvs vgs lvs
删除 pvremove vgremove lvremove
扩展 vgextend
缩小 vgreduce
更改
vgchange lvchange
数据迁移 pvmove
LVM创建
PV 物理卷,标记为LVM的可用空间的分区,在MBR磁盘上,用分区类型0x8e标记。(linux系统中的分区通过命令转换而成)
VG 卷组,建立在PV上,可以划分多个PV,一个或者多个物理卷的集合。可以认为是一个虚拟的大硬盘。 (多个磁盘分区组合在一起)
LV 逻辑卷,建立在VG之上,是可以认为对于卷组的虚拟分区。逻辑卷在使用之前必须格式化为特定的文件系统。 (分区)
PE 物理区域,PE的大小为4MB,如同block (块,区块) 的作用,是LVM最小的寻址单位
注意:
1 分区system ID修改为8e
2 分区不需要创建文件系统
[root@localhost ~]# fdisk /dev/sdc
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 0x6d23fab3.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p):
Using default response p
Partition number (1-4, default 1):
First sector (2048-4194303, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-4194303, default 4194303):
Using default value 4194303
Partition 1 of type Linux and of size 2 GiB is set
Command (m for help): l
0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT-
2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT-
3 XENIX usr 3c PartitionMagic 84 OS/2 hidden C: c6 DRDOS/sec (FAT-
4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx
5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data
6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / .
7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility
8 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt
9 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access
a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O
b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor
c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi eb BeOS fs
e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD ee GPT
f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ef EFI (FAT-12/16/
10 OPUS 55 EZ-Drive a7 NeXTSTEP f0 Linux/PA-RISC b
11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f1 SpeedStor
12 Compaq diagnost 5c Priam Edisk a9 NetBSD f4 SpeedStor
14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f2 DOS secondary
16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ fb VMware VMFS
17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fc VMware VMKCORE
18 AST SmartSleep 65 Novell Netware b8 BSDI swap fd Linux raid auto
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fe LANstep
1c Hidden W95 FAT3 75 PC/IX be Solaris boot ff BBT
1e Hidden W95 FAT1 80 Old Minix
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/sdc: 2147 MB, 2147483648 bytes, 4194304 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: 0x6d23fab3
Device Boot Start End Blocks Id System
/dev/sdc1 2048 4194303 2096128 8e Linux LVM
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─centos-root 253:0 0 17G 0 lvm /
└─centos-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 1G 0 disk
└─sdb1 8:17 0 1023M 0 part
sdc 8:32 0 2G 0 disk
└─sdc1 8:33 0 2G 0 part
sdd 8:48 0 3G 0 disk
└─sdd1 8:49 0 3G 0 part
sde 8:64 0 4G 0 disk
└─sde1 8:65 0 4G 0 part
sdf 8:80 0 5G 0 disk
└─sdf1 8:81 0 5G 0 part
安装lvm:
[root@localhost ~]# yum -y install lvm2
1. 创建物理卷PV(整个硬盘或者是分区,不能用已有文件系统的分区的硬盘[建立在raid之上])
[root@localhost ~]# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created.
2. 创建卷组VG
[root@localhost ~]# vgcreate vg0 /dev/sdb1
Volume group "vg0" successfully created
创建VG时,指定PE的size(默认为4M)
[root@localhost ~]# vgcreate -s 64M vg0 /dev/sdb1 -s 指定的PE大小,2的幂数
3. 创建逻辑卷LV
[root@localhost ~]# lvcreate -L 200M -n lv0 vg0
Logical volume "lv0" created.
[root@localhost ~]# lvcreate -l 200 -n lv03 vg01
-L : 指定尺寸
-l : 指定PE(physical extend)个数
-------------------------扩展---------------------------
[root@localhost ~]# lvcreate -l 50%VG -n lv02 vg01 //总空间的50%
[root@localhost ~]# lvcreate -l 100%FREE -n lv03 vg01 //剩余空间的100%
--------------------------------------------------------
4. 创建文件系统并挂载使用
[root@localhost ~]# mkfs.xfs /dev/vg0/lv0
meta-data=/dev/vg0/lv0 isize=512 agcount=4, agsize=12800 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=51200, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=855, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
挂载使用:
[root@localhost ~]# mount /dev/vg0/lv0 /mnt/
[root@localhost ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/centos-root 17811456 2011252 15800204 12% /
devtmpfs 485828 0 485828 0% /dev
tmpfs 497948 0 497948 0% /dev/shm
tmpfs 497948 7828 490120 2% /run
tmpfs 497948 0 497948 0% /sys/fs/cgroup
/dev/sda1 1038336 135280 903056 14% /boot
tmpfs 99592 0 99592 0% /run/user/0
/dev/mapper/vg0-lv0 201380 10464 190916 6% /mnt
LVM查看
1. pv查看
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <19.00g 0
/dev/sdb1 vg0 lvm2 a-- 1020.00m 820.00m
[root@localhost ~]# pvscan
PV /dev/sda2 VG centos lvm2 [<19.00 GiB / 0 free]
PV /dev/sdb1 VG vg0 lvm2 [1020.00 MiB / 820.00 MiB free]
Total: 2 [19.99 GiB] / in use: 2 [19.99 GiB] / in no VG: 0 [0 ]
[root@localhost ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name centos
PV Size <19.00 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 4863
Free PE 0
Allocated PE 4863
PV UUID TSqMcx-M7zq-jYB4-kn1t-EFZW-OVEL-kF2ZoY
--- Physical volume ---
PV Name /dev/sdb1
VG Name vg0
PV Size 1023.00 MiB / not usable 3.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 255
Free PE 205
Allocated PE 50
PV UUID Gf2LgS-Ycc2-2SCf-EFfk-h2Wd-AaJi-gT3inx
2. vg查看
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- <19.00g 0
vg0 1 1 0 wz--n- 1020.00m 820.00m
[root@localhost ~]# vgscan
Reading volume groups from cache.
Found volume group "centos" using metadata type lvm2
Found volume group "vg0" using metadata type lvm2
[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name centos
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size <19.00 GiB
PE Size 4.00 MiB
Total PE 4863
Alloc PE / Size 4863 / <19.00 GiB
Free PE / Size 0 / 0
VG UUID YevWvs-qjI3-0dph-sjCP-r26i-Klfv-knoS0I
--- Volume group ---
VG Name vg0
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 1020.00 MiB
PE Size 4.00 MiB
Total PE 255
Alloc PE / Size 50 / 200.00 MiB
Free PE / Size 205 / 820.00 MiB
VG UUID w3n5Dy-4d12-us2i-fcWk-pq14-bH6P-g2I7MV
3. lv查看
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- <17.00g
swap centos -wi-ao---- 2.00g
lv0 vg0 -wi-ao---- 200.00m
[root@localhost ~]# lvscan
ACTIVE '/dev/centos/swap' [2.00 GiB] inherit
ACTIVE '/dev/centos/root' [<17.00 GiB] inherit
ACTIVE '/dev/vg0/lv0' [200.00 MiB] inherit
[root@localhost ~]# lvdisplay
--- Logical volume ---
LV Path /dev/centos/swap
LV Name swap
VG Name centos
LV UUID MDFN3t-zofh-4Pdg-t90Y-ZXf8-qPKt-aUHgBb
LV Write Access read/write
LV Creation host, time bogon, 2023-12-05 20:05:31 -0500
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
--- Logical volume ---
LV Path /dev/centos/root
LV Name root
VG Name centos
LV UUID fs9qbg-PgA3-Oe6Q-wbE1-sVtN-D7ZQ-4HLDiw
LV Write Access read/write
LV Creation host, time bogon, 2023-12-05 20:05:31 -0500
LV Status available
# open 1
LV Size <17.00 GiB
Current LE 4351
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
--- Logical volume ---
LV Path /dev/vg0/lv0
LV Name lv0
VG Name vg0
LV UUID 3afbWV-GvsV-aNtZ-pv2M-UkgZ-SetM-UIWGYl
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2024-03-19 05:21:32 -0400
LV Status available
# open 1
LV Size 200.00 MiB
Current LE 50
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2
LVM扩容
通过交换PE的方式,达到弹性变更文件系统大小
将PE添加的LV逻辑卷,就可以扩容LV容量
剔除原有的PE,LV逻辑卷的容量会变小
默认PE大小是4M,LVM最多有65534个PE,所以LVM最大的VG是256G单位
vg管理
#注意:如果lv所在的vg有空间直接扩容就ok了
VG扩容
1. 创建pv
[root@localhost ~]# pvcreate /dev/sdc1
Physical volume "/dev/sdc1" successfully created.
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <19.00g 0
/dev/sdb1 vg0 lvm2 a-- 1020.00m 820.00m
/dev/sdc1 lvm2 --- <2.00g <2.00g
2. 扩大vg
[root@localhost ~]# vgextend vg0 /dev/sdc1 ----添加物理卷来增加容量
Volume group "vg0" successfully extended
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- <19.00g 0
vg0 2 1 0 wz--n- 2.99g <2.80g
vg缩容
1. vg在线缩容
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- <19.00g 0
vg0 2 1 0 wz--n- 2.99g 1.17g
vg1 2 1 0 wz--n- 8.99g 6.99g
[root@localhost ~]# vgreduce vg1 /dev/sde1 ----清除物理卷来减小容量
Removed "/dev/sde1" from volume group "vg1"
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- <19.00g 0
vg0 2 1 0 wz--n- 2.99g 1.17g
vg1 1 1 0 wz--n- <5.00g <3.00g
lv缩容
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- <17.00g
swap centos -wi-ao---- 2.00g
lv0 vg0 -wi-ao---- 8.00g
[root@localhost ~]#
[root@localhost ~]# lvreduce -L 4G /dev/vg0/lv0
WARNING: Reducing active and open logical volume to 4.00 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg0/lv0? [y/n]: y
Size of logical volume vg0/lv0 changed from 8.00 GiB (2048 extents) to 4.00 GiB (1024 extents).
Logical volume vg0/lv0 successfully resized.
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- <17.00g
swap centos -wi-ao---- 2.00g
lv0 vg0 -wi-ao---- 4.00g
lv扩容
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- <19.00g 0
vg0 2 1 0 wz--n- 2.99g <2.80g
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- <17.00g
swap centos -wi-ao---- 2.00g
lv0 vg0 -wi-ao---- 200.00m
[root@localhost ~]# lvextend -L 800M /dev/vg0/lv0 --- 扩展到800M
Size of logical volume vg0/lv0 changed from 200.00 MiB (50 extents) to 800.00 MiB (200 extents).
Logical volume vg0/lv0 successfully resized.
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- <17.00g
swap centos -wi-ao---- 2.00g
lv0 vg0 -wi-ao---- 800.00m
[root@localhost ~]# lvextend -L +1G /dev/vg0/lv0 --- 在原有的基础上增加1G
Size of logical volume vg0/lv0 changed from 800.00 MiB (200 extents) to 1.78 GiB (456 extents).
Logical volume vg0/lv0 successfully resized.
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- <17.00g
swap centos -wi-ao---- 2.00g
lv0 vg0 -wi-ao---- 1.78g
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- <17.00g
swap centos -wi-ao---- 2.00g
lv0 vg0 -wi-ao---- 1.78g
[root@localhost ~]# lvextend -l +10 /dev/vg0/lv0 在原有基础上加10个PE
Size of logical volume vg0/lv0 changed from 1.78 GiB (456 extents) to 1.82 GiB (466 extents).
Logical volume vg0/lv0 successfully resized.
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- <17.00g
swap centos -wi-ao---- 2.00g
lv0 vg0 -wi-ao---- 1.82g
FS(file system)文件系统扩容
[root@linux-server ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/vg1-lv1 xfs 149M 7.8M 141M 6% /mnt/lv1
/dev/mapper/vg1-lv2 ext4 74M 1.6M 67M 3% /mnt/lv2
[root@linux-server ~]# xfs_growfs /dev/vg1/lv1 #xfs扩容
[root@linux-server ~]# resize2fs /dev/vg1/lv2 #ext4扩容
[root@linux-server ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/vg1-lv1 xfs 1.7G 9.1M 1.7G 1% /mnt/lv1
/dev/mapper/vg1-lv2 ext4 74M 1.6M 67M 3% /mnt/lv2
删除lvm
1. 停止应用程序对该相应文件系统的访问
2. 卸载文件系统,包括/etc/fstab
3. lvremove
4. vgremove
5. pvremove
6. 删除分区
7. 踢除硬盘
注:顺序不要错误,否则硬盘可能出现错误
[root@localhost ~]# lvremove /dev/vg1/lv1
Do you really want to remove active logical volume vg1/lv1? [y/n]: y
Logical volume "lv1" successfully removed
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- <17.00g
swap centos -wi-ao---- 2.00g
lv0 vg0 -wi-a----- 1.82g
[root@localhost ~]# vgremove vg1
Volume group "vg1" successfully removed
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <19.00g 0
/dev/sdb1 vg0 lvm2 a-- 1020.00m 0
/dev/sdc1 vg0 lvm2 a-- <2.00g 1.17g
/dev/sde1 lvm2 --- <4.00g <4.00g
/dev/sdf1 lvm2 --- <5.00g <5.00g
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- <19.00g 0
vg0 2 1 0 wz--n- 2.99g 1.17g
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <19.00g 0
/dev/sdb1 vg0 lvm2 a-- 1020.00m 0
/dev/sdc1 vg0 lvm2 a-- <2.00g 1.17g
/dev/sde1 lvm2 --- <4.00g <4.00g
/dev/sdf1 lvm2 --- <5.00g <5.00g
[root@localhost ~]# pvremove /dev/sdf1
Labels on physical volume "/dev/sdf1" successfully wiped.
[root@localhost ~]# pvremove /dev/sde1
Labels on physical volume "/dev/sde1" successfully wiped.
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <19.00g 0
/dev/sdb1 vg0 lvm2 a-- 1020.00m 0
/dev/sdc1 vg0 lvm2 a-- <2.00g 1.17g
了解:
激活VG/LV :
[root@localhost ~]# lvchange -an /dev/vg01/lv01 //单独关闭激活某一个卷
[root@localhost ~]# lvscan
inactive '/dev/vg01/lv01' [128.00 MiB] inherit
ACTIVE '/dev/vg01/lv02' [128.00 MiB] inherit
ACTIVE '/dev/vg01/lv03' [128.00 MiB] inherit
[root@localhost ~]# vgchange -an vg01 //关闭激活某一个卷组,会影响下面的所有卷
0 logical volume(s) in volume group "vg01" now active
[root@localhost ~]# lvscan
inactive '/dev/vg01/lv01' [128.00 MiB] inherit
inactive '/dev/vg01/lv02' [128.00 MiB] inherit
inactive '/dev/vg01/lv03' [128.00 MiB] inherit
[root@localhost ~]# vgchange -ay(激活) vg01
[root@localhost ~]# lvscan
ACTIVE '/dev/vg01/lv01' [128.00 MiB] inherit
ACTIVE '/dev/vg01/lv02' [128.00 MiB] inherit
ACTIVE '/dev/vg01/lv03' [128.00 MiB] inherit
示例:
1. 创建pv
[root@anonymous ~]# pvcreate /dev/sdc1 /dev/sdd1
Physical volume "/dev/sdc1" successfully created.
Physical volume "/dev/sdd1" successfully created.
[root@anonymous ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <19.00g 0
/dev/sdb1 lvm2 --- 1023.00m 1023.00m
/dev/sdc1 lvm2 --- <2.00g <2.00g
/dev/sdd1 lvm2 --- <3.00g <3.00g
2. 创建vg
[root@anonymous ~]# vgcreate vg2 /dev/sdc1 /dev/sdd1
Volume group "vg2" successfully created
[root@anonymous ~]#
[root@anonymous ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- <19.00g 0
vg2 2 0 0 wz--n- 4.99g 4.99g
3. 创建lv
[root@anonymous ~]# lvcreate -L 4G -n lv2 vg2 --- 创建大小4G的lv
Logical volume "lv2" created.
[root@anonymous ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- <17.00g
swap centos -wi-ao---- 2.00g
lv2 vg2 -wi-a----- 4.00g
4. 扩容vg
[root@anonymous ~]# pvcreate /dev/sde1
Physical volume "/dev/sde1" successfully created.
[root@anonymous ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <19.00g 0
/dev/sdb1 lvm2 --- 1023.00m 1023.00m
/dev/sdc1 vg2 lvm2 a-- <2.00g 1016.00m
/dev/sdd1 vg2 lvm2 a-- <3.00g 0
/dev/sde1 lvm2 --- <4.00g <4.00g
[root@anonymous ~]# vgextend vg2 /dev/sde1
Volume group "vg2" successfully extended
[root@anonymous ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- <19.00g 0
vg2 3 1 0 wz--n- <8.99g <4.99g
5. 扩容lv大小为7G
[root@anonymous ~]# lvextend -L +3G /dev/vg2/lv2
Size of logical volume vg2/lv2 changed from 4.00 GiB (1024 extents) to 7.00 GiB (1792 extents).
Logical volume vg2/lv2 successfully resized.
[root@anonymous ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- <17.00g
swap centos -wi-ao---- 2.00g
lv2 vg2 -wi-a----- 7.00g
6. 缩小lv
[root@anonymous ~]# lvreduce -L -3G /dev/vg2/lv2
WARNING: Reducing active logical volume to 4.00 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg2/lv2? [y/n]: yes
Size of logical volume vg2/lv2 changed from 7.00 GiB (1792 extents) to 4.00 GiB (1024 extents).
Logical volume vg2/lv2 successfully resized.
[root@anonymous ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- <17.00g
swap centos -wi-ao---- 2.00g
lv2 vg2 -wi-a----- 4.00g
7. 缩小vg
[root@anonymous ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- <19.00g 0
vg2 3 1 0 wz--n- <8.99g <4.99g
[root@anonymous ~]# vgreduce vg2 /dev/sde1
Removed "/dev/sde1" from volume group "vg2"
[root@anonymous ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- <19.00g 0
vg2 2 1 0 wz--n- 4.99g 1016.00m
8. 删除lvs
[root@anonymous ~]# lvremove /dev/vg2/lv2
Do you really want to remove active logical volume vg2/lv2? [y/n]: yes
Logical volume "lv2" successfully removed
[root@anonymous ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- <17.00g
swap centos -wi-ao---- 2.00g
[root@anonymous ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- <19.00g 0
vg2 2 0 0 wz--n- 4.99g 4.99g
[root@anonymous ~]# vgremove vg2
Volume group "vg2" successfully removed
[root@anonymous ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- <19.00g 0
[root@anonymous ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <19.00g 0
/dev/sdb1 lvm2 --- 1023.00m 1023.00m
/dev/sdc1 lvm2 --- <2.00g <2.00g
/dev/sdd1 lvm2 --- <3.00g <3.00g
/dev/sde1 lvm2 --- <4.00g <4.00g
[root@anonymous ~]# pvremove /dev/sde1
Labels on physical volume "/dev/sde1" successfully wiped.
[root@anonymous ~]# pvremove /dev/sdd1
Labels on physical volume "/dev/sdd1" successfully wiped.
[root@anonymous ~]# pvremove /dev/sdc1
Labels on physical volume "/dev/sdc1" successfully wiped.
[root@anonymous ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <19.00g 0
/dev/sdb1 lvm2 --- 1023.00m 1023.00m
LVM快照
交换分区
交换分区管理 Swap---也叫虚拟内存
作用: ‘提升‘ 内存的容量,防止OOM(Out Of Memory)
现象是当内存不够的时候内核会随机杀死进程,它认为占用内存多的进程。(内核会先删除占用内存多的进程).
如何制作交换分区?
#我们可以将硬盘空间拿来当作内存来用
什么时候用到交换分区?
#当物理内存不够用的时候使用swap分区,防止物理内存耗尽
扩展知识
swap分区大小设置规则
在Linux系统,我们可以参照Redhat公司为RHEL5、RHEL6推荐的SWAP空间的大小划分原则,在你没有其他特别需求时,可以作为很好的参考依据。
内存小于4GB时,推荐不少于2GB的swap空间;
内存4GB~16GB,推荐不少于4GB的swap空间;
内存16GB~64GB,推荐不少于8GB的swap空间;
内存64GB~256GB,推荐不少于16GB的swap空间。
一、查看当前的交换分区
[root@anonymous ~]# free -m
total used free shared buff/cache available
Mem: 972 132 686 7 153 672
Swap: 2047 0 2047
[root@anonymous ~]# swapon -s
Filename Type Size Used Priority
/dev/dm-1 partition 2097148 0 -2
[root@anonymous ~]# cat /proc/swaps
Filename Type Size Used Priority
/dev/dm-1 partition 2097148 0 -2
二、增加交换分区 可以是基本分区,LVM,File
[root@anonymous ~]# fdisk /dev/sdc
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):
Using default response p
Partition number (1-4, default 1):
First sector (2048-4194303, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-4194303, default 4194303):
Using default value 4194303
Partition 1 of type Linux and of size 2 GiB is set
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): l
0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT-
2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT-
3 XENIX usr 3c PartitionMagic 84 OS/2 hidden C: c6 DRDOS/sec (FAT-
4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx
5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data
6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / .
7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility
8 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt
9 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access
a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O
b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor
c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi eb BeOS fs
e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD ee GPT
f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ef EFI (FAT-12/16/
10 OPUS 55 EZ-Drive a7 NeXTSTEP f0 Linux/PA-RISC b
11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f1 SpeedStor
12 Compaq diagnost 5c Priam Edisk a9 NetBSD f4 SpeedStor
14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f2 DOS secondary
16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ fb VMware VMFS
17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fc VMware VMKCORE
18 AST SmartSleep 65 Novell Netware b8 BSDI swap fd Linux raid auto
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fe LANstep
1c Hidden W95 FAT3 75 PC/IX be Solaris boot ff BBT
1e Hidden W95 FAT1 80 Old Minix
Hex code (type L to list all codes): 82
Changed type of partition 'Linux' to 'Linux swap / Solaris'
Command (m for help): p
Disk /dev/sdc: 2147 MB, 2147483648 bytes, 4194304 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: 0x6d23fab3
Device Boot Start End Blocks Id System
/dev/sdc1 2048 4194303 2096128 82 Linux swap / Solaris
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@anonymous ~]# partprobe /dev/sdc1 ---- 内核重新读取分区表
[root@anonymous ~]# ll /dev/sdc1
brw-rw----. 1 root disk 8, 33 Mar 19 10:32 /dev/sdc1
初始化
[root@anonymous ~]# mkswap /dev/sdc1
Setting up swapspace version 1, size = 2096124 KiB
no label, UUID=eb779c3e-557c-490d-951e-6cfcc7935706
挂载
[root@anonymous ~]# blkid /dev/sdc1
/dev/sdc1: UUID="eb779c3e-557c-490d-951e-6cfcc7935706" TYPE="swap"
[root@linux-server ~]# vim /etc/fstab #制作开机挂载
/dev/sdc1 swap swap defaults 0 0
[root@linux-server ~]# swapon -a #激活所有的swap分区(读取/etc/fstab)
[root@linux-server ~]# swapon -s # 查看交换分区信息
Filename Type Size Used Priority
/dev/dm-1 partition 2097148 0 -1
/dev/sdc1 partition 5242876 0 -2
#swapoff /dev/sdd1 #关闭swap分区
/etc/fatab
第一个字段:分区设备文件名或UUID,可以是硬盘的ID。
第二个字段:挂载点,应该是已经建立的空目录。
第三个字段:文件系统名称,如ext4。
第四个字段:挂载参数,与`mount`命令的挂载参数一致。
第五个字段:指定分区是否被`dump`备份,0代表不备份,1代表备份,2代表不定期备份。
第六个字段:指定分区是否被`fsck`检测,0代表不检测,其他数字代表检测的优先级,1的优先级比2高。
file制作
[root@linux-server ~]# dd if=/dev/zero of=/swap2.img bs=1M count=512
解释:dd 读入从空设备里面拿空块 到交换分区 块多大 一共多少兆
[root@linux-server ~]# mkswap /swap2.img #初始化
[root@linux-server ~]# vim /etc/fstab
/swap2.img swap swap defaults 0 0
[root@linux-server ~]# chmod 600 /swap2.img #交换分区权限需要设置为600,默认644权限不安全。
[root@linux-server ~]# swapon -a
[root@linux-server ~]# swapon -s
Filename Type Size Used Priority
/dev/dm-1 partition 2097148 0 -1
/dev/sdd1 partition 5242876 0 -2
/swap2.img file 524284 0 -3
mount详解
每一个文件系统都有独立的inode、block、superblock等信息,这个文件系统要能够链接到目录才能被我们使用,将文件系统与目录结合的动作,我们就称之为挂载
常用挂载选项详解
挂载设备文件名、UUID
常见的挂载选项
-t 文件系统类型
-o 指定文件系统属性
rw 读写
ro 只读
noexec 不允许执行二进制文件
exec 允许执行二进制文件
auto mount -a 开机自动挂载
noauto 开机不自动挂载
remount 在线重新挂载
命令
[root@linux-server ~]# mount #查看已经挂载上的系统的属性
案例一:exec/noexec
[root@linux-server ~]# mount /dev/vg1/lv1 /mnt/lv1/ #挂载默认是有执行权限的
[root@linux-server ~]# mount -o noexec /dev/vg1/lv2 /mnt/lv2 #不允许执行二进制文件
[root@linux-server ~]# cat /mnt/lv1/hello.sh
#!/bin/bash
echo "hello"
[root@linux-server ~]# cat /mnt/lv2/hello2.sh
#!/bin/bash
echo "hello"
[root@linux-server ~]# chmod +x /mnt/lv1/hello.sh
[root@linux-server ~]# chmod +x /mnt/lv2/hello2.sh
[root@linux-server ~]# /mnt/lv1/hello.sh
hello
[root@linux-server ~]# /mnt/lv2/hello2.sh
-bash: /mnt/lv2/hello2.sh: Permission denied
案例二: rw/ro
[root@centos ~]# mount /dev/vg01/lv02 /mnt/lv02/
[root@centos ~]# mount -o ro /dev/vg01/lv02 /mnt/lv02/
[root@centos lv01]# touch aaa
[root@centos lv01]# ls
lost+found aaa
[root@centos lv02]# touch aaa
touch: 无法创建"aaa": 只读文件系统
[root@centos-9 lv02]# rm -rf lost+found/
rm: 无法删除"lost+found": 只读文件系统
取消挂载
[root@linux-server ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/vg1-lv1 xfs 1.7G 34M 1.7G 2% /mnt/lv1
/dev/mapper/vg1-lv2 ext4 74M 1.6M 67M 3% /mnt/lv2
[root@linux-server ~]# umount /mnt/lv2/
raid磁盘阵列
介绍
RAID ( Redundant Array of Independent Disks )廉价磁盘冗余阵列。即独立磁盘冗余阵列,通常简称为磁盘阵列。简单地说, RAID 是由多个独立的高性能磁盘驱动器组成的磁盘子系统,从而提供比单个磁盘更高的存储性能和数据冗余的技术。
作用
容错、提升读写速率
分类
raid0
原理:将数据分散到两块硬盘里
1.读写性能高
2.没有数据备份功能
3.最少两块硬盘,偶数快
raid1
1.互为备份(镜像存储)
2.写性能差一些,读性能一般
3.高可用,坏一块不影响数据
4.硬盘数量为偶数(最小两块)
raid5
1.硬盘数量>=3
2.读性能高,写性能不高
3.可以坏掉一块硬盘,不影响业务
4.数据校验机制
raid6
奇偶校验(parity check)是一种简单的错误检测和纠正技术。
1.硬盘数>=4,最少4个硬盘
2.奇偶校验,校验位
3.读性能与RAID5相同,但写性能比RAID5更差
4.理论上可坏掉两个硬盘,不影响业务
raid01
raid10
RAID10可以看作RAID1与RAID0的结合体。有着双方共同的优点。(先做镜像再做条带)
1.硬盘数>=4,硬盘数为偶数,磁盘利用率50%
2.读性能高,写性能稍差
3.理论上可以坏三个硬盘,允许两个机组中各坏一个
硬盘数 硬盘利用率 读性能 写性能 高可用 特点
raid0 N(偶数块) N 100% 高 高 无 追求最大容量和读写速度,坏一块硬盘,数据异常。
raid1 N(偶数块) N/2 50% 低 低 中等 追求安全,只坏一块硬盘,不影像数据
raid5 n>=3 (n-1)/n 高 低 高 成本控制前提下,追求最大的容量,速度,以及高可用,可以坏掉一块硬盘
raid6 n>=4 (n-2)/n 高 低 很高 比raid的数据安全更高,但是写的速度比较低,可以坏掉两块硬盘
raid10 N>=4(偶数块) N/2 50% 高 中等 中等 综合raid1与raid0,即有速度,也有高可用,每个基组可以坏掉一个,数据不受影响
热盘点
1.相当RAID列阵的备份
2.当RAID阵列坏掉的数量超出RAID列阵最大限制,可加了热备盘,
3.热备盘可以临时顶替换掉的盘,相当于最后磁盘的一层保险。
不同场景raid的使用
RAID 实现方式
硬RAID: 需要RAID卡,有自己的CPU,处理速度快
软RAID: 通过操作系统实现,比如Windows、Linux
raid示例
raid0
需要2块盘
前提: 分区----system id -- fd
1. 准备至少两块盘
[root@localhost ~]# fdisk -l
设备 Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 fd Linux raid autodetect
/dev/sdb2 2099200 4196351 1048576 fd Linux raid autodetect
/dev/sdb3 4196352 6293503 1048576 fd Linux raid autodetect
/dev/sdb4 6293504 7317503 512000 5 Extended
/dev/sdb5 6295552 6705151 204800 fd Linux raid autodetect
/dev/sdb6 6707200 7116799 204800 fd Linux raid autodetect
2. 创建raid0
[root@localhost ~]# mdadm -C /dev/md0 -l 0 -n 2 /dev/sdb{1,2}
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.
3. 查看扫描信息
[root@localhost ~]# mdadm -D /dev/md0
/dev/md0:
Version : 1.2
Creation Time : Wed Apr 17 13:54:23 2024
Raid Level : raid0
Array Size : 2093056 (2044.00 MiB 2143.29 MB)
Raid Devices : 2
Total Devices : 2
Persistence : Superblock is persistent
Update Time : Wed Apr 17 13:54:23 2024
State : clean
Active Devices : 2
Working Devices : 2
Failed Devices : 0
Spare Devices : 0
Chunk Size : 512K
Consistency Policy : none
Name : localhost.localdomain:0 (local to host localhost.localdomain)
UUID : 0199dc2a:c7c8e60c:a74ddc25:96103d50
Events : 0
Number Major Minor RaidDevice State
0 8 17 0 active sync /dev/sdb1
1 8 18 1 active sync /dev/sdb2
4. 格式化挂载使用
[root@localhost ~]# mkfs.xfs /dev/md0
[root@localhost ~]# mount /dev/md0 /mnt/
raid5
需3块盘+1块备盘
1. 准备4块盘
[root@localhost ~]# ll /dev/sd*
brw-rw----. 1 root disk 8, 16 Apr 15 05:13 /dev/sdb
brw-rw----. 1 root disk 8, 32 Apr 15 05:13 /dev/sdc
brw-rw----. 1 root disk 8, 48 Apr 15 05:13 /dev/sdd
brw-rw----. 1 root disk 8, 64 Apr 15 05:13 /dev/sde
2. 创建raid5
[root@localhost ~]# mdadm -C /dev/md5 -l 5 -n 3 -x 1 /dev/sdb3 /dev/sdc{1,2,3}
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md5 started.
-C 创建RAID
/dev/md0 第一个RAID设备
-l5 RAID5
-n RAID成员的数量
-x 热备磁盘的数量
3. 查看raid信息
[root@localhost ~]# mdadm -D /dev/md05
/dev/md0:
Version : 1.2
Creation Time : Mon Apr 15 05:17:24 2024
Raid Level : raid5
Array Size : 20953088 (19.98 GiB 21.46 GB)
Used Dev Size : 10476544 (9.99 GiB 10.73 GB)
Raid Devices : 3
Total Devices : 4
Persistence : Superblock is persistent
Update Time : Mon Apr 15 05:17:48 2024
State : clean
Active Devices : 3
Working Devices : 4
Failed Devices : 0
Spare Devices : 1
Layout : left-symmetric
Chunk Size : 512K
Consistency Policy : resync
Name : localhost.localdomain:0 (local to host localhost.localdomain)
UUID : 90109ea9:f4546202:b4d5f651:d86c1c47
Events : 18
Number Major Minor RaidDevice State
0 8 16 0 active sync /dev/sdb
1 8 32 1 active sync /dev/sdc
4 8 48 2 active sync /dev/sdd
3 8 64 - spare /dev/sde ----系统选择的备用盘
4. 显示扫描信息
[root@localhost ~]# mdadm -Ds
ARRAY /dev/md0 metadata=1.2 spares=1 name=localhost.localdomain:0 UUID=90109ea9:f4546202:b4d5f651:d86c1c47
[root@disk ~]# cat /proc/mdstat >>>查看RAID设备的状态信息
5. 格式化挂载使用
[root@localhost ~]# mkfs.xfs /dev/md0
[root@localhost ~]# mount /dev/md0 /mnt/
6. 设置开机生效
[root@localhost ~]# mdadm -Ds > /etc/mdadm.conf
7. 模拟损坏一块盘,并移除
终端一:
[root@localhost ~]# watch -n 0.5 'mdadm -D /dev/md0'
终端二:
[root@localhost ~]# mdadm /dev/md0 -f /dev/sde -r /dev/sde
mdadm: set /dev/sde faulty in /dev/md0
mdadm: hot removed /dev/sde from /dev/md0
-f --fail
-r --remove
-----持续关注终端一的变化
8. 为md0添加新的备用盘
[root@localhost ~]# mdadm /dev/md0 -a /dev/sde
mdadm: added /dev/sde
[root@localhost ~]# mdadm /dev/md0 -a /dev/sdf
mdadm: added /dev/sdf
9. 查看raid5信息
[root@localhost ~]# mdadm -D /dev/md0
/dev/md0:
...
Number Major Minor RaidDevice State
0 8 16 0 active sync /dev/sdb
3 8 64 1 active sync /dev/sde
4 8 48 2 active sync /dev/sdd
5 8 32 - spare /dev/sdc
6 8 80 - spare /dev/sdf
10. 删除raid
[root@localhost ~]# umount /mnt/
[root@localhost ~]# mdadm -S /dev/md0 --- 停止raid
mdadm: stopped /dev/md0
[root@localhost ~]# rm -rf /dev/md0
[root@localhost ~]# mdadm --zero-superblock /dev/sdb
[root@localhost ~]# mdadm --zero-superblock /dev/sdc
[root@localhost ~]# mdadm --zero-superblock /dev/sde
[root@localhost ~]# mdadm --zero-superblock /dev/sdd
[root@localhost ~]# rm -rf /etc/mdadm.conf
[root@localhost ~]# mdadm -A /dev/md0 /dev/sdb{1,2,3} ---激活raid
思考:
raid1该如何制作使用?
作业:
实验要求:
# 分5个分区 创建一个raid5 和 一个raid0
[root@localhost ~]# mdadm -C /dev/md0 -l 0 -n 2 /dev/sdb{1,2}
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.
[root@localhost ~]# mdadm -C /dev/md5 -l 5 -n 3 -x 1 /dev/sdb3 /dev/sdc{1,2,3}
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md5 started.
将raid5和raid0做成lv0
实现lv0的自动挂载
删除lv 删除raid5和raid0
标签:管理,分区,bytes,dev,linux,磁盘,512,root,localhost
From: https://www.cnblogs.com/fenglei7093/p/18337405