一、磁盘分区
磁盘有两种分区方式:MBR、GPT
MBR分区不超过2T,
二、管理分区
fdisk 管理MBR分区
gdisk 管理GPT分区
子命令
1 [21:57:29 root@Centos7 ~]#fdisk /dev/sdb fdisk命令对sbd硬盘进行分区 2 Welcome to fdisk (util-linux 2.23.2). 3 4 Changes will remain in memory only, until you decide to write them. 5 Be careful before using the write command. 6 7 Device does not contain a recognized partition table 8 Building a new DOS disklabel with disk identifier 0x2c54f162. 9 10 Command (m for help): n n为增加分区选项 11 Partition type: 12 p primary (0 primary, 0 extended, 4 free) 13 e extended 14 Select (default p): p p选择分区类型为主分区 15 Partition number (1-4, default 1): 1 16 First sector (2048-41943039, default 2048): 要求输入起始扇区号默认即可,直接点击回车 17 Using default value 2048 18 Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +10G 指定第一个分区的大小为10G 19 Partition 1 of type Linux and of size 10 GiB is set 20 21 Command (m for help): n 22 Partition type: 23 p primary (1 primary, 0 extended, 3 free) 24 e extended 25 Select (default p): p 26 Partition number (2-4, default 2): 2 27 First sector (20973568-41943039, default 20973568): 28 Using default value 20973568 29 Last sector, +sectors or +size{K,M,G} (20973568-41943039, default 41943039): +5G 30 Partition 2 of type Linux and of size 5 GiB is set 31 32 Command (m for help): n 33 Partition type: 34 p primary (2 primary, 0 extended, 2 free) 35 e extended 36 Select (default p): p 37 Partition number (3,4, default 3): 3 38 First sector (31459328-41943039, default 31459328): 39 Using default value 31459328 40 Last sector, +sectors or +size{K,M,G} (31459328-41943039, default 41943039): +5G 41 Value out of range. 42 Last sector, +sectors or +size{K,M,G} (31459328-41943039, default 41943039): 43 Using default value 41943039 44 Partition 3 of type Linux and of size 5 GiB is set 45 46 Command (m for help): p 分区结束后P为打印当前分区信息 47 48 Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors 49 Units = sectors of 1 * 512 = 512 bytes 50 Sector size (logical/physical): 512 bytes / 512 bytes 51 I/O size (minimum/optimal): 512 bytes / 512 bytes 52 Disk label type: dos 53 Disk identifier: 0x2c54f162 54 55 Device Boot Start End Blocks Id System 56 /dev/sdb1 2048 20973567 10485760 83 Linux 57 /dev/sdb2 20973568 31459327 5242880 83 Linux 58 /dev/sdb3 31459328 41943039 5241856 83 Linux 59 60 Command (m for help): w w为保存当前分区并退出 61 The partition table has been altered! 62 63 Calling ioctl() to re-read partition table. 64 Syncing disks. 65 [21:58:24 root@Centos7 ~]#lsblk 查看分区结果 66 NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT 67 sda 8:0 0 200G 0 disk 68 ├─sda1 8:1 0 1G 0 part /boot 69 ├─sda2 8:2 0 100G 0 part / 70 ├─sda3 8:3 0 50G 0 part /data 71 ├─sda4 8:4 0 1K 0 part 72 └─sda5 8:5 0 2G 0 part [SWAP] 73 sdb 8:16 0 20G 0 disk 74 ├─sdb1 8:17 0 10G 0 part 75 ├─sdb2 8:18 0 5G 0 part 76 └─sdb3 8:19 0 5G 0 part 77 sr0 11:0 1 10.3G 0 rom 78 [21:58:33 root@Centos7 ~]#partprobe 重新设置内存中的内核分区表 79 Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only. 80 [21:58:51 root@Centos7 ~]#cat /proc/partitions 查看内存分区表是否更新 81 major minor #blocks name 82 83 8 0 209715200 sda 84 8 1 1048576 sda1 85 8 2 104857600 sda2 86 8 3 52428800 sda3 87 8 4 1 sda4 88 8 5 2097152 sda5 89 8 16 20971520 sdb 90 8 17 10485760 sdb1 91 8 18 5242880 sdb2 92 8 19 5241856 sdb3 93 11 0 10768384 sr0
创建GPT分区
1 [22:42:00 root@Centos7 ~]#gdisk /dev/sdb gdisk命令创建GPT分区 2 GPT fdisk (gdisk) version 0.8.10 3 4 Partition table scan: 5 MBR: not present 6 BSD: not present 7 APM: not present 8 GPT: not present 9 10 Creating new GPT entries. 11 12 Command (? for help): n n增加分区 13 Partition number (1-128, default 1): 1 设置分区号 14 First sector (34-41943006, default = 2048) or {+-}size{KMGTP}: 15 Last sector (2048-41943006, default = 41943006) or {+-}size{KMGTP}: +10G 指定分区大小 16 Current type is 'Linux filesystem' 17 Hex code or GUID (L to show codes, Enter = 8300): 18 Changed type of partition to 'Linux filesystem' 19 20 Command (? for help): n 21 Partition number (2-128, default 2): 2 22 First sector (34-41943006, default = 20973568) or {+-}size{KMGTP}: 23 Last sector (20973568-41943006, default = 41943006) or {+-}size{KMGTP}: +20G^H^H^H 24 Last sector (20973568-41943006, default = 41943006) or {+-}size{KMGTP}: +10G 25 Last sector (20973568-41943006, default = 41943006) or {+-}size{KMGTP}: 26 Current type is 'Linux filesystem' 27 Hex code or GUID (L to show codes, Enter = 8300): 28 Changed type of partition to 'Linux filesystem' 29 30 Command (? for help): p 31 Disk /dev/sdb: 41943040 sectors, 20.0 GiB 32 Logical sector size: 512 bytes 33 Disk identifier (GUID): 6B6805A2-2A57-4E97-B56E-8D51EAF426ED 34 Partition table holds up to 128 entries 35 First usable sector is 34, last usable sector is 41943006 36 Partitions will be aligned on 2048-sector boundaries 37 Total free space is 2014 sectors (1007.0 KiB) 38 39 Number Start (sector) End (sector) Size Code Name 40 1 2048 20973567 10.0 GiB 8300 Linux filesystem 41 2 20973568 41943006 10.0 GiB 8300 Linux filesystem 42 43 Command (? for help): w 44 45 Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING 46 PARTITIONS!! 47 48 Do you want to proceed? (Y/N): y 49 OK; writing new GUID partition table (GPT) to /dev/sdb. 50 The operation has completed successfully. 51 [22:42:55 root@Centos7 ~]#partprobe 52 Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only. 53 [22:43:22 root@Centos7 ~]#df -h 54 Filesystem Size Used Avail Use% Mounted on 55 devtmpfs 980M 0 980M 0% /dev 56 tmpfs 991M 0 991M 0% /dev/shm 57 tmpfs 991M 9.5M 981M 1% /run 58 tmpfs 991M 0 991M 0% /sys/fs/cgroup 59 /dev/sda2 100G 1.5G 99G 2% / 60 /dev/sda1 1014M 131M 884M 13% /boot 61 /dev/sda3 50G 33M 50G 1% /data 62 tmpfs 199M 0 199M 0% /run/user/0 63 [22:43:24 root@Centos7 ~]#lsblk 64 NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT 65 sda 8:0 0 200G 0 disk 66 ├─sda1 8:1 0 1G 0 part /boot 67 ├─sda2 8:2 0 100G 0 part / 68 ├─sda3 8:3 0 50G 0 part /data 69 ├─sda4 8:4 0 1K 0 part 70 └─sda5 8:5 0 2G 0 part [SWAP] 71 sdb 8:16 0 20G 0 disk 72 ├─sdb1 8:17 0 10G 0 part 73 └─sdb2 8:18 0 10G 0 part 74 sr0 11:0 1 10.3G 0 rom
三、文件系统
[22:43:27 root@Centos7 ~]#ls /lib/modules/`uname -r`/kernel/fs binfmt_misc.ko.xz ceph dlm fat gfs2 lockd nfs_common overlayfs udf btrfs cifs exofs fscache isofs mbcache.ko.xz nfsd pstore xfs cachefiles cramfs ext4 fuse jbd2 nfs nls squashfs
示例:
创建文件系统
1 [22:47:28 root@Centos7 ~]#mkfs.ext4 /dev/sdb1 创建ext4文件系统 2 mke2fs 1.42.9 (28-Dec-2013) 3 Filesystem label= 4 OS type: Linux 5 Block size=4096 (log=2) 6 Fragment size=4096 (log=2) 7 Stride=0 blocks, Stripe width=0 blocks 8 655360 inodes, 2621440 blocks 9 131072 blocks (5.00%) reserved for the super user 10 First data block=0 11 Maximum filesystem blocks=2151677952 12 80 block groups 13 32768 blocks per group, 32768 fragments per group 14 8192 inodes per group 15 Superblock backups stored on blocks: 16 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632 17 18 Allocating group tables: done 19 Writing inode tables: done 20 Creating journal (32768 blocks): done 21 Writing superblocks and filesystem accounting information: done 22 23 [22:49:42 root@Centos7 ~]#blkid 查看分区类型 24 /dev/sr0: UUID="2019-09-09-19-08-41-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos" 25 /dev/sda1: UUID="a6fc6fe1-c599-4f9e-81e3-0b06a3a373e9" TYPE="xfs" 26 /dev/sda2: UUID="7649e9c2-1502-4fde-9f60-67ab716bbbe7" TYPE="xfs" 27 /dev/sda3: UUID="48649b37-de95-4693-a221-9a8e795fa568" TYPE="xfs" 28 /dev/sda5: UUID="f1348665-8b05-45a6-adf7-10ac42e009a7" TYPE="swap" 29 /dev/sdb1: UUID="aebd2a64-0090-42c5-97a8-5443421ca880" TYPE="ext4" PARTLABEL="Linux filesystem" PARTUUID="5261a7e4-2f33-4f41-bc5f-6b364f74c20e" 30 /dev/sdb2: PARTLABEL="Linux filesystem" PARTUUID="504cb552-a0c6-448a-8dfc-19a50f010ee4"
四、挂载
mount常用命令选项
将磁盘/dev/sdb1 挂载到/mnt/test下
mount -r 只读模式挂载 LABEL=test以卷标方式挂载
lsof 可以查看当前谁在访问挂载点
fuser -v 可以查看当前谁在访问挂载点
fuser -km 可以杀掉当前访问挂载点的全部用户
持久挂载
永久挂载需要编辑/etc/fstab 文件,文件信息分别为磁盘UUID可通过blkid命令查看;挂载点;文件系统格式,挂载模式为默认,第一个0为不备份,第二个0为不做文件系统检查
1 # 2 # /etc/fstab 3 # Created by anaconda on Wed Apr 1 21:35:13 2020 4 # 5 # Accessible filesystems, by reference, are maintained under '/dev/disk' 6 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info 7 # 8 UUID=7649e9c2-1502-4fde-9f60-67ab716bbbe7 / xfs defaults 0 0 9 UUID=a6fc6fe1-c599-4f9e-81e3-0b06a3a373e9 /boot xfs defaults 0 0 10 UUID=48649b37-de95-4693-a221-9a8e795fa568 /data xfs defaults 0 0 11 UUID=f1348665-8b05-45a6-adf7-10ac42e009a7 swap swap defaults 0 0
编辑完成后执行 mount -a 命令
SWAP分区
1 [root@centos8 ~]#mkswap /dev/sdc1 2 Setting up swapspace version 1, size = 2 GiB (2147479552 bytes) 3 no label, UUID=d3140a7a-65b7-4cb7-8a2b-12d38aa98c6f 4 [root@centos8 ~]#blkid /dev/sdc1 5 /dev/sdc1: UUID="d3140a7a-65b7-4cb7-8a2b-12d38aa98c6f" TYPE="swap" 6 PARTUUID="b094d43d-01 7 [root@centos8 ~]#vim /etc/fstab 8 UUID=d3140a7a-65b7-4cb7-8a2b-12d38aa98c6f swap swap defaults 0 0 9 [root@centos8 ~]#swapon -a 10 [root@centos8 ~]#free -h 11 total used free shared buff/cache available 12 Mem: 3.7Gi 264Mi 3.2Gi 9.0Mi 261Mi 3.2Gi 13 Swap: 4.0Gi 0B 4.0Gi
swap的优先级
五、RAID
raid0
磁盘空间使用率:100%,故成本最低。
读性能:N*单块磁盘的读性能
写性能:N*单块磁盘的写性能
冗余:无,任何一块磁盘损坏都将导致数据不可用。
最少磁盘数:2,2+
raid1
RAID 1通过磁盘数据镜像实现数据冗余,在成对的独立磁盘上产生互为备份的数据。当原始数据繁忙时,可直接从镜像拷贝中读取数据,因此RAID 1可以提高读取性能。RAID 1是磁盘阵列中单位成本最高的,但提供了很高的数据安全性和可用性。当一个磁盘失效时,系统可以自动切换到镜像磁盘上读写,而不需要重组失效的数据。
磁盘空间使用率:50%,故成本最高。
读性能:只能在一个磁盘上读取,取决于磁盘中较快的那块盘
写性能:两块磁盘都要写入,虽然是并行写入,但因为要比对,故性能单块磁盘慢。
冗余:只要系统中任何一对镜像盘中有一块磁盘可以使用,甚至可以在一半数量的硬盘出现问题时系统都可以正常运行。
raid5
RAID 5是RAID 0和RAID 1的折中方案。RAID 5具有和RAID0相近似的数据读取速度,只是多了一个奇偶校验信息,写入数据的速度比对单个磁盘进行写入操作稍慢。同时由于多个数据对应一个奇偶校验信息,RAID5的磁盘空间利用率要比RAID 1高,存储成本相对较低,是目前运用较多的一种解决方案。
工作原理:
RAID5把数据和相对应的奇偶校验信息存储到组成RAID5的各个磁盘上,并且奇偶校验信息和相对应的数据分别存储于不同的磁盘上,其中任意N-1块磁盘上都存储完整的数据,也就是说有相当于一块磁盘容量的空间用于存储奇偶校验信息。因此当RAID5的一个磁盘发生损坏后,不会影响数据的完整性,从而保证了数据安全。当损坏的磁盘被替换后,RAID还会自动利用剩下奇偶校验信息去重建此磁盘上的数据,来保持RAID5的高可靠性。
磁盘空间利用率:(N-1)/N,即只浪费一块磁盘用于奇偶校验。
读性能:(n-1)*单块磁盘的读性能,接近RAID0的读性能。
冗余:只允许一块磁盘损坏。
最少磁盘数:3,3+
raid10
RAID10也被称为镜像阵列条带。像RAID0一样,数据跨磁盘抽取;和RAID1一样,每个磁盘都有一个镜像磁盘, 所以RAID 10的另一种会说法是 RAID 0+1。RAID10提供100%的数据冗余,支持更大的卷尺寸,但价格也相对较高。对大多数只要求具有冗余度而不必考虑价格的应用来说,RAID10提供最好的性能。使用RAID10,可以获得更好的可靠性,因为即使两个物理驱动器发生故障(每个阵列中一个),数据仍然可以得到保护。RAID10需要4 + 2*N 个磁盘驱动器(N >=0), 而且只能使用其中一半(或更小, 如果磁盘大小不一)的磁盘用量, 例如 4 个 250G 的硬盘使用RAID10 阵列, 实际容量是 500G。
虽然Raid10方案造成了50%的磁盘浪费,但是它提供了200%的速度和单磁盘损坏的数据安全性,并且当同时损坏的磁盘不在同一Raid1中,就能保证数据安全性。假如磁盘中的某一块盘坏了,整个逻辑磁盘仍能正常工作的。
当我们需要恢复RAID10中损坏的磁盘时,只需要更换新的硬盘,按照RAID10的工作原理来进行数据恢复,恢复数据过程中系统仍能正常工作。原先的数据会同步恢复到更换的硬盘中。
磁盘空间利用率:50%。
读性能:N/2*单块硬盘的读性能
写性能:N/2*单块硬盘的写性能
冗余:每组镜像最多只能坏一块
最少磁盘数:4,4+
六、逻辑卷
1 #创建物理卷 2 pvcreate /dev/sda3 3 #为卷组分配物理卷 4 vgcreate vg0 /dev/sda3 vgcreate+卷组名称+要组成卷组的物理卷 5 #从卷组创建逻辑卷 6 lvcreate -L 256M -n data vg0 lvcreate -n 指定逻辑卷名称 lvcreate -L指定从卷组分配的大小 7 #mkfs.xfs /dev/vg0/data 为创建的逻辑卷创建文件系统 8 #挂载 9 mount /dev/vg0/data /mnt/data#
通过卷组给逻辑卷扩展空间
lvextend 扩展逻辑卷,+100%free 表示将卷组剩余的空间全部分配
blkid命令确定新增加逻辑卷为ext4系统,resize2fs命令为同步新增空间ext4文件系统(xfs文件系统同步命令为xfs_growfs +挂载点文件名)
通过新增加磁盘扩展卷组
将新增加磁盘或分区id改为8e后用pvcreate命令指定为物理卷
用vgextend命令 +需要扩展的卷组名 +新增的分区或磁盘
lvextend -r 命令可直接扩展逻辑卷大小及文件系统同步,
逻辑卷快照
1 #为现有逻辑卷创建快照 2 lvcreate -l 64 -s -n data-snapshot -p r /dev/vg0/data 3 #挂载快照 4 mkdir -p /mnt/snap 5 mount -o ro /dev/vg0/data-snapshot /mnt/snap 6 #恢复快照 7 umount /dev/vg0/data-snapshot 8 umount /dev/vg0/data 9 lvconvert --merge /dev/vg0/data-snapshot 10 #删除快照 11 umount /mnt/databackup 12 lvremove /dev/vg0/databackup