Linux VPS 新增硬碟筆記:fdisk 分割、Btrfs 格式化與 fstab 設定

黑五買了一台 GreenCloud (aff) Storage VPS!

一步步在 Linux (Ubuntu/Debian) 上使用 fdisk 進行硬碟分割、格式化為 Btrfs 檔案系統,並設定 /etc/fstab 讓機器重開機後也能自動掛載。這套流程適用於所有 KVM 架構的 VPS。

以下指令我是用 root 跑的,如果權限不夠要加 sudo

1. 先 lsblk 看是哪個 disk 需要 partition

> lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sda      8:0    0  368K  1 disk 
sr0     11:0    1 1024M  0 rom  
vda    254:0    0   20G  0 disk 
|-vda1 254:1    0    1M  0 part 
|-vda2 254:2    0  122M  0 part /boot/efi
`-vda3 254:3    0 19.9G  0 part /
vdb    254:16   0  500G  0 disk 

2. 用 fdisk 做 partition

> fdisk /dev/vdb
  1. Command (m for help): g
  2. Command (m for help): n
    如果只要一個 partition 就都用 default
  3. Command (m for help): w
    將目前儲存在記憶體中的磁碟分割表變更寫入到磁碟上,然後退出 fdisk。在 w 之前退出的話,不會有任何變動
Welcome to fdisk (util-linux 2.41).
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 (MBR) disklabel with disk identifier 0x0c3294a3.

Command (m for help): g

Created a new GPT disklabel (GUID: E5606734-1335-4696-9501-B9A12D5B780B).

Command (m for help): n
Partition number (1-128, default 1): 1
First sector (2048-1048575966, default 2048): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-1048575966, default 1048573951): 

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

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

3. 如果要用 btrfs,安裝 btrfs-progs

> apt install btrfs-progs

4. format 成 btrfs

這時候跑 lsblk 會長這樣:

NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sda      8:0    0  368K  1 disk 
sr0     11:0    1 1024M  0 rom  
vda    254:0    0   20G  0 disk 
|-vda1 254:1    0    1M  0 part 
|-vda2 254:2    0  122M  0 part /boot/efi
`-vda3 254:3    0 19.9G  0 part /
vdb    254:16   0  500G  0 disk 
`-vdb1 254:17   0  500G  0 part

跑 mkfs 進行 format

> mkfs.btrfs -f /dev/vdb1
btrfs-progs v6.14
See https://btrfs.readthedocs.io for more information.

Performing full device TRIM /dev/vdb1 (500.00GiB) ...
NOTE: several default settings have changed in version 5.15, please make sure
      this does not affect your deployments:
      - DUP for metadata (-m dup)
      - enabled no-holes (-O no-holes)
      - enabled free-space-tree (-R free-space-tree)

Label:              (null)
UUID:               518709cc-6e80-43bf-afc9-bd2bd9a9791a
Node size:          16384
Sector size:        4096	(CPU page size: 4096)
Filesystem size:    500.00GiB
Block group profiles:
  Data:             single            8.00MiB
  Metadata:         DUP               1.00GiB
  System:           DUP               8.00MiB
SSD detected:       no
Zoned device:       no
Features:           extref, skinny-metadata, no-holes, free-space-tree
Checksum:           crc32c
Number of devices:  1
Devices:
   ID        SIZE  PATH     
    1   500.00GiB  /dev/vdb1

5. mount 到某個 path

我是選 /mnt/backups

> mkdir -p /mnt/backups
> mount -o compress=zstd /dev/vdb1 /mnt/backups
> lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sda      8:0    0  368K  1 disk 
sr0     11:0    1 1024M  0 rom  
vda    254:0    0   20G  0 disk 
|-vda1 254:1    0    1M  0 part 
|-vda2 254:2    0  122M  0 part /boot/efi
`-vda3 254:3    0 19.9G  0 part /
vdb    254:16   0  500G  0 disk 
`-vdb1 254:17   0  500G  0 part /mnt/backups

為了讓平常用的非 root 帳號也能讀寫,就要 chown 成實際會使用的使用者名稱,以 username 是 small 為例

> chown -R small:small /mnt/backups

6. 編輯 fstab

到這裡為止,如果 reboot 的話,mount 會消失,所以要寫到 fstab 裡。

先找到 UUID

> blkid /dev/vdb1
/dev/vdb1: UUID="518709cc-6e80-43bf-afc9-bd2bd9a9791a" UUID_SUB="6593363f-aee9-493d-b912-49f84f9effcc" BLOCK_SIZE="4096" TYPE="btrfs" PARTUUID="15bcaff2-b553-4711-94cd-95f81a2c9093"

編輯 /etc/fstab

> nano /etc/fstab

在最後面加上

UUID=518709cc-6e80-43bf-afc9-bd2bd9a9791a  /mnt/backups  btrfs  defaults,compress=zstd  0  0
  • UUID=…: Identifies the disk accurately.
  • /mnt/backups: Where it should live.
  • btrfs: The filesystem type.
  • defaults,compress=zstd: The options.
  • 0 0: These last two numbers tell Linux not to “dump” the disk and not to scan it during boot (since Btrfs handles its own scanning).

7. 確認 fstab 的修改是對的

> mount -a

如果沒有任何東西跳出來就表示搞定了!

有跳出東西的話,例如 UUID 錯的話會有像這樣的錯誤訊息:

mount: /mnt/backups: can't find UUID=a1b2c3d4-1234-5678-9abc-def123456789.

8. 如果要跑 YABS

如果要跑 YABS 的話就 cd/mnt/backups 跑 YABS 就可以測這個 disk 了。

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *