初始化Linux數據盤(fdisk)


操做場景node

本文以雲服務器的操做系統爲「CentOS 7.0 64位」爲例,採用fdisk分區工具爲數據盤設置分區。linux

MBR支持的磁盤最大容量爲2 TB,GPT最大支持的磁盤容量爲18 EB,所以當您初始化容量大於2 TB的磁盤時,分區形式請採用GPT。對於Linux操做系統而言,當磁盤分區形式選用GPT時,fdisk分區工具將沒法使用,須要採用parted工具。關於磁盤分區形式的更多介紹,請參見場景及磁盤分區形式介紹。服務器

不一樣雲服務器的操做系統的格式化操做可能不一樣,本文僅供參考,具體操做步驟和差別請參考對應的雲服務器操做系統的產品文檔。
注意:編輯器

首次使用雲硬盤時,若是您未參考本章節對雲硬盤執行初始化操做,主要包括建立分區和文件系統等操做,那麼當後續擴容雲硬盤時,新增容量部分的磁盤可能沒法正常使用。
前提條件ide

    已登陸雲服務器。
        彈性雲服務器請參見《彈性雲服務器用戶指南》。
        裸金屬服務器請參見《裸金屬服務器用戶指南》。
    已掛載數據盤至雲服務器,且該數據盤未初始化。工具

劃分分區並掛載磁盤ui

本操做以該場景爲例,當雲服務器掛載了一塊新的數據盤時,使用fdisk分區工具將該數據盤設爲主分區,分區形式默認設置爲MBR,文件系統設爲ext4格式,掛載在「/mnt/sdc」下,並設置開機啓動自動掛載。編碼

    執行如下命令,查看新增數據盤。操作系統

    fdisk -lorm

    回顯相似以下信息:

    [root@ecs-b656 test]# fdisk -l

    Disk /dev/xvda: 42.9 GB, 42949672960 bytes, 83886080 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: 0x000cc4ad

       Device Boot      Start         End      Blocks   Id  System
    /dev/xvda1   *        2048     2050047     1024000   83  Linux
    /dev/xvda2         2050048    22530047    10240000   83  Linux
    /dev/xvda3        22530048    24578047     1024000   83  Linux
    /dev/xvda4        24578048    83886079    29654016    5  Extended
    /dev/xvda5        24580096    26628095     1024000   82  Linux swap / Solaris

    Disk /dev/xvdb: 10.7 GB, 10737418240 bytes, 20971520 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

表示當前的雲服務器有兩塊磁盤,「/dev/xvda」是系統盤,「/dev/xvdb」是新增數據盤。

執行如下命令,進入fdisk分區工具,開始對新增數據盤執行分區操做。

fdisk 新增數據盤

以新掛載的數據盤「/dev/xvdb」爲例:

fdisk /dev/xvdb

回顯相似以下信息:

[root@ecs-b656 test]# fdisk /dev/xvdb
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 0xb00005bd.
Command (m for help):

輸入「n」,按「Enter」,開始新建分區。

回顯相似以下信息:

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended

表示磁盤有兩種分區類型:

    「p」表示主要分區。
    「e」表示延伸分區。

以建立一個主要分區爲例,輸入「p」,按「Enter」,開始建立一個主分區。

回顯相似以下信息:

Select (default p): p
Partition number (1-4, default 1):

「Partition number」表示主分區編號,能夠選擇1-4。

以分區編號選擇「1」爲例,輸入主分區編號「1」,按「Enter」。

回顯相似以下信息:

Partition number (1-4, default 1): 1
First sector (2048-20971519, default 2048):

「First sector」表示初始磁柱區域,能夠選擇2048-20971519,默認爲2048。

以選擇默認初始磁柱編號2048爲例,按「Enter」。

回顯相似以下信息:

First sector (2048-20971519, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519):

「Last sector」表示截止磁柱區域,能夠選擇2048-20971519,默認爲20971519。

以選擇默認截止磁柱編號20971519爲例,按「Enter」。

回顯相似以下信息:

Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519):
Using default value 20971519
Partition 1 of type Linux and of size 10 GiB is set
Command (m for help):

