Getting linux disk management 12-- series of partition, format and mount

In front of a series of articles explaining the VI editor, commonly used commands, firewall and network management services herein, this would explain the disk management knowledge.

This article will introduce a large number of Linux commands, part of which has been described in the "Getting Started series 5-- linux newbie of linux command will be" a text, forgotten or never learned in your own view.

Disk Management addresses and disk expansion disk quota management, when the server disk space reaches a certain level (personally think that the general use accounted for 70-80% of total capacity) need to consider adding a new disk to achieve expansion, the general procedure for the partition, format of, mount, validation and use. The disk quota is mainly for the user to set the maximum amount of disk space to use to prevent the situation when users take up too much disk. The following management operations will simulate these disks in a virtual machine, the production environment and operation commands are the same way.

Before demonstrating the specific operation to find out the relevant theoretical knowledge.

First, the disk management theory of knowledge

1.1 file structure and file positioning

Everything Linux file system, the file since it is involved in locating files and files stored lookup.

1.1.1 file structure

In the windows system, we will usually store files and then the next disk is divided into several different drive letter (such as C, D, E, F, etc.), data stored in a different drive letter. When looking for is find the appropriate storage folder based on the corresponding letter, and then find the corresponding file.

However, in Linux, the situation is somewhat different, everything is a file from the root directory of the beginning, not the same as windows letter saying there is, and the directory name under Linux is strictly case-sensitive.

Files stored in Linux there is a certain law, it is the use of a tree structure in accordance with the file system hierarchy standard (FHS) for files and define common use of directory (that is what defines what folder to put file) . Provision of specific directory tree directory structure and use FHS see the second article in the series - Centos directory structure part of the "linux Getting Started Series 2 GUI basic operations and directory structure", not discussed here.

Note: FHS only an agreed standard does not mandate a certain type of file must be placed in the specified folder, and therefore need to pay attention when taking over a new server, and some people did according to this standard may be placed in the file, you need to flexible treatment. Of course strongly recommended to manage the files by FHS requirements.

1.1.2 Positioning Files

Files are located by the path, into the absolute path and relative path .

Refers to an absolute path starting from the root directory to a file or directory, the relative path is relative to the current path.

Example assumed that the current path: / root / test / test1

[root@heimatengyun test1]# pwd
/root/test/test1
[root@heimatengyun test1]# ls
hello  test
[root@heimatengyun test1]# cat hello 
hello 
[root@heimatengyun test1]# cat /root/test/test1/hello 
hello
[root@heimatengyun test1]# 

Hello there is a file in / root / test / test1 directory, where "cat hello" hello command is relative to the file in the current directory, and "cat / root / test / test1 / hello" is given a complete absolute path. Have access to specific files through a relative and absolute paths.

Shortcuts 1.2 Linux file

1.2.1 Linux's "shortcut"

I believe we must know a shortcut under windows, especially shortcut software. For example, the first article, "Getting Started series 1-- linux and linux environment ready to install" when explaining Install Virtual Machine VMware, install the appropriate files in the specified drive letter, and it will create a shortcut on the desktop and menu, so every time start the virtual machine only need to click the desktop shortcut you can, without having to find each corresponding letter, and then enter the directory click on the appropriate file to run.

The obvious advantage of shortcuts, the same Linux system, there are shortcuts, but some differences shortcuts in Linux and Windows shortcuts.

The amount of the original Windows shortcut file pointing to a connection file, the original file once you delete or move the position of the connection shortcut will lead to failure. But in Linux it has two types of links: soft links and hard links .

  • Soft links

Also called symbolic link path name (symbolic link), only contains the linked file, the catalog file may be attached, may be linked across file systems. In Windows shortcuts similar to the original file is deleted, the linked file will fail.

  • Hard links

Hard link to the original file is the same file, but a different name, each adding a hard link, inode number of connections of the file is increased by one, and only when the number of connections of the file is 0 considered completely delete the file. It simply is actually a hard link pointing to the original file pointer, even if the original file is deleted, you can still be accessed via a hard link files. Directory files across partitions can not be linked.

1.2.2 Linux software, create hard links

Create a link using the ln command , syntax is: ln [options] link destination file name

Common parameters in the following table, more parameters and usage, please use the "man ln" command.

parameter effect
-s symbolic, create a symbolic link, if this parameter without default create a hard link
-f force, force the creation of links to the file or directory
-i Ask first before covering

Case:

(1) Create a soft link and delete the original file, verify that you can view the linked files

