####管理系统的简单分区和文件系统####
1.添加磁盘分区
[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
[root@localhost ~]# blkid ###显示系统正在使用的设备id
/dev/vda1: UUID="9bf6b9f7-92ad-441b-848e-0257cbb883d1" TYPE="xfs"
[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 0x03cf5f84.
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): ###默认第一块设备
First sector (2048-20971519, default 2048): ###默认存储起始位置
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +100M
###分区大小100M
Partition 1 of type Linux and of size 100 MiB is set
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 ~]# 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: 0x03cf5f84
Device Boot Start End Blocks Id System
/dev/vdb1 2048 206847 102400 83 Linux
[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
2.磁盘分区的临时及永久挂载
[root@localhost ~]# mkfs.xfs /dev/vdb1 ####格式化分区为xfs格式
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
[root@localhost ~]# blkid
/dev/vda1: UUID="9bf6b9f7-92ad-441b-848e-0257cbb883d1" TYPE="xfs"
/dev/vdb1: UUID="960ecb74-847c-4d00-b51d-ee1938359187" TYPE="xfs"
[root@localhost ~]# mount /dev/vdb1 /mnt/
[root@localhost ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3139420 7334480 30% /
devtmpfs 927072 0 927072 0% /dev
tmpfs 942660 140 942520 1% /dev/shm
tmpfs 942660 17024 925636 2% /run
tmpfs 942660 0 942660 0% /sys/fs/cgroup
/dev/vdb1 98988 5280 93708 6% /mnt
[root@localhost ~]# vim /etc/fstab ###修改配置文件
[root@localhost ~]# umount /mnt/
[root@localhost ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3139420 7334480 30% /
devtmpfs 927072 0 927072 0% /dev
tmpfs 942660 140 942520 1% /dev/shm
tmpfs 942660 17024 925636 2% /run
tmpfs 942660 0 942660 0% /sys/fs/cgroup
[root@localhost ~]# mount -a ### 使修改的配置文件操作生效
[root@localhost ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3139420 7334480 30% /
devtmpfs 927072 0 927072 0% /dev
tmpfs 942660 140 942520 1% /dev/shm
tmpfs 942660 17024 925636 2% /run
tmpfs 942660 0 942660 0% /sys/fs/cgroup
/dev/vdb1 98988 5280 93708 6% /mnt
3.磁盘分区的删除
[root@localhost ~]# umount /mnt ####设备卸载
[root@localhost ~]# vim /etc/fstab ###将配置命令删除
[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 ###删除磁盘分区
Selected partition 1
Partition 1 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 ~]# cat /proc/partitions ####查看可用设备
major minor #blocks name
253 0 10485760 vda
253 1 10484142 vda1
253 16 10485760 vdb
4.磁盘分区为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): 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): +4G
Partition 1 of type Linux and of size 4 GiB is set
Command (m for help): t ####改变磁盘格式
Selected partition 1
Hex code (type L to list all codes): l ###显示各个磁盘id
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 ###swap磁盘格式id
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.
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 4194304 vdb1
[root@localhost ~]# blkid
/dev/vda1: UUID="9bf6b9f7-92ad-441b-848e-0257cbb883d1" TYPE="xfs"
[root@localhost ~]# mkswap /dev/vdb1 ###将此分区用作交换区
Setting up swapspace version 1, size = 4194300 KiB
no label, UUID=a9ef4f27-402c-46b4-9bc5-dfd95ed073af
[root@localhost ~]# blkid
/dev/vda1: UUID="9bf6b9f7-92ad-441b-848e-0257cbb883d1" TYPE="xfs"
/dev/vdb1: UUID="a9ef4f27-402c-46b4-9bc5-dfd95ed073af" TYPE="swap"
[root@localhost ~]# swapon -a /dev/vdb1 ###激活新交换区
[root@localhost ~]# swapon -s ###显示当前交换区状态
Filename Type Size Used Priority
/dev/vdb1 partition 4194300 0 -1
##swap分区优先级的修改##
[root@localhost ~]# mkswap /dev/vdb2 ###swap磁盘格式化
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=1969bfb8-cfd3-4b15-902f-560f37cc6a20
[root@localhost ~]# swapon -a /dev/vdb2 ###swap分区激活
[root@localhost ~]# swapon -s
Filename Type Size Used Priority
/dev/vdb1 partition 4194300 0 -1
/dev/vdb2 partition 1048572 0 -2
[root@localhost ~]# swapoff /dev/vdb2 ###使分区不生效
[root@localhost ~]# swapon -a /dev/vdb2 -p 1 ###修改分区优先级并激活分区 -p+优先级
[root@localhost ~]# swapon -s
Filename Type Size Used Priority
/dev/vdb1 partition 4194300 0 -1
/dev/vdb2 partition 1048572 0 1
[root@localhost ~]# swapoff /dev/vdb2
[root@localhost ~]# swapoff /dev/vdb1
[root@localhost ~]# swapon -s
[root@localhost ~]# vim /etc/fstab ####修改配置文件
[root@localhost ~]# vim /etc/fstab
[root@localhost ~]# swapon -a ###使修改生效
[root@localhost ~]# swapon -s
Filename Type Size Used Priority
/dev/vdb1 partition 4194300 0 -1
/dev/vdb2 partition 1048572 0 1
##swap分区的删除##
[root@localhost ~]# swapoff /dev/vdb{1,2} ###使分区不生效
[root@localhost ~]# vim /etc/fstab ###删除相关配置文件
[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,2, default 2):
Partition 2 is deleted
Command (m for help): d
Selected partition 1
Partition 1 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 ~]# cat /proc/partitions ####显示可用分区
major minor #blocks name
253 0 10485760 vda
253 1 10484142 vda1
253 16 10485760 vdb
5.分配用户指定磁盘的使用额度
创建一个1G大小额磁盘分区
[root@localhost ~]# mkdir /pub/ ###创建目录
[root@localhost ~]# umount /pub ###卸载目录挂载
[root@localhost ~]# mount /dev/vdb1 /pub ###设备挂载
[root@localhost ~]# umount /pub ###卸载目录的挂载
[root@localhost ~]# mount -o usrquota,grpquota /dev/vdb1 /pub ###分配用户额度并挂载
[root@localhost ~]# chmod 777 /pub ###加满目录权限
[root@localhost ~]# quotaon -ugv /dev/vdb1 ###用户,组的显示
quotaon: Enforcing group quota already on /dev/vdb1
quotaon: Enforcing user quota already on /dev/vdb1
[root@localhost ~]# edquota -u westos ###设定用户的分配额度
[root@localhost ~]# rm -fr /pub/file
[root@localhost ~]# su - westos
测试:
[westos@localhost ~]$ dd if=/dev/zero of=/pub/file bs=1M count=300
dd: error writing ‘/pub/file’: Disk quota exceeded ###超出分配额度大小
201+0 records in
200+0 records out
209715200 bytes (210 MB) copied, 0.570013 s, 368 MB/s
[westos@localhost ~]$ dd if=/dev/zero of=/pub/file bs=1M count=200
200+0 records in
200+0 records out
209715200 bytes (210 MB) copied, 0.343856 s, 610 MB/s
[westos@localhost ~]$ dd if=/dev/zero of=/pub/file bs=1M count=100
100+0 records in
100+0 records out
104857600 bytes (105 MB) copied, 0.162963 s, 643 MB/s
6.模拟raid1,加快磁盘读写速度
[root@localhost ~]# umount /pub^C ###卸载挂载目录
[root@localhost ~]# fdisk /dev/vdb ###删除原分区并重新划分磁盘
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: dos
Disk identifier: 0xa4bc8e33
Device Boot Start End Blocks Id System
/dev/vdb1 2048 2099199 1048576 83 Linux
/dev/vdb2 2099200 4196351 1048576 83 Linux
/dev/vdb3 4196352 6293503 1048576 83 Linux
####划分三块1G大小磁盘
[root@localhost ~]# partprobe
[root@localhost ~]# mdadm -C /dev/md0 -a yes -l 1 -n 2 -x 1 /dev/vdb{1..3} ###激活并保持两块磁盘一起工作,一块空闲
mdadm: Note: this array has metadata at the start and
may not be suitable as a boot device. If you plan to
store '/boot' on this device please ensure that
your boot-loader understands md/v1.x metadata, or use
--metadata=0.90
Continue creating array? y
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.
[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 -h ####显示磁盘
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 10G 3.1G 7.0G 31% /
devtmpfs 906M 0 906M 0% /dev
tmpfs 921M 84K 921M 1% /dev/shm
tmpfs 921M 17M 904M 2% /run
tmpfs 921M 0 921M 0% /sys/fs/cgroup
/dev/md0 1021M 33M 988M 4% /mnt
[root@localhost ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3183020 7290880 31% /
devtmpfs 927072 0 927072 0% /dev
tmpfs 942660 84 942576 1% /dev/shm
tmpfs 942660 17040 925620 2% /run
tmpfs 942660 0 942660 0% /sys/fs/cgroup
/dev/md0 1044588 32928 1011660 4% /mnt
7.损坏磁盘后的恢复
[root@localhost ~]# watch -n 1 'cat /proc/mdstat;echo ====;df -h' ###监控命令[root@localhost ~]# mdadm -D /dev/md0 ###查看磁盘状态
/dev/md0:
Version : 1.2
Creation Time : Wed Apr 26 07:00:25 2017
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 : Wed Apr 26 07:07:43 2017
State : clean
Active Devices : 2
Working Devices : 3
Failed Devices : 0
Spare Devices : 1
Name : localhost:0 (local to host localhost)
UUID : e2948a71:119b8533:ce315772:1aaf39d0
Events : 17
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 -f /dev/md0 /dev/vdb2 ####毁坏其中一块磁盘
mdadm: set /dev/vdb2 faulty in /dev/md0
[root@localhost ~]# mdadm -D /dev/md0 ###查看当前磁盘状态
[root@localhost ~]# mdadm -r /dev/md0 /dev/vdb2 ###删除损坏磁盘
mdadm: hot removed /dev/vdb2 from /dev/md0
[root@localhost ~]# mdadm -D /dev/md0
[root@localhost ~]# mdadm -a /dev/md0 /dev/vdb2 ###新建添加一块磁盘
mdadm: added /dev/vdb2
[root@localhost ~]# mdadm -D /dev/md0
删除此磁盘:
[root@localhost ~]# umount /mnt
[root@localhost ~]# mdadm -S /dev/md0
mdadm: stopped /dev/md0
[root@localhost ~]# fdisk /dev/vdb ####d 删除划分好的分区
8.磁盘分区的加密
[root@localhost ~]# fdisk /dev/vdb ####划分新分区
[root@localhost ~]# cryptsetup luksFormat /dev/vdb1 ####对/dev/vdb1进行加密并设置解密密码
WARNING!
========
This will overwrite data on /dev/vdb1 irrevocably.
Are you sure? (Type uppercase yes): YES
Enter passphrase:
Verify passphrase:
[root@localhost ~]# cryptsetup open /dev/vdb1 westos ###打开加密磁盘
Enter passphrase for /dev/vdb1:
[root@localhost ~]# ll /dev/mapper/westos ###查看解锁后的卷
lrwxrwxrwx. 1 root root 7 Apr 26 07:55 /dev/mapper/westos -> ../dm-0
[root@localhost ~]# mkfs.xfs /dev/mapper/westos ###解密的卷上创建xfs文件系统
[root@localhost ~]# mount /dev/mapper/westos /mnt ###挂载文件系统
###永久加密###
[root@localhost ~]# vim /etc/fstab
[root@localhost ~]# vim /etc/crypttab
[root@localhost ~]# vim /root/diskpass
[root@localhost ~]# ll
total 16
-rw-------. 1 root root 8619 May 6 2014 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Apr 24 10:17 Desktop
-rw-r--r--. 1 root root 12 Apr 26 08:23 diskpass
drwxr-xr-x. 2 root root 6 Apr 24 07:18 Documents
drwxr-xr-x. 2 root root 6 Apr 24 07:18 Downloads
-rw-r--r--. 1 root root 0 Apr 25 09:35 ggg
drwxr-xr-x. 2 root root 6 Apr 24 07:18 Music
drwxr-xr-x. 2 root root 6 Apr 24 07:18 Pictures
drwxr-xr-x. 2 root root 6 Apr 24 07:18 Public
drwxr-xr-x. 2 root root 6 Apr 24 07:18 Templates
drwxr-xr-x. 2 root root 6 Apr 24 07:18 Videos
[root@localhost ~]# chmod 600 /root/diskpass
[root@localhost ~]# cryptsetup luksAddKey /dev/vdb1 /root/diskpass
Enter any passphrase:
[root@localhost ~]# cat /etc/fstab
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 / xfs defaults 1 1
/dev/mapper/westos /mnt xfs defaults 0 0
[root@localhost ~]# cat /root/diskpass
wangjiazhuo
[root@localhost ~]# cat /etc/crypttab
westos /dev/vdb1 /root/diskpass
重新启动查看: