磁盤分區文件系統掛載與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命令把文件做成一個文件系統,然后掛載就可以了。系統可以識別為loop設備,甚至當作優盤用。

但是 在Centos 6中掛載文件到文件需要先配對loop 設備,7不用匹配

 

 1 [root@CentOS-10-64-2019-0921 /data]$dd if=/dev/zero of=/data/file bs=10M count=10 #創建文件
 2 10+0 records in
 3 10+0 records out  4 104857600 bytes (105 MB) copied, 0.271914 s, 386 MB/s  5 [root@CentOS-10-64-2019-0921 /data]$ll file 
 6 -rw-r--r--. 1 root root 104857600 Oct 17 05:09 file
 7 [root@CentOS-10-64-2019-0921 /data]$mkfs.ext4 /data/f  8 f1.txt  f2.txt  file    
 9 [root@CentOS-10-64-2019-0921 /data]$mkfs.ext4 /data/file                #對文件創建ext4文件系統
10 mke2fs 1.41.12 (17-May-2010) 11 /data/file is not a block special device. 12 Proceed anyway? (y,n) y #是否繼續 Y繼續 13 Filesystem label=
14 OS type: Linux 15 Block size=1024 (log=0) 16 Fragment size=1024 (log=0) 17 Stride=0 blocks, Stripe width=0 blocks 18 25688 inodes, 102400 blocks 19 5120 blocks (5.00%) reserved for the super user 20 First data block=1
21 Maximum filesystem blocks=67371008
22 13 block groups
23 8192 blocks per group, 8192 fragments per group 24 1976 inodes per group 25 Superblock backups stored on blocks: 26     8193, 24577, 40961, 57345, 73729
27 
28 Writing inode tables: done                            
29 Creating journal (4096 blocks): done
30 Writing superblocks and filesystem accounting information: done
31 
32 This filesystem will be automatically checked every 36 mounts or 33 180 days, whichever comes first.  Use tune2fs -c or -i to override. 34 [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 ~]$                   #然后可以使用文件


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM