如何在 Stratis 中開始加密
Stratis 在其 官方網(wǎng)站 上被描述為“易于使用的 Linux 本地存儲管理”。請看這個 短視頻,快速演示基礎(chǔ)知識。該視頻是在 Red Hat Enterprise Linux 8 系統(tǒng)上錄制的。視頻中顯示的概念也適用于 Fedora 中的 Stratis。
Stratis 2.1 版本引入了對加密的支持。繼續(xù)閱讀以了解如何在 Stratis 中開始加密。
先決條件
加密需要 Stratis 2.1 或更高版本。這篇文章中的例子使用的是 Fedora 33 的預(yù)發(fā)布版本。Stratis 2.1 將用在 Fedora 33 的最終版本中。
你還需要至少一個可用的塊設(shè)備來創(chuàng)建一個加密池。下面的例子是在 KVM 虛擬機(jī)上完成的,虛擬磁盤驅(qū)動器為 5GB(/dev/vdb
)。
在內(nèi)核密鑰環(huán)中創(chuàng)建一個密鑰
Linux 內(nèi)核密鑰環(huán)用于存儲加密密鑰。關(guān)于內(nèi)核密鑰環(huán)的更多信息,請參考 keyrings
手冊頁(man keyrings
)。
使用 stratis key set
命令在內(nèi)核鑰匙圈中設(shè)置密鑰。你必須指定從哪里讀取密鑰。要從標(biāo)準(zhǔn)輸入中讀取密鑰,使用 -capture-key
選項。要從文件中讀取密鑰,使用 -keyfile-path <file>
選項。最后一個參數(shù)是一個密鑰描述。它將稍后你創(chuàng)建加密的 Stratis 池時使用。
例如,要創(chuàng)建一個描述為 pool1key
的密鑰,并從標(biāo)準(zhǔn)輸入中讀取密鑰,可以輸入:
# stratis key set --capture-key pool1key
Enter desired key data followed by the return key:
該命令提示我們輸入密鑰數(shù)據(jù)/密碼,然后密鑰就創(chuàng)建在內(nèi)核密鑰環(huán)中了。
要驗證密鑰是否已被創(chuàng)建,運(yùn)行 stratis key list
:
# stratis key list
Key Description
pool1key
這將驗證是否創(chuàng)建了 pool1key
。請注意,這些密鑰不是持久的。如果主機(jī)重啟,在訪問加密的 Stratis 池之前,需要再次提供密鑰(此過程將在后面介紹)。
如果你有多個加密池,它們可以有一個單獨(dú)的密鑰,也可以共享同一個密鑰。
也可以使用以下 keyctl
命令查看密鑰:
# keyctl get_persistent @s
318044983
# keyctl show
Session Keyring
701701270 --alswrv 0 0 keyring: _ses
649111286 --alswrv 0 65534 \_ keyring: _uid.0
318044983 ---lswrv 0 65534 \_ keyring: _persistent.0
1051260141 --alswrv 0 0 \_ user: stratis-1-key-pool1key
創(chuàng)建加密的 Stratis 池
現(xiàn)在已經(jīng)為 Stratis 創(chuàng)建了一個密鑰,下一步是創(chuàng)建加密的 Stratis 池。加密池只能在創(chuàng)建池時進(jìn)行。目前不可能對現(xiàn)有的池進(jìn)行加密。
使用 stratis pool create
命令創(chuàng)建一個池。添加 -key-desc
和你在上一步提供的密鑰描述(pool1key
)。這將向 Stratis 發(fā)出信號,池應(yīng)該使用提供的密鑰進(jìn)行加密。下面的例子是在 /dev/vdb
上創(chuàng)建 Stratis 池,并將其命名為 pool1
。確保在你的系統(tǒng)中指定一個空的/可用的設(shè)備。
# stratis pool create --key-desc pool1key pool1 /dev/vdb
你可以使用 stratis pool list
命令驗證該池是否已經(jīng)創(chuàng)建:
# stratis pool list
Name Total Physical Properties
pool1 4.98 GiB / 37.63 MiB / 4.95 GiB ~Ca, Cr
在上面顯示的示例輸出中,~Ca
表示禁用了緩存(~
否定了該屬性)。Cr
表示啟用了加密。請注意,緩存和加密是相互排斥的。這兩個功能不能同時啟用。
接下來,創(chuàng)建一個文件系統(tǒng)。下面的例子演示了創(chuàng)建一個名為 filesystem1
的文件系統(tǒng),將其掛載在 /filesystem1
掛載點(diǎn)上,并在新文件系統(tǒng)中創(chuàng)建一個測試文件:
# stratis filesystem create pool1 filesystem1
# mkdir /filesystem1
# mount /stratis/pool1/filesystem1 /filesystem1
# cd /filesystem1
# echo "this is a test file" > testfile
重啟后訪問加密池
當(dāng)重新啟動時,你會發(fā)現(xiàn) Stratis 不再顯示你的加密池或它的塊設(shè)備:
# stratis pool list
Name Total Physical Properties
# stratis blockdev list
Pool Name Device Node Physical Size Tier
要訪問加密池,首先要用之前使用的相同的密鑰描述和密鑰數(shù)據(jù)/口令重新創(chuàng)建密鑰:
# stratis key set --capture-key pool1key
Enter desired key data followed by the return key:
接下來,運(yùn)行 stratis pool unlock
命令,并驗證現(xiàn)在可以看到池和它的塊設(shè)備:
# stratis pool unlock
# stratis pool list
Name Total Physical Properties
pool1 4.98 GiB / 583.65 MiB / 4.41 GiB ~Ca, Cr
# stratis blockdev list
Pool Name Device Node Physical Size Tier
pool1 /dev/dm-2 4.98 GiB Data
接下來,掛載文件系統(tǒng)并驗證是否可以訪問之前創(chuàng)建的測試文件:
# mount /stratis/pool1/filesystem1 /filesystem1/
# cat /filesystem1/testfile
this is a test file
使用 systemd 單元文件在啟動時自動解鎖 Stratis 池
可以在啟動時自動解鎖 Stratis 池,無需手動干預(yù)。但是,必須有一個包含密鑰的文件。在某些環(huán)境下,將密鑰存儲在文件中可能會有安全問題。
下圖所示的 systemd 單元文件提供了一個簡單的方法來在啟動時解鎖 Stratis 池并掛載文件系統(tǒng)。歡迎提供更好的/替代方法的反饋。你可以在文章末尾的評論區(qū)提供建議。
首先用下面的命令創(chuàng)建你的密鑰文件。確保用之前輸入的相同的密鑰數(shù)據(jù)/密碼來代替passphrase
。
# echo -n passphrase > /root/pool1key
確保該文件只能由 root 讀?。?/p>
# chmod 400 /root/pool1key
# chown root:root /root/pool1key
在 /etc/systemd/system/stratis-filesystem1.service
創(chuàng)建包含以下內(nèi)容的 systemd 單元文件:
[Unit]
Description = stratis mount pool1 filesystem1 file system
After = stratisd.service
[Service]
ExecStartPre=sleep 2
ExecStartPre=stratis key set --keyfile-path /root/pool1key pool1key
ExecStartPre=stratis pool unlock
ExecStartPre=sleep 3
ExecStart=mount /stratis/pool1/filesystem1 /filesystem1
RemainAfterExit=yes
[Install]
WantedBy = multi-user.target
接下來,啟用服務(wù),使其在啟動時運(yùn)行:
# systemctl enable stratis-filesystem1.service
現(xiàn)在重新啟動并驗證 Stratis 池是否已自動解鎖,其文件系統(tǒng)是否已掛載。
結(jié)語
在今天的環(huán)境中,加密是很多人和組織的必修課。本篇文章演示了如何在 Stratis 2.1 中啟用加密功能。