[Make it clear, to say it! Disk Management]

Contents:
(a) Introduction hard disk structure
(b) how to partition management
(c) of the file system management
(4) to administer swap


(A) describes the structure of a hard disk
(1.1) apart and our hard disk is constructed, we found that each disk will have the layers of a circle, two intermediate part we call circle track, each tray It will be divided into a sheet piece, which we call a per sector, the size of each sector is 512 bytes (512B). Each track from the outer track to the inner are 0, Track 1, Track 2, etc., on each track will be divided into sector 0, sector 1, sector 2 and the like, for example, we have referred to the starting position of a hard disk track 0 0 sectors. The minimum hard disk structures we call "sector", the smallest units of the file system we call "block", so the sector is our smallest units in the division partition.
[Make it clear, to say it!  Disk Management]
(1.2) in our hard disk is composed of a plurality of sectors, for example, we first partition sector 2048 from beginning to end sector 6000, and then we partition format, default file system block size is 4k, that is, 8 sectors, the system default is 8 sectors a block.
(1.3) We now have a hard drive, we assume that a total of 10000 sectors, me with you altogether divided into four partitions, sda1 partition sector from 2048 to the end of 4000 beginning sector, sda2 partition start sector from 4001 to 6000 end zone, sda3 partition sector from 6001 to the end of 8000 beginning sector, sda4 partition sector starting from 8001 to the end of 10000 sectors. In order to record information partitioning in our system, we first sector of the hard disk recording a variety of information, which we call the MBR, the master boot record, the size of which is the first sector 512B , whose structure is composed of three parts: a first part which occupies 446 bytes, i.e. bootLoader boot loader, the main role is our system boot up; the second portion occupies 64 bytes, here information Zoning is the beginning and end of our specific sector of system partition table, major record; the third part size occupied 2B, terminator represented here.
[Make it clear, to say it!  Disk Management]
(1.4) Note that the major record partition table 64B of the primary and extended partitions information, other partitions can not be recorded. The system is divided into a main partition means directly from the hard disk can be formatted for use; refers to the extended partition divided directly from the hard disk, it can not be formatted for use, but which can be divided into more partitions; Logical the main partition is divided from the extended partition, not directly from the hard disk partition directly.
(1.5) and the system requires only one partition extended partition, each partition table recording partition information consumes 16 bytes, and therefore under normal conditions 64B of the partition table is divided at most 64/16 = 4 partitions. If we want to create more partitions, we need to create an extended partition, and then in the extended partition to create logical partition resolution, for the way we call such a partition MBR hard disk structure, its compatibility is better, not suitable larger than 2T hard disk partition. For 2T larger than the size of the hard disk to GPT we generally use to manage, it is a new hard disk management, it is possible to divide GPT 128 primary partitions. Generally, we can choose to divide the hard disk which format, this selection process, which we call initialize the disk label.
(1.6) In general we use at the time of the hard disk MBR operating structure is fdisk command; when we operate on GPT hard disk format, we are using gdisk command; a new hard drive if we do not know its format , if we want to operate the hard drive, then we can use the parted command can operate on both MBR disk format, it can operate on GPT formatted drives.


