Linux Disk Management



6.1 View the capacity of the disk or directory

Command df : used to view the total capacity, used capacity, remaining capacity, etc. of the mounted disk. You can add no parameters. The default unit is KB .

 

 

Among them , / and /boot are divided when we install the system. /dev and /dev/shm are memory partitions, and the default size is 1/2 of the memory size . If we save the file to this partition, it is equivalent to saving it in the memory. The advantage is that reading and writing is very fast, and the disadvantage is that when the system restarts files will be lost. The following partitions such as /run and /sys/fs/cgroup are all tmpfs , similar to /dev/shm , which are temporary file systems.

 

df -i : Indicates to check the usage status of inodes . If it is 100% used , even if there is surplus disk space, it will prompt that the disk space is full.

 

 

df -h : Indicates display in appropriate units, such as GB

 

df -k : means display in KB .

 

df -m : Indicates display in MB .

 

Note: If the Use% is more than 90% , you need to pay attention to the following, if the disk partition is full, it will cause the system to crash

 

 

Command du : used to view the size of the space occupied by a directory or file, the format is du [-abckmsh] [ file or directory name ]

 

du -a : Indicates that the sizes of all files and directories are listed. If no options and arguments are followed, only the size of the directory (including subdirectories) will be listed. If the du command does not specify a unit, the default display unit is " KB "

 

du -b : Indicates that the listed values ​​are output in units of B.

 

du -k : Indicates output in KB , which is the same as the default output value without any options.

 

du -m : means output in MB .

 

du -h : Indicates that the system automatically adjusts the unit.

 

du -c : indicates the final sum.

 

du -s : Indicates that only sums are listed.

 

 

6.2 Partitioning and Formatting of Disks

 

add virtual disk

 

 

 

 

After adding a new virtual disk, you need to restart the system.

 

 

 

command fdisk

FdiskLinux下硬盘的分区工具,是一个非常实用的命令,但是fdisk只能划分小于2TB的分区。格式为fdisk [-l] [设备名称],其选项只有-l。选项-l后面不加设备名称,会直接列出系统中所有的磁盘设备已经分区表;加上设备名称,则会列出该设备的分区表。

 

 

 

 

 

 

p:表示打印当前磁盘的分区情况。

 

n:表示建立一个新的分区

w:表示保存

q:表示退出

d:表示删除一个分区

 

开始分区:

 

备注:p+e<=4,p主分区、e扩展分区(内含逻辑分区)。

这样创建分区之后在保存! --创建4个主分区之后在继续创建:

备注:如果你想创建超过四个的分区,你必须先将一个扩展分区替换一个主分区。 --替换

打印当前分区状况。

 

在创建4个分区之后

 

备注:此时再分区就和之前不一样了,不用在选择是主分区还是扩展分区。 备注:当创建完3个主分区后,理应把剩余的磁盘空间全部划分给第四个扩展分区,否则会导致浪费。

 --再次打印当前分区状况。

备注:上例中,/dev/sdb4为扩展分区,这个分区是不可能格式化的。你可以把它看成一个空壳子,能使用的分区/dev/sdb5,它是/dev/sdb4的子分区,这个子分区被称为逻辑分区。 --》如果你发现分区不合理,想删除某个分区,那该怎么办?

--在只删除sdb1的前提下。

 

备注:在分区界面中,直接按Ctrl+C键退出,这样刚做的分区便全部取消。 --》如果创建一个扩展分区并把所有的空间给扩展分区,那么分区时会提示

备注:分区类型(primary partition)和逻辑分区(logical partition)。l表示逻辑分区,p表示主分区,逻辑分区id是由5开始的,因为前4个分区为主分区或扩展分区。如果把所有的磁盘空间分配给拓展分区,如果你在这里选择p,则会报错! --分区完成后,需要输入w命令来保存我们的配置。 

 

--然后使用fdisk -l /dev/sdb查看分区情况。

 

 

6.3格式化磁盘分区

mke2fs -b:表示分区时设定每个数据区块占用的空间大小。目前,每个数据块支持1024B2048B以及4096B

mke2fs -i:表示设定inode的大小

mke2fs -N:表示设定inode的数量。有时默认的inode数不够用,所以要自定义inode的数量。

mke2fs -c:表示在格式化前先检测以下磁盘是否有问题。加上这个选项

mke2fs -L:预设该分区的标签label

mke2fs -j:建立ext3格式的分区,如果使用mkfs.ext3 就不用加这个选项了

mke2fs -t:用来指定什么类型的文件系统,可以是ext2, ext3 也可以是 ext4.

备注:指定文件系统格式为ext4时,命令mke2fs -t ext4 /dev/sdb5等同于mkfs.ext4  /dev/sdb5。然而,mke2fs并不支持把分区格式化成XFS类型,而只能使用mkfs.xfs

备注:你是否注意到bsize(块大小)=4096这个指标,在这里涉及到了块的概念:磁盘在格式化的时候,会预先规定好每一块的大小,然后再把所有的空间分隔成一个一个的小块。存数据的时候,也是一块一块的写入,所以我们在指定块大小时要按需指定。 --在格式化时,指定块的大小。

备注:提示8192的块太大。 --重新格式化sdb5时,会提示这个分区已经格式化过。

--加-f选项就可以了。 

--给分区指定标签。

 

 

命令e2label:用于查看或修改分区的标签,只支持ext格式的文件系统,不支持XFS文件系统

 

 

6.4挂载/卸载磁盘

在挂载某个分区前需要先建立一个挂载点,这个挂载点是以目录的形式出现的。一旦把某一个分区挂载到了这个挂载点(目录)下,那么再往这个目录写数据使,则都会写到该分区中。这就需要你注意一下,在挂载该分区前,挂载点(目录)下必须是个空目录。其实目录不为空并不影响所挂载分区的使用,但是一旦挂载上了,那么该目录下以前的东西就不能看到了。只有卸载掉该分区后才能看到。

 

命令mount

不加任何东西使用mount命令,可以查看当前系统已经挂载的所有分区,以及分区文件系统的类型,挂载点和一些选项等信息,所以你如果想知道某个分区的文件系统类型直接用该命令查看即可。

mount -a

mount -t:用来指定挂载的分区类型,默认不指定,会自动识别

mount -o: 用来指定挂载的分区有哪些特性

/etc/fstab配置文件

 

这个文件是系统启动时,需要挂载的各个分区。

 

第一列就是分区的标识,可以写分区的LABEL,也可以写分区的UUID,当然也可以写分区名(/dev/sda1)

第二列是挂载点

第三列是分区的格式

第四列是mount的一些挂载参数。

第五列的数字表示是否被dump备份,是的话这里就是1,否则就是0

第六列是开机时是否自检磁盘。12都表示检测,0表示不检测,在Redhat/CentOS中,这个12还有个说法,/ 分区必须设为1,而且整个fstab中只允许出现一个1,这里有一个优先级的说法。12优先级高,所以先检测1,然后再检测2,如果有多个分区需要开机检测那么都设置成2吧,1检测完了后会同时去检测2

 

4列常用选项

async/sync : async表示和磁盘和内存不同步,系统每隔一段时间把内存数据写入磁盘中,而sync则会时时同步内存和磁盘中数据

auto/noauto : 开机自动挂载/不自动挂载;

default : 按照大多数永久文件系统的缺省值设置挂载定义,它包含了rw, suid, dev, exec, auto, nouser, async

ro : 按只读权限挂载

rw : 按可读可写权限挂载

exec/noexec 允许/不允许可执行文件执行,但千万不要把根分区挂载为noexec,那就无法使用系统了,连mount命令都无法使用了,这时只有重新做系统了

user/nouser : 允许/不允许root外的其他用户挂载分区,为了安全考虑,请用nouser

suid/nosuid : 允许/不允许分区有suid属性,一般设置nosuid

usrquota 启动使用者磁盘配额模式,磁盘配额相关内容在后续章节会做介绍

grquota : 启动群组磁盘配额模式

 

 

命令blkid

 

 

 

 

 

命令umount

umount:卸载磁盘(后面可跟挂载点,也可跟分区名,但是不可以跟LABELUUID

 

注:有时会遇到不能卸载的情况,原因是因为当前目录还在卸载的分区上。解决方法是:进入到其他目录;或者使用umount命令的-l选项。

 

6.5建立一个swap文件增加虚拟内存

类似与windows的虚拟内存,分区的时候一般大小为内存的2倍,如果你的内存超过8G,那么你分16G似乎是没有必要了。分16G足够日常交换了。然而,还会有虚拟内存不够用的情况发生。这时我们可增加一个虚拟的磁盘出来。基本的思路就是:建立swapfile -> 格式化为swap格式 -> 启用该虚拟磁盘。

 

文件格式化以后就挂在使用了(其中,free命令用来查看内存的使用情况,-m表示以MB为单位显示)。

 

备注:前后对比一下swap分区,我们发现多了1024M.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324483139&siteId=291194637