2021-03-02

磁盘分区文件系统挂载与swap分区

1.本文环境

  OS: Centos 6.10, 虚拟机:vm15.5

2. 什么是硬盘分区(Partition)?

  硬盘是计算机中存储信息的重要设备,计算机所运行的程序,所需要的数据,包括操作系统都在硬盘上,因此硬盘的重要可想而知,但硬盘往往不能直接使用,需要对硬盘进行分区。

  分区,顾名思义,就是需要对硬盘逻辑上的切割分成一块块独立磁盘分区。把硬盘划分成数个分区,将不同类的目录与文件可以存储进不同的分区。

2.1  分区的好处?

  利于对硬盘的管理

  安全性增加,如果一块分区出现问题,不至于影响其他分区

  对不同的分区可采用不同的文件系统

  实现磁盘空间配额限制 

  隔离系统和程序 

  甚至安装多个操作系统

  实现磁盘空间配额限制 

  优化读写性能

2.2 常见的硬盘分区方式?

  发现很多人有一个误区,把文件系统当作分区,比如最常见的win下的ntfs,fat16,fat32,这些都不是分区,而是文件系统,此文下面为专门介绍。

  常见的分区方式MBR(Master Boot Record),GPT(GUID Partition Table)这两种分区方式算是很常见,而APM,BSD 等分区很少见,有的已经被淘汰。

  本文主要介绍MBR,与GTP分区。

3. MBR分区

  MBR(Master Boot Record)主引导记录分区表,此分区最多只能有四个主分区,但是一般使用mbr分区不会使用四个主分区,大多都使用三个主分区加一个扩展分区,扩展分区不能直接存储数据,扩展分区相当于是硬盘分区的一种特定的标记。扩展分区下可以分逻辑分区,理论上逻辑分区可以分N个,这也是为什么你的电脑是MBR分区却可以有多个分区了。

  MBR 有两种寻址方式,CHS 与 LBA 

  CHA : 采用24bit位寻址 最大寻址空间8GB 意味着最大只能对8GB硬盘进行分区,所以现在的MBR分区都是使用LBA

  LBA  : LBA是一个整数,通过转换成CHS格式完成磁盘具体寻址,寻址有28位与48位最大对硬盘分区可达128GB与128PB

  在我的系统中(CentOS6.10),查看分区的方式使用有很多方式,比如 parted /dev/sda print  在 7上(此处7代表centos7 下面同义)  使用的查看方法更多

[root@CentOS-10-64-2019-0921 ~]$parted /dev/sda print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sda: 215GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type      File system     Flags
 1      1049kB  1075MB  1074MB  primary   ext4            boot
 2      1075MB  106GB   105GB   primary   ext4
 3      106GB   158GB   52.4GB  primary   ext4
 4      158GB   215GB   56.4GB  extended
 5      158GB   179GB   21.0GB  logical   ext4
 6      179GB   181GB   2147MB  logical   linux-swap(v1)

请看上面的代码,Partition Table: msdos 这行  msdos代表的就是mbr分区,然后看第五列(type列) 三个主分区(primary)一个扩展分区(extended)然后两个逻辑分区(logical)这就是我系统上的分区

 3.1 MBR原理简单的介绍

  讲一下讲简单的MBR原理,太底层的东西我也不会,在MBR中分区表中,有512字节记录这主分区的信息,512字节前446个字节是主引导程序,主要的分区表存放在后64字节

  最后两个字节是分区标志位 55 aa。主分区表的64个字节 分为4个16字节的分区表项,所代表四个分区,16个字节的第一个字节代表分区状态,80为活动位,00为非活动位,第二个字节为磁头号(head),磁头8位,第三个字节的低6位代码扇区号(sector),因此扇区是6位,第三个字节的高2位加上第四个字节是柱面号(cylinder)柱面占10位。第五个字节文件系统

  第6,7,8个字节是本分区的结束标志,磁头号结束标志第6个字节,扇区结束号第7个字节的低6位,柱面结束号,第7个字节的高2位和第8个字节。

  第9到12个字节代表本相对扇区数,

  第13到16个字节分区项的总扇区数

3.2 MBR分区创建使用

  在我的系统中对MBR系统的创建可使用parted gdisk fdisk 其中parted的操作是即时生效,不建议使用。

  fdisk主要是针对MBR分区使用的,不能对GTP创建,不过7上可以使用。

