存储管理
LVM
- 准备物理磁盘
可以是: /dev/sdb /dev/sdc1
[root@server0 ~]# ll /dev/vd{c,d,e}
brw-rw—-. 1 root disk 253, 32 Jun 6 17:38 /dev/vdc
brw-rw—-. 1 root disk 253, 48 Jun 6 17:38 /dev/vdd
brw-rw—-. 1 root disk 253, 64 Jun 6 17:38 /dev/vde
- 创建pv
[root@server0 ~]# pvcreate /dev/vdd
Physical volume “/dev/vdd” successfully created
[root@server0 ~]# pvscan
PV /dev/vdd lvm2 [2.00 GiB]
Total: 1 [2.00 GiB] / in use: 0 [0 ] / in no VG: 1 [2.00 GiB]
[root@server0 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/vdd lvm2 a– 2.00g 2.00g
- 创建vg
[root@server0 ~]# vgcreate vg1 /dev/vdd
Volume group “vg1” successfully created
[root@server0 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
vg1 1 0 0 wz–n- 2.00g 2.00g
[root@server0 ~]# vgscan
Reading all physical volumes. This may take a while…
Found volume group “vg1” using metadata type lvm2
[root@server0 ~]# vgdisplay
— Volume group —
VG Name vg1
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 1
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 1
Act PV 1
VG Size 2.00 GiB
PE Size 4.00 MiB
Total PE 511
Alloc PE / Size 0 / 0
Free PE / Size 511 / 2.00 GiB
VG UUID 7E4tlj-l0a2-ph52-OytH-eaq7-58K6-2S4n8V
- 创建lv
[root@server0 ~]# lvcreate -l 10 -n lv1 vg1
[root@server0 ~]# lvcreate -L 200M -n lv2 vg1
[root@server0 ~]# lvscan
ACTIVE ‘/dev/vg1/lv1’ [640.00 MiB] inherit
ACTIVE ‘/dev/vg1/lv2’ [256.00 MiB] inherit
- 创建文件系统并挂载
[root@server0 ~]# mkfs.xfs /dev/vg1/lv1
[root@server0 ~]# mkfs.ext4 /dev/vg1/lv2
[root@server0 ~]# mkdir /mnt/lv1 /mnt/lv2
[root@server0 ~]# vim /etc/fstab
/dev/vg1/lv1 /mnt/lv1 xfs defaults 0 0
/dev/vg1/lv2 /mnt/lv2 ext4 defaults 0 0
[root@server0 ~]# mount -a
[root@server0 ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/vg1-lv1 651948 32928 619020 6% /mnt/lv1
/dev/mapper/vg1-lv2 245671 2062 226406 1% /mnt/lv2
VG管理
扩大VG vgextend
- 创建pv
[root@server0 ~]# pvcreate /dev/vde
- 创建vg vgextend
[root@server0 ~]# vgextend vg1 /dev/vde
Volume group “vg1” successfully extended
[root@server0 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
vg1 2 2 0 wz–n- 3.99g 3.76g
减小VG vgreduce
通常先做数据的迁移
1.查看当前的VG中PV的使用情况
[root@server0 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/vdd vg1 lvm2 a– 2.00g 1.76g
/dev/vde vg1 lvm2 a– 2.00g 2.00g
- pvmove数据到其它PV
[root@server0 ~]# pvmove /dev/vdd
/dev/vdd: Moved: 16.7%
/dev/vdd: Moved: 100.0%
[root@server0 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/vdd vg1 lvm2 a– 2.00g 2.00g
/dev/vde vg1 lvm2 a– 2.00g 1.76g
3.vgreduce VG
[root@server0 ~]# vgreduce vg1 /dev/vdd
Removed “/dev/vdd” from volume group “vg1″
[root@server0 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
vg1 1 2 0 wz–n- 2.00g 1.76g
LV扩容
- lv扩容
[root@server0 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
vg1 2 2 0 wz–n- 1.88g 1.00g
[root@server0 ~]# lvextend -L 800M /dev/vg1/lv1
[root@server0 ~]# lvextend -L +800M /dev/vg1/lv1
[root@server0 ~]# lvextend -l 15 /dev/vg1/lv1
[root@server0 ~]# lvextend -l +15 /dev/vg1/lv1
=======================================
+50%FREE
=======================================
[root@server0 ~]# lvscan
ACTIVE ‘/dev/vg1/lv1’ [768.00 MiB] inherit
ACTIVE ‘/dev/vg1/lv2’ [512.00 MiB] inherit
- FS扩容
[root@server0 ~]# df -Th
/dev/mapper/vg1-lv1 xfs 637M 67M 570M 11% /mnt/lv1
/dev/mapper/vg1-lv2 ext4 240M 32M 192M 15% /mnt/lv2
a. xfs
[root@server0 ~]# xfs_growfs /dev/vg1/lv1
b. ext2/3/4
[root@server0 ~]# resize2fs /dev/vg1/lv2
[root@server0 ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/vg1-lv1 xfs 765M 67M 698M 9% /mnt/lv1
/dev/mapper/vg1-lv2 ext4 488M 32M 429M 7% /mnt/lv2
交换分区swap
交换分区管理 Swap
作用:'提升'内存的容量,防止OOM(Out Of Memory)
查看当前的交换分区
[root@server0 ~]# free -m
total used free shared buff/cache available
Mem: 489 140 145 4 202 304
Swap: 0 0 0
[root@server0 ~]# swapon -s
增加交换分区(可以是基本分区,LVM,File)
分区
- 准备分区
[root@server0 ~]# fdisk /dev/vdb (t转换分区的ID 82)
[root@server0 ~]# partprobe /dev/vdb
[root@server0 ~]# ll /dev/vdb*
brw-rw—-. 1 root disk 253, 16 12月 6 10:18 /dev/vdb
brw-rw—-. 1 root disk 253, 17 12月 6 10:18 /dev/vdb1
2.初始化
[root@server0 ~]# mkswap /dev/vdb1
3.挂载
[root@server0 ~]# blkid /dev/vdb1
/dev/vdb1: UUID=”ea5b1c77-e540-463c-9644-0d75450f8b4c” TYPE=”swap”
[root@server0 ~]# vim /etc/fstab
UUID=”ea5b1c77-e540-463c-9644-0d75450f8b4c” swap swap default 0 0
[root@server0 ~]# swapon -a (读取/etc/fstab)
[root@server0 ~]# swapon -s
Filename Type Size Used Priority
/dev/vdb1 partition 524284 0 -1
File
[root@server0 ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/vda1 xfs 10G 3.3G 6.8G 33% /
[root@server0 ~]# dd if=/dev/zero of=/swap2.img bs=1M count=512
或者
[root@server0 ~]# dd </dev/zero >/swap2.img bs=1M count=512
[root@server0 ~]# mkswap /swap2.img
[root@server0 ~]# vim /etc/fstab
/swap2.img swap swap default 0 0
[root@server0 ~]# swapon -a
swapon: /swap2.img: insecure permissions 0644, 0600 suggested.
[root@server0 ~]# chmod 600 /swap2.img
[root@server0 ~]# swapon -a
[root@server0 ~]# swapon -s
Filename Type Size Used Priority
/dev/vdb1 partition 524284 0 -1
/swap2.img file 524284 0 -2
对交换分区调优 [扩展iu]
多个交换分区分布在不同的磁盘
挂载时,使用相同的优先级
[root@server0 ~]# vim /etc/fstab
UUID=”ea5b1c77-e540-463c-9644-0d75450f8b4c” swap swap defaults,pri=1 0 0
UUID=”ea5b1c77-e540-465c-9644-0d75457f8b45″ swap swap defaults,pri=1 0 0
[root@server0 ~]# swapon -a
[root@server0 ~]# swapon -s
Filename Type Size Used Priority
dev/sdc1 partition 524284 0 1
挂载mount
mount -t 文件系统类型 -o 选项 /dev/sdb1 /mnt/disk1
mount -t ext4 -o rw /dev/sdb1 /mnt/disk1 //将sdb1挂载到disk1,权限是rw,类型是ext4
文件系统类型:vfat,ext4,xfs,nfs,cifs,iso9660,loop
Example:
1、exec/noexec
[root@test ~]# mount /dev/vg01/lv01 /mnt/lv01
[root@test ~]# mount -o noexec /dev/vg01/lv02 /mnt/lv02
[root@test ~]# cp -rf /bin/date /mnt/lv01
[root@test ~]# cp -rf /bin/date /mnt/lv02
[root@test ~]# /mnt/lv01/date
Fri Jan 10 10:40:21 CST 2019
[root@test ~]# /mnt/lv02/date
-bash: /mnt/lv02/date: Permission denied
[root@test ~]# mount
/dev/mapper/vg01-lv01 on /mnt/lv01 type ext4 (rw)
/dev/mapper/vg01-lv02 on /mnt/lv02 type ext4 (rw,noexec)
没有指定任何选项,则为默认选项 man mount
defaults
Use default options: rw, suid, dev, exec, auto, nouser, and async.
常见的挂载选项
rw 读写
ro 只读
suid 支持suid
dev 支持设备文件
nodev 不支持设备文件
noexec 不允许执行二进制文件
exec 允许执行二进制文件
auto mount -a 开机自动挂载
noauto mount -a 开机不自动挂载
async 异步写入
sync 同步同入
usrquota 支持用户级磁盘配额功能
grpquota 支持组级磁盘配额功能
acl 支持facl功能
remount 在线重新挂载
pri 指定优先级
2、async,sync
[root@test ~]# mount /dev/vg01/lv01 /mnt/lv01
[root@test ~]# mount -o sync /dev/vg01/lv02 /mnt/lv02
[root@test ~]# mount
/dev/mapper/vg01-lv01 on /mnt/lv01 type ext4 (rw)
/dev/mapper/vg01-lv02 on /mnt/lv02 type ext4 (rw,sync)
[root@test ~]# time cp -rf /etc /mnt/lv01
[root@test ~]# time cp -rf /etc /mnt/lv02
3、挂载时可以使用:
设备名 /dev/sda3
卷标 LABEL=server
UUID UUID=”353a34b8-9233-465c-b7cf-92c8d308c05b”
UDEV 可以给设备建立一个别名
Example:
使用卷标进行挂载
[root@test ~]# tune2fs -l /dev/vda1 |less
Filesystem volume name: <none>
[root@test ~]# tune2fs -L server /dev/sda3 //设置卷标LABEL
tune2fs 1.39 (29-May-2006)
[root@test ~]# vim /etc/fstab
LABEL=server /mnt/disk1 ext4 defaults 0 0
使用UUID挂载(推荐)
[root@test ~]# blkid /dev/sda5
/dev/sda5: UUID=”353a34b8-9233-465c-b7cf-92c8d308c05b” SEC_TYPE=”ext2″ TYPE=”ext3″
[root@test ~]# vim /etc/fstab
UUID=”353a34b8-9233-465c-b7cf-92c8d308c05b” /mnt/disk1 ext4 defaults 0 0
使用设备名挂载
[root@test ~]# mount -t ext4 /dev/sdb1 /mnt //临时挂载,重启之后挂载失效
[root@test ~]# vim /etc/fstab //永久挂载,重启后还在
/dev/sdb1 /mnt ext4 defaults 0 0
[root@test ~]# mount -a //将fstab配置文件中的挂载全部挂载一遍