磁盘分区文件系统挂载与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 下面同义)  使用的查看方法更多

  

 1 [root@CentOS-10-64-2019-0921 ~]$parted /dev/sda print
 2 Model: VMware, VMware Virtual S (scsi)
 3 Disk /dev/sda: 215GB
 4 Sector size (logical/physical): 512B/512B
 5 Partition Table: msdos
 6 
 7 Number  Start   End     Size    Type      File system     Flags
 8  1      1049kB  1075MB  1074MB  primary   ext4            boot
 9  2      1075MB  106GB   105GB   primary   ext4
10  3      106GB   158GB   52.4GB  primary   ext4
11  4      158GB   215GB   56.4GB  extended
12  5      158GB   179GB   21.0GB  logical   ext4
13  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的硬盘为例

  

 1 [root@CentOS-10-64-2019-0921 ~]$fdisk /dev/sdb   #使用fdisk  对硬盘sdb分区,而不是对分区,分区
 2 
 3 WARNING: GPT (GUID Partition Table) detected on '/dev/sdb'! The util fdisk doesn't support GPT. Use GNU Parted.
 4 #这个警告是我这个硬盘之前进行过GTP的分区,上面有残留的GTP分区信息,请忽略,看后面在CentOS6上fdisk不支持GTP分区
 5 
 6 WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
 7          switch off the mode (command 'c') and change display units to
 8          sectors (command 'u').
 9 
10 Command (m for help): m      #使用m看帮助 ,
11 Command action
12    a   toggle a bootable flag
13    b   edit bsd disklabel
14    c   toggle the dos compatibility flag
15    d   delete a partition
16    l   list known partition types
17    m   print this menu
18    n   add a new partition
19    o   create a new empty DOS partition table
20    p   print the partition table
21    q   quit without saving changes
22    s   create a new empty Sun disklabel
23    t   change a partition's system id
24    u   change display/entry units
25    v   verify the partition table
26    w   write table to disk and exit
27    x   extra functionality (experts only)
28 
29 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): 
 1 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +6G  
  #输入容量的话输入+号和单位,纯数字系统会作为柱面来识别
2 Command (m for help): p #打印硬盘信息 看 没有刚刚分好的区,这样误操作的话安全性很高 3 4 Disk /dev/sdb: 21.5 GB, 21474836480 bytes 5 255 heads, 63 sectors/track, 2610 cylinders 6 Units = cylinders of 16065 * 512 = 8225280 bytes 7 Sector size (logical/physical): 512 bytes / 512 bytes 8 I/O size (minimum/optimal): 512 bytes / 512 bytes 9 Disk identifier: 0x00000000 10 11 Device Boot Start End Blocks Id System 12 /dev/sdb1 1 784 6297448+ 83 Linux 13 14 Command (m for help): n 15 Command action 16 e extended 17 p primary partition (1-4) 18 e                   #这里创建了一个扩展分区 19 Partition number (1-4): 2 20 First cylinder (785-2610, default 785): 21 Using default value 785 22 Last cylinder, +cylinders or +size{K,M,G} (785-2610, default 2610): +3G #3G的扩展分区
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使用

 1 [root@CentOS-10-64-2019-0921 ~]$fdisk -l /dev/sdb       #这样查看
 2 
 3 WARNING: GPT (GUID Partition Table) detected on '/dev/sdb'! The util fdisk doesn't support GPT. Use GNU Parted.
 4 
 5 
 6 Disk /dev/sdb: 21.5 GB, 21474836480 bytes
 7 255 heads, 63 sectors/track, 2610 cylinders
 8 Units = cylinders of 16065 * 512 = 8225280 bytes
 9 Sector size (logical/physical): 512 bytes / 512 bytes
