華爲雲ECS-掛載Linux磁盤-Shell命令方式

前提條件

  • 華爲雲ECS雲服務器已掛載數據盤,且該盤或擴容部分未做過初始化處理。

一. 劃分分區並掛載磁盤

場景示例:雲服務器掛載了一塊新的數據盤,需要將該數據盤設爲主分區,文件系統設爲ext4格式,掛載在“/apps/soft”目錄下,並設置開機啓動自動掛載。

  1. 執行以下命令,查看新增數據盤。

    fdisk -l

    回顯類似如下信息,表示當前的雲服務器有兩塊磁盤,“/dev/vda”是系統盤,“/dev/vdb”是新增數據盤。

    [root@0001 apps]# fdisk -l
    
    Disk /dev/vda: 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: 0x000b623c
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/vda1   *        2048     2099199     1048576   83  Linux
    /dev/vda2         2099200    83886079    40893440   83  Linux
    
    Disk /dev/vdb: 1073.7 GB, 1073741824000 bytes, 2097152000 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
  2. 執行以下命令,進入fdisk模式,開始對新增數據盤執行分區操作。

    fdisk 新增數據盤

    以新掛載的數據盤“/dev/vdb”爲例:

    fdisk /dev/vdb

    回顯類似如下信息:

    [root@0001 apps]# fdisk /dev/vdb
    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 0x99d30fba.
    
    Command (m for help): 
  3. 輸入“n”,按“Enter”,開始新建分區。

    輸入“n”表示新增一個分區。

    回顯類似如下信息,表示磁盤有兩種分區類型:

    • “p”表示主要分區。
    • “e”表示延伸分區。
    Command (m for help): n
    Partition type:
       p   primary (0 primary, 0 extended, 4 free)
       e   extended
    Select (default p): 
  4. 以創建一個主要分區爲例,輸入“p”,按“Enter”,開始創建一個主分區。

    回顯類似如下信息,“Partition number”表示主分區編號,可以選擇1-4。

    Select (default p): p
    Partition number (1-4, default 1): 
  5. 以分區編號選擇“1”爲例,輸入主分區編號“1”,按“Enter”。

    回顯類似如下信息,“First sector”表示初始磁柱區域,可以選擇2048-20971519,默認爲2048。

    Partition number (1-4, default 1): 1
    First sector (2048-20971519, default 2048):
  6. 以選擇默認初始磁柱編號2048爲例,按“Enter”。

    回顯類似如下信息,“Last sector”表示截止磁柱區域,可以選擇2048-20971519,默認爲20971519。

    First sector (2048-2097151999, default 2048): 
    Using default value 2048
    Last sector, +sectors or +size{K,M,G} (2048-2097151999, default 2097151999): 
  7. 以選擇默認截止磁柱編號20971519爲例,按“Enter”。

    回顯類似如下信息,表示分區完成,即爲10GB的數據盤新建了1個分區。

    Last sector, +sectors or +size{K,M,G} (2048-2097151999, default 2097151999): 
    Using default value 2097151999
    Partition 1 of type Linux and of size 1000 GiB is set
    
    Command (m for help): 
  8. 輸入“p”,按“Enter”,查看新建分區的詳細信息。

    回顯類似如下信息,表示新建分區“/dev/vdb1的詳細信息。

    Command (m for help): p
    
    Disk /dev/vdb: 1073.7 GB, 1073741824000 bytes, 2097152000 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: 0x404beae3
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/vdb1            2048  2097151999  1048574976   83  Linux
    
    Command (m for help): 
  9. 輸入“w”,按“Enter”,將分區結果寫入分區表中。

    回顯類似如下信息,表示分區創建完成。

    Command (m for help): w
    The partition table has been altered!
    
    Calling ioctl() to re-read partition table.
    Syncing disks.
  10. 執行以下命令,將新的分區表同步到數據盤上。

    partprobe

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

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

    以設置文件系統爲“ext4”爲例:

    mkfs -t ext4 /dev/vdb1

    回顯類似如下信息:

    [root@0001 ~]# mkfs -t ext4 /dev/vdb1
    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
    65536000 inodes, 262143744 blocks
    13107187 blocks (5.00%) reserved for the super user
    First data block=0
    Maximum filesystem blocks=2409627648
    8000 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, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968, 
        102400000, 214990848
    
    Allocating group tables: done                            
    Writing inode tables: done                            
    Creating journal (32768 blocks): done
    Writing superblocks and filesystem accounting information: done     

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

  12. 執行以下命令,新建掛載路徑。

    mkdir 掛載路徑

    以新建掛載路徑“/apps/soft”爲例:

    mkdir /apps/soft

  13. 執行以下命令,將新建分區掛載到 Step 12中新建的路徑下。

    mount /dev/vdb1 掛載路徑

    以掛載新建分區至“/apps/soft”爲例:

    mount /dev/vdb1 /apps/soft

  14. 執行以下命令,查看掛載結果。

    df -TH

    回顯類似如下信息,表示新建分區“/dev/vdb1”已掛載至/apps/soft

    [root@0001 soft]# df -TH
    Filesystem     Type      Size  Used Avail Use% Mounted on
    devtmpfs       devtmpfs   34G     0   34G   0% /dev
    tmpfs          tmpfs      34G     0   34G   0% /dev/shm
    tmpfs          tmpfs      34G   19M   34G   1% /run
    tmpfs          tmpfs      34G     0   34G   0% /sys/fs/cgroup
    /dev/vda2      ext4       42G  5.1G   34G  14% /
    /dev/vda1      ext4      1.1G  180M  774M  19% /boot
    tmpfs          tmpfs     6.8G     0  6.8G   0% /run/user/0
    /dev/vdb1      ext4      1.1T   80M  1.1T   1% /apps/soft