表示分區完成,即爲10GB的數據盤新建了1個分區。

輸入「p」,按「Enter」,查看新建分區的詳細信息。

回顯相似以下信息:

Command (m for help): p

Disk /dev/xvdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0xb00005bd

   Device Boot      Start         End      Blocks   Id  System
/dev/xvdb1            2048    20971519    10484736   83  Linux

Command (m for help):

表示新建分區「/dev/xvdb1」的詳細信息。

輸入「w」,按「Enter」,將分區結果寫入分區表中。

回顯相似以下信息:。

Command (m for help): w
The partition table has been altered!

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

表示分區建立完成。
說明:

若是以前分區操做有誤,請輸入「q」,則會退出fdisk分區工具,以前的分區結果將不會被保留。

執行如下命令,將新的分區表變動同步至操做系統。

partprobe

執行如下命令,將新建分區文件系統設爲系統所需格式。

mkfs -t 文件系統格式 /dev/xvdb1

以設置文件系統爲「ext4」爲例:

mkfs -t ext4 /dev/xvdb1

回顯相似以下信息:

[root@ecs-b656 test]# mkfs -t ext4 /dev/xvdb1
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, 2621184 blocks
131059 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

格式化須要等待一段時間,請觀察系統運行狀態,不要退出。
注意:

不一樣文件系統支持的分區大小不一樣,請根據您的業務需求選擇合適的文件系統。

執行如下命令,新建掛載點。

mkdir 掛載點

以新建掛載點「/mnt/sdc」爲例:

mkdir /mnt/sdc

執行如下命令,將新建分區掛載到12中新建的掛載點下。

mount /dev/xvdb1 掛載點

以掛載新建分區至「/mnt/sdc」爲例:

mount /dev/xvdb1 /mnt/sdc

執行如下命令,查看掛載結果。

df -TH

回顯相似以下信息:

[root@ecs-b656 test]# df -TH
Filesystem     Type      Size  Used Avail Use% Mounted on
/dev/xvda2     xfs        11G  7.4G  3.2G  71% /
devtmpfs       devtmpfs  4.1G     0  4.1G   0% /dev
tmpfs          tmpfs     4.1G   82k  4.1G   1% /dev/shm
tmpfs          tmpfs     4.1G  9.2M  4.1G   1% /run
tmpfs          tmpfs     4.1G     0  4.1G   0% /sys/fs/cgroup
/dev/xvda3     xfs       1.1G   39M  1.1G   4% /home
/dev/xvda1     xfs       1.1G  131M  915M  13% /boot
/dev/xvdb1     ext4       11G   38M  9.9G   1% /mnt/sdc

    表示新建分區「/dev/xvdb1」已掛載至「/mnt/sdc」。

設置開機自動掛載磁盤

若是您須要在雲服務器系統啓動時自動掛載磁盤,不能採用在 /etc/fstab直接指定 /dev/xvdb1的方法,由於雲中設備的順序編碼在關閉或者開啓雲服務器過程當中可能發生改變,例如/dev/xvdb1可能會變成/dev/xvdb2。推薦使用UUID來配置自動掛載數據盤。
說明:

磁盤的UUID(universally unique identifier)是Linux系統爲磁盤分區提供的惟一的標識字符串。

    執行以下命令,查詢磁盤分區的UUID。

    blkid 磁盤分區

    以查詢磁盤分區「/dev/xvdb1」的UUID爲例:

    blkid /dev/xvdb1

    回顯相似以下信息:

    [root@ecs-b656 test]# blkid /dev/xvdb1
    /dev/xvdb1: UUID="1851e23f-1c57-40ab-86bb-5fc5fc606ffa" TYPE="ext4"

表示「/dev/xvdb1」的UUID。

執行如下命令,使用VI編輯器打開「fstab」文件。

vi /etc/fstab

按「i」,進入編輯模式。
將光標移至文件末尾,按「Enter」,添加以下內容。

UUID=1851e23f-1c57-40ab-86bb-5fc5fc606ffa /mnt/sdc      ext4 defaults     0   2

按「ESC」後,輸入「:wq」,按「Enter」。

保存設置並退出編輯器。

相關文章
相關標籤/搜索