(二)如何对分区进行管理
(2.1)图形化界面操作
(2.1.1)我们在vms002主机上再重新添加一块20G的硬盘,然后开机进入系统当中。
[Make it clear, to say it!  Disk Management]
[Make it clear, to say it!  Disk Management]
[Make it clear, to say it!  Disk Management]
(2.1.2)首先我们在图形化的界面进行操作,我们选择“工具”--“磁盘”,然后便可以看到我们自己添加的20G的硬盘(图2-5),然后我们便可以自己选择是通过MBR还是GPT格式的硬盘进行分区(图2-6)。
[Make it clear, to say it!  Disk Management]
[Make it clear, to say it!  Disk Management]
[Make it clear, to say it!  Disk Management]
(2.1.3)我们选择MBR的方式进行格式化(图2-7),然后我们开始划分分区,我们自定义一个xfs类型的分区,创建2G的分区大小(图2-8)。
[Make it clear, to say it!  Disk Management]
[Make it clear, to say it!  Disk Management]
[Make it clear, to say it!  Disk Management]
(2.2)
(2.2.1)以上我们是通过图形化的界面方式对硬盘进行分区的,我们也是可以使用命令行的方式进行操作的,由于刚刚图形化界面的操作将硬盘格式化为GPT的格式了,所以我们首先将vms002主机的GPT格式先删除掉,然后我们再设置硬盘的格式为MBR,也可以设置格式类型为loop表明硬盘格式设置为未知类型。
# parted /dev/sdb mklabel gpt---将vms002主机的GPT格式先删除掉
# parted -s /dev/sdb mklabel msdos---使用-s参数指明直接修改硬盘类型不需要交互操作
# parted -s /dev/sdb mklabel loop---如果我们不需要任何的格式类型,可以使用loop
[Make it clear, to say it!  Disk Management]
(2.2.2)如果我们希望查看硬盘的所有分区信息,我们可以使用如下的命令操作。
# fdisk -l---可以显示我们当前系统的所有硬盘的分区信息
# fdisk -l /dev/sda---查看/dev/sda分区的信息
# fdisk -l /dev/sdb---查看/dev/sdb分区的信息
[Make it clear, to say it!  Disk Management]
[Make it clear, to say it!  Disk Management]
[Make it clear, to say it!  Disk Management]
(2.2.3)现在我们需要对sdb分区来进行划分分区,我们使用fdisk命令来进行操作,进入交互界面后,我们如果需要获取帮助,我们可以输入指令m(图2-14);如果我们需要删除分区,可以输入指令d;如果我们需要列出分区的类型,我们可以输入指令l;如果我们需要添加一块新的分区,可以输入指令n;如果我们需要打印分区表,我们可以输入指令P;如果我们对交互界面的操作不保存并退出,我们可以输入指令q;如果我们希望能够保存退出,我们可以输入指令w。
# fdisk /dev/sdb---设置对sdb进行划分分区
[Make it clear, to say it!  Disk Management]
(2.2.4)我们输入p可以打印当前的分区情况,接着我们输入n可以创建一个新的分区,我们知道主分区加扩展分区最多只能划分4个,我们现在先创建一个分区,同时设置分区的大小为2G,我们知道2G等于2*1024M等于2*1024*1024K等于2*1024*1024*2扇区,而系统中分区开始的位置是从2048扇区开始的,所以我们设置的2G大小的扇区的结束位置应该在(2048+2*1024*1024*2-1)即4196351这个点的扇区的位置。
[Make it clear, to say it!  Disk Management]
(2.2.5)由于上面我们计算扇区的方式非常的生涩难懂,一般是不会用到的,其实我们还可以使用更方便的方式进行添加,我们直接添加一个2G大小的分区即可。
[Make it clear, to say it!  Disk Management]
[Make it clear, to say it!  Disk Management]
(2.2.6)接着我们按照相同的方式划分出如下的两个主分区。
[Make it clear, to say it!  Disk Management]
(2.2.7)因为我们已经划分了3个主分区,如果我们还想继续再多划分3个分区,则此时我们应该最后一个分区划分为扩展分区,然后在扩展分区上创建3个逻辑分区即可。
[Make it clear, to say it!  Disk Management]
[Make it clear, to say it!  Disk Management]
(2.3)
(2.3.1)我们也可以在只创建2个主分区的情况下,将剩余的空间全部划分给逻辑分区使用。
[Make it clear, to say it!  Disk Management]
(2.3.2)然后在扩展分区上创建5个逻辑分区,每个逻辑分区的大小为2G。
[Make it clear, to say it!  Disk Management]
[Make it clear, to say it!  Disk Management]
(2.3.3)每个分区的用途是不一样的,我们看分区的ID列都是以数字表示的,有的分区是可以直接格式化使用的,例如Linux 83,但是有的分区时不可以直接格式化使用的,只能用来做其他用途,例如Extended 5。我们在交互界面输入l即可查看所有分区类型的解释。
[Make it clear, to say it!  Disk Management]
(2.3.4)我们将逻辑分区5-9都转换为了LVN类型的分区
[Make it clear, to say it!  Disk Management]
(2.3.5)如果我们对以上的操作不想保存,那么我们直接输入q退出即可,如果我们对执行的操作时认可的并且需要保存,我们输入w即可保存。我们在非系统盘里,创建一个分区会自动同步到分区表,如果我们在非系统盘里,往往不会自动同步分区表,因此这个时候我们应该要使用partprobe手动同步分区表。
# partprobe /dev/sdb---我们应该要使用手动同步分区表(RHEL 7系统)
# partx -a /dev/sdb---在RHEL 6系统中的分区同步方式
[Make it clear, to say it!  Disk Management]


