ubuntu 18.04 增加新磁盤、掛載、格式化

20200118 在阿里雲 ECS linux 上嘗試雲盤卸載、掛載操作,先在本地 virtualbox 上嘗試和學習

1、停止虛擬機,設置–存儲–增加虛擬硬盤

** 注意:要選 控制器 SATA

選擇 創建新的虛擬盤(使用現有磁盤的話,後續就不用格式化、掛載了)
在這裏插入圖片描述
** 因爲是學習,所以,全部使用缺省,一路回車即可

2、掛載新硬盤

  1. 查看文件系統 (file system)

    df -h

     Filesystem      Size  Used Avail Use% Mounted on
     udev            1.9G     0  1.9G   0% /dev
     tmpfs           395M 1008K  394M   1% /run
     /dev/sda2        98G   11G   83G  11% /
     tmpfs           2.0G     0  2.0G   0% /dev/shm
     tmpfs           5.0M     0  5.0M   0% /run/lock
     tmpfs           2.0G     0  2.0G   0% /sys/fs/cgroup
     /dev/loop0       91M   91M     0 100% /snap/core/6405
     /dev/loop1       90M   90M     0 100% /snap/core/6673
     /dev/loop2       92M   92M     0 100% /snap/core/6531
     Downloads       234G  171G   63G  74% /media/sf_Downloads
     tmpfs           395M     0  395M   0% /run/user/1000
    

    沒有看到我們新建的硬盤,看不到分區信息

  2. 查看塊設備 (block device)

    lsblk -f

     NAME   FSTYPE   LABEL           UUID                                 MOUNTPOINT
     loop0  squashfs                                                      /snap/core/6405
     loop1  squashfs                                                      /snap/core/6673
     loop2  squashfs                                                      /snap/core/6531
     sda                                                                  
     ├─sda1                                                               
     └─sda2 ext4                     c9db3d96-f9f2-11e8-b72e-0800274e1103 /
     sdb                                                                  
     sr0    iso9660  VBox_GAs_5.2.22 2018-11-08-19-58-33-33 
    

    最後部分 sdb 就是剛剛新增加的硬盤,可以看到 FSTYPE 和 UUID、MOUNTPOINT 都是空白,看不到分區信息

  3. 查看分區信息

    $ sudo fdisk -l

     Disk /dev/loop0: 91 MiB, 95416320 bytes, 186360 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 /dev/loop1: 89.3 MiB, 93581312 bytes, 182776 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 /dev/loop2: 91.1 MiB, 95522816 bytes, 186568 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 /dev/sda: 100 GiB, 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
     Disklabel type: gpt
     Disk identifier: 82F6786F-CA1E-4E0B-9D35-560A90F23622
     
     Device     Start       End   Sectors  Size Type
     /dev/sda1   2048      4095      2048    1M BIOS boot
     /dev/sda2   4096 209713151 209709056  100G Linux filesystem
     
     
     Disk /dev/sdb: 10 GiB, 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 /dev/sdb: 10 GiB, 10737418240 bytes, 20971520 sectors
    就是剛剛新增加的硬盤,沒有Start End Sectors Size Type 信息
    也就是分區信息是空白

  4. 給硬盤 /dev/sdb 創建新分區
    $ sudo fdisk /dev/sdb

     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 0xd97b7fdf.
     
     Command (m for help): 
    

m 表示命令行提示,每次輸入 m 都會彈出命令行列表

Help:

  DOS (MBR)
   a   toggle a bootable flag
   b   edit nested BSD disklabel
   c   toggle the dos compatibility flag

  Generic
   d   delete a partition
   F   list free unpartitioned space
   l   list known partition types
   n   add a new partition
   p   print the partition table
   t   change a partition type
   v   verify the partition table
   i   print information about a partition

  Misc
   m   print this menu
   u   change display/entry units
   x   extra functionality (experts only)

  Script
   I   load disk layout from sfdisk script file
   O   dump disk layout to sfdisk script file

  Save & Exit
   w   write table to disk and exit
   q   quit without saving changes

  Create a new label
   g   create a new empty GPT partition table
   G   create a new empty SGI (IRIX) partition table
   o   create a new empty DOS partition table
   s   create a new empty Sun partition table


Command (m for help): 

其中, 輸入 n add a new partition (新建一個分區)

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): 