fdisk doesn’t understand GUID Partition Table (GPT) and it is not designed for large partitions. In particular case  use  more
       advanced GNU parted(8)

下面使用fdisk进行分区创建

  1.对硬盘进行分区,我以一个20g的硬盘为例

[root@CentOS-10-64-2019-0921 ~]$fdisk /dev/sdb   #使用fdisk  对硬盘sdb分区,而不是对分区,分区

WARNING: GPT (GUID Partition Table) detected on '/dev/sdb'! The util fdisk doesn't support GPT. Use GNU Parted.
#这个警告是我这个硬盘之前进行过GTP的分区,上面有残留的GTP分区信息,请忽略,看后面在CentOS6上fdisk不支持GTP分区

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): m      #使用m看帮助 ,
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help): n   # 新建分区
 1 Command (m for help): n               # 新建分区
 2 Command action                        #选择分区类型  e 扩展  p 主分区
 3    e   extended         
 4    p   primary partition (1-4)
 5 p                       #我选的P               6 Partition number (1-4): 1           #分区编号1    
 7 First cylinder (1-2610, default 1):   #开始柱面位置,6的分区以柱面划分  7的分区是以扇区划分
 8 Using default value 1           #我回车直接默认
 9 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610):
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +6G    #输入容量的话输入+号和单位,纯数字系统会作为柱面来识别
Command (m for help): p  #打印硬盘信息 看 没有刚刚分好的区,这样误操作的话安全性很高

Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         784     6297448+  83  Linux

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
e                    #这里创建了一个扩展分区
Partition number (1-4): 2
First cylinder (785-2610, default 785):
Using default value 785
Last cylinder, +cylinders or +size{K,M,G} (785-2610, default 2610): +3G  #3G的扩展分区

1

2

3

4

5

Command (m for help): w   #w  保存刚刚分好的分区信息

The partition table has been altered!

 

Calling ioctl() to re-read partition table.

Syncing disks.

  这里成功了,有时候会报一个警告,内存和硬盘同步。使用  partx -a /dev/sda   对硬盘    就好了

1 [root@CentOS-10-64-2019-0921 ~]$lsblk -l /dev/sdb    #查看刚刚的分区
2 NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
3 sdb    8:16   0  20G  0 disk 
4 sdb1   8:17   0   6G  0 part 
5 sdb2   8:18   0   1K  0 part 

看见没有,sdb2 居然只有1K???  不对啊 我刚刚分的明明可是3G啊

之前就说了扩展分区不存数据,只是一个标志而已,这个1K保存着刚刚分区的信息,而扩展分区下的逻辑分区只能对这3G使用

[root@CentOS-10-64-2019-0921 ~]$fdisk -l /dev/sdb       #这样查看

WARNING: GPT (GUID Partition Table) detected on '/dev/sdb'! The util fdisk doesn't support GPT. Use GNU Parted.


Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         784     6297448+  83  Linux
/dev/sdb2             785        1177     3156772+   5  Extended

4. GTP分区介绍  

  GPT:GUID(Globals Unique Identifiers) partition table 支持128个分区, 使用64位,支持8Z( 512Byte/block )64Z ( 4096Byte/block) 用128位UUID(Universally Unique   Identifier) 表示磁盘和分区 GPT分区表 自动备份在头和尾两份,并有CRC校验位

                                                         

  

 

   4.1 GPT创建

    在Centos6.10上没有gdisk命令,因此需要安装gdisk命令

       1 yum install gdisk

    GPT创建使用gdisk  与上面的fdisk使用方法基本一致

    我先格掉刚刚的20G硬盘分区,还是对它进行GPT分区。

[root@CentOS-10-64-2019-0921 ~]$gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.10

Caution: invalid main GPT header, but valid backup; regenerating main header
from backup!

Caution! After loading partitions, the CRC doesn't check out!
Warning! Main partition table CRC mismatch! Loaded backup partition table
instead of main partition table!

Warning! One or more CRCs don't match. You should repair the disk!

Partition table scan:
  MBR: not present
  BSD: not present
  APM: not present
  GPT: damaged

Found invalid MBR and corrupt GPT. What do you want to do? (Using the
GPT MAY permit recovery of GPT data.)
 1 - Use current GPT
 2 - Create blank GPT

Your answer: 2         #由于这个硬盘还残留这之前的分区信息所以会与这样的显示,选择2 创建新的GPT分区
Command (? for help): ?
b    back up GPT data to a file
c    change a partition's name
d    delete a partition
i    show detailed information on a partition
l    list known partition types
n    add a new partition
o    create a new empty GUID partition table (GPT)
p    print the partition table
q    quit without saving changes
r    recovery and transformation options (experts only)
s    sort partitions
t    change a partition's type code
v    verify disk
w    write table to disk and exit
x    extra functionality (experts only)
?    print this menu            #基本用法与fdisk一样

Command (? for help):

 

Command (? for help): n            # n  建分区
Partition number (1-128, default 1):     #GPT没有扩展分区,创建的都是主分区
First sector (34-41943006, default = 2048) or {+-}size{KMGTP}:     #First sector  在6上 gdisk以扇区划分的
Last sector (2048-41943006, default = 41943006) or {+-}size{KMGTP}: +10G    #  10g的分区
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'

Command (? for help):
Command (? for help): w   # 跟fdisk 一样保存更改的分区

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): y  #确认继续
OK; writing new GUID partition table (GPT) to /dev/sdb.
The operation has completed successfully.
[root@CentOS-10-64-2019-0921 ~]$

看看分区表写入了没有

[root@CentOS-10-64-2019-0921 ~]$lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0     11:0    1  3.7G  0 rom
sda      8:0    0  200G  0 disk
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0 97.7G  0 part /
├─sda3   8:3    0 48.8G  0 part /data
├─sda4   8:4    0    1K  0 part
├─sda5   8:5    0 19.5G  0 part /IMP
└─sda6   8:6    0    2G  0 part [SWAP]
sdb      8:16   0   20G  0 disk
└─sdb1   8:17   0   10G  0 part
[root@CentOS-10-64-2019-0921 ~]$lsblk -l /dev/sdb1
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb1   8:17   0  10G  0 part

分区表已经出来了 sdb1 10G

  小结:对磁盘分区的提示信息看清楚,跟随提示来完成基本没问题,当然还可以使用脚本分区。

 

  5.文件系统

  5.1 文件系统是操作系统用于明确存储设备或分区上的文件的方法和数据结构;即 在存储设备上组织文件的方法。操作系统中负责管理和存储文件信息的 软件结 构称为文件管理系统,文件系统是对文件存储设备的空间进行组织和分配,负责文 件存储并对存入的文件进行保护和检索的系统。具体地说,它负责为用户建立 文件,存入、读出、修改、转储文件,控制文件的存取,安全控制,日志,压缩,加密等

  5.2 常见文件系统

   ext系类,fat16,fat32,xfs,swap,光盘,ntfs,ffs,ufs等,raw是未经处理或者未经格式化产生的文件系统 。

  在我的系统中查看系统支持的文件系统

   1 cat /proc/filesystems
   1 [root@CentOS-10-64-2019-0921 ~]$ls /lib/modules/2.6.32-754.el6.x86_64/kernel/fs/
   2 autofs4  cachefiles  configfs  dlm       exportfs  ext3  fat      fuse  jbd   jffs2  mbcache.ko  nfs_common  nls       ubifs  xfs
   3 btrfs    cifs        cramfs    ecryptfs  ext2      ext4  fscache  gfs2  jbd2  lockd  nfs         nfsd        squashfs  udf
   4 [root@CentOS-10-64-2019-0921 ~]$

  5.3 文件系统创建

    创建文件系统使用mkfs

   1 [root@CentOS-10-64-2019-0921 ~]$mkfs.
   2 mkfs.cramfs   mkfs.ext2     mkfs.ext3     mkfs.ext4     mkfs.ext4dev  mkfs.msdos    mkfs.vfat     
   3 [root@CentOS-10-64-2019-0921 ~]$mkfs.

    当然一下命令也是可以的

   1 mkdosfs [mkfs]       (8)  - create an MS-DOS file system under Linux
   2 mke2fs [mkfs]        (8)  - create an ext2/ext3/ext4 filesystem
   3 mkfs                 (8)  - build a Linux file system

    下面来使用mkfs.ext4  创建ext4文件系统 XFS文件系统        CentOS 6 好像不支持

[root@CentOS-10-64-2019-0921 ~]$mkfs.ext4 /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
655360 inodes, 2621440 blocks
131072 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2684354560
80 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 34 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@CentOS-10-64-2019-0921 ~]$

查看文件系统  blkid

[root@CentOS-10-64-2019-0921 ~]$blkid
/dev/sda1: UUID="bd77c2f9-ed4a-46ec-972b-9c905fa97c8d" TYPE="ext4"
/dev/sda2: UUID="d18fb14c-1a2f-4925-b68a-41988aca5c4c" TYPE="ext4"
/dev/sda3: UUID="f91bf801-129a-4679-96d4-0567b5102ffc" TYPE="ext4"
/dev/sda5: UUID="ece4577f-d929-45dd-81a9-59f5c7e6e625" TYPE="ext4"
/dev/sda6: UUID="9d141267-bb3b-47d3-b3bb-6d22eba6d85d" TYPE="swap"
/dev/sdb1: UUID="ef04113b-fb79-4a8f-a21a-b09963266399" TYPE="ext4"
[root@CentOS-10-64-2019-0921 ~]$

管理文件系统的命令 tune2fs

[root@CentOS-10-64-2019-0921 ~]$tune2fs -l /dev/sdb1
tune2fs 1.41.12 (17-May-2010)
Filesystem volume name:   <none>
Last mounted on:          <not available>
Filesystem UUID:          ef04113b-fb79-4a8f-a21a-b09963266399
Filesystem magic number:  0xEF53
Filesystem revision #:    1 (dynamic)
Filesystem features:      has_journal ext_attr resize_inode dir_index filetype extent flex_bg sparse_super large_file huge_file uninit_bg dir_nlink extra_isize
Filesystem flags:         signed_directory_hash
Default mount options:    (none)
Filesystem state:         clean
Errors behavior:          Continue
Filesystem OS type:       Linux
Inode count:              655360
Block count:              2621440
Reserved block count:     131072
Free blocks:              2541777
Free inodes:              655349
First block:              0
Block size:               4096
Fragment size:            4096
Reserved GDT blocks:      639
Blocks per group:         32768
Fragments per group:      32768
Inodes per group:         8192
Inode blocks per group:   512
Flex block group size:    16
Filesystem created:       Thu Oct 17 02:42:18 2019
Last mount time:          n/a
Last write time:          Thu Oct 17 02:42:20 2019
Mount count:              0
Maximum mount count:      34
Last checked:             Thu Oct 17 02:42:18 2019
Check interval:           15552000 (6 months)
Next check after:         Tue Apr 14 02:42:18 2020
Lifetime writes:          291 MB
Reserved blocks uid:      0 (user root)
Reserved blocks gid:      0 (group root)
First inode:              11
Inode size:              256
Required extra isize:     28
Desired extra isize:      28
Journal inode:            8
Default directory hash:   half_md4
Directory Hash Seed:      4fe081ea-af2b-4d07-ab0a-5feabef490e2
Journal backup:           inode blocks
[root@CentOS-10-64-2019-0921 ~]$
[root@CentOS-10-64-2019-0921 ~]$tune2fs --help       # tune2fs 帮助查看  还可以使用dumpe2fs 查看块组的信息
tune2fs 1.41.12 (17-May-2010)
tune2fs: invalid option -- '-'
Usage: tune2fs [-c max_mounts_count] [-e errors_behavior] [-g group]
    [-i interval[d|m|w]] [-j] [-J journal_options] [-l]
    [-m reserved_blocks_percent] [-o [^]mount_options[,...]]
    [-r reserved_blocks_count] [-u user] [-C mount_count] [-L volume_label]
    [-M last_mounted_dir] [-O [^]feature[,...]]
    [-E extended-option[,...]] [-T last_check_time] [-U UUID]
    [ -I new_inode_size ] device
[root@CentOS-10-64-2019-0921 ~]$

小结:创建文件分区并不复杂,使用相应的工具可以好了。在Centos6 上手动建立的文件系统没有acl权限需要自己加上

