红帽7 磁盘划分

一、挂载硬件设备node

mount命令用于挂载文件系统,格式为“mount 文件系统 挂载目录”。挂载是在使用硬件设备前所执行的最后一步操做。只需使用mount命令把硬盘设备或分区与一个目录文件进行关联,而后就能在这个目录中看到硬件设备中的数据了。linux

mount命令中的参数以及做用vim

参数 做用
-a 挂载全部在/etc/fstab中定义的文件系统
-t 指定文件系统的类型

 

 

 

 

 

 

 

例如,要把设备 /dev/sdb2 挂载到 /back 目录,只须要在mount命令中填写设备与挂载目录参数就行,系统会自动去判断要挂载文件的类型app

[root@localhost Desktop]# mount  /dev/sdb2  /back

 虽然按照上面的方法执行mount命令后就能当即使用文件系统了,但系统在重启后挂载就会失效,也就是说咱们须要每次开机后都手动挂载一下。若是想让硬件设备和目录永久地进行自动关联,就必须把挂载信息按照指定的填写格式  “设备文件 挂载目录 格式类型 权限选项 是否备份 是否自检”  写入到/etc/fstab文件中。这个文件中包含着挂载所需的诸多信息项目,一旦配置好以后就能一劳永逸了。async

用于挂载信息的指定填写格式中,各字段所表示的意义ide

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

 

 

 

 

 

 

 

 

 

 

若是想将文件系统为ext4的硬件设备 /dev/sdb2 在开机后自动挂载到 /back 目录上,并保持默认权限且无需开机自检,就须要在/etc/fstab文件中写入下面的信息,这样在系统重启后也会成功挂载。ui

[root@localhost Desktop]# vim /etc/fstab 
#
# /etc/fstab
# Created by anaconda on Wed Sep  5 21:24:14 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/rhel-root   /                       xfs     defaults        1 1
UUID=1e81605b-4fdf-46b6-a0e4-244ea1ac9e7d /boot                   xfs     defaults        1 2
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0
/dev/sdb2 /back ext4 defaults 0 0

umount命令:用于撤销已经挂载的设备文件,格式为 “umount [挂载点/设备文件]” 。咱们挂载文件系统的目的是为了使用硬件资源,而卸载文件系统就意味再也不使用硬件的设备资源;相对应地,挂载操做就是把硬件设备与目录进行关联的动做,所以卸载操做只须要说明想要取消关联的设备文件或挂载目录的其中一项便可,通常不须要加其余额外的参数。spa

[root@localhost Desktop]# umount  /dev/sdb2

二、添加硬盘设备 设计

第1步:首先把虚拟机系统关机,稍等几分钟会自动返回到虚拟机管理主界面,而后单击“编辑虚拟机设置”选项,在弹出的界面中单击“添加”按钮,新增一块硬件设备3d

在虚拟机中模拟添加了硬盘设备后就应该能看到抽象成的硬盘设备文件了。第二个被识别的SCSI设备应该会被保存为/dev/sdb,这个就是硬盘设备文件了。但在开始使用该硬盘以前还须要进行分区操做。

(1)fdisk命令

在Linux系统中,管理硬盘设备最经常使用的方法就当属fdisk命令了。fdisk命令用于管理磁盘分区,格式为“fdisk  [磁盘名称]”,它提供了集添加、删除、转换分区等功能于一身的“一站式分区服务”。

fdisk命令中的参数以及做用

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

 

 

 

 

 

 

 

 

 

 

 

 

 第1步:咱们首先使用fdisk命令来尝试管理 /dev/sdb 硬盘设备。在看到提示信息后输入参数p来查看硬盘设备内已有的分区信息,其中包括了硬盘的容量大小、扇区个数等信息

[root@localhost Desktop]# 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 0xab9c86d8.

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: 0xab9c86d8

   Device Boot      Start         End      Blocks   Id  System

第2步:输入参数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

第3步:在确认建立一个主分区后,系统要求您先输入主分区的编号。咱们在前文得知,主分区的编号范围是1~4,所以这里输入默认的1就能够了。接下来系统会提示定义起始的扇区位置,这不须要改动,咱们敲击回车键保留默认设置便可,系统会自动计算出最靠前的空闲扇区的位置。最后,系统会要求定义分区的结束扇区位置,这其实就是要去定义整个分区的大小是多少。咱们不用去计算扇区的个数,只须要输入+2G便可建立出一个容量为2GB的硬盘分区

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

