답변1
제공된 링크 @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