在 cron 下運行時掛載和卸載的行為不同

在 cron 下運行時掛載和卸載的行為不同

在 AWS 中運行 CentOS 6,我所看到的讓我感到困惑。

有一個s3fs掛載/etc/fstab有時會失去讀寫能力。我有一個 cron 作業,運行了幾個月,效果很好,它會簡單地測試安裝是否每分鐘都正常,如果它失去了連接,它就會失去umount連接mount。安裝座在無負載情況下往往會比在實際負載情況下更頻繁地消失,因此這是一個很好的解決方案。

由於某種原因,這停止工作,現在機器無法從共享中讀取/寫入,因為機器在配置後啟動時所做的第一件事就是umount共享mount

現在我在嘗試閱讀時遇到的錯誤是這樣的:

cp: cannot open `/app/canary.txt' for reading: Input/output error

/var/log/messages我看到這個:

kernel: s3fs[3077]: segfault at e66000 ip 00007f833663d94e sp 00007ffc849c5b18
error 4 in libc-2.12.so[7f83365b4000+18a000]

現在,當我以 root 身分在控制台中運行完全相同的腳本時,它運行得非常完美。卸下和安裝驅動器並使其處於工作狀態。

我的第一個猜測是環境中的某些東西導致了差異,所以我source /root/.bash_profile在腳本中添加了 a ,但無濟於事。

/etc/fstab 中的行是一個怪物,但這是我們在多次嘗試微調後發現效果最好的行:

ourbucket /app fuse.s3fs _netdev,allow_other,endpoint=us-west-2,url=https://s3-us-west-2.amazonaws.com,use_path_request_style,use_sse,gid=1001,umask=0007,use_cache=/srv/s3fs,retries=20,parallel_count=30,connect_timeout=30,readwrite_timeout=60,stat_cache_expire=86400,max_stat_cache_size=100000 0 0

這就是 cronfile 的樣子:

* * * * * root /usr/bin/sudo /root/check_mount.sh

我嘗試了使用和不使用 sudo 的情況,因為我認為這可能會影響環境。

我已經嘗試過該腳本的許多變體,但大多數命令都在某個時刻使用過。無論umount我做什麼類型,都會出現同樣的問題。

\cp /app/canary.txt /tmp/canary.txt
retVal=$?
sleep 1
if [ ${retVal} -ne 0 ]; then
    echo "Copy failed, trying to umount"
    umount /app
    echo "umount returned $?"
    sleep 1
    echo "Trying umount -f"
    umount -f /app
    echo "umount -f returned $?"
    sleep 1
    echo "Trying fusermount -u"
    /usr/local/bin/fusermount -u /app
    echo "fusermount returned $?"
    sleep 1
    echo "Trying to mount"
    mount /app
    echo "mount returned $?"
    sleep 1
    echo "Trying copy after mount"
    \cp /app/canary.txt /tmp/canary.txt
fi

這個腳本最初是在 中python,關鍵部分只是在 中os.system,但我想從等式中刪除它。它給出了同樣的問題。

答案1

這是我的完整解決方案:

首先,我目視審核了audit.log。為了捕捉正確的事情並且只捕捉正確的事情,我曾經audit2allow創建一個策略並鍵入執行規則。

grep mount /var/log/audit/audit.log | audit2allow -R -M mounts3fs

我 grep for mount 所以我只得到正確的東西。

這創建了一個mounts3fs.ppmounts3fs.te文件。這mounts3fs.te看起來像這樣:

policy_module(mounts3fs, 1.0)

require {
    type file_t;
    type var_t;
    type mount_t;
    type cert_t;
    class dir { write remove_name add_name };
    class file { create unlink link setattr };
}

#============= mount_t ==============
#!!!! The source type 'mount_t' can write to a 'dir' of the following types:
# user_home_t, etc_runtime_t, var_run_t, mount_var_run_t, mount_tmp_t, user_home_dir_t, etc_t, nfs_t, tmpfs_t, tmp_t, var_t

allow mount_t cert_t:dir { write remove_name add_name };
allow mount_t cert_t:file { create unlink };
allow mount_t file_t:dir { remove_name add_name };
allow mount_t file_t:file { unlink link setattr };
allow mount_t var_t:file link;

要安裝該策略,我會執行以下命令:

semodule -i mounts3fs.pp

我發現這對於某些操作來說還不夠,所以我創建了一個額外的策略,如下所示:

module s3fs 1.0;

require {
    type file_t;
    type mount_t;
    class dir create;
    class file create;
}

#============= mount_t ==============

#!!!! This avc is allowed in the current policy
allow mount_t file_t:dir create;
allow mount_t file_t:file create;

selinux還是可以直接下地獄的。

相關內容