新秀篇 ##Linux磁盘管理##

一.硬盘查看:
fdisk -l 系统真实存在的
这里写图片描述
vda第一个虚拟磁盘
vdb第二个虚拟磁盘
–IDE表示老式硬盘(并口硬盘)
/dev/sda表示第一块串口硬盘,a表示第一块
这里写图片描述
硬盘名字sata
/dev/hd0串口硬盘
/dev/cdrom /dev/sr0表示光驱,cdrom为sro的快捷方式,cdrom可以不存在
/dev/mapper
blkid 可以使用的
cat /proc/partitions 系统识别的

大小对比:df <= blkid < cat /proc/partitions < fdisk -l
df -h 2n次方『更准确』 df -H 10n次方
%%%优盘挂载与卸载实验:
第一个真机shell:
【1】插上移动设备
【2】打开一个真机shell,切换到超级用户
【3】fdisk -l 查看优盘
【4】用df查看优盘挂载路径
【5】挂载优盘到/mnt/下
【6】移动路径到/mnt/下
[kiosk@foundation20 Desktop]$ su - ##切换用户
Password:
Last login: Mon Apr 30 09:35:53 CST 2018 on pts/4
[root@foundation20 ~]# fdisk -l ##查看优盘
这里写图片描述
Disk /dev/sda: 120.0 GB, 120034123776 bytes, 234441648 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: 0x0002ea0d

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 1026047 512000 83 Linux
/dev/sda2 1026048 234440703 116707328 8e Linux LVM

Disk /dev/mapper/rhel_foundation20-root: 53.7 GB, 53687091200 bytes, 104857600 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/rhel_foundation20-swap: 4026 MB, 4026531840 bytes, 7864320 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/rhel_foundation20-home: 61.7 GB, 61723377664 bytes, 120553472 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/sdb: 15.7 GB, 15745482752 bytes, 30752896 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: 0x0045d92a

