使用bash将文件存储在RAM中 - 可能吗?

时间:2011-12-04 16:12:39

标签: bash encryption ram file-storage

我要用Arch Linux使用LUKS加密我的硬盘;通常你使用密钥文件或密码短语解锁加密卷,但我想要两者兼顾。

# If keyfile exists, try to use that
if [ -f ${ckeyfile} ]; then
    if eval /sbin/cryptsetup --key-file ${ckeyfile} luksOpen ${cryptdev} ${cryptname} ${CSQUIET}; then
        dopassphrase=0
    else
        echo "Invalid keyfile. Reverting to passphrase."
    fi
fi
# Ask for a passphrase
if [ ${dopassphrase} -gt 0 ]; then
    echo ""
    echo "A password is required to access the ${cryptname} volume:"

    #loop until we get a real password
    while ! eval /sbin/cryptsetup luksOpen ${cryptdev} ${cryptname} ${CSQUIET}; do
        sleep 2;
        done
    fi
    if [ -e "/dev/mapper/${cryptname}" ]; then
        if [ ${DEPRECATED_CRYPT} -eq 1 ]; then
            export root="/dev/mapper/root"
        fi
    else
        err "Password succeeded, but ${cryptname} creation failed, aborting..."
        exit 1
    fi

上面的代码处理卷的解密,因为您可以看到它检查密钥文件是否有效,如果不是,则恢复密码。我想解决这个问题的方法是获取密钥文件和密码,将这些密码文件的值相加并创建一个新的密钥文件来打开卷。问题是我不知道保存文件的位置,我想将其保存在RAM中,但我不知道是否可以使用bash。

if  poll_device "${cryptdev}" ${rootdelay}; then
    if /sbin/cryptsetup isLuks ${cryptdev} >/dev/null 2>&1; then
        [ ${DEPRECATED_CRYPT} -eq 1 ] && warn_deprecated
        if [ -f ${ckeyfile} ]; then
            bool=true
            while $bool; then
                echo "Enter passphrase: "
                read passphrase
                tmpkey="tmpkeyfile"
                cp ${ckeyfile} ${tmpkey} #Create a temporary keyfile
                echo passphrase >> ${tmpkey} #Add the passphrase to the keyfile
                if eval /sbin/cryptsetup --key-file ${tmpkey} luksOpen ${cryptdev} ${cryptname} ${CSQUIET}; then
                    bool=false

上面的代码是我的想法,我没有尝试过代码,因为我没有机会,但我不认为将临时密钥文件保存在硬盘驱动器上是安全的。<登记/> 我不太了解bash,所以这里和那里可能会有一些错误。但我只是想表明我如何解决问题的想法,也许任何人都可以帮助我实际上让它发挥作用 是否可以将密钥文件存储在RAM中,还是有其他替代方法?

1 个答案:

答案 0 :(得分:4)

我认为您可能正在寻找的是shm / shmfs,或者称为tmpfs。

以下是一些关于使用shm / tmpfs以及如何安装并保护它的链接

http://www.cyberciti.biz/tips/what-is-devshm-and-its-practical-usage.html

https://wiki.archlinux.org/index.php//dev/shm

看起来很简单,可以创建一个小分区并使用它。如果您的脚本将动态创建挂载,则需要以root身份运行。