1 [root@CentOS-10-64-2019-0921 /mnt]$touch acl
2 [root@CentOS-10-64-2019-0921 /mnt]$setfacl -m u:example:rw acl 
3 setfacl: acl: Operation not supported
4 [root@CentOS-10-64-2019-0921 /mnt]$

6.swap分区

  6.1  什么是swap分区

    严格来说swap是一种文件系统。先创建swap分区,然后为swap分区创建swap文件系统。

    swap交换分区是系统RAM的补充,即交换区,系统在物理运行内存不够时,没有足够的 RAM 保存系统处理的数据时会将数据写入 swap 分区 与swap进行交换,swap分区可以当作虚拟内存。.

  6.2 查看swap分区

1 [root@CentOS-10-64-2019-0921 /mnt]$swapon -s
2 Filename                Type        Size    Used    Priority
3 /dev/sda6                               partition    2097148    0    -1
4 [root@CentOS-10-64-2019-0921 /mnt]$

6.3 创建swap分区,swap分区可以使用单独的swap,也可使用一个文件作为swap。当然使用单独的分区效果更好。分别介绍

    6.3.1  使用独立分区作为swap

     我在上面对磁盘分区使用过fdisk,gdisk,那么创建swap分区时也可以任用其一,两个命令的基本使用方法基本一样。

     因为gdisk在CentOS6上是需要安装的,我这里就使用fdisk了

[root@CentOS-10-64-2019-0921 /]$fdisk /dev/sdb       #使用fdisk 对硬盘分区

WARNING: GPT (GUID Partition Table) detected on '/dev/sdb'! The util fdisk doesn't support GPT. Use GNU Parted.


WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n        #n 跟前面的一样
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-2610, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +8g
Unsupported suffix: 'g'.     #这里小写g还不支持而报错,7上我记得小写是可以的
Supported: 10^N: KB (KiloByte), MB (MegaByte), GB (GigaByte)
            2^N: K  (KibiByte), M  (MebiByte), G  (GibiByte)
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +8G   #创建8G的分区

Command (m for help): p   # p  是打印分区信息

Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1045     8393931   83  Linux

Command (m for help): t        #因为swap分区需要换分区号  输入t
Selected partition 1
Hex code (type L to list codes): 82    #swap的分区号是82  当然果然不知道可以使用L查看
Changed system type of partition 1 to 82 (Linux swap / Solaris)
#这里已经更改好了
Command (m for help): w            #w保存更改

以上的对swap分区的创建,然后需要对swap进行创建swap文件系统

[root@CentOS-10-64-2019-0921 /]$lsblk                  #查看创建好的分区
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0     11:0    1  3.7G  0 rom
sda      8:0    0  200G  0 disk
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0 97.7G  0 part /
├─sda3   8:3    0 48.8G  0 part /data
├─sda4   8:4    0    1K  0 part
├─sda5   8:5    0 19.5G  0 part /IMP
└─sda6   8:6    0    2G  0 part [SWAP]
sdb      8:16   0   20G  0 disk
└─sdb1   8:17   0    8G  0 part
[root@CentOS-10-64-2019-0921 /]$mkswap /dev/sdb1          #创建swap
Setting up swapspace version 1, size = 8393924 KiB
no label, UUID=2e2c378c-0966-42dc-89da-488602e89531

先看看我系统的swap

1 [root@CentOS-10-64-2019-0921 /]$free -h
2              total       used       free     shared    buffers     cached
3 Mem:          979M       393M       586M       260K        93M       129M
4 -/+ buffers/cache:       170M       809M
5 Swap:         2.0G         0B       2.0G

使用swap /dev/sdb1  命令挂载swap

[root@CentOS-10-64-2019-0921 /]$swapon /dev/sdb1
[root@CentOS-10-64-2019-0921 /]$free -h
             total       used       free     shared    buffers     cached
Mem:          979M       398M       580M       260K        94M       129M
-/+ buffers/cache:       175M       803M
Swap:          10G         0B        10G           #现在的swap已经挂载好了 一共10g
[root@CentOS-10-64-2019-0921 /]$

6.4 使用文件创建swap 

