###1.磁盘分区####
fdisk /dev/磁盘
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 0x18f0d755.

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-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): wq                      ##保存退出
partprobe                             ##同步分区表
fdisk -l                              ##显示系统中所有可以使用的设备信息
      -c                              ##禁用旧的DOS模式
      -u                              ##以扇区的格式显示输出
blkid                                 ##显示系统正在使用的设备id
cat /proc/partitions                  ##查看系统可识别的分区
####2.文件系统#####
现在常见的文件系统有 ext4 (Red Hat Enterprise Linux 6 的标准文件系统),ext2(Linux中较旧的文件系统),vfat (针对较旧的版本的 Microsoft Windows 开发,在大量的系统和设备上受支持),xfs (Red Hat Enterprise Linux 7 的标准文件系统).
mkfs -t xfs /dev/vdb1                  ##格式化
meta-data=/dev/vdb1              isize=256    agcount=4, agsize=6400 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=25600, 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
mkfs.xfs /dev/vdb2                 ##格式化
meta-data=/dev/vdb2              isize=256    agcount=4, agsize=6400 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=25600, 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

####3磁盘加密####
 cryptsetup luksFormat /dev/vdb3           ##加密

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

Are you sure? (Type uppercase yes): YES      ##必须是大写YES
Enter passphrase:
Verify passphrase:
cryptsetup open /dev/vdb3 westos              ##解密
mkfs.xfs /dev/mapper/westos                   ##格式化
mount /dev/mapper/westos /mnt                 ##挂载
df                                            ##查看挂载
umount /dev/mapper/westos /mnt                ##卸载
cryptsetup close westos                       ##关闭加密文件
####开机自动挂载磁盘加密#####
vim /etc/crypttab                             ##编辑配置文件
westos             /dev/vdb3/   /root/lukspsfile                   
解密后设备管理文件       设备         加密字符所在文件
vim /root/lukspsfile                          ##编辑加密文件
chmod 600 /root/lukspsfile                    ##设置权限只允许超级用户访问
cryptsetup luksAddKey /dev/vdb3 /root/lukspsfile  ##将vdb3和lukspsfile关联
vim /etc/fstab                                ##编辑开机启动脚本
/dev/vdb1          /mnt        xfs     defaults,usrquota   0 0
:wq
reboot
##重启后会自动激活挂载在/mnt下
####加密清除#####
vim /etc/fstab                          ##编辑配置文件清除之前添加的内容
> /etc/crypttab                         ##清空文件内容
umount /mnt                             ##卸载
cryptsetup close westos                 ##关闭加密
mkfs.xfs /dev/vdb3                      ##格式化

####4.磁盘阵列#####
fdisk /dev/vdb                          ##划分磁盘
partprobe                               ##同步分区表
cat /proc/partitions                    ##查看系统能识别的分区
mdadm -C /dev/md0 -a yes -l 1 -n 2 -x 1 /dev/vdb{1..3}
mkfs.xfs /dev/md0                       ##格式化
mount /dev/md0 /mnt                     ##挂载
watch -n 1 cat /proc/mdstat             ##监控

mdadm -f /dev/md0 /dev/vdb1             ##破坏第一个分区
mdadm -r /dev/md0 /dev/vdb1             ##删除第一个分区
mdadm -a /dev/md0 /dev/vdb1             ##添加分区
mdadm -D /dev/md0                       ##删除
umount /mnt/                            ##卸载
mdadm -S /dev/md0                       ##关闭设备
fdisk /dev/vdb                          ##可以利用此命令删除分区
partprobe
#####5.配额#####
mkdir /pub                              ##创建目录
mount -o usrquota /dev/vdb1 /pub        ##
chmod 1777 /pub                         ##设置权限
quotaon -uv /dev/vdb1                   ##显示用户配额
edquota -u student                      ##编辑配额用户

Disk quotas for user student (uid 1000):
  Filesystem                   blocks       soft       hard     inodes     soft     hard
  /dev/vdb1                     10240          0      10240          1        0        0
