实现新增加硬盘的磁盘分区和文件系统挂载

实现新增加硬盘的磁盘分区和文件系统挂载

1. 查看磁盘设备

[root@centos7 ~]#ls /dev/sda*
/dev/sda  /dev/sda1  /dev/sda2  /dev/sda3  /dev/sda4  /dev/sda5

2. 添加硬盘

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

3. 别名实现新添加硬盘的识别

[root@centos7 ~]#alias scandisk='echo - - - > /sys/class/scsi_host/host0/scan;echo - - - > /sys/class/scsi_host/host1/scan;echo - - - > /sys/class/scsi_host/host2/scan'
[root@centos7 ~]#alias scandisk
alias scandisk='echo - - - > /sys/class/scsi_host/host0/scan;echo - - - > /sys/class/scsi_host/host1/scan;echo - - - > /sys/class/scsi_host/host2/scan'
[root@centos7 ~]#scandisk
[root@centos7 ~]#lsblk
[root@centos7 ~]#lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  200G  0 disk
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0  100G  0 part /
├─sda3   8:3    0   50G  0 part /data
├─sda4   8:4    0    1K  0 part
└─sda5   8:5    0    2G  0 part [SWAP]
sdb      8:16   0  200G  0 disk
sr0     11:0    1  9.5G  0 rom

[root@centos7 ~]#ls /dev/sd*
/dev/sda  /dev/sda1  /dev/sda2  /dev/sda3  /dev/sda4  /dev/sda5  /dev/sdb

4. 创建分区

MBR:Master Boot Record,1982年,使用32位表示扇区数,分区不超过2T

MBR分区中一块硬盘最多有4个主分区,也可以3主分区+1扩展(N个逻辑分区)
MBR分区:主和扩展分区对应的1–4,/dev/sda3,逻辑分区从5开始,/dev/sda5

GPT:GUID(Globals Unique Identifiers) partition table 支持128个分区,使用64位,支持8Z(
512Byte/block )64Z ( 4096Byte/block)

fdisk /dev/sda:管理MBR分区
gdisk /dev/sda:类fdisk的GPT分区工具

子命令:
p:分区列表
t:更改分区类型
n:创建新分区
d:删除分区
v:校验分区
u:转换单位
w:保存并退出
q:不保存并退出

[root@centos7 ~]#yum -y install gdisk
[root@centos7 ~]#gdisk /dev/sdb
Command (? for help): n
Partition number (1-128, default 1): 1
First sector (34-419430366, default = 2048) or {
    
    +-}size{
    
    KMGTP}: 2048