10 I/O size (minimum/optimal): 512 bytes / 512 bytes
11 Disk identifier: 0x00000000
12 
13 Device Boot      Start         End      Blocks   Id  System
14 /dev/sdb1               1         784     6297448+  83  Linux
15 /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分区。

    

 1 [root@CentOS-10-64-2019-0921 ~]$gdisk /dev/sdb
 2 GPT fdisk (gdisk) version 0.8.10
 3 
 4 Caution: invalid main GPT header, but valid backup; regenerating main header
 5 from backup!
 6 
 7 Caution! After loading partitions, the CRC doesn't check out!
 8 Warning! Main partition table CRC mismatch! Loaded backup partition table
 9 instead of main partition table!
10 
11 Warning! One or more CRCs don't match. You should repair the disk!
12 
13 Partition table scan:
14   MBR: not present
15   BSD: not present
16   APM: not present
17   GPT: damaged
18 
19 Found invalid MBR and corrupt GPT. What do you want to do? (Using the
20 GPT MAY permit recovery of GPT data.)
21  1 - Use current GPT
22  2 - Create blank GPT
23 
24 Your answer: 2         #由于这个硬盘还残留这之前的分区信息所以会与这样的显示,选择2 创建新的GPT分区
 1 Command (? for help): ?
 2 b    back up GPT data to a file
 3 c    change a partition's name
 4 d    delete a partition
 5 i    show detailed information on a partition
 6 l    list known partition types
 7 n    add a new partition
 8 o    create a new empty GUID partition table (GPT)
 9 p    print the partition table
10 q    quit without saving changes
11 r    recovery and transformation options (experts only)
12 s    sort partitions
13 t    change a partition's type code
14 v    verify disk
15 w    write table to disk and exit
16 x    extra functionality (experts only)
17 ?    print this menu            #基本用法与fdisk一样
18 
19 Command (? for help): 
1 Command (? for help): n            # n  建分区
2 Partition number (1-128, default 1):     #GPT没有扩展分区,创建的都是主分区
3 First sector (34-41943006, default = 2048) or {+-}size{KMGTP}:     #First sector  在6上 gdisk以扇区划分的
4 Last sector (2048-41943006, default = 41943006) or {+-}size{KMGTP}: +10G    #  10g的分区
5 Current type is 'Linux filesystem'
6 Hex code or GUID (L to show codes, Enter = 8300): 
7 Changed type of partition to 'Linux filesystem'
8 
9 Command (? for help): 
1 Command (? for help): w   # 跟fdisk 一样保存更改的分区
2 
3 Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
4 PARTITIONS!!
5 
6 Do you want to proceed? (Y/N): y  #确认继续
7 OK; writing new GUID partition table (GPT) to /dev/sdb.
8 The operation has completed successfully.
9 [root@CentOS-10-64-2019-0921 ~]$

    看看分区表写入了没有

 1 [root@CentOS-10-64-2019-0921 ~]$lsblk
 2 NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
 3 sr0     11:0    1  3.7G  0 rom  
 4 sda      8:0    0  200G  0 disk 
 5 ├─sda1   8:1    0    1G  0 part /boot
 6 ├─sda2   8:2    0 97.7G  0 part /
 7 ├─sda3   8:3    0 48.8G  0 part /data
 8 ├─sda4   8:4    0    1K  0 part 
 9 ├─sda5   8:5    0 19.5G  0 part /IMP
10 └─sda6   8:6    0    2G  0 part [SWAP]
11 sdb      8:16   0   20G  0 disk 
12 └─sdb1   8:17   0   10G  0 part 
13 [root@CentOS-10-64-2019-0921 ~]$lsblk -l /dev/sdb1
14 NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
15 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 好像不支持

 1 [root@CentOS-10-64-2019-0921 ~]$mkfs.ext4 /dev/sdb1
 2 mke2fs 1.41.12 (17-May-2010)
 3 Filesystem label=
 4 OS type: Linux
 5 Block size=4096 (log=2)
 6 Fragment size=4096 (log=2)
 7 Stride=0 blocks, Stripe width=0 blocks
 8 655360 inodes, 2621440 blocks
 9 131072 blocks (5.00%) reserved for the super user