分區類型直接回車,缺省即可

Using default response p.
Partition number (1-4, default 1): 

分區計數,缺省從 1 開始(前一次理解成:個數,錯了!)

Using default response p.
Partition number (1-4, default 1): 
First sector (2048-20971519, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-20971519, default 20971519): 

Created a new partition 1 of type 'Linux' and of size 10 GiB.

Command (m for help): 

起始扇區和結束扇區 (sector)也缺省
最後,又來到了 Command (m for help):

Save & Exit

   w   write table to disk and exit
   q   quit without saving changes

輸入 w 寫入磁盤分區表,生效!

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
  1. 查看分區結果,確認!

$ df -h

和剛纔一樣,沒有出現 sdb1

分別查詢一下文件系統

$ sudo file -s /dev/sda1
/dev/sda1: data

$ sudo file -s /dev/sda2
/dev/sda2: Linux rev 1.0 ext4 filesystem data, UUID=c9db3d96-f9f2-11e8-b72e-0800274e1103 (needs journal recovery) (extents) (64bit) (large files) (huge files)

$ sudo file -s /dev/sdb1
/dev/sdb1: data

可見,sdb1 和 sda1 是一樣的,沒有格式化成爲 Linux 文件系統

$ lsblk -f

NAME   FSTYPE   LABEL           UUID                                 MOUNTPOINT
loop0  squashfs                                                      /snap/core/6405
loop1  squashfs                                                      /snap/core/6673
loop2  squashfs                                                      /snap/core/6531
sda                                                                  
├─sda1                                                               
└─sda2 ext4                     c9db3d96-f9f2-11e8-b72e-0800274e1103 /
sdb                                                                  
└─sdb1                                                               
sr0    iso9660  VBox_GAs_5.2.22 2018-11-08-19-58-33-33 

可以看到 , sdb1 的 FSTYPE 還是空白,sda2 的格式是 ext4

$ sudo fdisk -l

...

Disk /dev/sdb: 10 GiB, 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
Disklabel type: dos
Disk identifier: 0xd97b7fdf

Device     Boot Start      End  Sectors Size Id Type
/dev/sdb1        2048 20971519 20969472  10G 83 Linux

這次最後部分可以看到 /dev/sdb1 的分區信息 2048 20971519 20969472 10G 83 Linux
但是沒有文件系統信息

3、格式化新硬盤

  1. 格式化新硬盤/dev/sdb1 ,格式: ext4

    $ sudo mkfs -t ext4 /dev/sdb1

     mke2fs 1.44.1 (24-Mar-2018)
     Creating filesystem with 2621184 4k blocks and 655360 inodes
     Filesystem UUID: b3a3cec3-93bb-4d8d-95cf-cc9aadd9f4a6
     Superblock backups stored on blocks: 
     	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
     
     Allocating group tables: done                            
     Writing inode tables: done                            
     Creating journal (16384 blocks): done
     Writing superblocks and filesystem accounting information: done 
    
  2. df -h 還是看不到 sdb1 的文件系統格式,因爲該硬盤還沒有被掛載

  3. 查看硬盤 /dev/sdb1 的文件系統

    $ sudo file -s /dev/sdb1

     /dev/sdb1: Linux rev 1.0 ext4 filesystem data, UUID=b3a3cec3-93bb-4d8d-95cf-cc9aadd9f4a6 (extents) (64bit) (large files) (huge files)
    
  4. 查看塊設備信息
    $ lsblk -f

     NAME   FSTYPE   LABEL           UUID                                 MOUNTPOINT
     loop0  squashfs                                                      /snap/core/6405
     loop1  squashfs                                                      /snap/core/6673
     loop2  squashfs                                                      /snap/core/6531
     sda                                                                  
     ├─sda1                                                               
     └─sda2 ext4                     c9db3d96-f9f2-11e8-b72e-0800274e1103 /
     sdb                                                                  
     └─sdb1 ext4                     b3a3cec3-93bb-4d8d-95cf-cc9aadd9f4a6 
     sr0    iso9660  VBox_GAs_5.2.22 2018-11-08-19-58-33-33  
    

可以看到新硬盤已經被正確的格式化成爲 Linux 系統格式 (ext4)了

