操作系统 centos7
一、RAID
yum install -y mdadm
md0重启后变成md127
vi /etc/mdadm.conf
1.
-l级别 -n 盘数
raid10
mdadm -Cv /dev/md0 -a yes -n 4 -l 10 /dev/sdb /dev/sdc
/dev/sdd /dev/sde
raid5
mdadm -Cv /dev/md0 -a yes -n 3 -l 5 -x /dev/sdb /dev/sdc
/dev/sdd /dev/sde
mdadm -S /dev/md0
mdadm -D /dev/md0
2.
mkdir /RAID
mount /dev/md0 /RAID
mount -a
df -h
mdadm -D /dev/md0
3.
echo "/dev/md0 /RAID ext4 defaults 0 0"
4.模拟损坏
mdadm /dev/md0 -f /dev/sdb
mdadm -D /dev/md0
umount /RAID
mdadm /dev/md0 -a /dev/sdb ---a类似于add
mdadm -D /dev/md0
二、LVM
/boot /dev/sda1xfs
/ /dev/mapper/centos-root xfs
swap /dev/mapper/centos-swapswap
sda中部分空间sda3创建逻辑卷,添加sdb扩充逻辑卷
/data /dev/mapper/vg01-lv01 xfs
1、创建逻辑卷并挂载
[root@centos7 ~]# df -h
Filesystem Size Used Avail Use% Mounted
on
/dev/mapper/centos-root 8.0G 5.0G 3.1G 62% /
devtmpfs 470M 0 470M 0% /dev
tmpfs 487M 20M 467M 4% /dev/shm
tmpfs 487M 9.1M 478M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda1 2.0G 166M 1.9G 9% /boot
tmpfs 98M 52K 98M 1%
/run/user/0
[root@centos7 ~]# fdisk -l
Disk /dev/sda: 16.1 GB, 16106127360 bytes, 31457280
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: 0x000ce56f
Device
Boot Start End Blocks Id System
/dev/sda1 * 2048 4196351 2097152 83 Linux
/dev/sda2 4196352 25184255 10493952 8e Linux LVM
Disk /dev/mapper/centos-root: 8589 MB, 8589934592 bytes,
16777216 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/mapper/centos-swap: 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
[root@centos7 ~]# fdisk /dev/sda
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 (2 primary, 0 extended, 2 free)
e extended
Select (default p): p
Partition number (3,4, default 3): 3
First sector (25184256-31457279, default
25184256):
Using default value 25184256
Last sector, +sectors or +size{K,M,G} (25184256-31457279,
default 31457279):
Using default value 31457279
Partition 3 of type Linux and of size 3 GiB is set
Command (m for help): t
Partition number (1-3, default 3): 3
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): p
Device
Boot Start End Blocks Id System
/dev/sda1 * 2048 4196351 2097152 83 Linux
/dev/sda2 4196352 25184255 10493952 8e Linux LVM
/dev/sda3 25184256 31457279 3136512 8e Linux LVM
Command (m for help): w
The partition table has been altered!
WARNING: Re-reading the partition table failed with error 16:
Device or resource busy.
The kernel still uses the old table. The new table will be
used at
the next reboot or after you run partprobe(8) or
kpartx(8)
Syncing disks.
[root@centos7 ~]# fdisk -l
Device
Boot Start End Blocks Id System
/dev/sda1 * 2048 4196351 2097152 83 Linux
/dev/sda2 4196352 25184255 10493952 8e Linux LVM
/dev/sda3 25184256 31457279 3136512 8e Linux LVM
[root@centos7 ~]# reboot
[root@centos7 ~]# pvcreate /dev/sda3
Physical volume "/dev/sda3" successfully
created.
[root@centos7 ~]# vgcreate vg01 /dev/sda3
Volume group "vg01" successfully
created
[root@centos7 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n-
10.00g 4.00m
vg01 1 0 0 wz--n-
<2.99g <2.99g
[root@centos7 ~]# lvcreate -n lv01 -L 3050M vg01
Rounding up size to full physical extent
2.98 GiB
Logical volume "lv01" created.
lvcreate -l 100%VG -n lv01 vg01 -- 创建新的 lv 把所有的vg
[root@centos7 ~]# mkfs.xfs /dev/vg01/lv01
meta-data=/dev/vg01/lv01 isize=512 agcount=4, agsize=195328 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0,
sparse=0
data = bsize=4096 blocks=781312, 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
[root@centos7 ~]# mkdir /data
[root@centos7 ~]# mount /dev/vg01/lv01 /data
[root@centos7 ~]# df -h
Filesystem Size Used Avail Use% Mounted
on
/dev/mapper/centos-root 8.0G 5.0G 3.1G 62% /
devtmpfs 470M 0 470M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 8.3M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda1 2.0G 166M 1.9G 9% /boot
tmpfs 98M 12K 98M 1%
/run/user/42
tmpfs 98M 0 98M 0%
/run/user/0
/dev/mapper/vg01-lv01 3.0G 33M 3.0G 2% /data
2、增加新盘扩逻辑卷
[root@centos7 ~]# 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
0x6e0a3a89.
Command (m for help): p
Disk /dev/sdb: 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: 0x6e0a3a89
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
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): p
Disk /dev/sdb: 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: 0x6e0a3a89
Device
Boot Start End Blocks Id System
/dev/sdb1 2048 4194303 2096128 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: 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: 0x6e0a3a89
Device
Boot Start End Blocks Id System
/dev/sdb1 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@centos7 ~]# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully
created.
[root@centos7 ~]# vgextend vg01 /dev/sdb1
Volume group "vg01" successfully
extended
[root@centos7 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n-
10.00g 4.00m
vg01 2 1 0
wz--n- 4.98g 2.00g
[root@centos7 ~]# mount /dev/vg01/lv01 /data
[root@centos7 ~]# df -h
Filesystem Size Used Avail Use% Mounted
on
/dev/mapper/centos-root 8.0G 5.0G 3.1G 62% /
devtmpfs 470M 0 470M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 8.3M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda1 2.0G 166M 1.9G 9% /boot
tmpfs 98M 8.0K 98M 1% /run/user/42
tmpfs 98M 0 98M 0%
/run/user/0
/dev/mapper/vg01-lv01 3.0G 33M 3.0G 2% /data
[root@centos7 ~]# lvextend -L +2048M
/dev/vg01/lv01
Size of logical volume vg01/lv01 changed
from 2.98 GiB (763 extents) to 4.98 GiB (1275 extents).
Logical volume vg01/lv01 successfully
resized.
lvextend -l +100%FREE /dev/vg01/lv01--
把lv 扩展成 新的 100% 范围
[root@centos7 ~]# df -h
Filesystem Size Used Avail Use% Mounted
on
/dev/mapper/centos-root 8.0G 5.0G 3.1G 62% /
devtmpfs 470M 0 470M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 8.3M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda1 2.0G 166M 1.9G 9% /boot
tmpfs 98M 8.0K 98M 1% /run/user/42
tmpfs 98M 0 98M 0%
/run/user/0
/dev/mapper/vg01-lv01 3.0G 33M 3.0G 2% /data
ext4 需要验证后再执行
--检测文件完整性
e2fsck
-f/dev/vg01/lv01
--生效
resize2fs/dev/vg01/lv01
[root@centos7 ~]# xfs_growfs
/dev/vg01/lv01 --xfs支持在线扩盘
meta-data=/dev/mapper/vg01-lv01 isize=512 agcount=4,
agsize=195328 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0
spinodes=0
data = bsize=4096 blocks=781312, 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 781312 to 1305600
备注:lvextend-r -L +2G /dev/vg01/lv01可一步搞定扩容,即加上-r
分区及空间均扩展
[root@centos7 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n-
10.00g 4.00m
vg01 2 1 0
wz--n- 4.98g 4.00m
[root@centos7 ~]# lvs
LV VG Attr LSize Pool Origin
Data% Meta% Move Log Cpy%Sync
Convert
root centos -wi-ao----
8.00g
swap centos -wi-ao----
2.00g
lv01 vg01 -wi-ao---- 4.98g
[root@centos7 ~]# df -h
Filesystem Size Used Avail Use% Mounted
on
/dev/mapper/centos-root 8.0G 5.0G 3.1G 62% /
devtmpfs 470M 0 470M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 8.3M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda1 2.0G 166M 1.9G 9% /boot
tmpfs 98M 12K 98M 1%
/run/user/42
tmpfs 98M 0 98M 0%
/run/user/0
/dev/mapper/vg01-lv01 5.0G 33M 5.0G 1% /data
[root@centos7 ~]# pvs
PV VG Fmt Attr
PSize PFree
/dev/sda2 centos lvm2
a-- 10.00g 4.00m
/dev/sda3 vg01 lvm2 a-- <2.99g 0
/dev/sdb1 vg01 lvm2 a-- <2.00g 4.00m
[root@centos7 ~]# blkid /dev/vg01/lv01
/dev/vg01/lv01: UUID="2adcfae7-db45-4133-9b04-f3fb470b1b1c"
TYPE="xfs"
[root@centos7 ~]# lsblk -f
NAME FSTYPE LABEL
UUID MOUNTPOINT
sda
├─sda1 xfs 0568a563-ff67-49f7-a89c-bacf086ce14a /boot
├─sda2 LVM2_mem LVLhTI-SOuG-Vthc-BNEs-HvGu-gNK1-51gKnr
│ ├─centos-root xfs 7db33ca8-f0f1-427b-b209-bb3f90fa736f /
│ └─centos-swap swap b938d5f2-61cc-4770-8a53-5b7502a6c103 [SWAP]
└─sda3 LVM2_mem B30Huy-VgBm-BCRV-0MqW-lcFZ-3jaj-tuu8Gb
└─vg01-lv01 xfs 2adcfae7-db45-4133-9b04-f3fb470b1b1c /data
sdb
└─sdb1 LVM2_mem oeyDLL-uDnL-bSyW-OeKX-K2fp-tLk1-ddf87C
└─vg01-lv01 xfs 2adcfae7-db45-4133-9b04-f3fb470b1b1c /data
sr0
[root@centos7 ~]# vi /etc/fstab 添加启动
/dev/mapper/vg01-lv01 /data xfs defaults 0 0
或者
UUID=2adcfae7-db45-4133-9b04-f3fb470b1b1c /data xfs
defaults 0 0
[root@centos7 data]# dd if=/dev/mapper/vg01-lv01 of=test3
bs=4M count=950 测试写入文件
950+0 records in
950+0 records out
3984588800 bytes (4.0 GB) copied, 5.00772 s, 796 MB/s
[root@centos7 data]# df -h
Filesystem Size Used Avail Use% Mounted
on
/dev/mapper/centos-root 8.0G 5.0G 3.1G 62% /
devtmpfs 470M 0 470M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 8.3M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda1 2.0G 166M 1.9G 9% /boot
/dev/mapper/vg01-lv01 5.0G 4.8G 258M 95% /data
tmpfs 98M 8.0K 98M 1% /run/user/42
tmpfs 98M 0 98M 0%
/run/user/0
快照功能
[root@qinvm data]# lvcreate -n snap -s -p r -L 1G
/dev/vg02/data
Logical volume "snap" created.
[root@qinvm data]# blkid
/dev/sr0: UUID="2018-11-25-23-54-16-00" LABEL="CentOS 7
x86_64" TYPE="iso9660"
PTTYPE="dos"
/dev/sda1: UUID="10213c55-87b5-478c-931d-654c33c8d458"
TYPE="xfs"
/dev/sda2: UUID="lhX19c-qCS1-XZfi-uz3S-IYkH-nF3m-Q9QHuT"
TYPE="LVM2_member"
/dev/sdb1: UUID="kCLcGX-YVlU-uMhU-2man-LCTm-enye-JnOXNo"
TYPE="LVM2_member"
/dev/sdb2: UUID="6cede7c1-dd64-4923-ad4a-4391cc928c99"
TYPE="swap"
/dev/sdb3: UUID="kh4jrn-Kk5A-90Gh-VfrX-b0YY-PSZB-ihEjzF"
TYPE="LVM2_member"
/dev/mapper/vg01-root:
UUID="125aa16f-a93f-4e91-bb38-51d754ed426b"
TYPE="xfs"
/dev/mapper/vg01-swap:
UUID="aa6186f0-6f05-4cb2-b379-c80b7678c38a"
TYPE="swap"
/dev/sdb4: UUID="89D0qb-RlBX-DjFY-NvC6-2ggO-1GOv-KNj3jd"
TYPE="LVM2_member"
/dev/sdc1: UUID="mN7eWF-Kz1Y-pWN8-QRTs-mCeA-HVCp-oNhxSD"
TYPE="LVM2_member"
/dev/mapper/vg02-data:
UUID="0080c53a-3d80-4036-93c4-4c7c740d90ec"
TYPE="ext4"
/dev/mapper/vg02-snap:
UUID="0080c53a-3d80-4036-93c4-4c7c740d90ec"
TYPE="ext4"
[root@qinvm data]# mount /dev/vg02/snap
/mnt/snap (ext4)
--xfs
mount -o nouuid /dev/vg02/snap /snap
[root@qinvm data]# lvdisplay
--- Logical volume ---
LV Path /dev/vg02/data
LV Name data
VG Name vg02
LV UUID FpnEGj-Q1uu-rOUO-PgAV-PZFA-YMqB-7eXpXL
LV Write Access read/write
LV Creation host, time qinvm, 2021-05-17
08:07:16 +0800
LV snapshot status source of
snap [active]
LV Status available
# open 1
LV Size 2.00 GiB
Current LE 512
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:2
--- Logical volume ---
LV Path /dev/vg02/snap
LV Name snap
VG Name vg02
LV UUID d8lPKP-1ydn-HUOO-bqkH-ewXs-ylmX-fLUMcV
LV Write Access read
only
LV Creation host, time qinvm, 2021-05-17
08:10:43 +0800
LV snapshot status active destination for
data
LV Status available
# open 0
LV Size 2.00 GiB
Current LE 512
COW-table size 1.00 GiB
COW-table LE 256
Allocated to snapshot 0.01%
Snapshot chunk size 4.00 KiB
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:5
[root@qinvm ~]# umount /data
[root@qinvm ~]# umount /mnt/snap
[root@qinvm ~]# lvconvert --merge /dev/vg02/snap
Merging of volume vg02/snap started.
vg02/data: Merged: 100.00%
[root@qinvm ~]# mount /dev/vg02/data /data
删除快照
lvremove /dev/vg02/snap
删除LV VG PV
[root@qinvm /]# umount /data
[root@qinvm /]# lvremove /dev/vg02/data
Do you really want to remove active logical volume vg02/data?
[y/n]: y
Logical volume "data" successfully
removed
[root@qinvm /]# vgremove /dev/vg02
Volume group "vg02" successfully
removed
[root@qinvm /]# pvremove /dev/sdc1
Labels on physical volume "/dev/sdc1"
successfully wiped.
swap分区添加
加硬盘不生效可重启也可执行
如下 echo '- - -' >
/sys/class/scsi_host/host0/scan
1、LVM模式
[root@qinvm ~]# echo -e 'n\np\n\n\n+2G\nw\n' | fdisk
/dev/sdb
[root@qinvm ~]# partprobe /dev/sdb --刷新分区
[root@qinvm ~]# fdisk /dev/sdb
t L 8e w
[root@qinvm ~]# pvcreate /dev/sdb1
[root@qinvm ~]# vgextend vg01 /dev/sdb1
[root@qinvm ~]# lvextend -L +2000M
/dev/vg01/swap
[root@qinvm ~]# sync;sync
[root@qinvm ~]# swapoff /dev/vg01/swap
[root@qinvm ~]# mkswap /dev/vg01/swap
mkswap: /dev/vg01/swap: warning: wiping old swap
signature.
Setting up swapspace version 1, size = 4145148
KiB
no label,
UUID=de15ae30-f773-493c-a5b9-aa1bb9eddec8
[root@qinvm ~]# free -h
total used free shared buff/cache available
Mem: 972M 603M 64M 52M 303M 119M
Swap: 0B 0B 0B
[root@qinvm ~]# swapon /dev/vg01/swap
[root@qinvm ~]# free -h
total used free shared buff/cache available
Mem: 972M 607M 60M 52M 304M 116M
Swap: 4.0G 0B 4.0G
[root@qinvm ~]# 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
├─vg01-root
253:0 0 17G 0 lvm /
└─vg01-swap
253:1 0 4G 0 lvm [SWAP]
sdb 8:16 0 10G 0
disk
└─sdb1 8:17 0 2G 0
part
└─vg01-swap
253:1 0 4G 0 lvm [SWAP]
2、单盘模式
[root@qinvm ~]# echo -e 'n\np\n\n\n+2G\nt\n82\nw\n' |
fdisk /dev/sdb
[root@qinvm ~]# partprobe /dev/sdb
[root@qinvm ~]# sync
[root@qinvm ~]# mkswap /dev/sdb2
Setting up swapspace version 1, size = 1048572
KiB
no label,
UUID=6cede7c1-dd64-4923-ad4a-4391cc928c99
[root@qinvm ~]# vim /etc/fstab
UUID=6cede7c1-dd64-4923-ad4a-4391cc928c99 swap swap
defaults 0 0
[root@qinvm ~]# swapon /dev/sdb2 或者swapon -a
[root@qinvm ~]# free -h
total used free shared buff/cache available
Mem: 972M 629M 67M 87M 275M 63M
Swap: 5.0G 39M 4.9G
[root@qinvm ~]# 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
├─vg01-root 253:0 0 17G 0 lvm /
└─vg01-swap 253:1 0 4G 0 lvm [SWAP]
sdb 8:16 0 10G 0
disk
├─sdb1 8:17 0 2G 0 part
│ └─vg01-swap 253:1 0 4G 0
lvm [SWAP]
└─sdb2 8:18 0 1G 0 part [SWAP]
[root@qinvm ~]# cat /proc/swaps
Filename Type Size Used Priority
/dev/dm-1 partition 4145148 34740
-2
/dev/sdb2 partition 1048572
0 -3 此处是优先级 可在fstab中将defaults换为pri=1,则优先使用sdb2,设置完需要swapoff -a swapon
-a swapon -s
--内存占用70%使用swap
[root@qinvm ~]# cat
/proc/sys/vm/swappiness
30
--可选优化
[root@qinvm ~]# echo 0 >
/proc/sys/vm/swappiness
--删除swap分区
sed -i.bak '/swap/d' /etc/fstab
swapoff -a
删分区