10 First data block=0
11 Maximum filesystem blocks=2684354560
12 80 block groups
13 32768 blocks per group, 32768 fragments per group
14 8192 inodes per group
15 Superblock backups stored on blocks: 
16     32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
17 
18 Writing inode tables: done                            
19 Creating journal (32768 blocks): done
20 Writing superblocks and filesystem accounting information: done
21 
22 This filesystem will be automatically checked every 34 mounts or
23 180 days, whichever comes first.  Use tune2fs -c or -i to override.
24 [root@CentOS-10-64-2019-0921 ~]$

查看文件系统  blkid

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

管理文件系统的命令 tune2fs

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

      

 1 [root@CentOS-10-64-2019-0921 /]$fdisk /dev/sdb       #使用fdisk 对硬盘分区
 2 
 3 WARNING: GPT (GUID Partition Table) detected on '/dev/sdb'! The util fdisk doesn't support GPT. Use GNU Parted.
 4 
 5 
 6 WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
 7          switch off the mode (command 'c') and change display units to
 8          sectors (command 'u').
 9 
10 Command (m for help): n        #n 跟前面的一样
11 Command action
12    e   extended
13    p   primary partition (1-4)
14 p
15 Partition number (1-4): 1
16 First cylinder (1-2610, default 1): 
17 Using default value 1
18 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +8g
19 Unsupported suffix: 'g'.     #这里小写g还不支持而报错,7上我记得小写是可以的
20 Supported: 10^N: KB (KiloByte), MB (MegaByte), GB (GigaByte)
21             2^N: K  (KibiByte), M  (MebiByte), G  (GibiByte)
22 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +8G   #创建8G的分区
23 
24 Command (m for help): p   # p  是打印分区信息                        
25 
26 Disk /dev/sdb: 21.5 GB, 21474836480 bytes
27 255 heads, 63 sectors/track, 2610 cylinders
28 Units = cylinders of 16065 * 512 = 8225280 bytes
29 Sector size (logical/physical): 512 bytes / 512 bytes
30 I/O size (minimum/optimal): 512 bytes / 512 bytes
31 Disk identifier: 0x00000000
32 
33    Device Boot      Start         End      Blocks   Id  System
34 /dev/sdb1               1        1045     8393931   83  Linux
35 
36 Command (m for help): t        #因为swap分区需要换分区号  输入t
37 Selected partition 1
38 Hex code (type L to list codes): 82    #swap的分区号是82  当然果然不知道可以使用L查看 
39 Changed system type of partition 1 to 82 (Linux swap / Solaris)
40 #这里已经更改好了
41 Command (m for help): w            #w保存更改

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

 1 [root@CentOS-10-64-2019-0921 /]$lsblk                  #查看创建好的分区
 2 NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
 3 sr0     11:0    1  3.7G  0 rom  
 4 sda      8:0    0  200G  0 disk 
 5 ├─sda1   8:1    0    1G  0 part /boot
 6 ├─sda2   8:2    0 97.7G  0 part /
 7 ├─sda3   8:3    0 48.8G  0 part /data
 8 ├─sda4   8:4    0    1K  0 part 
 9 ├─sda5   8:5    0 19.5G  0 part /IMP
10 └─sda6   8:6    0    2G  0 part [SWAP]
11 sdb      8:16   0   20G  0 disk 
12 └─sdb1   8:17   0    8G  0 part 
13 [root@CentOS-10-64-2019-0921 /]$mkswap /dev/sdb1          #创建swap
14 Setting up swapspace version 1, size = 8393924 KiB
15 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

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

  6.4 使用文件创建swap 

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

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

1 [root@CentOS-10-64-2019-0921 /data]$free -h    
2              total       used       free     shared    buffers     cached
3 Mem:          979M       900M        78M       260K        69M       660M
4 -/+ buffers/cache:       170M       808M
5 Swap:          10G         0B        10G   #现在是swap是10G
6 [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命令把文件做成一个文件系统,然后挂载就可以了。甚至当作优盘用。把文件挂载文件写到/etc/fstab文件中是default项可以写blind。

猜你喜欢

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