4、 掛載新硬盤 sdb1

  1. 建立掛載點 (掛載目錄)
    在根目錄上創建一個新的掛載目錄/data02
    sudo mkdir /data02

  2. 將新磁盤分區掛載到/data02目錄下
    sudo mount /dev/sdb1 /data02

  3. 檢查文件系統
    $ df -l

     Filesystem     1K-blocks      Used Available Use% Mounted on
     udev             1988768         0   1988768   0% /dev
     tmpfs             403972      1016    402956   1% /run
     /dev/sda2      102684600  10562244  86863248  11% /
     tmpfs            2019848         0   2019848   0% /dev/shm
     tmpfs               5120         0      5120   0% /run/lock
     tmpfs            2019848         0   2019848   0% /sys/fs/cgroup
     /dev/loop0         93184     93184         0 100% /snap/core/6405
     /dev/loop1         91392     91392         0 100% /snap/core/6673
     /dev/loop2         93312     93312         0 100% /snap/core/6531
     Downloads      244912536 178979984  65932552  74% /media/sf_Downloads
     tmpfs             403968         0    403968   0% /run/user/1000
     /dev/sdb1       10254612     36888   9677104   1% /data02
    

可以看到,/dev/sdb1 已經存在於當前文件系統了

  1. 測試實際使用
    $ cd /data02
    $ ls
    lost+found
    $ sudo touch test20200119.txt
    $ sudo vim test20200119.txt
    輸入一些內容,
    $ cat test20200119.txt
    確認是可以使用的!

  2. 重啓試試
    目前雖然已經掛載了新硬盤 sdb1,但是,並沒有設置爲開機掛載,所以,重啓之後,應該是看不到該硬盤的
    $ sudo reboot

5、設置爲開機掛載

** 接上:沒有設置,直接重啓之後, df -h 是看不到 sdb1 的

** 不同的系統文件內容可能會有些差別,我這裏是 Ubuntu 18.04

  1. 查詢 sdb1 的 UUID ,後續需要使用

    $ blkid

     /dev/sr0: UUID="2018-11-08-19-58-33-33" LABEL="VBox_GAs_5.2.22" TYPE="iso9660"
     /dev/sda2: UUID="c9db3d96-f9f2-11e8-b72e-0800274e1103" TYPE="ext4" PARTUUID="e4d00f7b-dcc8-42a4-b9cf-1bacf911c6ce"
     /dev/sdb1: UUID="b3a3cec3-93bb-4d8d-95cf-cc9aadd9f4a6" TYPE="ext4" PARTUUID="d97b7fdf-01"
    

    或者,
    $ cd /dev/disk
    $ ls

    by-id by-label by-partuuid by-path by-uuid

    $ ls by-uuid/ -l

     total 0
     lrwxrwxrwx 1 root root  9 Jan 19 15:18 2018-11-08-19-58-33-33 -> ../../sr0
     lrwxrwxrwx 1 root root 10 Jan 19 15:19 b3a3cec3-93bb-4d8d-95cf-cc9aadd9f4a6 -> ../../sdb1
     lrwxrwxrwx 1 root root 10 Jan 19 15:19 c9db3d96-f9f2-11e8-b72e-0800274e1103 -> ../../sda2
    
  2. 增加配置
    sudo vim /etc/fstab

     UUID=c9db3d96-f9f2-11e8-b72e-0800274e1103 / ext4 defaults 0 0
     /swap.img       none    swap    sw      0       0
     # by wzh 20190102 restart error!
     # 192.168.1.191:/home /common/home
     
     # 192.168.1.191:/var/nfs/general /common/general
    
     192.168.1.191:/home /common/home nfs defaults 0 0
     192.168.1.191:/var/nfs/general /common/general nfs defaults 0 0
    

    後面部分是從前設置共享磁盤留下的,和本次無關

其中總共 6 個字段參數,具體意義我也沒有太明白

照着第一行照貓畫虎,增加一行
UUID=b3a3cec3-93bb-4d8d-95cf-cc9aadd9f4a6 /data02 ext4 defaults 0 0

完成之後,內容如下

UUID=c9db3d96-f9f2-11e8-b72e-0800274e1103 / ext4 defaults 0 0
# add new HDD by wzh 20200119
UUID=b3a3cec3-93bb-4d8d-95cf-cc9aadd9f4a6 /data02 ext4 defaults 0 0

/swap.img       none    swap    sw      0       0
# by wzh 20190102 restart error!
# 192.168.1.191:/home /common/home

