Skip to contentMount File Systems
- 檔案系統被掛載在目錄上
- Mount file system:
mount [-t <fstype>] [-o <options>] <device> <mount_point>- Example:
mount -t ext3 /dev/sda3 /usr
- Unmount file system:
umount - Examples:
umount /usrumount /dev/sda3
- Default mount point for removable media:
/run/media/<user>/<media-label>/
- Display UUIDs of block devices:
blkid
在 Windows 系統中,磁碟機透過字母代表不同的磁碟分區。
Linux 系統則不使用字母來指定分區,而是將分區掛載到檔案系統中的目錄上,這些目錄也稱為 mount point (掛載點)。
例如,在 Linux 系統中添加一個新的硬碟,需要先進行分區和格式化,然後使用檔案系統中的目錄(例如 /data/)並使用 mount 指令將該硬碟掛載到該目錄上。
注意:你也可以掛載通過網路檔案系統 (NFS) 共享的遠端檔案系統到你的檔案系統中自行建立的目錄上。
/mnt/ 目錄被預設用來臨時掛載本地和遠端的檔案系統。
所有 removable devices 的預設掛載點為 /rur/media/<user>/<media-label>/。
Example of the blkid command:
螢幕輸出 :
/etc/fstab
在 Linux 系統中,檔案系統和它們的掛載點(mount points)是在 /etc/fstab 檔案中。每一行都有六個欄位,用於掛載每個檔案系統,這些欄位分別提供以下掛載檔案系統的資訊:
- 第一個欄位:列出裝置檔案的名稱、檔案系統標籤或 UUID。使用
LABEL=<label> 或 UUID=<uuid> 可以確保即使使用的裝置檔案變更(例如更換 IDE 控制器上的硬碟),該分割區也能正確掛載。 - 第二個欄位:列出掛載點,即該檔案系統應該掛載在的目錄。此處指定的目錄必須已經存在。您可以通過切換到相應的目錄訪問媒體內容。
- 第三個欄位:列出檔案系統類型(例如 ext3、XFS)。
- 第四個欄位:顯示掛載選項。多個掛載選項用逗號分隔(例如 noauto、user、sync)。
- 第五個欄位:指示是否使用備份程式(dump utility)進行檔案系統備份。0 表示不備份。
- 第六個欄位:指示開機時使用 fsck 程式檢查檔案系統的順序:
- 0:不檢查的檔案系統
- 1:根目錄
- 2:所有其他可修改的檔案系統(不同驅動器上的檔案系統並行檢查)
雖然 /etc/fstab 列出了開機時需要掛載的檔案系統及其掛載點,但它不包含任何在開機後掛載的檔案系統的資訊。
/etc/mtab 檔案列出了當前已掛載的檔案系統及其掛載點。mount 和 umount 指令會影響已掛載檔案系統的狀態並修改 /etc/mtab 檔案。
Using mount, you can override the default settings in /etc/fstab.
Mount Options for mount and /etc/fstab
remount,File system can be mounted more than oncerw, ro, File system is writeable or read-onlysync, async,Synchronous(同步) or asynchronous(不同步) input and outputatime, noatime,Access time of a file is updated or notnodev, dev,Device files from being interpreted as such or notnoexec, exec,Prohibit the execution of programs or notnosuid, suid, Ignore SUID and SGID bits or notauto, noauto,Mount file system automatically or notuser, nouser, Allow user to mount file system or notdefaults, rw, suid, dev, exec, auto, nouser, and async
The noauto and user options are usually combined for removable media such as removable medias.
Currently Mounted File Systems
/etc/mtab 列出了當前掛載的檔案系統和它們的掛載點。
在某些情況下,用 mount 指令無法正確顯示已掛載的檔案系統。
核心還保留了/proc/mounts 的信息,它列出了所有當前 mounted partitions (掛載的分區)。
如果 /proc/mounts 與 /etc/mtab 的資訊有衝突,則 /proc/mounts 的資訊總是比 /etc/mtab 更正確和最新。
這種情況可能發生在安裝作業系統到 USB 隨身碟時,若沒有內部硬碟連接,或是重新啟動時未正確掛載 USB 隨身碟等。這些情況可能會讓核心的列舉過程出錯,導致 /etc/mtab 和 /proc/mounts 的資訊不同步。
因此,如果有疑問,就要始終相信 /proc/* 檔案反映的是核心實際看到的東西。/proc/mounts 列出了核心實際掛載的檔案系統,也就是核心真正進行掛載的資訊。
File System Health Tools
df,Reports file system disk space usage(使用狀況)du,Estimate(估計) file space usagelsof,Lists open filesfuser,Displays the PIDs of processes using the specified filesfsck, e2fsck, xfs_check,Check and optionally(可選擇的) repair Linux file systemstune2fs,Adjust tunable(可調整的) file system parameters(參數) on ext2/ext3/ext4 file systemsresize2fs,ext2/ext3/ext4 file systems
df 命令
提供硬碟和它們的分區或其他磁碟在檔案系統中掛載的資訊,以及它們佔用的空間大小。
如果使用不帶參數的 df 命令,會顯示目前所有已掛載檔案系統上的可用空間。
如果提供檔案名稱,df 會顯示此檔案所在檔案系統上的可用空間。
一些有用的選項包括 :
-h,以 MB 或 GB 顯示人類可讀格式-i,顯示 inode 資訊而非區塊使用情況-l,限制僅列出本機檔案系統- 例如,要以人類可讀格式列出所有本機檔案系統的資訊,可以輸入
df -lh。
du 命令
提供檔案和目錄佔用的空間資訊。
一些有用的選項包括
-c,顯示總計-h,以人類可讀格式顯示-s,僅顯示每個引數的總計--exclude=<pattern>,排除符合模式的檔案- 例如,要以人類可讀格式顯示檔案資訊,但排除以
.o 結尾的檔案,可以輸入以下指令:du -h --exclude='*.o'
lsof 命令
列出開啟的檔案。
如果不使用任何選項輸入 lsof,會列出所有屬於所有活動程序的開啟檔案。
開啟的檔案可以是常規檔案、目錄、設備檔案、庫、串流或網路檔案(Internet socket、NFS 檔案或 UNIX domain socket)。
lsof 命令可以列出當前正在開啟的檔案資訊,也可以使用 -r 選項以重複模式執行,重複顯示輸出結果直到接收到中斷或結束訊號。
一些常用的選項包括
-c <character>,僅列出檔案名稱以指定字元開頭的檔案-s,顯示檔案大小-u <user1>,<user2>,僅列出指定使用者所開啟的檔案- 例如,要列出 root 和 geeko 使用中的開啟檔案及其檔案大小,您可以輸入
lsof -s -u root,geeko。
fuser 命令
顯示使用指定檔案或檔案系統的程序 ID。在預設顯示模式下,每個檔案名稱後面跟一個字母,該字母描述訪問方式的類型:
c:當前目錄e:正在運行的可執行檔f:已開啟檔案(在預設顯示模式下省略)r:根目錄m:映射到內存的檔案或共享庫
如果沒有存取指定的檔案,或者發生致命錯誤,則顯示非零返回代碼。如果找到至少一個存取,fuser 則返回零。
一些有用的選項 :
-a,可以返回所有檔案的信息,即使它們沒有被任何程序訪問-v,則是詳細模式,可以顯示更多詳細信息-u,會在每個 PID 後面附加程序所有者的用戶名。-m,可以檢查訪問 /home 分區上檔案的程序的 PID 信息。
fsck 命令
fsck 是一個用來檢查和修復 Linux 檔案系統的指令。
BtrFS 的進階功能需要特定的 BtrFS 工具才能處理。
通常情況下,fsck 會在不同的實體磁碟驅動器上並行運行檔案系統,以減少檢查所有檔案系統所需的總時間。
如果您沒有在命令行上指定檔案系統並且沒有指定 -A 選項,fsck 將按照 /etc/fstab 中的順序順序檢查檔案系統。
fsck 是系統上可用的各種檔案系統檢查器(fsck.<fstype>)的前端。
fsck 工具首先在 /sbin/ 中查找 system-specific checker (系統特定的檢查器),然後在 /etc/fs/ 和 /etc/ 中查找,最後在列在 PATH 環境變數中的目錄中查找。
要檢查特定的檔案系統,請使用以下語法:fsck <device>。
舉例來說,如果您想要檢查 /dev/sda2 這個檔案系統,您可以輸入 fsck /dev/sda2。
fsck 提供了一些選項,例如
-A,透過 /etc/fstab 檔案並嘗試一次性檢查所有檔案系統-N,僅顯示會執行什麼,而不執行-V,詳細輸出
在 Linux 系統中斷電或沒有將分割區卸載的情況下關閉電腦,可能會導致檔案系統出現錯誤。
下次開機時,會偵測到電腦未正確關閉,並進行檔案系統檢查。如果檔案系統中發現錯誤,會嘗試進行修復。如果無法修復,電腦將無法正常啟動,您需要輸入 root 密碼並提供有關如何解決問題的提示。在檔案系統嚴重損壞的情況下,您甚至可能需要使用救援系統來修復系統。
根據檔案系統的類型,您可以使用相應的工具(例如 /sbin/e2fsck、/sbin/reiserfsck 或 /sbin/xfs_check)。這些工具檢查檔案系統的 correct superblock (正確超級塊[位於包含檔案系統結構資訊的分割區開頭的區塊])、有問題的資料區塊或資料區塊配置錯誤。
在 ext2(或 ext3/4)檔案系統中可能的問題是 superblock (超級塊) 的損壞。您可以使用 dumpe2fs 查看檔案系統中所有超級塊的位置。
然後,使用 e2fsck,您可以使用其中一個備份副本,如以下示例:
這個例子是在說明如何使用 ext2 檔案系統的儲存區塊 32768 裡的超級區塊,並在檔案系統檢查完成後適當地更新主要超級區塊。
Note: 在一個 4KB 的區塊大小中,每隔 32768 個區塊就會儲存一個超級區塊的備份。
在 ext2、ext3 和 ext4 檔案系統格式中,有一個特別的 lost+found 目錄。當系統發生崩潰時,Linux 會自動檢查整個檔案系統。那些無法分配名稱的檔案或檔案片段不會直接被刪除,而是會儲存在這個目錄中。
透過檢視這個目錄的內容,您可以試著還原出檔案的原始名稱和用途。
Configure Logical Volume Management (LVM)
LVM
Front end to the device mapper to easily and flexibly(靈活的) manage logical storage
Supports features such as:
- On-line volume creation
- On-line volume resizing
- Copy-on-write snapshots
LVM Components
LVM 是用來更靈活地管理儲存空間的工具
- Physical Volumes (PV) - 包含 LVM metadata 的物理裝置
- 物理硬碟、磁碟分割區、RAID volumes
- Physical Volumes 被組成 Volume Group
- Physical Extent (PE) - 在 LVM Group 中分配的基本存儲單位
- Physical Volumes 包含 PE
- 包含資料的 PE 可從一個 Physical Volume 移動到另一個 PV
- Volume Groups (VG) - Physical Volumes 的集合
- VG 必須被細分為 Logical Volumes 才能使用存儲空間
- VG 可以通過添加或刪除 Physical Volumes 來調整大小
- Logical Volumes (LV) - VG 的細分
- 可包含可掛載的檔案系統
- 可在線調整 Logical Volumes 的大小和建立快照
LVM extents 應該比 4MB 大得多,例如 128MB。這樣可以減少 overhead(開銷)。
LVM extents 應該與儲存空間對齊。這意味著它們應該適合於 1MB 的邊界。可以使用 pvs +o pe_start 命令檢查。
如果可能,volume manager 的 Stripe (條帶)大小應與儲存系統對齊。參見 man lvcreate 中的 --stripes 和 --stripesize 選項。
LVM 層對性能只有輕微的影響。在測試中(多年前)發現了 3-5% 的性能下降。
LVM Configuration File /etc/lvm/lvm.conf
- Is loaded during the initialization phase of LVM
- Is used by certain LVM tools
- Is comprised of a series of sections, with each section containing one or more directives
- 這個設定檔是由一系列「區段」所組成,每個區段都包含一個或多個「指令」
LVM Services
/usr/lib/systemd/system/lvm2-lvmetad.service- Metadata caching daemon for LVM (lvmetad)
- Receives notifications(通知) from udev rules
- Has an up-to-date and consistent image of the available volume groups
/usr/lib/systemd/system/lvm2-monitor.service- Executes
vgchange --monitor y - Start or stop monitoring a mirrored or snapshot logical volume with
dmeventd, if it is installed
lvmetad 是 LVM 的 Metadata 緩存背景程序。此背景程序接收 udev 規則的通知(在使用 lvmetad 時必須安裝此規則才能正常工作)。
通過這些通知,lvmetad 具有系統中可用 VG 的最新且一致的映像。
預設情況下,即使運行,LVM 也不會使用 lvmetad。
Create LVM Physical Volumes
- Syntax:
pvcreate /dev/<device> - Partitions can be used directly
- 若要使用完整的磁碟,需先刪除主引導紀錄中的分割表:
- WARNING: All data on the hard disk or partition used will be lost!
View LVM Physical Volume Info
pvscan,Detect(掃描) physical volumes and volume group membershippvs,View existing physical volumespvdisplay,View attributes(屬性) of physical volumespvchange [-ay | -an],Activate/Deactivate(啟用/禁用) physical volumes
Create and Activate LVM Volume Groups
vgcreate,Create a volume groupvgscan,Discover and list volume groupsvgchange,Activate/Deactivate a volume group
LVM 開始運作時,並不會自動知道現有的 volume groups,需要先發現它們。
透過 vgscan 搜尋後,LVM 可能會知道 volume groups,但它們尚未啟用。
使用 vgchange 工具也可以更改其他屬性,例如調整大小。
vgcreate 工具用於創建新的 volume group。如果要創建名為 system 的 volume groups 並將 physical volume /dev/sda9 加入其中,請輸入以下命令:
螢幕輸出
檢查
螢幕輸出
View LVM Volume Groups
vgs,View existing discovered volume groupsvgdisplay <vg_name>,View attributes of a volume group
Examples:
螢幕輸出
Values:
VG – Volume group name
#PV – Number of physical volumes
#LV – Number of logical volumes
#SN – Snapshot number
Attr – Attributes:
- Permissions:
(w)riteable, (r)ead-only Resi(z)eable (可調整大小)E(x)ported (匯出)(p)artial: one or more physical volumes belonging to the volume group are missing from the system- Allocation(分配) policy:
(c)ontiguous, c(l)ing, (n)ormal, (a)nywhere, (i)nherited (c)lustered
VSize – Size of the volume group
VFree – Free space on the volume group
Create LVM Logical Volumes
- Create a basic logical volume:
lvcreate -L <size> -n <lv_name> <vg_name>
- Create a striped logical volume:
lvcreate -i 3 -L <size> -n <lv_name> <vg_name>
- Create a mirrored logical volume:
lvcreate -m 1 -L <size> -n <lv_name> <vg_name>
- Create a snapshot logical volume:
lvcreate -L <size> -n <lv_name> <orig_lv_name>
Logical Volume Types
Logical volumes 的結構和功能不同,因此它們可以是不同的類型
Common Types (the first three are relevant in practice):
- Normal
- Mirrored
- Snapshot
- Origin
- Special Types
- PVmove
- Virtual
To create a logical volume, use lvcreate, specifying the size, the name for the logical volume, and the volume group:
螢幕輸出
View LVM Logical Volumes
lvscan,View/Discover logical volumeslvs,Show existing logical volumeslvdisplay,View attributes of logical volume(s)
lvscan shows the device to use for the formatting and mounting:
螢幕輸出
The lvs command represents the volume type in the first field of the Attr column:
- normal =
------ - mirrored =
m----- - mirrored (no initial sync) =
M----- - snapshot =
s----- - snapshot (invalid) =
S----- - pvmove =
p----- - origin =
o----- - virtual =
v-----
Manage LVM Physical Volumes
pvmove,Move used PVs to a different physical volumepvremove,Remove an existing physical volume (including LVM meta-data)pvresize,Resize a physical volume
在使用 pvremove 從一個 VG 中移除一個 PV 之前,它的內容必須使用 pvmove 移動到另一個 PV 中。
Manage LVM Volume Groups
Extending a Volume Group
- New Physical Volumes can be created if new storage devices become available.
- Volume Groups can then be extended by adding these new Physical Volumes to the Volume Groups.
Reduce a Volume Group
pvmove is used to move the PEs off of the physical volume you will be removing from the group- You must have at least the same amount of free space available in the volume group as there is used space on the physical volume who’s PEs you will be moving
- Use the
pvscan utility to determine Physical Volume usage - The
dm_snapshot kernel module must be loaded
Manage LVM Volume Groups
- Extend a volume group (add physical volumes)
vgextend <vg_name> /dev/<device> /dev/<device> ...
- Reduce a volume group
pvmove /dev/<device>vgreduce <vg_name> /dev/<device>
- Split and merge volume groups
- Remove a volume group
- Activate/Deactivate logical volumes
lvchange -a {y|n} <lv_name>
Merge(合併) Volume Groups
- LVM volume groups can be merged if their PEs are the same size and their LV and PV summaries match
- The source volume group must be inactive before merging
- The destination volume group can be active or inactive
vgchange -a n <source_vg>vgmerge <destination_vg> <source_vg>
Split(拆分) Volume Groups
- LVM volume groups can be split if all PVs contain at least one copy of the VG metadata
- LV cannot be split between the two VGs
vgsplit <existing_vg> <new_vg> <pv_path> <pv_path> …
A volume group cannot be removed if any logical volume on it is still open.
lvchange 還允許更改 logical volumes 的其他 attributes (屬性),而不僅僅是 activation (啟動)。
Extend a Logical Volume
- Logical volumes can be extended if there is unused space available in the volume group
- Once the logical volume has been extended, then you must grow the file system in the LV.
lvextend -L +size /dev/<vg_name>/<lv_name>lvresize -L +size /dev/<vg_name>/<lv_name>
Reduce a Logical Volume
- The file system on the logical volume must be reduced before reducing the logical volume
- Most file systems cannot be reduced while they are mounted (and XFS cannot be reduced at all)
lvreduce -L -size /dev/<vg_name>/<lv_name>lvresize -L -size /dev/<vg_name>/<lv_name>
LVM Logical Volume Snapshots
LVM Snapshots (快照) 本質上是 logical volumes,創建快照會創建一個新的 logical volume(因此需要 VG 中的未分配空間)。
LVM 快照在時間點凍結 volume 的視圖,而原始 volume 繼續被修改。
LVM 快照可以像任何其他 logical volumes 一樣掛載和訪問。
不再需要快照時,可以刪除快照而不影響原始卷。空間隨後將返回到 VG 作為未分配空間。
使用快照需要加載 dm_snapshot 內核模塊。
創建快照時,metadata 被複製並引用為替代 logical volume。
快照使用 VG 中的未分配空間,就像普通 logical volume 一樣。
LVM 快照使用 copy-on-write。當原始 volume 中的檔案更改時,底層 PE 被複製到快照 volume 中。
如果快照用盡空間,則被丟棄。
Create LVM Snapshots
- Create an LVM snapshot
lvcreate -s -L <size> -n <snap_name> <orig_lv_name>
- View Snapshots and their original logical volumes
- Remove a snapshot volume
lvremove /dev/<vg_name>/<snap_name>
Configure Software RAID
- RAID: Redundant Array of Independent (or Inexpensive) Disks
- Software RAID:
- Advantages of RAID systems without the additional cost(花費) of hardware RAID controllers CPU time and memory overhead
Multiple Device (MD) RAID
- Multiple Device (MD) is the default software RAID subsystem in the Linux kernel
- Device mapper RAID is also available but MD is more mature(成熟) and robust(耐用)
- MD does not use the device mapper
- MD can create linear arrays(線性陣列) using its own meta-data or use external meta-data
- Disk Data Format (DDF)
- Intel Matrix Storage Manager (IMSM) meta-data
Myths about Software/Hardware RAID
- 軟體 RAID 比硬體 RAID慢
- 通常是錯誤的。
- 沒有校驗位的 RAID 性能與硬體 RAID 相當(甚至因為雙工機制更好)。
- 現代 CPU 比硬體 RAID 控制器上的專用處理器快得多,可以以較低的開銷快速生成校驗位(通常<5%)。
- 硬體 RAID 總是優於軟體 RAID
- 通常是錯誤的(但要看情況)。
- 硬體 RAID 的優點:更容易啟動,可以被電池備份和緩存。
- 硬體 RAID 的缺點:更昂貴,需要專用控制器。
:::info
重要提示:軟體 RAID 不支援集群檔案系統(如 OCFS2),因為 RAID 不支援同時啟動。如果要 OCFS2 的 RAID,需要讓儲存子系統來處理 RAID。
:::
若要在 SLES 機器的開機時啟動 RAID 服務,請使用以下命令:
RAID Levels
- RAID 0 - Disks are pooled(集合)
- good performance
- no redundancy (despite the name), if one disk fails data is lost
- RAID 1 – Mirroring(鏡像)
- Data is lost when the last disk fails
- RAID 5 - Data is distributed across disks
- one disk may fail
- data is lost when two disks fail
- RAID 6 - Similar to RAID 5
- two disks may fail
- data is lost when three disks fail
- RAID 10 – Mirrored Stripe Sets
- Performance of RAID 0 with fault tolerance of RAID 1
- data is lost if a disk is lost in each strip set
RAID 0
是一種提高數據存取性能的技術,透過將每個檔案的資料區塊分散到多個硬碟驅動器上,來達到此目的。
雖然它的名字中有「RAID」這個詞,但實際上 RAID 0 並不能提供數據備份的功能,它只是將多個硬碟組成一個虛擬硬碟,用以提高存取速度。
RAID 0 的效能非常好,但若其中任何一個硬碟故障,整個 RAID 0 系統就會失效,並且會導致您的數據遺失。
RAID 1
這個等級提供足夠的資料安全性,因為資料會被 1:1 地複製到另一個硬碟上,稱為硬碟鏡像。
如果一個硬碟被損壞,其內容的副本會在另一個鏡像硬碟上提供。除了一個硬碟之外,所有硬碟都可能受損而不會危及您的資料。
然而,如果損壞未被檢測到,損壞的資料可能會被鏡像到正確的硬碟上,從而損壞資料。
與使用單個硬碟訪問相比,在複製過程中寫入效能略有下降(比單個硬碟慢10%至20%),但讀取訪問速度顯著快於任何一個正常的物理硬碟,因為資料是重複的,可以並行掃描。
RAID 1通常提供了近乎是單個硬碟的兩倍讀取事務速率和幾乎相同的寫入事務速率。
RAID 5
RAID 5 是性能和冗餘性方面介於 Level 0 和 Level 1 之間的優化折衷方案。
硬碟空間等於使用的硬碟數減去一。數據像 RAID 0 一樣分佈在硬碟上。
為了安全起見,在其中一個分區上創建奇偶校驗區塊,它們用 XOR 連接在一起,以便在系統故障時,通過相應的奇偶校驗區塊重新構建內容。
使用 RAID 5,同時最多只能故障一個硬碟。如果有一個硬碟故障,必須盡快更換,以避免失去數據風險。
RAID 6
RAID 6 和 RAID 5 相似,最少需要 4 個硬碟,最多允許 2 個硬碟故障。但如果有 3 個硬碟故障,就會丟失數據。
RAID 10
All disks can be lost in a single strip set with no data loss if the other strip set has lost no disks. This RAID implementation combines features of RAID 0 and RAID 1: the data is first mirrored to separate disk arrays, which are inserted into a new RAID 0; type array. In each RAID 1 sub-array, one disk can fail without any damage to the data. A minimum of four disks and an even number of disks is needed to run a RAID 10. This type of RAID is used for database application where a huge load is expected.
如果一組磁碟陣列沒有損失磁碟,那麼在另一組磁碟陣列中失去所有磁碟時也不會喪失任何資料。這種 RAI D結合了 RAID 0 和 RAID 1 的特點:首先將數據鏡像到不同的磁碟陣列中,然後將它們插入到新的RAID 0陣列中。在每個 RAID 1 子陣列中,最多可損失一個磁碟而不會損壞數據。RAID 10 至少需要四個磁碟和偶數個磁碟才能運行。這種 RAID 通常用於預計承受大量負載的數據庫應用程式。
Manage RAIDs: Monitoring
- /proc/mdstat : 系統上 RAID 的狀態
範例 :
螢幕輸出
使用 watch 命令定期執行 cat 命令。
使用 -d 選項可以突顯連續更新之間的差異。例如:
如果您沒有此檔案,您需要首先加載 RAID 核心模塊:modprobe md_mod
mdadm Modes and Options
- Assemble Modes
- Build Modes
- Create Modes
- Follow or Monitor Modes
- Grow Modes
- Incremental Assembly Modes
Create an Array
Assemble(組裝) an Existing Array
Assemble (activate/start) an array:
mdadm -A <array>mdadm -A -s
-n,Number of disks in the array-s, Scan for available arrays-c <config> ,Specify the configuration file to use when scanning. Options: /etc/mdadm.conf, partitions
- Examples:
mdadm -A /dev/md0mdadm -A -s
Deactivate(停用) an Array
- Deactivate (Stop) an array
- Example:
Replace a Disk in an Array
- Mark a disk in the array as faulty(錯誤):
- Remove a disk from the array:
- Repeat the above for any other array that uses this physical disk, such as
/dev/md1 and /dev/sdb2 - 關閉電源並更換磁碟。
- (可選)使用 fdisk 或 parted 對磁碟進行分區。
- Partitions may not be smaller than before
- Add the new disk to your array:
Remove an Array
mdadm -S <array>mdadm --zero-superblock <devices>-S,Stop the specified array-zero-superblock,Write zeros to the super block of the specified device
- Example:
mdadm -S /dev/md0mdadm --zero-superblock /dev/sd[d-e]
:::danger
NOTE:
- To remove an array completely, it has to be stopped first!
- Make sure to also delete entries in /etc/fstab relating to the array
:::
Generate a mdadm.conf File
mdadm -E -s --brief > /etc/mdadm.conf-E,Examine(檢查) the metadata of scanned/specified disks-s,Scan for existing arrays--brief,Display output in brief format (for mdadm.conf)
- Example:
mdadm -E -s > /etc/mdadm.conf
Display Info About an Array
- Display info about an array:
mdadm -Q <array>-Q,Query basic information on an array--detail,Full information on an array
- Display info about an array member disk:
mdadm --examine <disk>--examine,Query basic information on a disk from an array
Offline Uncorrectable Sectors
- Output of smartctl, such as:
- Trigger a resync:
/usr/bin/echo check > /sys/block/mdx/md/sync_action
array 會被讀取,如果一個磁區在一個磁碟上無法讀取,則使用其他磁碟中的相同信息重新寫入它。
注意:如果您的 RAID 未覆蓋整個硬盤,它僅在硬盤錯誤位於其中一個 RAID 中時才起作用。
可以設置一個定期執行該命令的 cron 作業。
上述命令通常可以修復錯誤,但是持續出現此類錯誤的磁碟可能應該被替換。
Monitor RAIDs
- In
/etc/sysconfig/mdadm- Set MDADM_MAIL to a proper email address
- Make sure that mail arrives at its destination
- Activate the monitoring with
systemctl start mdmonitor.service
RAID 系統需要監控,以避免忽略磁碟損壞的問題。
RAID 系統的優點是可以容許單個磁碟故障(不包括 RAID 0)。這表示當磁碟出現故障時,可能不會立即被發現,直到下一個磁碟故障時,數據才會丟失。因此需要監控 RAID 系統以避免出現此類問題。
Define and Describe Btrfs
Btrfs: The “Better” File System
- Built-in volume management with subvolumes
- Built-in copy on write
- Powerful snapshot capabilities
- Massive Scalability:
- 18.4 Exabytes (16EB w/ effective shrink)
- Other Capabilities:
- Compression(壓縮)
- Built-in data integrity(完整性) (checksums)
- SSD optimization(優化)
Btrfs Support
- Btrfs is supported
- As “/” file system
- For migration from ext2/3/4 (except “/”)
- Everything you can do with the YaST partitioner is supported
- Exceptions to official support
- RAID
- Integrated Volume Management
- Compression and Encryption
- Recommendation for data volumes: XFS
- Performance and scalability are proven for 8+ years
- No extra costs for XFS in SUSE Linux Enterprise
FAQ:
Is there any limitation for copy on write in terms of transactions?
Ans: The disk space in the limitation. As every change may need additional blocks, two sets of parameters must be chosen carefully according to the use case:
- Disk space available to the whole volume
- Automated cleanup rules
Is there a recommendation of how many % of free space should be kept?
Ans: Since SUSE Linux Enterprise 11 SP2, the default for a root file system on Btrfs is 20 GiB, while the system will need not more than 8 GiB usually. In other words, when starting there should be more than 70% free.
For safe operation we suggest that a file system is not filled up to more than 90%, i.e. 10% free.
Future development for snapper might introduce automated cleanup rules also based on free space or fill-level.
Copy on Write
Normal Write
Existing blocks of a file are overwritten when the content changes
Copy on Write
If content of a block changes, the changed block is put beside the non-changed block
Instead the metadata (block-list) changes
Benefit
Implementation of “transactions” in the file system is easy, as old content is still available
因為 Btrfs 對所有的檔案系統寫入都是寫時復制,所以有可能進入這樣一種情況:當檔案系統變滿時,當你試圖刪除一個檔案時,你會得到一個錯誤,說 “no space left on device”,檔案不能被刪除。
如果出現這種情況,你可以嘗試一些事情:
如果你不是在一個時間緊迫的情況下,運行:
如果報告說你的數據和 metadata 都有可用的空間,運行以下程序:
這將需要一些時間,但應該會釋放出一些空間,讓你能夠刪除檔案。
如果你處於時間緊迫的情況下,你可以嘗試兩種不同的方法,或者如果第一種方法不起作用:
通過運行以下命令,來凍結該檔案:
這將在不引起任何新的 metadata 操作的情況下刪除該檔案
用nodatacow選項重新掛載該檔案系統:
這將允許你在不執行寫時復制操作的情況下刪除該檔案

Subvolumes

A Btrfs consists of one or more subvolumes. Subvolumes in Btrfs have a number and a name. There is always at least one subvolume, the tree root subvolume with the number 0. The root subvolume is represented by the @ character.
Subvolumes appear as directories in the file system and, at first glance, cannot be distinguished from regular directories. Usually they appear as a subdirectory in the “parent” file system, but they can be mounted separately and not “just a subdirectory”.
Subvolumes can include a complete file system tree.
Subvolumes are simliar to two “separate” file systems, which are using the same pool of data blocks (and other infrastructure).
The benefits of using subvolumes are:
- Different parts (subvolumes) of a file system can have different attributes, such as quotas or snapshoting rules.
- Copy on Write is possible across volumes.
Manage Subvolumes from the CLI
- btrfs subvolume create
[<Path>/]<Name>- Example:
btrfs subvolume create /data/SV4
- btrfs subvolume set-default
<ID> <Path>- Example:
btrfs subvolume set-default 264 /data
- btrfs subvolume delete
[<Path>/]<Name>- Example:
btrfs subvolume delete /data/SV4
- btrfs subvolume list
[-p] <Path>- Example:
btrfs subvolume list -p /data
- btrfs subvolume find-new
[<Path>/]<Name> <GenPointer>- Example:
btrfs subvolume find-new /data/SV3 35
Mount a subvolume
mount -o subvol=<Name> /dev/<sdXX> /<Mountpoint>mount -o subvolid=<ID> /dev/<sdXX> /<Mountpoint>
Understand NFSv4