su - student
dd if=/dev/zero of=/pub/file bs=1M count=100
dd: error writing ‘/pub/file’: Disk quota exceeded   ##报错 count 过大
11+0 records in
10+0 records out
10485760 bytes (10 MB) copied, 0.00763009 s, 1.4 GB/s
dd if=/dev/zero of=/pub/file bs=1M count=10
10+0 records in
10+0 records out
10485760 bytes (10 MB) copied, 0.00670675 s, 1.6 GB/s


###lvm#####
#1.LVM建立
1.划分物理分区并将分区id修改为8e
watch -n 1 'echo "=== pvinfo ===";pvs; echo "=== vginfo ===";vgs; echo "=== lvinfo ===";lvs;'                           ##监控pvs;vgs;lvs

pvcreate /dev/vdb1                       ##创建物理分区
vgcreate westos /dev/vdb1                ##创建名为westos的物理分区组
lvcreate -L 100M -n lv0 westos
mkfs.xfs /dev/westos/lv0
mount /dev/westos/lv0 /mnt

watch -n 1 'echo "=== pvinfo ===";pvs; echo "=== vginfo ===";vgs; echo "=== lvinfo ===";lvs; echo "===";df -h /mnt'          ##监控命令
lvextend -L 200M /dev/westos/lv0              ##扩展lv到200M
xfs_growfs /dev/westos/lv0                     ##扩展文件系统
#当要扩展的内存大于
pvcreate /dev/vdb2                             ##创建pv
vgextend westos /dev/vdb2                     ##扩展vg
lvextend -L 600M /dev/westos/lv0              ##扩展lv到600
xfs_growfs /dev/westos/lv0                     ##扩展文件系统
#分区可以无限扩展,但文件系统不能
######缩减
umount /mnt                                   ##卸载
mkfs.ext4 /dev/westos/lv0                     ##以ext4格式化文件
e2fsck -f /dev/westos/lv0                     ##检测/dev/westos/lv0 
resize2fs /dev/westos/lv0 550                 ##缩减文件系统
lvreduce -L 550 /dev/westos/lv0               ##缩减lv
resize2fs /dev/westos/lv0 200M
lvreduce -L 200 /dev/westos/lv0
pvmove /dev/vdb1 /dev/vdb2                    ##将/dev/vdb1数据移动到/dev/vdb2
vgreduce westos /dev/vdb1                     ##
pvremove /dev/vdb1
#如果先缩减lv后缩减文件系统可以进行以下操作
lvreduce -L 150M /dev/westos/lv0               ##缩减lv到150M
resize2fs /dev/westos/lv0 150M                 ##缩减文件系统到150M
umount /mnt                                    ##卸载
mount /dev/mapper/westos-lv0 /mnt              ##挂载出错
mount: wrong fs type, bad option, bad superblock on /dev/mapper/westos-lv0,
       missing codepage or helper program, or other error

       In some cases useful info is found in syslog - try
       dmesg | tail or so.
lvextend -L 200M /dev/westos/lv0               ##扩展lv到200M
#####快照######
umount /mnt
lvcreate -L 10M -n lv0-backup -s /dev/westos/lv0     ##给/dev/westos/lv0 创建一个快照名为 lv0-backup 大小为10M
mount /dev/mapper/westos-lv0--backup /mnt            ##挂载

#####删除设备#####
umount /mnt
lvremove /dev/westos/lv0                           ##删除/dev/westos/lv0中的lv
vgremove westos                                    ##删除 westos 组
pvremove /dev/vdb2                                 ##删除/dev/vdb2 物理分区
pvremove /dev/vdb1                                 ##删除/dev/vdb1 物理分区

###建立非交互式的分区脚本##
vim test.sh
#!/bin/bash
fdisk /dev/vdb << end
n


+1G
wq
end

sh test.sh                                        ##执行脚本命令
l