Freebsd

如何在 FreeBSD 13 上添加加密交換文件

  • December 8, 2021

我想在 FreeBSD 13 中添加加密交換文件。

我已閱讀文件頁面,但沒有說明如何為交換文件設置加密。

我也讀過另一個,但它只給出了加密交換分區的例子,而不是交換文件。目的是在系統啟動時啟動加密交換文件。系統執行最新的 FreeBSD 13。

請解釋如何實現這一點。

提供的連結@r bert很接近,但可以從一些微調中受益。

感謝 FreeBSD 論壇上的原始創建者mb2015。此方法創建自定義服務腳本並在/etc/rc.conf.

首先,/usr/local/etc/rc.d/encrypted_swapfile通過複製和粘貼這個大cat命令來創建它來執行它,然後執行它chmod後面的命令:

# cat << 'EOF' > /usr/local/etc/rc.d/encrypted_swapfile
#!/bin/sh

# PROVIDE: encrypted_swapfile
# REQUIRE: swaplate
# BEFORE: LOGIN
# KEYWORD: nojail shutdown

. /etc/rc.subr

name="encrypted_swapfile"
rcvar="encrypted_swapfile_enable"

start_cmd="${name}_start"
stop_cmd="${name}_stop"

load_rc_config "$name"
: ${encrypted_swapfile_enable:="NO"}
: ${encrypted_swapfile_file:="/usr/swap0"}
: ${encrypted_swapfile_size:="2G"}

SWFILE="$encrypted_swapfile_file";
SWFILEDIR="$(dirname "$SWFILE")";
SWDEVLINK="/var/run/encrypted_swapfile_device";

encrypted_swapfile_start()
{
   # Create and mount a one-time encrypted swap file.
   # This is a workaround for the inability to do this via an /etc/fstab entry.
   # See https://forums.freebsd.org/threads/encrypt-swap-file.44519/#post-292933
   #
   if [ ! -e "$SWDEVLINK" ]; then
       if [ -w "$SWFILEDIR" ]; then
           truncate -s "$encrypted_swapfile_size" "$encrypted_swapfile_file" &&
           chmod 0600 "$encrypted_swapfile_file" &&
           SWMD="$(mdconfig -a -t vnode -f "$encrypted_swapfile_file")" &&
           if [ $? -eq 0 ] && [ -n $SWMD ] && [ -e "/dev/$SWMD" ]; then
               chmod 0600 "/dev/$SWMD" &&
               geli onetime -e AES-XTS -l 256 -d "/dev/$SWMD" &&
               chmod 0600 "/dev/$SWMD.eli" &&
               swapon "/dev/$SWMD.eli" &&
               ln -f -s "/dev/$SWMD.eli" "$SWDEVLINK";
               unset SWMD;
           fi
       else
           echo "Could not create encrypted swap file in $SWFILEDIR; check permissions." &&
           return 1;
       fi
   else
       SWMD="$(readlink "$SWDEVLINK")" &&
       swapinfo | grep -vq "^$SWMD " &&
       echo "Encrypted swap file already exists; enabling." &&
       swapon "$SWMD";
       unset SWMD;
   fi
   return 0;
}

encrypted_swapfile_stop()
{
   if [ -e "$SWDEVLINK" ]; then
       SWMD=$(readlink "$SWDEVLINK") &&
       swapoff "$SWDEVLINK" &&
       mdconfig -du "${SWMD%.eli}" &&
       rm "$SWDEVLINK" &&
       rm "$SWFILE";
   else
       echo "No encrypted swap file found; nothing to stop.";
       rm -f "$SWDEVLINK";
   fi
}

run_rc_command "$1"

EOF
# chmod 755 /usr/local/etc/rc.d/encrypted_swapfile

接下來,編輯/etc/rc.conf以啟用這個新服務,並配置它使用的變數:

encrypted_swapfile_enable="yes"
encrypted_swapfile_file="/usr/swap0"
encrypted_swapfile_size="2G"

現在您的加密交換文件可以通過啟動或停止encrypted_swapfile服務分別啟動或停用:

# swapinfo
Device          1024-blocks     Used    Avail Capacity
# service encrypted_swapfile start
# swapinfo
Device          1024-blocks     Used    Avail Capacity
/dev/md0.eli        2097152        0  2097152     0%
# service encrypted_swapfile stop
# swapinfo
Device          1024-blocks     Used    Avail Capacity

引用自:https://unix.stackexchange.com/questions/680062