linux---系统分区管理,划分分区,GPT分区方式,swap分区挂载,磁盘加密,磁盘阵列,设备管理

系统分区管理

df <= blkid < cat /proc/partitions < fdisk -l
df ##查看系统正在使用的设备
blkid ##查看系统里面可以使用的设备
cat /proc/partitions ##查看系统识别的设备
fdisk -l ##查看硬盘
df -h ##2的n次方,1024
df -H ##10的n次方 1000 -h比-H统计出来的数小,但是精确


在真机里做以下实验:

硬盘的挂载卸载

先插入U盘,用df查看有/dev/sdb1 –>u盘

[root@foundation68 ~]# umount /dev/sdb1/    ##卸载u盘
[root@foundation68 ~]# mount /dev/sdb1 /mnt/    ##将u盘挂载到/mnt下
[root@foundation68 ~]# cd /mnt

再打开一个shell

[root@foundation68 ~]# umount /dev/sdb1/    ##卸载u盘
[root@foundation68 ~]# mount /dev/sdb1 /mnt/    ##将u盘挂载到/mnt下
[root@foundation68 ~]# cd /mnt
另开一个shell
[root@foundation68 ~]# umount /dev/sdb1/
umount: /mnt: target is busy.                   ##u盘正在运行,卸载不掉
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))
[root@foundation28 ~]# fuser -kvm /dev/sdb1/    ##杀死后台运行
                     USER        PID ACCESS COMMAND
/dev/sdb1:           root     kernel mount /mnt
                     root       9277 ..c.. bash
[root@foundation68 ~]# umount /dev/sdb1   ##现在就可以卸载了

切换到第一个shell
[root@foundation68 mnt]# Killed ##进程已经被杀死

[root@foundation68 ~]# fuser -vm /dev/sdb1/  ##查看
                     USER        PID ACCESS COMMAND
/dev/sdb1:           root     kernel mount /run/media/kiosk/00A4-2CBF

指定设备激活或更改

[root@foundation68 ~]# mount -o ro /dev/sdb1 /mnt/ ##mount -o添加参数
[root@foundation68 ~]# touch /mnt/file1
touch: cannot touch ‘/mnt/file1’: Read-only file system

用mount查看为ro不能建立文件只读

[root@foundation68 ~]# umount /dev/sdb1
[root@foundation68 ~]# mount -o rw /dev/sdb1 /mnt/

用mount查看为rw可以建立文件为可写


划分分区

以下实验步骤需在虚拟机server里完成:

[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 0x55916b15.

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): 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

重复以上步骤到第四个分区:

Command (m for help): n
Partition type:
   p   primary (3 primary, 0 extended, 1 free)
   e   extended
Select (default e):     ##显示分区表信息
Using default response e
Selected partition 4
First sector (616448-20971519, default 616448):   ##默认回车
Using default value 616448
Last sector, +sectors or +size{K,M,G} (616448-20971519, default 20971519):     ##默认回车
Using default value 20971519
Partition 4 of type Extended and of size 9.7 GiB is set

Command (m for help): p    ##显示分区表信息
Command (m for help): wq    ##保存分区策略并退出fdisk界面

[root@localhost ~]# partprobe    ##手动同步分区列表
[root@localhost ~]# cat /proc/partitions  ##查看设备是否被系统识别

这里写图片描述
格式化挂载:

[root@localhost ~]# mount /dev/vdb1 /mnt/  ##无法挂载
[root@localhost ~]# mkfs.xfs /dev/vdb1  ##格式化设备,在设备上安装文件系统xfs
[root@localhost ~]# blkid ##查看可用设备,并且可以看到被格式化的
[root@localhost ~]# mount /dev/vdb1 /mnt/  ##可以挂载/dev/vdb1
[root@localhost ~]# df    ##查看/dev/vdb1被挂载

这里写图片描述
注意:不能格式化/dev/vdb4,它是一个容器,否则整个硬盘将缺少更多空间使用。


GPT分区方式

解决一个分区不能超过2tb