Last sector (2048-419430366, default = 419430366) or {
    
    +-}size{
    
    KMGTP}: +1G
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): p
Disk /dev/sdb: 419430400 sectors, 200.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 0351662F-7E98-4CC8-B5D4-8973B19FC01C
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 419430366
Partitions will be aligned on 2048-sector boundaries
Total free space is 417333181 sectors (199.0 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048         2099199   1024.0 MiB  8300  Linux filesystem

Command (? for help): n
Partition number (2-128, default 2): 2
First sector (34-419430366, default = 2099200) or {
    
    +-}size{
    
    KMGTP}:
Last sector (2099200-419430366, default = 419430366) or {
    
    +-}size{
    
    KMGTP}: +100G
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): p
Disk /dev/sdb: 419430400 sectors, 200.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 0351662F-7E98-4CC8-B5D4-8973B19FC01C
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 419430366
Partitions will be aligned on 2048-sector boundaries
Total free space is 207617981 sectors (99.0 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048         2099199   1024.0 MiB  8300  Linux filesystem
   2         2099200       211814399   100.0 GiB   8300  Linux filesystem

Command (? for help): n
Partition number (3-128, default 3): 3
First sector (34-419430366, default = 211814400) or {
    
    +-}size{
    
    KMGTP}:
Last sector (211814400-419430366, default = 419430366) or {
    
    +-}size{
    
    KMGTP}: +50G
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): p
Disk /dev/sdb: 419430400 sectors, 200.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 0351662F-7E98-4CC8-B5D4-8973B19FC01C
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 419430366
Partitions will be aligned on 2048-sector boundaries
Total free space is 102760381 sectors (49.0 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048         2099199   1024.0 MiB  8300  Linux filesystem
   2         2099200       211814399   100.0 GiB   8300  Linux filesystem
   3       211814400       316671999   50.0 GiB    8300  Linux filesystem

Command (? for help): n
Partition number (4-128, default 4): 4
First sector (34-419430366, default = 316672000) or {
    
    +-}size{
    
    KMGTP}:
Last sector (316672000-419430366, default = 419430366) or {
    
    +-}size{
    
    KMGTP}: +1k
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): p
Disk /dev/sdb: 419430400 sectors, 200.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 0351662F-7E98-4CC8-B5D4-8973B19FC01C
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 419430366
Partitions will be aligned on 2048-sector boundaries
Total free space is 102760379 sectors (49.0 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048         2099199   1024.0 MiB  8300  Linux filesystem
   2         2099200       211814399   100.0 GiB   8300  Linux filesystem
   3       211814400       316671999   50.0 GiB    8300  Linux filesystem
   4       316672000       316672001   1024 bytes  8300  Linux filesystem

Command (? for help): n
Partition number (5-128, default 5): 5
First sector (34-419430366, default = 316674048) or {
    
    +-}size{
    
    KMGTP}:
Last sector (316674048-419430366, default = 419430366) or {
    
    +-}size{
    
    KMGTP}: +2G
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): p
Disk /dev/sdb: 419430400 sectors, 200.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 0351662F-7E98-4CC8-B5D4-8973B19FC01C
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 419430366
Partitions will be aligned on 2048-sector boundaries
Total free space is 98566075 sectors (47.0 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048         2099199   1024.0 MiB  8300  Linux filesystem
   2         2099200       211814399   100.0 GiB   8300  Linux filesystem
   3       211814400       316671999   50.0 GiB    8300  Linux filesystem
   4       316672000       316672001   1024 bytes  8300  Linux filesystem
   5       316674048       320868351   2.0 GiB     8300  Linux filesystem

Command (? for help): w

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.

5. 创建文件系统

[root@centos7 ~]#lsblk -f
NAME   FSTYPE  LABEL           UUID                                 MOUNTPOINT
sda
├─sda1 xfs                     b36add53-d3f4-4ef7-9444-3c69bffb95db /boot
├─sda2 ext4                    099f9e4c-8094-4e19-ace1-f99b7f81744b /
├─sda3 xfs                     756b01bd-ccee-4563-a492-7368bb57b100 /data
├─sda4
└─sda5 swap                    076fc1a8-ac81-4378-8fcf-6968c31519bc [SWAP]
sdb
├─sdb1
├─sdb2
├─sdb3
├─sdb4
└─sdb5
sr0    iso9660 CentOS 7 x86_64 2020-11-02-15-15-23-00

[root@centos7 ~]#mkfs.xfs /dev/sdb1
[root@centos7 ~]#mkfs.ext4 /dev/sdb2
[root@centos7 ~]#mkfs.xfs /dev/sdb3
[root@centos7 ~]#mkswap /dev/sdb5

[root@centos7 ~]#lsblk -f
NAME   FSTYPE  LABEL           UUID                                 MOUNTPOINT
sda
├─sda1 xfs                     b36add53-d3f4-4ef7-9444-3c69bffb95db /boot
├─sda2 ext4                    099f9e4c-8094-4e19-ace1-f99b7f81744b /
├─sda3 xfs                     756b01bd-ccee-4563-a492-7368bb57b100 /data
├─sda4
└─sda5 swap                    076fc1a8-ac81-4378-8fcf-6968c31519bc [SWAP]
sdb
├─sdb1 xfs                     2a668187-9013-4187-9177-bcbcff8842f0
├─sdb2 ext4                    cd43676b-cd3f-409c-8c62-374db8c02ffe
├─sdb3 xfs                     1d8d09e7-dd06-4e08-8021-b34f64cf77b1
├─sdb4
└─sdb5 swap                    5a4fa393-66c2-4530-a058-f2602130da6a
sr0    iso9660 CentOS 7 x86_64 2020-11-02-15-15-23-00

[root@centos7 ~]#partprobe  #同步分区表
Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.

6. 挂载

[root@centos7 ~]#vim /etc/fstab
UUID=cd43676b-cd3f-409c-8c62-374db8c02ffe /root                   ext4    defaults        1 1
UUID=2a668187-9013-4187-9177-bcbcff8842f0 /boot                   xfs     defaults        0 0
UUID=1d8d09e7-dd06-4e08-8021-b34f64cf77b1 /data                   xfs     defaults        0 0
UUID=5a4fa393-66c2-4530-a058-f2602130da6a swap                    swap    defaults        0 0

[root@centos7 ~]#mount -a  #使添加新的挂载项生效
[root@centos7 ~]#swapon -a  #激活所有的交换分区
[root@centos7 ~]#lsblk -f
NAME   FSTYPE  LABEL           UUID                                 MOUNTPOINT
sda
├─sda1 xfs                     b36add53-d3f4-4ef7-9444-3c69bffb95db /boot
├─sda2 ext4                    099f9e4c-8094-4e19-ace1-f99b7f81744b /
├─sda3 xfs                     756b01bd-ccee-4563-a492-7368bb57b100 /data
├─sda4
└─sda5 swap                    076fc1a8-ac81-4378-8fcf-6968c31519bc [SWAP]
sdb
├─sdb1 xfs                     2a668187-9013-4187-9177-bcbcff8842f0 /boot
├─sdb2 ext4                    cd43676b-cd3f-409c-8c62-374db8c02ffe /root
├─sdb3 xfs                     1d8d09e7-dd06-4e08-8021-b34f64cf77b1 /data
├─sdb4
└─sdb5 swap                    5a4fa393-66c2-4530-a058-f2602130da6a [SWAP]
sr0    iso9660 CentOS 7 x86_64 2020-11-02-15-15-23-00

7. 查看磁盘空间使用情况

[root@centos7 ~]#df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        980M     0  980M   0% /dev
tmpfs           991M     0  991M   0% /dev/shm
tmpfs           991M  9.6M  981M   1% /run
tmpfs           991M     0  991M   0% /sys/fs/cgroup
/dev/sda2        99G  2.6G   91G   3% /
/dev/sdb3        50G   33M   50G   1% /data
/dev/sdb1      1014M   33M  982M   4% /boot
tmpfs           199M     0  199M   0% /run/user/0
/dev/sdb2        99G   61M   94G   1% /root
[root@centos7 ~]#free -h
              total        used        free      shared  buff/cache   available
Mem:           1.9G        165M        1.5G        9.5M        317M        1.6G
Swap:          4.0G          0B        4.0G

猜你喜欢

转载自blog.csdn.net/weixin_51867896/article/details/125986743
今日推荐