# 192.168.1.191:/var/nfs/general /common/general

# del by wzh 20200119 ,do not mount common
# 192.168.1.191:/home /common/home nfs defaults 0 0
# 192.168.1.191:/var/nfs/general /common/general nfs defaults 0 0
  1. sudo mount -a 生效
    因爲實驗用的虛擬機剛纔有以前的共享目錄設置,所以, mount -a 一直不能結束,註釋掉之後,ok!

  2. 重啓之後驗證

    $ df -l

     Filesystem     1K-blocks      Used Available Use% Mounted on
     udev             1988768         0   1988768   0% /dev
     tmpfs             403972      1012    402960   1% /run
     /dev/sda2      102684600  10553136  86872356  11% /
     tmpfs            2019848         0   2019848   0% /dev/shm
     tmpfs               5120         0      5120   0% /run/lock
     tmpfs            2019848         0   2019848   0% /sys/fs/cgroup
     /dev/loop0         93184     93184         0 100% /snap/core/6405
     /dev/loop2         91392     91392         0 100% /snap/core/6673
     /dev/loop1         93312     93312         0 100% /snap/core/6531
     /dev/sdb1       10254612     36896   9677096   1% /data02
     Downloads      244912536 179000660  65911876  74% /media/sf_Downloads
     tmpfs             403968         0    403968   0% /run/user/1000
    

其中新硬盤信息:
/dev/sdb1 10254612 36896 9677096 1% /data02

查看剛纔測試的文件

$ cd /data02

dhbm@ubuntu1804192:/data02$ ls
lost+found  test  test20200119.txt

6、 總結

Linux 掛載新硬盤的路線

  1. 掛接新硬盤
  2. 查看分區,如果沒有或者不支持,則創建新分區
  3. 格式化分區
  4. 創建掛載點(目錄)
  5. 掛載設備到掛載點
  6. 如果需要開機掛載,修改 /etc/fstab 配置

7、後記

  1. 20200120 上阿里雲 ECS 查看以前的雲盤分區信息,發現 磁盤標籤類型(Disklabel typ)都是 dos
    我自己的 virtualbox 裏面 Disk /dev/sda 的類型是 gpt
    大於 2 T 磁盤的時候再說了!

  2. 再次追加一個磁盤,分區的時候做成了 extend ,顯示 fdisk -l 中是 sdc 下的 sdc1 ,然後
    格式化的時候, 故意 sudo mkfs -t ext4 /dev/sdc 漏掉 sdc1,直接格式化 sdc ,回頭在看的時候,發現 sdc1 沒有了

  3. 重新分區sdc (sudo fdisk /dev/sdc),這次故意製作了一個小的分區,並且
    分區計數,缺省從 1 開始(前一次理解成:個數,錯了!)
    Using default response p.
    Partition number (1-4, default 1):
    輸入 4
    ,然後,完成之後,繼續 n 新建分區,這次提示的是邏輯分區
    Command (m for help): n
    Partition type
    p primary (0 primary, 1 extended, 3 free)
    l logical (numbered from 5)
    Select (default p): l

Adding logical partition 5
First sector (4096-12048, default 4096):
Last sector, +sectors or +size{K,M,G,T,P} (4096-12048, default 12048):

Created a new partition 5 of type ‘Linux’ and of size 3.9 MiB.

以上完成之後,是這樣子的
Device Boot Start End Sectors Size Id Type
/dev/sdc4 2048 12048 10001 4.9M 5 Extended
/dev/sdc5 4096 12048 7953 3.9M 83 Linux

  1. 說明富餘的地方還沒有分區,再次 sudo fdisk /dev/sdc
    這次全部按照缺省,不是創建的 extend,也不是 logical 邏輯分區,直接分區剩餘部分,最後結果是這樣子的
    Device Boot Start End Sectors Size Id Type
    /dev/sdc1 12288 20971519 20959232 10G 83 Linux
    /dev/sdc4 2048 12048 10001 4.9M 5 Extended
    /dev/sdc5 4096 12048 7953 3.9M 83 Linux

sdc1 是最後這次操作的結果

  1. linux 上的分區和早先 dos 操作很想,所以,也很複雜!所以,一般都是簡單的處理:附加的磁盤整個就是一個 primary !
發佈了69 篇原創文章 · 獲贊 11 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章