【配置與安裝】CentOS7 磁盤分區(主分區、擴展分區和邏輯分區)的創建、掛載與刪除


創建磁盤分區

  • 查看磁盤分區情況:fdisk -l
[root@model ~]# fdisk -l                                                      //查看系統中所有磁盤的分區列表

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

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      591871      294912   83  Linux
/dev/sda2          591872   189335551    94371840   83  Linux
/dev/sda3       189335552   209715199    10189824   8e  Linux LVM

Disk /dev/sdb: 1099.5 GB, 1099511627776 bytes, 2147483648 sectors             //sdb磁盤未分區,1099.5GB
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: 0x3238f946

   Device Boot      Start         End      Blocks   Id  System

Disk /dev/mapper/centos-swap: 8334 MB, 8334082048 bytes, 16277504 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 65536 bytes / 65536 bytes

 

  • 創建主分區
[root@model ~]# 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): m                                         //查看菜單信息                 
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   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                                         //新增分區
Partition type:
   p   primary (0 primary, 0 extended, 4 free)                  //主分區
   e   extended                                                 //擴展分區
Select (default p): p
Partition number (1-4, default 1): 1                            //選擇分區號,范圍為1~4(主分區和擴展分區加起來最多只能建立 4 個,而擴展分區最多只能建立 1 個),這里選擇1
First sector (2048-2147483647, default 2048): 2048              //分區的起始扇面,默認從2048開始  
Last sector, +sectors or +size{K,M,G} (2048-2147483647, default 2147483647): +10G           //指定該磁盤分區的大小,默認占用整個磁盤;這里用 +size{K,M,G} 命令指定 10G
Partition 1 of type Linux and of size 10 GiB is set

Command (m for help): p                                         //打印當前磁盤的分區列表

Disk /dev/sdb: 1099.5 GB, 1099511627776 bytes, 2147483648 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: 0x3238f946

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

Command (m for help): w                                          //將本次配置寫入硬盤
The partition table has been altered!

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

[root@model ~]# lsblk                                            //lsblk 列表查看block分區
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
fd0 2:0 1 4K 0 disk
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 288M 0 part /boot
├─sda2 8:2 0 90G 0 part /
└─sda3 8:3 0 9.7G 0 part
├─centos-pool00_tmeta 253:0 0 8M 0 lvm
│ └─centos-pool00-tpool 253:2 0 7.8G 0 lvm
│ ├─centos-swap 253:3 0 7.8G 0 lvm [SWAP]
│ └─centos-pool00 253:4 0 7.8G 1 lvm
└─centos-pool00_tdata 253:1 0 7.8G 0 lvm
└─centos-pool00-tpool 253:2 0 7.8G 0 lvm
├─centos-swap 253:3 0 7.8G 0 lvm [SWAP]
└─centos-pool00 253:4 0 7.8G 1 lvm
sdb 8:16 0 1T 0 disk
└─sdb1 8:17 0 10G 0 part                                         //找到新建的分區/dev/sdb1
sr0 11:0 1 1024M 0 rom 

 

新建的分區還沒有格式化和掛載,所以還不能使用。用上述方法依次創建2、3號主分區,並預留4號分區作為擴展分區。

  • 創建擴展分區

[root@model ~]# 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): n                                          //新建分區
Partition type:
p primary (3 primary, 0 extended, 1 free)
e extended
Select (default e): e                                            //選擇擴展分區
Selected partition 4
First sector (62916608-2147483647, default 62916608):            //默認起始扇面
Using default value 62916608
Last sector, +sectors or +size{K,M,G} (62916608-2147483647, default 2147483647):       //默認將剩余全部空間分配給該擴展分區 
Using default value 2147483647
Partition 4 of type Extended and of size 994 GiB is set

Command (m for help): w                                          //將本次設置寫入磁盤

The partition table has been altered!

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

 

這里把 /dev/sdb 硬盤的所有剩余空間都建立為擴展分區,也就是建立 3 個主分區,剩余空間都建立成擴展分區,再於擴展分區中建立邏輯分區。擴展分區是不能被格式化和直接使用的,所以還要在擴展分區內部再建立邏輯分區。 

  • 創建邏輯分區
[root@model ~]# 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): n                                           //新建分區
All primary partitions are in use
Adding logical partition 5                                        //所有主分區已分配完,默認新建邏輯分區,從分區號從 5 開始
First sector (62918656-2147483647, default 62918656):             //起始扇面
Using default value 62918656
Last sector, +sectors or +size{K,M,G} (62918656-2147483647, default 2147483647): +5G        //指定邏輯分區的大小為 5G
Partition 5 of type Linux and of size 5 GiB is setCommand (m for help): w                                           //保存設置
The partition table has been altered!

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

 

 所有分區過程中如果不保存並退出是不會生效的,所以建立錯了也沒有關系,使用 q 命令不保存退出即可。如果使用了 w 命令,就會保存退出。有時因為系統的分區表正忙,所以需要重新啟動系統才能使新的分區表生效。

  • 格式化分區
[root@model ~]# mkfs.xfs /dev/sdb1                                //將分區/dev/sdb1的文件類型格式化為xfs
mke2fs 1.42.9 (28-Dec-2013)
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=2151677952
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

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

------------- 修改磁盤分區的文件類型 -----------------

[root@model ~]# blkid /dev/sdb6                                   //查看 /dev/sdb6 的 UUID 和 文件類型
/dev/sdb6: UUID="89f4f8e4-0592-4e91-9064-7388152fec32" TYPE="xfs"                             
[root@model ~]# mkfs.ext4 /dev/sdb6                               //將邏輯分區的文件類型從 xfs 修改為 ext4, 如果是從 ext4 修改為 xfs 需要在 命令后加 ‘-f’ 強制修改
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
40 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

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

[root@model ~]# blkid /dev/sdb6
/dev/sdb6: UUID="846373a4-35b0-4988-9574-a508da3906f2" TYPE="ext4" 

掛載磁盤分區 

[root@model ~]# mount /dev/sdb1 /usr/exdata1                       //將 /dev/sdb1 磁盤分區掛載到新建的掛載點 /usr/exdata1 上
[root@model ~]# df -T -h  | grep /dev/sdb1                         //查看 該分區是否已掛載
Filesystem     Type      Size  Used Avail Use% Mounted on
/dev/sdb1      ext4      9.8G   37M  9.2G   1% /usr/exdata1

[root@model ~]# vim /etc/fstab                                     //修改fstab配置文件,將該磁盤分區設置為開機自動掛載
 # Add command by Kay.sun in 2020/9/29 16:58                       //在配置文件中添加一行命令
 /dev/sdb1 /usr/exdata1 xfs defaults 1 2                           // /dev/sdb1 選擇一個分區;xfs 是當前分區的格式; defaults = rw, suid, dev, exec, auto, nouser, and async; 1 需要 dump;0 不需要 dump; 2 開機時檢查順序,非 boot 文件系統為 1,其它文件系統都為 2
[root@model ~]# df -h | grep /dev/sdb1                             //重啟后再次查看分區掛載情況,發現已經自動掛載~]# umount /usr/

刪除磁盤分區

 

[root@model ~]# 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): d                                         //刪除分區
Partition number (1-6, default 6): 6                            //選擇待刪除的分區號
Partition 6 is deleted

Command (m for help): w                                         //保存設置
The partition table has been altered!

Calling ioctl() to re-read partition table.


免責聲明!

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



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