linux磁碟分割槽格式化-fdisk命令工具

天生帥才發表於2018-06-14

本文主要講述使用fdisk工具對磁碟進行分割槽和格式化的方法

首先要明確分割槽是針對磁碟進行的操做,磁碟分割槽會建立分割槽表,類似vda,sda的是磁碟,vda1,sda1的是分割槽

1.檢視磁碟分割槽狀態

1.1. 檢視分割槽掛載情況

df -h

1.2.檢視伺服器掛載的磁碟

fdisk -l
fdisk -l /dev/vdb

2.磁碟分割槽

2.1.對vdb磁碟進行分割槽

fdisk /dev/vdb

進入互動式的分割槽介面,以下是分割槽操作步驟

m列印選單
n新建分割槽
    p選擇主分割槽
    e選擇擴充套件分割槽
        1選擇分割槽號
            選擇初始位置,預設為1
            選擇結束為止,預設為磁碟結尾

分割槽建立完成,可以列表檢視,儲存退出

p列印分割槽表
w寫入分割槽表儲存
q退出

2.2.重新載入磁碟分割槽資訊

無返回就是載入磁碟資訊成功,針對磁碟進行操作

partprobe /dev/vdb

注意:
使用fdisk工具只是將分割槽資訊寫到磁碟,
在格式化分割槽值前需要使用partprobe讓kernel重新讀取分割槽資訊,如果不成功則需要重啟系統

2.3.對磁碟分割槽進行格式化

使用ext4檔案系統

mkfs.ext4 /dev/vdb1

2.4.格式化完成後續優化配置,取消磁碟自檢

tune2fs -c -1 /dev/vdb1 

3.分割槽掛載

3.1.掛載磁碟分割槽到指定目錄

mkdir -p /data
mount /dev/vdb1 /data

3.2.配置開機自動掛載分割槽

/etc/fstab 增加一行

vim /etc/fstab 
-------------------------------------------------------
/dev/vdb1               /data                   ext4    defaults        0 0
-------------------------------------------------------

5.例項演示:使用Ubuntu系統進行磁碟分割槽操作

5.1.檢視磁碟分割槽資訊

root@zstest:~# df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            7.9G     0  7.9G   0% /dev
tmpfs           1.6G  3.2M  1.6G   1% /run
/dev/vda1        50G  5.7G   41G  13% /
tmpfs           7.9G     0  7.9G   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           7.9G     0  7.9G   0% /sys/fs/cgroup
tmpfs           1.6G     0  1.6G   0% /run/user/0

5.2.列表檢視磁碟資訊

root@zstest:~# fdisk -l
Disk /dev/vda: 50 GiB, 53687091200 bytes, 104857600 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
Disklabel type: dos
Disk identifier: 0xd6804155

Device     Boot Start       End   Sectors Size Id Type
/dev/vda1  *     2048 104855551 104853504  50G 83 Linux


Disk /dev/vdb: 50 GiB, 53687091200 bytes, 104857600 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

5.3.對磁碟vdb進行分割槽

root@zstest:~# fdisk /dev/vdb

Welcome to fdisk (util-linux 2.27.1).
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.
Created a new DOS disklabel with disk identifier 0x63c3e6e0.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-104857599, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-104857599, default 104857599): 

Created a new partition 1 of type `Linux` and of size 50 GiB.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

5.4.檢視分割槽資訊

root@zstest:~# fdisk -l
Disk /dev/vda: 50 GiB, 53687091200 bytes, 104857600 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
Disklabel type: dos
Disk identifier: 0xd6804155

Device     Boot Start       End   Sectors Size Id Type
/dev/vda1  *     2048 104855551 104853504  50G 83 Linux


Disk /dev/vdb: 50 GiB, 53687091200 bytes, 104857600 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
Disklabel type: dos
Disk identifier: 0x63c3e6e0

Device     Boot Start       End   Sectors Size Id Type
/dev/vdb1        2048 104857599 104855552  50G 83 Linux

5.5.重新載入磁碟分割槽資訊

root@zstest:~# partprobe /dev/vdb

5.6.對指定磁碟分割槽進行格式化,使用ext4檔案系統

root@zstest:~# mkfs.ext4 /dev/vdb1
mke2fs 1.42.13 (17-May-2015)
Creating filesystem with 13106944 4k blocks and 3276800 inodes
Filesystem UUID: 047fb8b0-e6ed-42db-bf5c-2a6d388fc34d
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
        4096000, 7962624, 11239424

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done   
This filesystem will be automatically checked every 38 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

5.7.根據提示進行操作,取消磁碟自檢

root@zstest:~# tune2fs -c -1 /dev/vdb1 
tune2fs 1.42.13 (17-May-2015)
Setting maximal mount count to -1

5.8.掛載分割槽到指定目錄

mkdir -p /data
mount /dev/vdb1 /data

5.9.配置開機自動掛載分割槽

vim /etc/fstab 
-------------------------------------------------------
# made by tssc
/dev/vdb1               /data                   ext4    defaults        0 0
-------------------------------------------------------

 完畢,呵呵呵呵

 

相關文章