(三)对文件系统的管理
(3.1)创建完分区后我们需要创建文件系统,即进行文件系统的格式化。我们可以选择将文件系统格式化为xfs文件系统的类型,xfs文件系统支持大数据,xfs文件系统最大可支持256EB,单个文件最大可达8EB,基本ext4文件系统支持的功能,xfs文件系统都是支持的,但是xfs文件系统唯一的劣势是不支持文件系统的缩小。我们使用mkfs命令,使用-t选项指明类型,使用-f选项指明直接操作,此时我们便实现了将/dev/sdb1分区格式化为xfs文件系统了。
# mkfs -t xfs -f /dev/sdb1---指定sdb1分区的文件系统类型为xfs
[Make it clear, to say it!  Disk Management]
(3.2)以上的方式我们可以对/dev/sdb1的分区进行格式化,我们还可以按照下面的方式进行格式化操作(图3-2),如果我们需要指定文件系统的最小单位block的大小,我们也可以按照如下的方式操作(图3-3)。
# mkfs.xfs -f /dev/sdb1---指定sdb1分区的文件系统类型为xfs
# mkfs.xfs -f -b size=1024 /dev/sdb1---需要指定文件系统的最小单位block的大小
[Make it clear, to say it!  Disk Management]
[Make it clear, to say it!  Disk Management]
(3.3)接着我们格式化完外分区后,在系统中创建一个aa的目录,并把分区sdb1分区挂载到aa目录下,此时我们发现分区挂载到目录下面是成功的。
# mkdir /aa---创建一个aa的目录
# mount /dev/sdb1 /aa---把分区sdb1分区挂载到aa目录下
# df -hT---查看系统的分区挂载情况
[Make it clear, to say it!  Disk Management]
(3.4)如果我们想要在系统启动后仍然能够挂载光盘,此时我们应该在/etc/fstab文件中进行编辑,我们如果需要防止磁盘的篡位的情况发生,我们应该使用设备的UUID来进行编辑(图3-5和图3-6)。此时我们发现使用“# mount -a”命令后,我们发现此时系统已经将/dev/sdb1分区挂载到/aa目录中了(图3-7)。
# blkid---查看所有分区的UUID信息
# xfs_info /dev/sdb1---重新获取格式化后的分区信息
# xfs_admin -u /dev/sdb1---单独查看/dev/sdb1分区的UUID信息
# mount -a---其中-a表示的是auto,即将所有的信息全部重新挂载一次
[Make it clear, to say it!  Disk Management]
[Make it clear, to say it!  Disk Management]
[Make it clear, to say it!  Disk Management]
(3.5)对于分区的UUID信息,我们也是可以重新生成并绑定的,此时我们先通过uuidgen命令生成一个新的UUID信息,然后将生成的UUID信息和/dev/sdb1分区重新进行绑定,并需要在/etc/fstab文件中修改UUID信息(图3-9),此时我们才可以实现开机自动挂载分区的目标(图3-10)。
# uuidgen---重新生成一个新的UUID
# xfs_admin -U 092706ec-d3d3-4ce4-baaa-f77e9fc0a221 /dev/sdb1---我们将生成的UUID信息和/dev/sdb1分区重新进行绑定
[Make it clear, to say it!  Disk Management]
[Make it clear, to say it!  Disk Management]
[Make it clear, to say it!  Disk Management]