[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     ##删除1-4的硬盘
Partition number (1-4, default 4): 1
Partition 1 is deleted

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

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

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

[root@localhost ~]# umount /mnt    ##解除挂载,不然会有报错
[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?   ##分区类型                                                   
aix    amiga  bsd    dvh    gpt    loop   mac    msdos  pc98   sun    
New disk label type? gpt
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/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: gpt      ##出来gpt说明更改分区方式成功                                             
[root@localhost ~]# fdisk /dev/vdb
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
First sector (34-20971486, default 2048):  ##默认回车
Last sector, +sectors or +size{K,M,G,T,P} (2048-20971486, default 20971486): +100M
Created partition 1


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


Command (m for help): p

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: gpt


#         Start          End    Size  Type            Name
 1         2048       206847    100M  Linux filesyste 
 2       206848       411647    100M  Linux filesyste 

Command (m for help): wq
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.

这里写图片描述
这里写图片描述
再转换到dos分区方式:

[root@localhost ~]# parted /dev/vdb
[root@localhost ~]# fdisk -l
Disk label type: dos    ##类型更改为dos

这里写图片描述
永久挂载硬盘:
先用df看有没有挂载,有的话需要先卸载

[root@localhost ~]# vim /etc/fstab   ##编写挂载文件
  写入:/dev/vdb1  /mnt  xfs   defaults   0 0    
[root@localhost ~]# mount -a    ##自动全部挂载
[root@localhost ~]# df

这里写图片描述
这里写图片描述


swap分区挂载

[root@localhost ~]# fdisk  /dev/vdb  ##新建一个硬盘
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 (206848-20971519, default 206848): 
Using default value 206848
Last sector, +sectors or +size{K,M,G} (206848-20971519, default 20971519): +1000M
Partition 2 of type Linux and of size 1000 MiB is set

Command (m for help): t    ##更改名字
Partition number (1,2, default 2): 2
Hex code (type L to list all codes): l   ##查看
         82  Linux swap / So
 Hex code (type L to list all codes): 82   ##更改名字编号
Changed type of partition 'Linux' to 'Linux swap / Solaris'
Command (m for help): wq   ##退出保存

[root@localhost ~]# partprobe    ##手动同步
[root@localhost ~]# cat /proc/partitions       
  253       18    1024000 vdb2
[root@localhost ~]# mkswap /dev/vdb2   ##格式化
mkswap: /dev/vdb2: warning: wiping old xfs signature.
Setting up swapspace version 1, size = 1023996 KiB
no label, UUID=d6dfca53-27f8-46ba-b86b-dcae3541957d
[root@localhost ~]# swapon -a /dev/vdb2
[root@localhost ~]# swapon -s    ##查看
Filename                Type        Size    Used    Priority
/dev/vdb2                               partition   1023996 0   -1
[root@localhost ~]# vim /etc/fstab   ##永久挂载
     /dev/vdb2  swap  swap  defaults   0 0
[root@localhost ~]# swapon -a    ##没有报错就成功了
[root@localhost ~]# fdisk -l
   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048      206847      102400   83  Linux
/dev/vdb2          206848     2254847     1024000   82  Linux swap / Solaris   ##名字有变化了

删除/dev/vdb2

[root@localhost ~]# vim /etc/fstab   ##删除里面/dev/vdb2的永久挂载
[root@localhost ~]# swapoff /dev/vdb2  ##关闭/dev/vdb2
[root@localhost ~]# fdisk /dev/vdb
Command (m for help): d
Partition number (1,2, default 2): 2       ##删除
[root@localhost ~]# partprobe
[root@localhost ~]# fdisk -l
  Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048      206847      102400   83  Linux

磁盘加密

[root@localhost ~]# fdisk /dev/vdb

这里写图片描述


[root@localhost ~]# partprobe
[root@localhost ~]# cryptsetup  luksFormat /dev/vdb1    ##加密

WARNING!
========
This will overwrite data on /dev/vdb1 irrevocably.

Are you sure? (Type uppercase yes): YES    ##大写
Enter passphrase: westos2018
Verify passphrase: westos2018
[root@localhost ~]# mount /dev/vdb1 /mnt    
mount: unknown filesystem type 'crypto_LUKS'   ##挂载不上
[root@localhost ~]# ls /dev/mapper/  
control
[root@localhost ~]# cryptsetup open /dev/vdb1 dmf(自定义,解密后的形态)   ##打开加密
Enter passphrase for /dev/vdb1:    ##输入密码
[root@localhost ~]# ls /dev/mapper/   
control  dmf

这里写图片描述

[root@localhost ~]# mkfs.xfs /dev/mapper/dmf    ##格式化
[root@localhost ~]# mount /dev/mapper/dmf  /mnt/   ##打开加密后可以挂载
[root@localhost ~]# touch /mnt/file{1..10}
[root@localhost ~]# ls /mnt
file1  file10  file2  file3  file4  file5  file6  file7  file8  file9
[root@localhost ~]# umount /mnt  ##卸载
[root@localhost ~]# df   ##查看/mnt没有挂载

这里写图片描述

[root@localhost ~]# cryptsetup close dmf    ##关闭解密
[root@localhost ~]# ll /dev/mapper  ##原始文件
[root@localhost ~]# mount /dev/vdb1  /mnt    ##磁盘加密不能挂载
[root@localhost ~]# mount /dev/mapper/dmf /mnt  ##磁盘加密不能挂载

这里写图片描述

开机自动加密挂载:

[root@localhost ~]# vim /root/diskpass
                   ##westos2018   添加密码钥匙    
[root@localhost ~]# chmod 600 /root/diskpass   ##给权限
[root@localhost ~]# cryptsetup luksAddKey /dev/vdb1 /root/diskpass      ##将加密钥匙给/dev/vdb1
Enter any passphrase: 
[root@localhost ~]# vim /etc/crypttab 
               ##写入:dmf /dev/vdb1   /root/diskpass
[root@localhost ~]# vim /etc/fstab 
        ##删除之前写的   写入:/dev/mapper/dmf  /mnt  xfs  defaults 0 0
[root@localhost ~]# df   ##查看此时没有挂载上
[root@localhost ~]# reboot  ##重启
[root@localhost ~]# df
Filesystem      1K-blocks    Used Available Use% Mounted on
/dev/vda1        10473900 3731544   6742356  36% /
devtmpfs           469344       0    469344   0% /dev
tmpfs              484932      80    484852   1% /dev/shm
tmpfs              484932   12760    472172   3% /run
tmpfs              484932       0    484932   0% /sys/fs/cgroup
/dev/mapper/dmf   1018540   32928    985612   4% /mnt             ##开机后自动挂载
[root@localhost ~]# cat /var/log/boot.log   ##重启后界面的解码过程
[  OK  ] Found device /dev/mapper/dmf.
         Mounting /mnt...
[  OK  ] Mounted /mnt.

这里写图片描述
这里写图片描述

加密清除

[root@localhost ~]# vim /etc/fstab     ##删除写入的东西
[root@localhost ~]# vim /etc/crypttab  ##删除写入的东西
[root@localhost ~]# rm -fr /root/diskpass
[root@localhost ~]# mkfs.xfs /dev/vdb1
mkfs.xfs: cannot open /dev/vdb1: Device or resource busy  ##出现这种情况说明没有卸载
[root@localhost ~]# umount /mnt
[root@localhost ~]# cryptsetup close dmf   ##关闭磁盘加密
[root@localhost ~]# mkfs.xfs /dev/vdb1 -f   ##强行格式化

这里写图片描述

注意:
1.每次实验的时候要先用df查看是否挂载,以免实验出错
2.编写脚本一定要认真,不然会出现很多问题
3.设置密码时,密码要大于8个字节,密码强度要高一些


磁盘阵列

[root@localhost ~]# fdisk /dev/vdb  ##创建三个分区,分区标示为raid

这里写图片描述
这里写图片描述

[root@localhost ~]# watch -n 1 cat /proc/mdstat  ##监控

再连接一个shell

[root@localhost ~]# mdadm -C /dev/md0 -a yes -l 1 -n 2 -x 1 /dev/vdb{1..3}   ##制作双磁盘一个闲置
##-C 创建/dev/md0;-a如果/dev/md0不存在,自动建立;-l 1 给它一个级别;-n 2 有两块设备;-x 1 读的快)

这里写图片描述

[root@localhost ~]# mkfs.xfs /dev/md0
[root@localhost ~]# mount /dev/md0 /mnt
[root@localhost ~]# df
[root@localhost ~]# df -H

这里写图片描述
这里写图片描述

设备管理

[root@localhost ~]# mdadm -D /dev/md0    ##查看raid状态
[root@localhost ~]# mdadm /dev/md0 -f /dev/vdb2     ##损坏硬盘
mdadm: set /dev/vdb2 faulty in /dev/md0
[root@localhost ~]# mdadm /dev/md0 -r /dev/vdb2    ##删除指定硬盘
[root@localhost ~]# mdadm -D /dev/md0
[root@localhost ~]# mdadm /dev/md0 -a /dev/vdb2  ##添加指定硬盘
[root@localhost ~]# mdadm -D /dev/md0

这里写图片描述
这里写图片描述
这里写图片描述
设备删除

[root@localhost ~]# umount /mnt
[root@localhost ~]# mdadm -S /dev/md0   ##停止使用
[root@localhost ~]# fdisk /dev/vdb    ##删除分区  d 1
[root@localhost ~]# partprobe

这里写图片描述

总结:
1.需要格式化/dev/md0
2.创建三个分区,分区标示要为raid
3.对比损坏硬盘和添加硬盘的个数

猜你喜欢

转载自blog.csdn.net/argued_d/article/details/80170451