Device Boot Start End Blocks Id System
/dev/sdb1 * 64 30752895 15376416 b W95 FAT32 ##我的移动设备
[root@foundation20 ~]# df ##查看优盘挂载路径
这里写图片描述
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/rhel_foundation20-root 52403200 16262600 36140600 32% /
devtmpfs 1867064 0 1867064 0% /dev
tmpfs 1881152 588 1880564 1% /dev/shm
tmpfs 1881152 9112 1872040 1% /run
tmpfs 1881152 0 1881152 0% /sys/fs/cgroup
/dev/mapper/rhel_foundation20-home 60247304 4063900 56183404 7% /home
/dev/sda1 508588 153088 355500 31% /boot
/dev/loop0 3654720 3654720 0 100% /var/www/html/source7.0
/dev/loop1 3947824 3947824 0 100% /var/www/html/source7.2
tmpfs 376232 36 376196 1% /run/user/1000
tmpfs 376232 0 376232 0% /run/user/0
/dev/sdb1 15346360 6020016 9326344 40% /run/media/kiosk/KINGSTON ##表示我的移动设备
[root@foundation20 ~]# umount /dev/sdb1 ##卸载
[root@foundation20 ~]# mount /dev/sdb1 /mnt ##把优盘挂载到/mnt下
[root@foundation20 ~]# cd /mnt ##切换到/mnt/下
第二个真机shell:
【1】切换到超级用户
【2】卸载移动设备
【3】关闭进程
【4】成功卸载
这里写图片描述
[kiosk@foundation20 Desktop]$ su - ##切换用户
Password:
Last login: Mon Apr 30 09:33:50 CST 2018 on pts/3
[root@foundation20 ~]# umount /mnt/ ##卸载移动设备
umount: /mnt: target is busy. ##发现不能卸载
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1)) ##可用推荐的两个命令结束进程
[root@foundation20 ~]# fuser -kvm /dev/sdb1 ##结束进程
USER PID ACCESS COMMAND
/dev/sdb1: root kernel mount /mnt
root 5831 ..c.. bash
[root@foundation20 ~]# umount /mnt/ ##进行卸载
成功标示:在第一个shell出现killed
[root@foundation20 mnt]# Killed ##卸载成功
%%%指定设备激活或更改:
[kiosk@foundation21 Desktop]$ su -
Password:
Last login: Mon Apr 30 09:32:09 CST 2018 on pts/3
[root@foundation21 ~]# umount /mnt/ ##卸载
umount: /mnt: target is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1)) ##两种查看方式
[root@foundation21 ~]# fuser -kvm /dev/sdb1 ##查看占用后台的PID
USER PID ACCESS COMMAND
/dev/sdb1: root kernel mount /mnt
root 14537 ..c.. bash
[root@foundation21 ~]# fuser -kvm /dev/sdb1 ##杀死后台
USER PID ACCESS COMMAND
/dev/sdb1: root kernel mount /mnt
root 14537 ..c.. bash
[root@foundation21 ~]# umount /mnt/ ##卸载成功
%%lsof查看占用后台的PID就可以结合kill -9强制杀死
%%mount -o添加参数。
[root@foundation21 ~]# mount -o ro /dev/sdb1 /mnt/
[root@foundation21 ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda3 307974224 29625448 278348776 10% /
devtmpfs 1868044 0 1868044 0% /dev
tmpfs 1881152 188 1880964 1% /dev/shm
tmpfs 1881152 9036 1872116 1% /run
tmpfs 1881152 0 1881152 0% /sys/fs/cgroup
/dev/sda1 505580 149472 356108 30% /boot
/dev/loop0 3654720 3654720 0 100% /var/www/html/source7.0
/dev/loop1 3947824 3947824 0 100% /var/www/html/source7.2
tmpfs 376232 36 376196 1% /run/user/1000
tmpfs 376232 0 376232 0% /run/user/0
/dev/sdb1 15100688 8722224 6378464 58% /mnt
[root@foundation21 ~]# touch /mnt/file1
touch: cannot touch ‘/mnt/file1’: Read-only file system
用mount查看为ro不能建立文件只读
[root@foundation21 ~]# umount /dev/sdb1
[root@foundation21 ~]# mount -o rw /dev/sdb1 /mnt/
用mount查看为rw可以建立文件可写
[root@foundation21 ~]# touch /mnt/file1

二.管理系统存储
磁盘介绍与分区规则:电脑的系统都放在磁盘中,磁盘管理着系统的启动,当pc接上电源后,停在固定磁道的磁头需要去到系统所在的扇形区域启动系统,所以需要在磁头开始的地方写上引导程序,让接通电源后,磁头直接去系统所在的扇区,启动系统。这个引导程序是写在磁盘上的,为了不浪费空间,它应该尽可能的小,现在最小的是446个字节,而与其最接近的完整字节应该为512字节,512-446=66,将这多余的66个字节分为64和2,63记录的是分区表,而我们通过实验知道一个分区需要在分区表中占据16个字节,所以64个字节的空间只能有四个分区。如果我们想要得到多于4个的分区,则需要将第四个分区变为扩展分区,在扩展分区里还可以添加逻辑分区。
mb2主引导记录,打开分区。
mpt记录分区信息
512-446(mb2)(最小的字节)=66=64(mpt)+2
mbr分区方式,主分区,剩下的放在容器中叫做扩展分区,容器里面的东西为逻辑分区,在linux中最多放16个,每个不超过2t

分区划分

fdisk /dev/vdb ##查看硬盘
elcome 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): m ##帮助
这里写图片描述
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition ##删除分区
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types ##列出系统可用的分区类型
m print this menu
n add a new partition ##新建分区
o create a new empty DOS partition table
p print the partition table ##显示分区
q quit without saving changes ##退出
s create a new empty Sun disklabel
t change a partition’s system id ##修改分区功能id
u change display/entry units
v verify the partition table
w write table to disk and exit ##保存更改到分区表中[保存到硬盘保存]
x extra functionality (experts only)
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): 1 ##主分区id
First sector (2048-20971519, default 2048): ##此分区起始位置 【默认】
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +100M ##分区大小
这里写图片描述
Partition 1 of type Linux and of size 100 MiB is set
建立4个主分区后,就不能建立主分区,只能删除其中的一个主分区,然后建立拓展分区即可
Command (m for help): n
If you want to create more than four partitions, you must replace a
primary partition with an extended partition first.

Command (m for help): d
Partition number (1-4, default 4): 4
Partition 4 is deleted