二. 設置開機自動掛載磁盤

如果需要在華爲雲ECS系統啓動時自動掛載磁盤,不能採用在 /etc/fstab直接指定 /dev/vdb1的方法,因爲雲中設備的順序編碼在關閉或者開啓雲服務器過程中可能發生改變。推薦使用UUID來配置自動掛載數據盤。

 說明:

磁盤的UUID(universally unique identifier)是Linux系統爲存儲設備提供的唯一的標識字符串。

  1. 執行如下命令,查詢磁盤分區的UUID。

    blkid 磁盤分區

    以查詢磁盤分區“/dev/vdb1”的UUID爲例:

    blkid /dev/vdb1

    回顯類似如下信息,表示“/dev/xvdb1”的UUID。

    [root@0001 soft]# blkid /dev/vdb1
    /dev/vdb1: UUID="d751796e-f913-41eb-b8eb-98f7a1524784" TYPE="ext4"
  2. 執行以下命令,使用VI編輯器打開“fstab”文件。

    vim /etc/fstab

  3. 按“i”,進入編輯模式。
  4. 將光標移至文件末尾,按“Enter”,添加如下內容。
    UUID=d751796e-f913-41eb-b8eb-98f7a1524784 /apps/soft              ext4    defaults        0 2
  5. 按“ESC”後,輸入“:wq”,按“Enter”。

    保存設置並退出編輯器。

  6. 參數解釋

    UUID=d751796e-f913-41eb-b8eb-98f7a1524784 #要掛載的磁盤分區的UUID
    /apps/soft #掛載目錄
    ext4 #分區格式爲ext4
    defaults #掛載時所要設定的參數(只讀,讀寫,啓用quota等),輸入defaults包括的參數有(rw、dev、exec、auto、nouser、async)
    0 #使用dump是否要記錄,0爲不需要,1爲需要
    2 #2是開機時檢查的順序,boot系統文件爲1,其他文件系統都爲2,如不要檢查就爲0
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章