(四)管理swap
(4.1)swap分区在系统的物理内存不够用的时候,把物理内存中的一部分空间释放出来,以供当前运行的程序使用。那些被释放的空间可能来自一些很长时间没有什么操作的程序,这些被释放的空间被临时保存到swap分区中,等到那些程序要运行时,再从swap分区中恢复保存的数据到内存中。
(4.2)在我们的系统中交换分区的作用很重要,即使我们系统的内存足够大,但是也是仍然需要到使用交换分区。而我们的swap分区主要是在/proc/sys/vm目录下,其中swappiness的值为30表示的是当我们的物理内存的使用率达到(100%-swappiness%)即70%的时候,此时我们的系统将开始使用交换分区,所以当我们需要对服务器进行优化的时候,我们就可以通过修改swappiness的值,例如修改为0,这样就可以实现在系统的内存消耗完毕后才开始使用交换分区。
[Make it clear, to say it!  Disk Management]
(4.3)另外,也并非是系统就能把“物理内存+swap”全部使用完毕,真正能分配出去的内存是由“CommitLimit=物理内存*overcommit_ratio+swap”决定。例如现在我们系统中实际的内存空间是100G,我们交换分区的大小设置的是2G,而系统中overcommit_ratio参数的值为50%,因此系统可用的内存空间的大小为100*0.5+2即为52G,如果我们现在运行一个redis的进程需要消耗60G的内存,此时我们会想当然地认为系统的内存是100G足够去运行这个redis程序,但是实际运行后我们发现后台的日志总是会有OOM killed的报错提示,这就是由于系统的overcommit_ratio参数设置不合理而导致我们系统中实际的可用内存只有52G,所以才会出现缓冲区溢出的问题。
(4.4)经验总结:一般我们的系统中交换分区的设置方式遵循如下的规则:
物理内存<=2G---swap为1.5~2倍
物理内存为4G---swap为4G
物理内存为4~16G---swap>=8G
物理内存为16~32G---swap为16G
物理内存>=32G---swap为32G
(4.5)我们如果需要查询当前的系统有多少的交换分区,则我们可以按照如下的两种方式查看
# swapon -s---查看当前系统的交换分区的信息(方法一)
# cat /proc/swaps---查看当前系统的交换分区的信息(方法二)
[Make it clear, to say it!  Disk Management]
(4.6)如果我们的交换分区不够用了,我们是可以添加交换分区的,由于我们在创建分区的时候将/dev/sdb2分区设置为swap类型(图4-4),所以我们还需要在系统中将此/dev/sdb2分区格式化为swap类型(图4-5),而系统在使用交换分区的时候是根据权限的大小来判断交换分区的使用优先级的,数值越大优先级越高,例如-1是大于-2的,所以sda2是优先于sda1使用的。
# fdisk -l /dev/sdb---查看系统中/dev/sdb分区设置的类型
# mkswap /dev/sdb2---将/dev/sdb2分区格式化为swap类型
# swapon /dev/sdb2---将/dev/sdb2分区激活
# swapon -s---查看当前系统swap分区的使用情况
[Make it clear, to say it!  Disk Management]
[Make it clear, to say it!  Disk Management]
[Make it clear, to say it!  Disk Management]
(4.7)例如我们现在想要修改提高/dev/sdb2的优先级,我们可以通过如下的方式操作
# swapoff /dev/sdb2---首先将/dev/sdb2的swap分区关闭掉
# Swapon -p 3 / dev / sdb2 --- then change the priority of improving / dev / sdb2 and turned into effect
[Make it clear, to say it!  Disk Management]
(4.8) if we want the system to reboot still able to mount the swap partition, we can / etc / fstab file are set to mount a swap partition, and set the priority to 3 (Figure 4-7), then we need to remember to use "# swapon -a" command will take effect for all swap swap partition (Figure 4-8).
[Make it clear, to say it!  Disk Management]
[Make it clear, to say it!  Disk Management]
(4.9) is more than we used to create a partition way to add swap partition, but in fact, sometimes our system there is no extra partition, when in fact we can add a swap file by the way, we can 500M to create a file using the dd command, and then create the file format for the exchange swap partition, and mount use.
# Dd if = / dev / zero of = / swapfile bs = 1M count = 500 --- create a 500M file
# chmod 400 / swapfile --- setting file permissions we created 400
# mkswap / swapfile --- the / swapfile swap partition formatted as type
# swapon / swapfile --- a / swapfile activate partition
# swapon -s --- see the current mount of the system swap
[Make it clear, to say it!  Disk Management]

------ This concludes the article, thanks for reading ------

Guess you like

Origin blog.51cto.com/13613726/2460749