Linux 格式化和掛載數據盤

本文描述如何用一個新的數據盤建立一個單分區數據盤並掛載文件系統。本文僅適用於使用 fdisk 命令對一個不大於 2 TB 的數據盤執行分區操做。若是須要分區的數據盤大於 2 TB,請參考 32TB 塊存儲分區html

您還能夠根據業務須要,對數據盤進行多分區配置。建議使用系統自帶的工具進行分區操做。node

操做須知

單獨 購買的數據盤 須要先 掛載數據盤,而後才能格式化。ide

隨實例時一塊兒購買的數據盤,無需掛載,直接格式化。工具

您須要知道掛載到實例的數據盤的設備名:您能夠經過 ECS 管理控制檯 > 磁盤詳情 > 磁盤掛載信息 查到數據盤的 設備名。數據盤的設備名由系統默認分配,從 /dev/xvdb 開始日後順序排列,分佈範圍包括 /dev/xvdb/dev/xvdz優化

操做步驟

在本示例中,咱們用一個新的 20 GiB 數據盤(設備名爲 /dev/xvdb)建立一個單分區數據盤並掛載一個 ext3 文件系統。使用的實例是 I/O 優化實例,操做系統爲 CentOS 6.8。ui

  1. 遠程鏈接實例spa

  2. 運行 fdisk -l 命令查看實例是否有數據盤。若是執行命令後,沒有發現 /dev/vdb,表示您的實例沒有數據盤,無需格式化數據盤,請忽略本文後續內容。操作系統

    • 若是您的數據盤顯示的是 dev/xvd?,表示您使用的是非 I/O 優化實例。3d

    • 其中 ? 是 a−z 的任一個字母。code

  3. 建立一個單分區數據盤,依次執行如下命令:

    1. 運行 fdisk /dev/vdb:對數據盤進行分區。

    2. 輸入 n 並按回車鍵:建立一個新分區。

    3. 輸入 p 並按回車鍵:選擇主分區。由於建立的是一個單分區數據盤,因此只須要建立主分區。

      說明:若是要建立 4 個以上的分區,您應該建立至少一個擴展分區,即選擇 e

    4. 輸入分區編號並按回車鍵。由於這裏僅建立一個分區,能夠輸入 1。

    5. 輸入第一個可用的扇區編號:按回車鍵採用默認值 1。

    6. 輸入最後一個扇區編號:由於這裏僅建立一個分區,因此按回車鍵採用默認值。

    7. 輸入 wq 並按回車鍵,開始分區。

      [root@iXXXXXXX ~]# fdisk /dev/vdb
      Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
      Building a new DOS disklabel with disk identifier 0x5f46a8a2.
      Changes will remain in memory only, until you decide to write them.
      After that, of course, the previous content won't be recoverable.
      Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
      WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
        switch off the mode (command 'c') and change display units to
        sectors (command 'u').
      Command (m for help): n
      Command action
      e   extended
      p   primary partition (1-4)
      p
      Partition number (1-4): 1
      First cylinder (1-41610, default 1): 1
      Last cylinder, +cylinders or +size{K,M,G} (1-41610, default 41610):
      Using default value 41610
      Command (m for help): wq
      The partition table has been altered!
      Calling ioctl() to re-read partition table.
      Syncing disks.

       

  4. 查看新的分區:運行命令 fdisk -l。若是出現如下信息,說明已經成功建立了新分區 /dev/vdb1。

    [root@iXXXXXXX ~]# fdisk -l
    Disk /dev/vda: 42.9 GB, 42949672960 bytes
    255 heads, 63 sectors/track, 5221 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk identifier: 0x00053156
    Device Boot      Start         End      Blocks   Id  System
    /dev/vda1   *           1        5222    41942016   83  Linux
    Disk /dev/vdb: 21.5 GB, 21474836480 bytes
    16 heads, 63 sectors/track, 41610 cylinders
    Units = cylinders of 1008 * 512 = 516096 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk identifier: 0x5f46a8a2
    Device Boot      Start         End      Blocks   Id  System
    /dev/vdb1               1       41610    20971408+  83  Linux

     

  5. 在新分區上建立一個文件系統:運行命令 mkfs.ext3 /dev/vdb1

    • 本示例要建立一個 ext3 文件系統。您也能夠根據本身的須要,選擇建立其餘文件系統,例如,若是須要在 Linux、Windows 和 Mac 系統之間共享文件,您可使用 mkfs.vfat 建立 VFAT 文件系統。

    • 建立文件系統所需時間取決於數據盤大小。

      [root@iXXXXXXX ~]# mkfs.ext3 /dev/vdb1
      mke2fs 1.41.12 (17-May-2010)
      Filesystem label=
      OS type: Linux
      Block size=4096 (log=2)
      Fragment size=4096 (log=2)
      Stride=0 blocks, Stripe width=0 blocks
      1310720 inodes, 5242852 blocks
      262142 blocks (5.00%) reserved for the super user
      First data block=0
      Maximum filesystem blocks=4294967296
      160 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, 2654208,
       4096000
      Writing inode tables: done
      Creating journal (32768 blocks): done
      Writing superblocks and filesystem accounting information: done
      This filesystem will be automatically checked every 37 mounts or
      180 days, whichever comes first.  Use tune2fs -c or -i to override.

       

  6. (建議)備份 etc/fstab:運行命令 cp /etc/fstab /etc/fstab.bak

  7. 向 /etc/fstab 寫入新分區信息:運行命令 echo /dev/vdb1 /mnt ext3 defaults 0 0 >> /etc/fstab

    說明:Ubuntu 12.04 不支持 barrier,因此對該系統正確的命令是:echo '/dev/vdb1 /mnt ext3 barrier=0 0 0' >> /etc/fstab

    若是須要把數據盤單獨掛載到某個文件夾,好比單獨用來存放網頁,請將以上命令 /mnt 替換成所需的掛載點路徑。

  8. 查看 /etc/fstab 中的新分區信息:運行命令 cat /etc/fstab

    [root@iXXXXXXX ~]# cat /etc/fstab
     #
     # /etc/fstab
     # Created by anaconda on Thu Feb 23 07:28:22 2017
     #
     # 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=3d083579-f5d9-4df5-9347-8d27925805d4 /                       ext4    defaults        1 1
     tmpfs                   /dev/shm                tmpfs   defaults        0 0
     devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
     sysfs                   /sys                    sysfs   defaults        0 0
     proc                    /proc                   proc    defaults        0 0
     /dev/vdb1 /mnt ext3 defaults 0 0

     

  9. 掛載文件系統:運行命令 mount /dev/vdb1 /mnt

  10. 查看目前磁盤空間和使用狀況:運行命令 df -h。若是出現新建文件系統的信息,說明掛載成功,可使用新的文件系統了。

    掛載操做完成後,不須要重啓實例便可開始使用新的文件系統。

    [root@iXXXXXXX ~]# mount /dev/vdb1 /mnt
    [root@iXXXXXXX ~]# df -h
    Filesystem      Size  Used Avail Use% Mounted on
    /dev/vda1        40G  6.6G   31G  18% /
    tmpfs           499M     0  499M   0% /dev/shm
    /dev/vdb1        20G  173M   19G   1% /mnt

    本文來源:

    https://help.aliyun.com/document_detail/25426.html?spm=a2c4e.11153987.0.0.3eab779dm49zra

相關文章
相關標籤/搜索