第4步:再次使用参数p来查看硬盘设备中的分区信息。果真就能看到一个名称为 /dev/sdb一、起始扇区位置为204八、结束扇区位置为4196351的主分区了。这时候千万不要直接关闭窗口,而应该敲击参数w后回车,这样分区信息才是真正的写入成功。

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: 0xab9c86d8

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   83 Linux Command (m for help): w The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

第5步:在上述步骤执行完毕以后,Linux系统会自动把这个硬盘主分区抽象成/dev/sdb1设备文件。咱们可使用file命令查看该文件的属性,有些时候系统并无自动把分区信息同步给Linux内核,咱们能够输入 partprobe命令 手动将分区信息同步到内核,并且通常推荐连续两次执行该命令,效果会更好。

[root@localhost Desktop]# file /dev/sdb1
/dev/sdb1: block special

若是硬件存储设备没有进行格式化,则Linux系统没法得知怎么在其上写入数据。所以,在对存储设备进行分区后还须要进行格式化操做。在Linux系统中用于格式化操做的命令是mkfs。在Shell终端中输入mkfs名后再敲击两下用于补齐命令的Tab键

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

把 /dev/sdb1 格式化为xfs的文件系统,则命令应为 mkfs.xfs /dev/sdb1 。

[root@localhost Desktop]# 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

接下来就是要来挂载并使用存储设备了。与之相关的步骤也很是简单:首先是建立一个用于挂载设备的挂载点目录;而后使用 mount 命令将存储设备与挂载点进行关联;最后

使用 df -h 命令来查看挂载状态和硬盘使用量信息。

[root@localhost Desktop]# mkdir /newfile
[root@localhost Desktop]# mount /dev/sdb1 /newfile/
[root@localhost Desktop]# df -h Filesystem             Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root   18G  2.9G   15G  17% /
devtmpfs               985M     0  985M   0% /dev
tmpfs                  994M  140K  994M   1% /dev/shm
tmpfs                  994M  8.8M  986M   1% /run
tmpfs                  994M     0  994M   0% /sys/fs/cgroup
/dev/sda1              497M  119M  379M  24% /boot
/dev/sr0               3.5G  3.5G     0 100% /run/media/root/RHEL-7.0 Server.x86_64
/dev/sdb1              2.0G   33M  2.0G   2% /newfile

使用mount命令挂载的设备文件会在系统下一次重启的时候失效。若是想让这个设备文件的挂载永久有效,则须要把挂载的信息写入到配置文件中

[root@localhost Desktop]# vim /etc/fstab 
#
# /etc/fstab
# Created by anaconda on Wed Sep  5 21:24:14 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/rhel-root   /                       xfs     defaults        1 1
UUID=1e81605b-4fdf-46b6-a0e4-244ea1ac9e7d /boot                   xfs     defaults        1 2
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0
/dev/sdb1 /newfile xfs defaults 0 0                                              

三、添加交换分区

SWAP(交换)分区是一种经过在硬盘中预先划分必定的空间,而后将把内存中暂时不经常使用的数据临时存放到硬盘中,以便腾出物理内存空间让更活跃的程序服务来使用的技术,其设计目的是为了解决真实物理内存不足的问题。但因为交换分区毕竟是经过硬盘设备读写数据的,速度确定要比物理内存慢,因此只有当真实的物理内存耗尽后才会调用交换分区的资源。交换分区的建立过程与前文讲到的挂载并使用存储设备的过程很是类似。在对/dev/sdb存储设备进行分区操做前,有必要先说一下交换分区的划分建议:在生产环境中,交换分区的大小通常为真实物理内存的1.5~2倍。

[root@localhost Desktop]# 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): 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: 0xab9c86d8

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   83  Linux

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): +2G
Partition 2 of type Linux and of size 2 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: 0xab9c86d8

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   83  Linux
/dev/sdb2         4196352     8390655     2097152   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.
View Code