[root@CentOS-10-64-2019-0921 /data]$dd if=/dev/zero of=/data/swap bs=10M count=500   #使用dd创建一个5G的文件
500+0 records in
500+0 records out
5242880000 bytes (5.2 GB) copied, 17.67 s, 297 MB/s
[root@CentOS-10-64-2019-0921 /data]$ll swap
-rw-r--r--. 1 root root 5242880000 Oct 17 04:40 swap
[root@CentOS-10-64-2019-0921 /data]$ll -h swap
-rw-r--r--. 1 root root 4.9G Oct 17 04:40 swap
[root@CentOS-10-64-2019-0921 /data]$
[root@CentOS-10-64-2019-0921 /data]$mkswap /data/swap     #对文件创建swap系统
mkswap: /data/swap: warning: don't erase bootbits sectors
        on whole disk. Use -f to force.
Setting up swapspace version 1, size = 5119996 KiB
no label, UUID=ae10e648-2e0a-40b1-9d38-a8c7c2083503
[root@CentOS-10-64-2019-0921 /data]$blkid /data/swap    #查看/data/swap  已经是swap了
/data/swap: UUID="ae10e648-2e0a-40b1-9d38-a8c7c2083503" TYPE="swap"

然后跟上面一样挂载swap  使用swapon /data/swap

[root@CentOS-10-64-2019-0921 /data]$free -h
             total       used       free     shared    buffers     cached
Mem:          979M       900M        78M       260K        69M       660M
-/+ buffers/cache:       170M       808M
Swap:          10G         0B        10G   #现在是swap是10G
[root@CentOS-10-64-2019-0921 /data]$

挂载/data/swapon

1 [root@CentOS-10-64-2019-0921 /data]$free -h
2              total       used       free     shared    buffers     cached
3 Mem:          979M       905M        74M       260K        69M       661M
4 -/+ buffers/cache:       174M       805M
5 Swap:          14G         0B        14G

已经出来了

 

总结:在分区的创建与文件系统的创建时,注意分区磁盘与分区,对每一步的操作要确认没有问题,看看对硬盘更改是否同步到内存。不同步的话需要解决。

在使用挂载的时候,mount的挂载会下次失效,因此持久挂载需要写入/etc/fstab文件下。对照系统分区时的格式写就好了,主要注意的是用文件当作swap时持久挂载最好是写文件名,不要写uuid,因为文件太多。

另,文件可以挂载到文件目录,先把文件使用mkfs命令把文件做成一个文件系统,然后挂载就可以了。系统可以识别为loop设备,甚至当作优盘用。

但是 在Centos 6中挂载文件到文件需要先配对loop 设备,7不用匹配

[root@CentOS-10-64-2019-0921 /data]$dd if=/dev/zero of=/data/file bs=10M count=10   #创建文件
10+0 records in
10+0 records out
104857600 bytes (105 MB) copied, 0.271914 s, 386 MB/s
[root@CentOS-10-64-2019-0921 /data]$ll file
-rw-r--r--. 1 root root 104857600 Oct 17 05:09 file
[root@CentOS-10-64-2019-0921 /data]$mkfs.ext4 /data/f
f1.txt  f2.txt  file
[root@CentOS-10-64-2019-0921 /data]$mkfs.ext4 /data/file                #对文件创建ext4文件系统
mke2fs 1.41.12 (17-May-2010)
/data/file is not a block special device.
Proceed anyway? (y,n) y                 #是否继续 Y继续
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
25688 inodes, 102400 blocks
5120 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
13 block groups
8192 blocks per group, 8192 fragments per group
1976 inodes per group
Superblock backups stored on blocks:
    8193, 24577, 40961, 57345, 73729

Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 36 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@CentOS-10-64-2019-0921 /data]$

接下如果直接挂载file文件会报错 如下

1 [root@CentOS-10-64-2019-0921 /data]$mount /data/file /mnt
2 mount: /data/file is not a block device (maybe try `-o loop'?)  #可以使用 -o loop  我下面的代码使用配对的方式
3 [root@CentOS-10-64-2019-0921 /data]$
[root@CentOS-10-64-2019-0921 ~]$losetup /dev/loop0 /data/file   #先将文件与loop设备关联
[root@CentOS-10-64-2019-0921 ~]$mount /dev/loop0 /mnt           #然后把loop设备挂载到文件目录上
[root@CentOS-10-64-2019-0921 ~]$                   #然后可以使用文件

转载自:https://www.cnblogs.com/dreamfreedom/p/11691055.html

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值