Command (m for help): n
Partition type:
p primary (3 primary, 0 extended, 1 free)
e extended
Select (default e): e
Selected partition 4
First sector (2069-20971519, default 6144):
Using default value 6144
Last sector, +sectors or +size{K,M,G} (6144-204799, default 204799): +18M
Partition 4 of type Extended and of size 18 MiB is set
这里写图片描述
p显示分区结果
这里写图片描述
Command (m for help): wq ##保存退出,如果按q表示放弃更改退出
[root@localhost mnt]# partprobe ##同步分区表
cat /proc/partitions ##查看系统识别的分区信息
这里写图片描述
对磁盘分区结束后,并不能进行挂载,我们需要让他获取文件系统后才能进行挂载,对1,2,3进行格式化,而4不能执行这个命令,因为他本来就是一个容器,不能进行挂载【mkfs.xfs/dev/vdb1】
这里写图片描述

将新建分区变化到gpt类型

[root@localhost ~]# parted /dev/vdb
GNU Parted 3.1
Using /dev/vdb
Welcome to GNU Parted! Type ‘help’ to view a list of commands.
(parted) mklabel

New disk label type? gpt ##选择类型
(parted) quit
Information: You may need to update /etc/fstab.

[root@localhost ~]# fdisk /dev/vdb #建立gpt类型的主分区
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
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 number (1-128, default 1): 1 ##能建立128个主分区
First sector (34-20971486, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-20971486, default 20971486): +100
First sector (34-20971486, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-20971486, default 20971486): +10M
Created partition 1

Command (m for help): n
Partition number (2-128, default 2): 2
First sector (34-20971486, default 22528):
Last sector, +sectors or +size{K,M,G,T,P} (22528-20971486, default 20971486): +100M
Created partition 2

Command (m for help): n
Partition number (3-128, default 3): 3
First sector (34-20971486, default 227328):
Last sector, +sectors or +size{K,M,G,T,P} (227328-20971486, default 20971486): +20M
Created partition 3

Command (m for help): n
Partition number (4-128, default 4): 4
First sector (34-20971486, default 268288):
Last sector, +sectors or +size{K,M,G,T,P} (268288-20971486, default 20971486): +19M
Created partition 4

Command (m for help): n
Partition number (5-128, default 5): 5
First sector (34-20971486, default 307200):
Last sector, +sectors or +size{K,M,G,T,P} (307200-20971486, default 20971486): +38M
Created partition 5

将新建分区变化到mbr类型

[root@localhost ~]# parted /dev/vdb
GNU Parted 3.1
Using /dev/vdb
Welcome to GNU Parted! Type ‘help’ to view a list of commands.
(parted) mklabel
New disk label type? ##按Tab键查看类型
aix amiga bsd dvh gpt loop mac msdos pc98 sun
New disk label type? msdos
Warning: The existing disk label on /dev/vdb will be destroyed and all data on
this disk will be lost. Do you want to continue?
Yes/No? yes
(parted) quit
Information: You may need to update /etc/fstab.
[root@localhost ~]# fdisk -l
Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 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 ##出现DOS表示更改成功
Disk identifier: 0x00013f3e

Device Boot Start End Blocks Id System
/dev/vda1 * 2048 20970332 10484142+ 83 Linux

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0x0005a330

Device Boot Start End Blocks Id System

编辑配置文件进行永久挂载

注意:虚拟机需要建立过分区,格式化过

[root@localhost ~]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/vda1       10473900 3755624   6718276  36% /
devtmpfs          469344       0    469344   0% /dev
tmpfs             484932     140    484792   1% /dev/shm
tmpfs             484932   12784    472148   3% /run
tmpfs             484932       0    484932   0% /sys/fs/cgroup
[root@localhost ~]# vim /etc/fstab
    编辑内容:/dev/vdb1   /mnt   xfs   defaults    0    0
[root@localhost ~]# mount -a
[root@localhost ~]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/vda1       10473900 3755736   6718164  36% /
devtmpfs          469344       0    469344   0% /dev
tmpfs             484932     140    484792   1% /dev/shm
tmpfs             484932   12784    472148   3% /run
tmpfs             484932       0    484932   0% /sys/fs/cgroup
/dev/vdb1          98988    5280     93708   6% /mn
swap分区永久挂载

[root@localhost ~]# fdisk /dev/vdb ##进入分区
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): t ##修改类型参数t
Partition number (1-4, default 4): 2
Hex code (type L to list all codes): l ##查看swap类型编号

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): wq ##退出保存
The partition table has been altered!