使用SWAP分区专用的格式化命令 mkswap,对新建的主分区进行格式化操做

[root@localhost Desktop]# mkswap /dev/sdb2
Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=a7bc373a-1c22-447c-8fea-b251466e7d1d

使用swapon命令把准备好的SWAP分区设备正式挂载到系统中。咱们可使用free -m命令查看交换分区的大小变化

[root@localhost Desktop]# free -m
             total       used       free     shared    buffers     cached
Mem:          1987        906       1081          9          1        298
-/+ buffers/cache:        606       1381 Swap:  2047          0       2047
[root@localhost Desktop]# swapon /dev/sdb2 
[root@localhost Desktop]# free -m
             total       used       free     shared    buffers     cached
Mem:          1987        908       1079          9          1        298
-/+ buffers/cache:        607       1380 Swap:         4095          0       4095
[root@localhost Desktop]# vim /etc/fstab 
#
# /etc/fstab
# Created by anaconda on Wed Sep  5 21:24:14 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/rhel-root   /                       xfs     defaults        1 1
UUID=1e81605b-4fdf-46b6-a0e4-244ea1ac9e7d /boot                   xfs     defaults        1 2
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0
/dev/sdb1 /newfile xfs defaults 0 0
/dev/sdb2 swap swap defaults 0 0

四、软硬方式连接

在Linux系统中存在硬连接和软链接两种文件。

硬连接(hard link):能够将它理解为一个“指向原始文件inode的指针”,系统不为它分配独立的inode和文件。因此,硬连接文件与原始文件实际上是同一个文件,只是名字不一样。咱们每添加一个硬连接,该文件的inode链接数就会增长1;并且只有当该文件的inode链接数为0时,才算完全将它删除。换言之,因为硬连接其实是指向原文件inode的指针,所以即使原始文件被删除,依然能够经过硬连接文件来访问。须要注意的是,因为技术的局限性,咱们不能跨分区对目录文件进行连接。

软连接(也称为符号连接[symbolic link]):仅仅包含所连接文件的路径名,所以能连接目录文件,也能够跨越文件系统进行连接。可是,当原始文件被删除后,连接文件也将失效,从这一点上来讲与Windows系统中的“快捷方式”具备同样的性质。

ln命令

ln命令用于建立连接文件,格式为“ln [选项] 目标”,在使用ln命令时,是否添加-s参数,将建立出性质不一样的两种“快捷方式”。

ln命令中可用的参数以及做用

参数 做用
-s 建立“符号连接”(若是不带-s参数,则默认建立硬连接)
-f 强制建立文件或目录的连接
-i 覆盖前先询问
-v 显示建立连接的过程

 

 

 

 

 

 

 

 

建立一个相似于Windows系统中快捷方式的软连接。这样,当原始文件被删除后,就没法读取新建的连接文件了。

[root@localhost Desktop]# echo "hello world" > text.txt
[root@localhost Desktop]# ln -s text.txt copy.txt [root@localhost Desktop]# cat copy.txt 
hello world
[root@localhost Desktop]# cat text.txt 
hello world
[root@localhost Desktop]# ls -l text.txt 
-rw-r--r--. 1 root root 12 Sep 15 20:09 text.txt
[root@localhost Desktop]# rm -rf text.txt [root@localhost Desktop]# cat copy.txt cat: copy.txt: No such file or directory

接下来针对一个原始文件建立一个硬连接,即至关于针对原始文件的硬盘存储位置建立了一个指针,这样一来,新建立的这个硬连接就再也不依赖于原始文件的名称等信息,也不会由于原始文件的删除而致使没法读取。同时能够看到建立硬连接后,原始文件的硬盘连接数量增长到了2。

[root@localhost Desktop]# echo "hello world" > test.txt
[root@localhost Desktop]# ln test.txt copy.txt
[root@localhost Desktop]# cat copy.txt 
hello world
[root@localhost Desktop]# cat test.txt 
hello world
[root@localhost Desktop]# ls -l test.txt 
-rw-r--r--. 2 root root 12 Sep 15 20:14 test.txt
[root@localhost Desktop]# rm -rf test.txt 
[root@localhost Desktop]# cat copy.txt 
hello world
相关文章
相关标签/搜索