[root@heimatengyun test]# mkdir testln
[root@heimatengyun test]# cd testln/
[root@heimatengyun testln]# echo "symbolic link">slink
[root@heimatengyun testln]# ln -s slink slink.ln
[root@heimatengyun testln]# cat slink
symbolic link
[root@heimatengyun testln]# cat slink.ln 
symbolic link
[root@heimatengyun testln]# ll
total 4
-rw-r--r--. 1 root root 14 Dec 26 23:23 slink
lrwxrwxrwx. 1 root root  5 Dec 26 23:24 slink.ln -> slink
[root@heimatengyun testln]# rm -f slink
[root@heimatengyun testln]# cat slink.ln 
cat: slink.ln: No such file or directory
[root@heimatengyun testln]# ls
slink.ln
[root@heimatengyun testln]# rm slink.ln 
rm: remove symbolic link ‘slink.ln’? y
[root@heimatengyun testln]# ls
[root@heimatengyun testln]# 

Testln create folders and switch to slink create the original file under the folder, and then create a soft link file slink slink.ln. View Files found reference amount is 1, and the link will file arrow logo. At this time, delete the original file slink, and then link the file slink.lnye can not access. Although the file can not be accessed but the file still exists, you can delete it manually.

(2) create a hard link and delete the original file, verify that you can view the linked files

[root@heimatengyun testln]# ls
[root@heimatengyun testln]# echo "hard link">hlink
[root@heimatengyun testln]# ln hlink hlink.ln
[root@heimatengyun testln]# cat hlink
hard link
[root@heimatengyun testln]# cat hlink.ln 
hard link
[root@heimatengyun testln]# ll
total 8
-rw-r--r--. 2 root root 10 Dec 26 23:30 hlink
-rw-r--r--. 2 root root 10 Dec 26 23:30 hlink.ln
[root@heimatengyun testln]# rm -f hlink
[root@heimatengyun testln]# cat hlink.ln 
hard link
[root@heimatengyun testln]# ll
total 4
-rw-r--r--. 1 root root 10 Dec 26 23:30 hlink.ln

Hlink create the original file, and then hlink file hard links hlin.ln files, view file reference amount shown as 2, and there is not the same as soft link arrow logo. Delete the original file, linked files still normal visit, to see the amount of application file has been changed to 1 again.

1.3 physical hardware device naming rules

Hardware devices in Linux is a file format definition and management, and file naming convention needs, udev device manager in the / dev / directory defines the name of core equipment for all of the equipment, it is the current default Linux device management tools , run as a daemon in the form of equipment to manage files in the / dev directory issued to uevent by listening kernel. By naming the purpose is to allow users to guess the name of the device file to the device roughly the property and zoning information.

Linux system common hardware device file names are as follows:

hardware equipment file name
IDE devices /dev/hd[a-d]
SCSI / SATA / U disk /dev/sd[a-p]
Floppy /dev/fd[0-1]
printer /dev/lp[0-15]
CD-ROM /dev/cdrom
mouse /dev/mouse
Tape Drives / Dev / st0 or / dev / ht0

Also commonly used SCSI / SATA hard drives or optical drives, such as when the disk expansion disk space management equipment will be used when the installation CD-ROM image will be used optical devices.

A hard disk device typically begins with "/ dev / sd", and a host may have multiple hard disks, so the system uses a ~ p to represent 16 different hard disk, the default is allocated from a. Section number there are certain rules: primary or extended partition are numbered from 1-4, logical partitions are numbered starting from 5.

Suppose a device file named: / dev / sda5, we detailed analysis of the file name under which the device information is included?

file

Save the / dev / directory file should be a hardware device; SD represents is a storage device; A represents a system interface similar to the first device is identified; 5 indicates that the device is a logical partition. Sentence Description: This is the first system to a hardware device identified in the logical partition of the partition number of device files 5.

Then look at the disk device is another example, see the machine mount