Calling ioctl() to re-read partition table.

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@localhost ~]# partprobe ##同步
[root@localhost ~]# mkswap /dev/vdb2 ##相当于格式化
Setting up swapspace version 1, size = 102396 KiB
no label, UUID=76d4ad44-ed21-49ad-9847-6b80c2e64a7a
[root@localhost ~]# swapon -a /dev/vdb2
[root@localhost ~]# swapon -s ##查看
Filename Type Size Used Priority
/dev/vdb2 partition 102396 0 -1

[root@localhost ~]# vim /etc/fstab ##编辑配置文件
编辑内容:/dev/vdb2 swap swap defaults 0 0
[root@localhost ~]# swapon -a ##重启服务
[root@localhost ~]# fdisk -l
Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 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: 0x00013f3e

Device Boot Start End Blocks Id System
/dev/vda1 * 2048 20970332 10484142+ 83 Linux

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0x0005a330

Device Boot Start End Blocks Id System
/dev/vdb1 2048 206847 102400 83 Linux
/dev/vdb2 206848 411647 102400 82 Linux swap / Solaris ###挂载成功
/dev/vdb3 411648 616447 102400 83 Linux
/dev/vdb4 616448 821247 102400 5 Extended

删除swap分区

[root@localhost ~]# vim /etc/fstab
编辑内容:删除掉最后一行
[root@localhost ~]# swapoff /dev/vdb2
[root@localhost ~]# fdisk /dev/vdb ##在里面删除第二个分区
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-4, default 4): 2
Partition 2 is deleted

Command (m for help): wq ##退出保存
The partition table has been altered!

Calling ioctl() to re-read partition table.

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@localhost ~]# partprobe ##更新分区表
[root@localhost ~]# cat /proc/partitions ##进行查看删除结果
major minor #blocks name

253 0 10485760 vda
253 1 10484142 vda1
253 16 10485760 vdb
253 17 102400 vdb1
253 19 102400 vdb3
253 20 1 vdb4

磁盘加密保护

1.将/dev/vdb1从mount上卸载,然后删除该分区,重新分区,大小为1G
2.cryptsetup luksFormat /dev/vdb1 对新分区加LUKS锁
这里写图片描述
##YES必须大写
3.mount /dev/vdb1 /mnt 不能进行挂载【需要解锁】
这里写图片描述
4.cryptsetup open /dev/vdb1 westos 进行解锁
这里写图片描述
5. mkfs.xfs /dev/mapper/westos 进行格式化
这里写图片描述
6.将新建的分区挂载到/mnt下并且写东西在里面
这里写图片描述
7.操作完后记得关闭加密锁
这里写图片描述
8.使其开机时自动挂载

[root@localhost ~]# vim /root/diskpass     
   写入密码:xa85215295    
[root@localhost ~]# chmod 600 /root/diskpass
  给一个只有自己读写的权限
[root@localhost ~]# cryptsetup luksAddKey /dev/vdb1 /root/diskpass
Enter any passphrase: 
[root@localhost ~]# vim /etc/fstab
   编辑内容:/dev/mapper/westos    /mnt  xfs  defaults 0 0
[root@localhost ~]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/vda1       10473900 3754952   6718948  36% /
devtmpfs          469344       0    469344   0% /dev
tmpfs             484932      84    484848   1% /dev/shm
tmpfs             484932   12776    472156   3% /run
tmpfs             484932       0    484932   0% /sys/fs/cgroup
[root@localhost ~]# reboot      重启计算机
Connection to 172.25.254.220 closed by remote host.
Connection to 172.25.254.220 closed.
[kiosk@foundation20 Desktop]$ ssh root@172.25.254.220
root@172.25.254.220's password: 
Last login: Mon Apr 30 04:10:41 2018
[root@localhost ~]# df
Filesystem         1K-blocks    Used Available Use% Mounted on
/dev/vda1           10473900 3755536   6718364  36% /
devtmpfs              469344       0    469344   0% /dev
tmpfs                 484932     152    484780   1% /dev/shm
tmpfs                 484932   12820    472112   3% /run
tmpfs                 484932       0    484932   0% /sys/fs/cgroup
/dev/mapper/westos     96940    5176     91764   6% /mnt

三.磁盘阵列
1.建立3个分区,大小为+1GB

