Linux使用fdisk命令新建分區詳細步驟


前言:
本文將演示在LINUX系統中如何添加主分區,SWAP分區,邏輯分區

 

1.VMware添加硬盤

#我們添加 SCSI 磁盤

 

 

 

 

2.重啟或者掃描主機更新磁盤信息

[16:46:02 root@c7-1 ~]#fdisk -l    #此時新加的 sdb 磁盤還沒出現,如果不想重啟虛擬機執行下條命令

Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x000f2378

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    10487807     4194304   82  Linux swap / Solaris
/dev/sda3        10487808   209715199    99613696   83  Linux
[16:51:11 root@c7-1 ~]#echo "- - -" > /sys/class/scsi_host/host0/scan    #全盤掃描主機更新信息,這樣就不需要重啟虛擬機了
[16:51:28 root@c7-1 ~]#fdisk -l  #再次查看 sdb 已經出現

Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x000f2378

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    10487807     4194304   82  Linux swap / Solaris
/dev/sda3        10487808   209715199    99613696   83  Linux

Disk /dev/sdb: 64.4 GB, 64424509440 bytes, 125829120 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

 

3.開始分區

#分區規划,sdb1主分區10G,sdb2 SWAP分區10G,sdb3擴展分區20G,sdb5邏輯分區10G

#步驟:主分區 -- SWAP分區 -- 擴展分區 -- 邏輯分區

#邏輯分區是在擴展分區的基礎上分的

[16:51:29 root@c7-1 ~]#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 0x1976e323.

Command (m for help): n  #開始分主分區
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p   #主分區
Partition number (1-4, default 1): 1  #分區號可以默認也可手動選
First sector (2048-125829119, default 2048):   #扇區默認
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-125829119, default 125829119): +10G    #主分區設定10G
Partition 1 of type Linux and of size 10 GiB is set

Command (m for help): p  #顯示分區信息

Disk /dev/sdb: 64.4 GB, 64424509440 bytes, 125829120 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: 0x1976e323

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    20973567    10485760   83  Linux

Command (m for help): n  #開始分 SWAP 分區
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p  #選擇主分區
Partition number (2-4, default 2): 2
First sector (20973568-125829119, default 20973568): 
Using default value 20973568
Last sector, +sectors or +size{K,M,G} (20973568-125829119, default 125829119): +10G  #大小設定10G
Partition 2 of type Linux and of size 10 GiB is set

Command (m for help): t  #設定分區系統號碼,82為 SWAP 
Partition number (1,2, default 2): 2   #我們將 sdb2 設定為 swap 分區,別寫成 1 了
Hex code (type L to list all codes): 82  #設定分區類型
Changed type of partition 'Linux' to 'Linux swap / Solaris'

Command (m for help): p  #查看分區情況

Disk /dev/sdb: 64.4 GB, 64424509440 bytes, 125829120 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: 0x1976e323

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    20973567    10485760   83  Linux
/dev/sdb2        20973568    41945087    10485760   82  Linux swap / Solaris

Command (m for help): n  #開始添加擴展分區
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): e  # e 添加擴展分區
Partition number (3,4, default 3): 
First sector (41945088-125829119, default 41945088): 
Using default value 41945088
Last sector, +sectors or +size{K,M,G} (41945088-125829119, default 125829119): +20G  #大小設定20G
Partition 3 of type Extended and of size 20 GiB is set

Command (m for help): n  #開始添加邏輯分區
Partition type:
   p   primary (2 primary, 1 extended, 1 free)
   l   logical (numbered from 5)
Select (default p): l  # l 添加邏輯分區
Adding logical partition 5
First sector (41947136-83888127, default 41947136): 
Using default value 41947136
Last sector, +sectors or +size{K,M,G} (41947136-83888127, default 83888127): +10G  #大小設定10G
Partition 5 of type Linux and of size 10 GiB is set

Command (m for help): p  #查看分區情況

Disk /dev/sdb: 64.4 GB, 64424509440 bytes, 125829120 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: 0x1976e323

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    20973567    10485760   83  Linux
/dev/sdb2        20973568    41945087    10485760   82  Linux swap / Solaris
/dev/sdb3        41945088    83888127    20971520    5  Extended
/dev/sdb5        41947136    62918655    10485760   83  Linux

Command (m for help): w  #保存並退出
The partition table has been altered!

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

[
16:54:55 root@c7-1 ~]#partprobe /dev/sdb  #更新分區表信息

 

4.格式化文件系統並掛載

(1)格式化主分區

mkdir /opt/data{1,2}    #創建兩個掛載目錄給主分區和邏輯分區

mkfs.xfs /dev/sdb1     #格式化主分區為 xfs 格式

 

(2)格式化SWAP分區

mkswap /dev/sdb2  #將 sdb2 設置為 SWAP

swapon /dev/sdb2  #開啟 sdb2 交換功能

 

(3)格式化邏輯分區

mkfs.xfs /dev/sdb5  #將 sdb5 設置為 xfs 格式

 

(4)將分區信息寫進 /etc/fstab 配置文件中實現自動掛載

[17:05:50 root@c7-1 ~]#vim /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Fri Jul 23 15:33:12 2021
#
# 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
#
UUID=adf79f65-0a7d-4159-adde-099e05b62786 /                       xfs     defaults        0 0
UUID=e269bb76-c9d1-499b-a0d3-9bb86dbbb72d /boot                   xfs     defaults        0 0
UUID=61978a53-09e9-4404-96d1-7b3e2bfe7bba swap                    swap    defaults        0 0
/dev/sdb1 /opt/data1 xfs defaults 0 0 /dev/sdb2 swap swap defaults 0 0 /dev/sdb5 /opt/data2 xfs defaults 0 0

  [17:06:07 root@c7-1 ~]#mount -a  #重載 /etc/fstab 文件,不需要重啟即可生效

 

5.驗證

[17:36:44 root@c7-1 ~]#free -h  #SWAP增加了10G
              total        used        free      shared  buff/cache   available
Mem:           1.8G        709M        126M         23M        982M        938M
Swap:           13G          0B         13G
[17:36:52 root@c7-1 ~]#cat /proc/partitions 
major minor  #blocks  name

   8        0  104857600 sda
   8        1    1048576 sda1
   8        2    4194304 sda2
   8        3   99613696 sda3
  11        0    4554752 sr0
   8       16   62914560 sdb
   8       17   10485760 sdb1
   8       18   10485760 sdb2
   8       19          1 sdb3
   8       21   10485760 sdb5
[17:37:01 root@c7-1 ~]#df -h  #sdb1和sdb5已經掛載成功
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        895M     0  895M   0% /dev
tmpfs           910M     0  910M   0% /dev/shm
tmpfs           910M   11M  900M   2% /run
tmpfs           910M     0  910M   0% /sys/fs/cgroup
/dev/sda3        95G  5.3G   90G   6% /
/dev/sda1      1014M  179M  836M  18% /boot
tmpfs           182M  4.0K  182M   1% /run/user/42
tmpfs           182M     0  182M   0% /run/user/0
tmpfs           182M   36K  182M   1% /run/user/1000
/dev/sr0        4.4G  4.4G     0 100% /run/media/syhj/CentOS 7 x86_64
/dev/sdb1        10G   33M   10G   1% /opt/data1
/dev/sdb5        10G   33M   10G   1% /opt/data2
[17:37:13 root@c7-1 ~]#lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  100G  0 disk 
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0    4G  0 part [SWAP]
└─sda3   8:3    0   95G  0 part /
sdb      8:16   0   60G  0 disk 
├─sdb1   8:17   0   10G  0 part /opt/data1
├─sdb2   8:18   0   10G  0 part [SWAP]
├─sdb3   8:19   0    1K  0 part 
└─sdb5   8:21   0   10G  0 part /opt/data2
sr0     11:0    1  4.4G  0 rom  /run/media/syhj/CentOS 7 x86_64
[17:37:28 root@c7-1 ~]#blkid -o list
device                                    fs_type        label           mount point                                   UUID
------------------------------------------------------------------------------------------------------------------------------------------------------------
/dev/sda1                                 xfs                            /boot                                         e269bb76-c9d1-499b-a0d3-9bb86dbbb72d
/dev/sda2                                 swap                           <swap>                                        61978a53-09e9-4404-96d1-7b3e2bfe7bba
/dev/sda3                                 xfs                            /                                             adf79f65-0a7d-4159-adde-099e05b62786
/dev/sr0                                  iso9660        CentOS 7 x86_64 /run/media/syhj/CentOS 7 x86_64               2019-09-11-18-50-31-00
/dev/sdb1                                 xfs                            /opt/data1                                    be41ef53-be5a-4d67-89ac-541b594a4d85
/dev/sdb2                                 swap                           <swap>                                        bb50bc18-b642-4917-b552-e25bdda74a1d
/dev/sdb5                                 xfs                            /opt/data2                                    69d1003d-3049-4e7a-9e31-bf1cbf4efaab

 


免責聲明!

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



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