[root@heimatengyun ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
...省略部分内容
/dev/sda1                497M  119M  379M  24% /boot

/ Dev / sda1 indicates that this is the first system to a hardware device identified in the primary partition of the partition number of a device file.

1.4 Linux file system type

The role of the file system is rational planning hard disk, the file executed by the user in the hardware storage device build, write, read, modify, dump and control operations are relying on the file system to complete.

Linux system to support dozens of file systems, common are the following:

File system description
Ext3 It is a journaling file system, the file system can avoid data loss when the system is abnormal downtime, and can automatically fix inconsistencies and erroneous data. The disadvantage is that when a large hard disk capacity, long time to repair
ext4 Improved version of Ext3, is RHEL6 default file system management , storage capacity up to 1EB, which greatly improves the efficiency of reading and writing than Ext3
XFS Is a high performance journaling file system, is RHEL7 default file management system , the advantage is very fast recovery after unplanned downtime, and supports a maximum storage capacity of 18EB

After getting a new hard drive storage device, you need to partition, and then format the file system, and finally to mount and use it normally. Hard disk partition operations depends on the requirements and size of the hard disk, may choose not partitioned, but the hard disk must be formatted.

Just when it comes to Linux system to support dozens of file system, in fact, the underlying operating system for each file must have a lot of differences, but in order to allow users to read and write files is not concerned about the underlying structure of the hard drive, Linux kernel software layer program provides a user interface to VFS (Virtual file system), this interface encapsulates the characteristics and details of the underlying file system, so the user is actually unified the VFS operation in operating files, and thus do not have to care about each different file systems .

VFS architecture diagram is as follows:

file

1.5 mount command to mount hardware

I believe we have been used on windows mobile hard disk or U disk, plug and play, no other operation is very convenient. But in fact the windows are in fact quietly help us to do a lot of things, it allows us to access the newly inserted device. In the Linux system when we get a new hard disk, we need to partition, format, and only then mount use.

When the data is actually mounted device or partition the hard disk is required, first associate it with a file directory existing processes mentioned herein. Mounting and unmounting the main use two commands: mount, umount. The following were introduced.

1.5.1 mount command

grammar:

mount [argument] file system mount directory

parameter:

parameter effect
-a Mount all file systems in / etc / fstab defined
-t Specifies the type of file system

Wherein -a parameter, it automatically checks / etc / fstab file whether or omissions file device is mounted after performing, if so, the automatic loading operation. The -t parameter specifies the relative need for newer Linux systems, the system will automatically judge.

Case:

(1) the equipment / dev / sdb2 mount / test directory

Fill device and mount the directory parameter to the mount command, the system will automatically go to determine the type of file you want to mount.

[root@heimatengyun ~]# mount /dev/sdb2 /backup

Description: here only to illustrate the use of the mount command, in fact, at this time and did not join the second sdb block device hardware, so if you execute this command may not succeed immediately. The next section, "Adding hard" combat will demonstrate how to complete a new disk device mounted to the system.

执行上边mount命令后设备即可挂载成功,但是这是临时的,当系统重启后挂载就会失效,需要每次开机都手动挂载一下,这比较麻烦。我们可以通过将挂载信息按指定格式写入/etc/fstab文件,这样硬件设备就会在每次开机后自动进行关联。

下面我们研究下该fstab文件的内容和格式:

[root@heimatengyun ~]# cat /etc/fstab 
...省略注释部分
/dev/mapper/centos-root /                       xfs     defaults        1 1
UUID=bfd83b27-ed2b-4770-8d17-9e5412f2be27 /boot                   xfs     defaults        1 2
/dev/mapper/centos-swap swap                    swap    defaults        0 0

可以看到,其格式固定为: “设备文件 挂载目录 格式类型 权限选项 是否备份 是否自检

各个自动含义如下表:

字段 含义
设备文件 一 般 为 设 备 的 路 径 + 设 备 名 称 , 也 可 以 写 唯 一 识 别 码 ( UUID , Universally Unique Identifier)
挂载目录 指定要挂载到的目录,需在挂载前创建好
格式类型 指定文件系统的格式,比如 Ext3、Ext4、XFS、SWAP、iso9660(此为光盘设备)等
权限选项 若设置为 defaults,则默认权限为:rw, suid, dev, exec, auto, nouser, async
是否备份 若为 1 则开机后使用 dump 进行磁盘备份,为 0 则不备份
是否自检 若为 1 则开机后自动进行磁盘自检,为 0 则不自检

学习fstab文件格式后,我们便可以将上边通过命令挂载的/dev/sdb2设备信息写入fstab文件,使其永久生效。

假设文件系统类型为ext4,保持默认全选无须开机自检,则在fstab文件添加如下一行即可。

/dev/sdb2 /test ext4 defaults 0 0
1.5.2 umount命令

umount 命令用于撤销已经挂载的设备文件。挂载文件系统的目的是为了使用硬件资源,而卸载文件系统就意味不再使用硬件的设备资源。挂载操作就是把硬件设备与目录进行关联的动作,因此卸载操作只需要说明想要 取消关联的设备文件或挂载目录的其中一项即可。

语法:

​ umount [挂载点/设备文件]

案例:

​ 假设卸载掉前边挂载的/dev/sdb2设备,命令如下:

[root@heimatengyun ~]# umount /dev/sdb2

1.6 du命令查看目录或文件大小

我们使用du命令查看一个或多个文件占用了多大的硬盘空间。

语法:

​ du [选项] [文件或目录]

选项:

参数 作用
-h human-readable,已人类可读的方式显示单位(K、M、G等)
-s summarize,仅显示后边参数的总数

案例:

(1)使用du命令查看目录大小

以我本机的test目录为例,其中有目录有文件,如下图:

[root@heimatengyun ~]# ll test/
total 104
-rw-rwxr--+ 1 root root 135 Dec  1 23:26 case.sh
-rw-rwxr--+ 1 root root 219 Dec  2 23:57 function.sh
-rw-rwxr--+ 1 root root  91 Dec  2 21:55 getsum.sh
-rw-rwxr--+ 1 root root  85 Dec  2 21:48 greeting.sh
-rwxrwxrwx+ 1 root root  53 Dec  1 09:22 hello.sh
-rw-rwxr--+ 1 root root 186 Dec  1 23:11 if.sh
-rwsrwxrwx+ 1 root root 145 Dec  1 16:06 mypid.sh
-rw-rwxr--+ 1 root root 142 Dec  3 00:18 para.sh
-rw-rwxr--+ 1 root root 338 Dec  1 16:00 pid.sh
-rw-rwxr--+ 1 root root  99 Dec  1 15:22 sum.sh
drwxrwxrwx+ 2 test test  29 Dec 20 14:52 test1
-rw-rwxr--+ 1 root root   9 Nov 30 20:43 test1.txt
-rw-rwxr--+ 1 root root   0 Nov 30 15:34 test2.txt
drwxr-xr-x. 2 root root  21 Dec 26 23:31 testln
-rw-rwxr--+ 1 root root 139 Dec  2 23:55 while.sh

接下来,我们看下不同参数的作用

[root@heimatengyun ~]# du test/
20      test/test1
4       test/testln
132     test/
[root@heimatengyun ~]# du -s test/
132     test/
[root@heimatengyun ~]# du -sh test/
132K    test/

通过以上案例,我们可以看到,仅仅可以看到test目录下的一级目录大小,如果加-s参数则仅仅能看到自身目录大小,如果加-h参数则可以看到添加了单位k。

如何查看目录下所有内容呢?

(2)查看目录包括文件和目录的所有内容的大小

[root@heimatengyun ~]# du test/*
8       test/case.sh
8       test/function.sh
8       test/getsum.sh
8       test/greeting.sh
8       test/hello.sh
8       test/if.sh
8       test/mypid.sh
8       test/para.sh
8       test/pid.sh
8       test/sum.sh
20      test/test1
8       test/test1.txt
4       test/test2.txt
4       test/testln
8       test/while.sh
[root@heimatengyun ~]# du -s test/*
8       test/case.sh
8       test/function.sh
8       test/getsum.sh
8       test/greeting.sh
8       test/hello.sh
8       test/if.sh
8       test/mypid.sh
8       test/para.sh
8       test/pid.sh
8       test/sum.sh
20      test/test1
8       test/test1.txt
4       test/test2.txt
4       test/testln
8       test/while.sh
[root@heimatengyun ~]# du -sh test/*
8.0K    test/case.sh
8.0K    test/function.sh
8.0K    test/getsum.sh
8.0K    test/greeting.sh
8.0K    test/hello.sh
8.0K    test/if.sh
8.0K    test/mypid.sh
8.0K    test/para.sh
8.0K    test/pid.sh
8.0K    test/sum.sh
20K     test/test1
8.0K    test/test1.txt
4.0K    test/test2.txt
4.0K    test/testln
8.0K    test/while.sh
[root@heimatengyun ~]# 

对比以上三个命令可以看出,如果命令后用*表示查看所有内容,那么-s参数将失去意义,加与不加此参数效果都一样。

1.7 df命令查看磁盘空间

df命令用户查磁盘的挂载信息及空间占用情况。

语法:

​ df [参数] [目录]

常用参数:

参数 作用
-a all,显示所有文件,包括虚拟文件系统
-h human-readable,以人类可读的方式大小,比如单位K,M,G等

案例:

(1)查看指定目录挂载状态及占用的磁盘空间

[root@heimatengyun ~]# df -h /newDisk/
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdb1       2.0G   33M  2.0G   2% /newDisk

(2)查看所有一级目录占用空间大小

[root@heimatengyun ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   18G  3.7G   14G  21% /
devtmpfs                 905M     0  905M   0% /dev
tmpfs                    914M   80K  914M   1% /dev/shm
tmpfs                    914M  8.9M  905M   1% /run
tmpfs                    914M     0  914M   0% /sys/fs/cgroup
/dev/sdb1                2.0G   33M  2.0G   2% /newDisk
/dev/sda1                497M  119M  379M  24% /boot

如果加-a参数,则显示所有的目录

[root@heimatengyun ~]# df -ah
Filesystem               Size  Used Avail Use% Mounted on
rootfs                    18G  3.7G   14G  21% /
proc                        0     0     0    - /proc
sysfs                       0     0     0    - /sys
devtmpfs                 905M     0  905M   0% /dev
...省略部分内容

具体参数可以通过man命令查看,并多多练习查看不同参数的不同结果。

二、添加硬盘

通过前面的学习,我们已经具备磁盘管理的能力,因此本节将模拟真实生产环境中对服务器添加磁盘设备进行扩容管理。

整体步骤为:在虚拟机中模拟添加入一块新的硬盘存储设备、分区、格 式化、挂载、检查系统的挂载状态、真实使用硬盘验证是否成功添加。

2.1 添加新磁盘

此过程我们在虚拟机中模拟添加一块新的磁盘设备。

确保关闭虚拟机后,我们在虚拟机管理主界面单击“编辑虚拟机设置”选项

file

在弹出框中单击“添加按钮”

file

选择硬件类型为“磁盘”

file

点击“下一步”按钮,在新弹出的对话框中,保持默认的磁盘类型为SCSI

file

点击“下一步”按钮,在新弹出对话中中保持默认的“创建新的虚拟磁盘”

file

单击“下一步”按钮,在新弹出的界面中保持默认的容量即可

file

单击“下一步”按钮,在新界面中选择磁盘文件的保存位置,也可以保持默认即可。

file

点击“完成”按钮,将返回虚拟机设置界面,并在此界面中可以看到刚才新添加的磁盘。

file

单击“确定”按钮,磁盘添加完成,并在虚拟机管理主界面中也可以看到刚才添加的新磁盘。

file

根据前面讲解的物理设备命名规则,我们大胆猜想设备名称应该为sdb,表示是第二块识别到的磁盘。后边的步骤中,我们将验证这个猜想的正确性。

2.2 磁盘分区

正式分区之前,先介绍一下Linux的磁盘分区命令fdisk,它提供了集添加、删除、转换分区等功能于一身的全套服务。

语法:fdisk [磁盘名称]

fdisk提供了交互式的参数,可以根据需求动态调整,常见参数如下:

参数 作用
m 查看全部可用的参数
n 添加新的分区
d 删除某个分区信息
l 列出所有可用的分区类型,所有磁盘信息包括未加载磁盘
t 改变某个分区的类型
p 查看分区信息
w 保存并退出
q 不保存直接退出

除了fdisk之外,再介绍一个命令lsblk,这个命令可以列出所有可用块设备的信息,而且还能显示他们之间的依赖关系。

2.2.1 查看新添加的设备

学会fdisk和lsblk命令之后,我们先查看一下第一步新添加的磁盘信息(找出还没有分区的设备)。

可以使用fdisk -l 命令查看,结果如下:

[root@heimatengyun ~]# fdisk -l
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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
...省略部分内容

也可以通过lsblk命令查看,结果如下:

[root@heimatengyun ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk 
├─sda1            8:1    0  500M  0 part /boot
└─sda2            8:2    0 19.5G  0 part 
  ├─centos-swap 253:0    0    2G  0 lvm  [SWAP]
  └─centos-root 253:1    0 17.5G  0 lvm  /
sdb               8:16   0   20G  0 disk 
sr0              11:0    1  3.9G  0 rom  

可以看到设备名称为sdb,跟我们前面猜想的设备名称吻合。

2.2.2 磁盘分区

使用fdisk命令管理/dev/sdb硬盘设备

通过参数p查看硬盘设备已有的分区信息,可以看到硬盘的容量大小、扇区个数等信息:

[root@heimatengyun ~]# fdisk /dev/sdb 
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 0xf69cc7c1.
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0xf69cc7c1
   Device Boot      Start         End      Blocks   Id  System

通过n参数添加新分区,接下来会提示创建主分区(p)还是扩展分区(e),我们用参数p创建主分区:

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p

接下来会让输入主分区编号,我们采用默认1,接下来会提示输入扇区的起始位置,我们保持默认直接按回车键,系统会自动帮我们计算最靠前的空闲扇区位置。随后提示我们输入扇区结束位置,我们直接输入+2G即可:

Partition number (1-4, default 1): 1
First sector (2048-41943039, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +2G
Partition 1 of type Linux and of size 2 GiB is set

这个扇区结束位置其实是定义整个分区的大小,我们无需计算扇区的个数,直接输入+2g即可创建一个容量为2GB的硬盘分区。

接下来我们再次用p参数查看刚才的分区信息:

Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0xf69cc7c1
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   83  Linux

这样我们就可以看到名称为/dev/sdb1的主分区信息了,可以看到它的起始扇区位置为2048,结束扇区位置为4196351。

此时通过w参数保存并回车:

Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.

这样分区信息就写入成功,可以通过前文讲解的fdisk -l命令或lsblk命令查看硬件设备的分区信息。

经过上面的一些列操作之后,Linux 系统会自动把这个硬盘主分区抽象成/dev/sdb1设备文件。下面我们就通过file命令进行查看该设备文件。

2.2.3 查看分区设备文件

可以通过file命令查看文件该分区设备文件。在查看之前,我们先学习以下file命令

语法格式:file [参数选项] 目录或文件

作用:辨识文件类型

明确语法格式之后,我们用file命令查看刚才的分区文件/dev/sdb1设备文件类型

[root@heimatengyun ~]# file /dev/sdb1
/dev/sdb1: block special

由此可见,刚才添加分区的操作已经成功了。

注意:有时候系统并没有自动把分区信息同步给Linux内核,因此执行上边语句时可能得到的结果为:/dev/sdb1: cannot open (No such file or directory)。如果出现这种情况需要手动执行partprobe命令将分区信息同步到内核中,并且建议是连续执行两次partprobe命令。如果执行二次还是无效,可以直接重启计算机,重启之后就可以了。

确认分区设备文件成功之后,我们在补充下file命令的案例:

[root@heimatengyun ~]# file test/
test/: directory
[root@heimatengyun ~]# file test/test1.txt 
test/test1.txt: ASCII text

可以看到file命令可以方便的查看出目录或文件的类型。

分区创建成功,接下来就要格式化磁盘。

2.3 磁盘格式化

如果硬件存储设备没有进行格式化,Linux 系统无法得知怎么在其上写入数据。Linux中用于磁盘格式化的命令为mkfs。

语法格式:mkfs.文件系统类型 设备分区文件

我们通过命令补齐命令可以查看到mkfs命令根据系统类型分为不同的命令:

[root@heimatengyun ~]# mkfs
mkfs         mkfs.cramfs  mkfs.ext3    mkfs.fat     mkfs.msdos   mkfs.xfs
mkfs.btrfs   mkfs.ext2    mkfs.ext4    mkfs.minix   mkfs.vfat 

了解mkfs命令之后,我们使用xfs文件类型将之前创建的分区/dev/sdb1进行格式化:

[root@heimatengyun ~]# mkfs.xfs /dev/sdb1 
meta-data=/dev/sdb1              isize=256    agcount=4, agsize=131072 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=524288, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

这样就完成了分区的格式,接下来对文件进行挂载后就可以使用设备了。

2.4 挂载磁盘

挂载磁盘使用之前讲解的mount命令,需要先创建一个挂载点目录。

[root@heimatengyun ~]# mkdir /newDisk
[root@heimatengyun ~]# mount /dev/sdb1 /newDisk/
[root@heimatengyun ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   18G  3.7G   14G  21% /
devtmpfs                 905M     0  905M   0% /dev
tmpfs                    914M   80K  914M   1% /dev/shm
tmpfs                    914M  8.9M  905M   1% /run
tmpfs                    914M     0  914M   0% /sys/fs/cgroup
/dev/sda1                497M  119M  379M  24% /boot
/dev/sdb1                2.0G   33M  2.0G   2% /newDisk

挂载成功后,通过df -h命令即可查看到已经挂载的设备。

接下来就可以使用新加的磁盘设备了。

切换到新加设备所在的目录,添加文件并查看文件及其占用的大小:

[root@heimatengyun ~]# cd /newDisk/
[root@heimatengyun newDisk]# echo 'test'>test.txt
[root@heimatengyun newDisk]# ll
total 4
-rw-r--r--. 1 root root 5 Dec 28 18:57 test.txt
[root@heimatengyun newDisk]# du -sh /newDisk/
4.0K    /newDisk/

可以看到文件创建成功。

接下来使用df -h命令查看挂载状态和硬盘使用量:

[root@heimatengyun ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   18G  3.7G   14G  21% /
devtmpfs                 905M     0  905M   0% /dev
tmpfs                    914M   80K  914M   1% /dev/shm
tmpfs                    914M  8.9M  905M   1% /run
tmpfs                    914M     0  914M   0% /sys/fs/cgroup
/dev/sdb1                2.0G   33M  2.0G   2% /newDisk
/dev/sda1                497M  119M  379M  24% /boot

可以看到磁盘挂载成功,至此,说明新挂载的文件没有任何问题,可以正常使用。

到目前为止磁盘可以正常使用了,但是正如前面提到的mount命令挂载的设备需要写入fstab文件才能永久生效,因此我们编辑fstab文件将挂载信息写入:

[root@heimatengyun newDisk]# vi /etc/fstab 
...省略部分内容,添加以下一行即可
/dev/sdb1   /newDisk xfs defaults 0 0

这样就完成了新磁盘设备添加。

三、添加交换分区

交换分区(SWAP)是在硬盘中预先划分一定的空间,当真实物理内存不够用时,将内存中暂时不常用的数据临时放到硬盘中,以便腾出物理内存空间让更活跃的程序服务来使用的技术。

但是需要注意的是,毕竟硬盘的读写速度肯定比内存man,所以也只有当真实物理内存耗尽后才会调用交换分区的资源。

交换分区的创建过程与挂载过程基本一致,前面添加的设备总大小为20G,/dev/sdb挂载使用了2G,此处添加一个4G的主分区作为交换分区,过程如下:

3.1 创建分区

[root@heimatengyun ~]# fdisk /dev/sdb 
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 (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (2-4, default 2): 
First sector (4196352-41943039, default 4196352): 
Using default value 4196352
Last sector, +sectors or +size{K,M,G} (4196352-41943039, default 41943039): +4G
Partition 2 of type Linux and of size 4 GiB is set
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0xf69cc7c1
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   83  Linux
/dev/sdb2         4196352    12584959     4194304   83  Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@heimatengyun ~]# 

3.2 格式SWAP分区

交换分区格式化使用的命令与产品分区格式化有点区别,使用的是mkswap命令

[root@heimatengyun ~]# mkswap /dev/sdb2       
/dev/sdb2: No such file or directory
[root@heimatengyun ~]# partprobe

如果遇到上述情况,需要执行partprobe或重启即可

[root@heimatengyun ~]# mkswap /dev/sdb2
Setting up swapspace version 1, size = 4194300 KiB
no label, UUID=ef101e7c-e17d-42b7-8124-bed7c27a08e0

3.3 挂载分区

挂载SWAP分区使用的是swapon命令,注意普通分区使用mount。

先使用free 命令查看下交互分区的大小,然后再用swapon命令挂载SWAP分区设备,再次对比查看分区大小的变化:

[root@heimatengyun ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          1826        605       1221          8          1        219
-/+ buffers/cache:        383       1443
Swap:         2047          0       2047
[root@heimatengyun ~]# swapon /dev/sdb2 
[root@heimatengyun ~]# free -m          
             total       used       free     shared    buffers     cached
Mem:          1826        608       1218          8          1        219
-/+ buffers/cache:        386       1440
Swap:         6143          0       6143

可以看到,挂载后SWAP交换分区变大了,说明挂载成功。

与普通分区挂载一样,也需要将配置信息写入fstab文件使其开启自动挂载,在fstab文件加入最后一行,注意文件系统各类型为swap。

[root@heimatengyun ~]# vim /etc/fstab 
...省略部分内容
/dev/sdb1 /newDisk xfs defaults 0 0
/dev/sdb2 swap swap defaults 0 0

这样交换分区也就添加成功。

四、限定用户磁盘容量

前文提到过,Linux系统是多用户多任务操作系统,这就意味着可能有很多用户同时在使用该操作系统。

但是服务器硬件资源是有限的,我们需要通过磁盘容量配额服务来限制某位用户或某个用户组针对特定文件夹可以使用的 最大硬盘空间或最大文件个数,一旦达到这个最大值就不再允许继续使用。

磁盘容量配额功能通过quota命令实现,分为软限制和硬限制,当达到软限制时提示用户但仍可以使用,直到达到硬配额为止;而达到硬限制则直接不允许用户在继续使用。

4.1 开启quota支持

RHEL7已经安装quota 磁盘容量配额服务程序包,但存储设备默认没有开启对quota 的支持,需要手动编辑/etc/fstab配置文件。

在fstab配置文件中的挂载目录/boot中添加uquota参数使其支持磁盘配额功能。

[root@heimatengyun ~]# vim /etc/fstab 
...省略部分内容
UUID=bfd83b27-ed2b-4770-8d17-9e5412f2be27 /boot xfs  defaults,uquota 1 2
...省略部分内容

重启系统,查看是否生效

[root@heimatengyun ~]# reboot
[root@heimatengyun ~]# mount |grep boot
/dev/sda1 on /boot type xfs (rw,relatime,seclabel,attr2,inode64,usrquota)

可以看到包含了usrquota表明已经生效。

4.2 xfs_quota命令

xfs_quota命令是一个专门针对XFS文件系统来管理quota磁盘容量配额服务而设计的命令。我们先了解一下xfs_quota命令的基本用法。

语法:

​ xfs_quota [参数] 配额 文件系统

参数:

参数 作用
-x 专家模式,可以对quota服务进行更多复杂的配置
-c 于以参数的形式设置要执行的命令

接下来我们使用 xfs_quota 命令来设置用户对/boot 目录的 quota 磁盘容量配额。

(1)先创建一个普通用户:testquota,并对/boot目录添加其他人写入的权限,保证用户能正常写入数据:

[root@heimatengyun ~]# useradd testquota
[root@heimatengyun ~]# chmod -Rf o+w /boot/

(2)限制testquota用户磁盘使用量和创建文件的大小

具体的限额控制包括:硬盘使用量的软限制和硬限制分别为1MB 和3MB;创建文件数量的软限制和硬限制分别为2个和4个。

创建磁盘配额并查看设置是否成功:

[root@heimatengyun ~]# xfs_quota -x -c 'limit bsoft=1m bhard=3m isoft=2 ihard=4 testquota' /boot/
[root@heimatengyun ~]# xfs_quota -x -c report /boot/
User quota on /boot (/dev/sda1)
                               Blocks                     
User ID          Used       Soft       Hard    Warn/Grace     
---------- -------------------------------------------------- 
root            95328          0          0     00 [--------]
testquota           0       1024       3072     00 [--------]

创建成功后,切换到普通用户并分别创建一个2M和4M的文件。(选择2M的目的是刚好超过最小配额但又没超过最大配额,而选择4M则是超过了最大配额,这样就能模拟出磁盘配额的效果)

[root@heimatengyun ~]# su - testquota 
[testquota@heimatengyun ~]$ dd if=/dev/zero of=/boot/testquota bs=2M count=1
1+0 records in
1+0 records out
2097152 bytes (2.1 MB) copied, 0.00166952 s, 1.3 GB/s
[testquota@heimatengyun ~]$ dd if=/dev/zero of=/boot/testquota bs=4M count=1 
dd: error writing ‘/boot/testquota’: Disk quota exceeded
1+0 records in
0+0 records out
3145728 bytes (3.1 MB) copied, 0.00411569 s, 764 MB/s
[testquota@heimatengyun ~]$ ll /boot/testquota 
-rw-rw-r--. 1 testquota testquota 3145728 Dec 28 21:07 /boot/testquota
[testquota@heimatengyun ~]$ exit
logout

可以看到2M的文件创建成功,但是4M的文件提示失败了,说明针对用户的磁盘配额生效了。

4.3 edquota命令

edquota commands for editing the user's quota quota, after setting a quota for the quota limit users disk capacity can be used as needed to modify edquota command limit value.

grammar:

the edquota [parameters] [user]

parameter:

parameter effect
-u To make the settings for which users
-g To set up a user group for which

edquota command invokes the Vi editor to make root administrator to modify the details to be restricted. Below the disk usage of a user's hard quota testquota upgrade from 5M to 8M:

[root@heimatengyun ~]# edquota -u testquota
Disk quotas for user testquota (uid 1002):
  Filesystem     blocks  soft       hard     inodes     soft hard
  /dev/sda1      3072    1024       3072          1        2 4

Modify and save it.

This article describes the basic Centos7 system disk management technology, the next will explain a little more advanced disk array RAID and LVM technology.

Guess you like

Origin www.cnblogs.com/heimatengyun/p/12334964.html