[root@localhost ~]# fdisk /dev/vdb
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 0x806c2db1.

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

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

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 (4196352-20971519, default 4196352): 
Using default value 4196352
Last sector, +sectors or +size{K,M,G} (4196352-20971519, default 20971519): +1G
Partition 3 of type Linux and of size 1 GiB is set

2.修改三个分区的类型,保存退出,刷新分区表

Command (m for help): t
Partition number (1-3, default 3): 1
Hex code (type L to list all codes): fd
Changed type of partition 'Linux' to 'Linux raid autodetect'

Command (m for help): t
Partition number (1-3, default 3): 2
Hex code (type L to list all codes): fd
Changed type of partition 'Linux' to 'Linux raid autodetect'

Command (m for help): t
Partition number (1-3, default 3): 3
Hex code (type L to list all codes): fd
Changed type of partition 'Linux' to 'Linux raid autodetect'

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]#partprobe

3.做一个监控命令 watch -n 1 cat /proc/mdstat
这里写图片描述
4.mdadm -C /dev/md0 -a yes -l 1 -n 2 -x 1 /dev/vdb{1..3}
-a yes 【表示没有/dev/md0这个文件时自动建立】
-l 【表示这两个磁盘是用来读的,同时写的话读的快】
-n 2 【表示用两个磁盘分区读】
-x 1 【表示闲置的一个】
@@监控命令中显示:vdb1和vdb2在读取状态,vdb3为闲置状态
这里写图片描述
5.格式化并且挂载到/mnt下

[root@localhost ~]# mkfs.xfs   /dev/md0
meta-data=/dev/md0               isize=256    agcount=4, agsize=65500 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=262000, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal log           bsize=4096   blocks=853, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@localhost ~]# mount /dev/md0  /mnt/
[root@localhost ~]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/vda1       10473900 3761788   6712112  36% /
devtmpfs          469344       0    469344   0% /dev
tmpfs             484932     140    484792   1% /dev/shm
tmpfs             484932   12816    472116   3% /run
tmpfs             484932       0    484932   0% /sys/fs/cgroup
/dev/md0         1044588   32928   1011660   4% /mnt
[root@localhost ~]# df -H
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        11G  3.9G  6.9G  36% /
devtmpfs        481M     0  481M   0% /dev
tmpfs           497M  144k  497M   1% /dev/shm
tmpfs           497M   14M  484M   3% /run
tmpfs           497M     0  497M   0% /sys/fs/cgroup
/dev/md0        1.1G   34M  1.1G   4% /mnt

6。删除磁盘阵列

[root@localhost ~]# mdadm -S  /dev/md0
mdadm: stopped /dev/md0
[root@localhost ~]# fdisk  /dev/vdb
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-3, default 3): 1
Partition 1 is deleted

Command (m for help): d
Partition number (2,3, default 3): 2
Partition 2 is deleted

Command (m for help): d
Selected partition 3
Partition 3 is deleted

Command (m for help): d
No partition is defined yet!

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# partprobe

模拟磁盘损坏如何进行管理

[root@localhost ~]# mdadm   -D   /dev/md0   ##查看磁盘状态
/dev/md0:
        Version : 1.2
  Creation Time : Mon Apr 30 04:47:11 2018
     Raid Level : raid1
     Array Size : 1048000 (1023.61 MiB 1073.15 MB)
  Used Dev Size : 1048000 (1023.61 MiB 1073.15 MB)
   Raid Devices : 2
  Total Devices : 3
    Persistence : Superblock is persistent

    Update Time : Mon Apr 30 05:07:54 2018
          State : clean 
 Active Devices : 2
Working Devices : 3
 Failed Devices : 0
  Spare Devices : 1

           Name : localhost.localdomain:0  (local to host localhost.localdomain)
           UUID : 5a630ed5:1e9ecc42:c7d10cfb:928bc9e9
         Events : 19

    Number   Major   Minor   RaidDevice State
       0     253       17        0      active sync   /dev/vdb1
       1     253       18        1      active sync   /dev/vdb2

       2     253       19        -      spare   /dev/vdb3
[root@localhost ~]# mdadm   /dev/md0   -f  /dev/vdb2
mdadm: set /dev/vdb2 faulty in /dev/md0

1.mdadm /dev/md0 -f /dev/vdb2 强制损坏/dev/vdb2【此时3会代替2的位置,和1一起同时读】
这里写图片描述
2.当/dev/vdb2损坏时,/dev/md0仍然挂载在/mnt下并没用受到影响

[root@localhost ~]# mdadm   /dev/md0   -f  /dev/vdb2
mdadm: set /dev/vdb2 faulty in /dev/md0
[root@localhost ~]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/vda1       10473900 3761576   6712324  36% /
devtmpfs          469344       0    469344   0% /dev
tmpfs             484932     140    484792   1% /dev/shm
tmpfs             484932   12848    472084   3% /run
tmpfs             484932       0    484932   0% /sys/fs/cgroup
[root@localhost ~]# mdadm   -D   /dev/md0
/dev/md0:
        Version : 1.2
  Creation Time : Mon Apr 30 04:47:11 2018
     Raid Level : raid1
     Array Size : 1048000 (1023.61 MiB 1073.15 MB)
  Used Dev Size : 1048000 (1023.61 MiB 1073.15 MB)
   Raid Devices : 2
  Total Devices : 3
    Persistence : Superblock is persistent

    Update Time : Mon Apr 30 05:09:58 2018
          State : clean 
 Active Devices : 2
Working Devices : 2
 Failed Devices : 1
  Spare Devices : 0

           Name : localhost.localdomain:0  (local to host localhost.localdomain)
           UUID : 5a630ed5:1e9ecc42:c7d10cfb:928bc9e9
         Events : 38

    Number   Major   Minor   RaidDevice State
       0     253       17        0      active sync   /dev/vdb1
       2     253       19        1      active sync   /dev/vdb3

       1     253       18        -      faulty   /dev/vdb2

3.mdadm /dev/md0 -r /dev/vdb2 删除/dev/vdb2这个扇区【可以看到我们的磁盘阵列中只有/dev/vdb1和/dev/vdb3

[root@localhost ~]# mdadm  /dev/md0  -r   /dev/vdb2
mdadm: hot removed /dev/vdb2 from /dev/md0
[root@localhost ~]# mdadm   -D   /dev/md0
/dev/md0:
        Version : 1.2
  Creation Time : Mon Apr 30 04:47:11 2018
     Raid Level : raid1
     Array Size : 1048000 (1023.61 MiB 1073.15 MB)
  Used Dev Size : 1048000 (1023.61 MiB 1073.15 MB)
   Raid Devices : 2
  Total Devices : 2
    Persistence : Superblock is persistent

    Update Time : Mon Apr 30 05:10:33 2018
          State : clean 
 Active Devices : 2
Working Devices : 2
 Failed Devices : 0
  Spare Devices : 0

           Name : localhost.localdomain:0  (local to host localhost.localdomain)
           UUID : 5a630ed5:1e9ecc42:c7d10cfb:928bc9e9
         Events : 39

    Number   Major   Minor   RaidDevice State
       0     253       17        0      active sync   /dev/vdb1
       2     253       19        1      active sync   /dev/vdb3

4.将/dev/vdb2添加到/dev/md0上

[root@localhost ~]# mdadm  /dev/md0  -a  /dev/vdb2
mdadm: added /dev/vdb2
[root@localhost ~]# mdadm   -D   /dev/md0
/dev/md0:
        Version : 1.2
  Creation Time : Mon Apr 30 04:47:11 2018
     Raid Level : raid1
     Array Size : 1048000 (1023.61 MiB 1073.15 MB)
  Used Dev Size : 1048000 (1023.61 MiB 1073.15 MB)
   Raid Devices : 2
  Total Devices : 3
    Persistence : Superblock is persistent

    Update Time : Mon Apr 30 05:11:06 2018
          State : clean 
 Active Devices : 2
Working Devices : 3
 Failed Devices : 0
  Spare Devices : 1

           Name : localhost.localdomain:0  (local to host localhost.localdomain)
           UUID : 5a630ed5:1e9ecc42:c7d10cfb:928bc9e9
         Events : 40

    Number   Major   Minor   RaidDevice State
       0     253       17        0      active sync   /dev/vdb1
       2     253       19        1      active sync   /dev/vdb3

       3     253       18        -      spare   /dev/vdb2

5.进行卸载,删除分区

猜你喜欢

转载自blog.csdn.net/china_zgd/article/details/80180583