外接设备移除会导致文件共享移除,再次插入时需要重新设置外接设备共享(smb,ftp等)
具体描述
设备环境:物理机,局域网
BUG现象:我的打算是当U盘插入的时候就会自动的共享这个U盘文件,这样子我在局域网的时候就能修改或者查看这些文件,然后并执行相关的这个定时备份。但是如果我把这个U盘拔出来去别的地方拷贝了文件之后,想再插入的时候让他自动继续共享,这个U盘里面的文件就失效了。在系统设置里面是可以设置外部设备共享,但是他必须要勾选。勾选了之后,如果移除这个设备之后,这个勾选就失效了。
出现频率:必现
需求
就是像其他用户反馈的一样,首先加入一个支持外接设备插入时备份的功能,第二个是文件共享对外接设备要有一个记忆功能,可以根据他的那个序列号来进行存储,就是如果同一个序列号的同名外接设备拔出了之后又插入的话仍然执行相关的共享的设置
临时解决办法
为了解决这样子的一个问题,我就去寻找了他SMB和FTP的配置文件,也就是相当于他这个移除的时候会把相关的共享设置的配置文件进行修改,而我写一个Python的程序去监控这个文件,当发现修改的时候就马上把它替换回去,因为他的修改只执行一次,只需要我一直监控这样子,配置文件将一直存在共享,应该是能生效的。
/usr/trim/logs/share_service.log:9055:[27 22:45:29.358] [warning] [1397] [IsMountPoint], path: /vol00/SG_Flash not existed
/usr/trim/logs/share_service.log:9056:[27 22:45:29.358] [warning] [1397] [smb] clear path: /vol00/SG_Flash true
/usr/trim/logs/share_service.log:9081:[27 22:45:29.382] [warning] [1397] [IsMountPoint], path: /vol00/SG_Flash not existed
/usr/trim/logs/share_service.log:9082:[27 22:45:29.382] [warning] [1397] [ftp] clear path: /vol00/SG_Flash true
这里可以看出这个文件共享服务会在外部设备移除时移除共享,却不会在外部设备插入时加入共享。
于是有下面的解决办法
smb
在路径/etc/samba/users
中,可以看到
ls
1000.share.conf 1001.share.conf
cat 1001.share.conf
[Photos]
path = /vol1/1001/Photos
browseable = yes
available = yes
writeable = yes
hide special files = yes
hide unreadable = yes
oplocks = yes
[外接存储-SG_Flash]
path = /vol00/SG_Flash
browseable = yes
available = yes
writeable = yes
hide special files = yes
hide unreadable = yes
oplocks = yes
[学习]
path = /vol1/1001/学习
browseable = yes
available = yes
writeable = yes
hide special files = yes
hide unreadable = yes
oplocks = yes
当外部设备移除时,对应用户的conf文件就会删除外接存储的相关配置。
于是我们需要在设置中选择开启了对应协议外接存储的共享后,执行一个python监控程序
sudo vim periodic_check.py
import os
import time
import hashlib
import shutil
import sys
def calculate_md5(file_path):
"""计算文件的 MD5 值"""
if not os.path.exists(file_path):
return None
hasher = hashlib.md5()
with open(file_path, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hasher.update(chunk)
return hasher.hexdigest()
def backup_file(file_path, backup_path):
"""创建备份文件"""
if not os.path.exists(backup_path):
shutil.copy(file_path, backup_path)
print(f"已创建备份文件:{backup_path}")
def replace_with_backup(file_path, backup_path):
"""用备份文件替换原文件,并将备份文件重命名为原文件名"""
if os.path.exists(backup_path):
os.replace(backup_path, file_path) # 用 .bak 覆盖 .conf,并重命名
print(f"文件内容发生变化,用备份文件 {backup_path} 替换了原文件,并将其重命名为 {file_path}。")
else:
print(f"备份文件 {backup_path} 不存在,无法替换原文件。")
def periodic_check(file_path, interval):
"""每隔一段时间检查配置文件"""
# 保存上次的 MD5 值
last_md5 = None
backup_filename = file_path + ".bak"
while True:
print("开始检查配置文件...")
if os.path.exists(file_path):
# 如果没有备份文件,创建备份
backup_file(file_path, backup_filename)
# 计算当前文件的 MD5 值
current_md5 = calculate_md5(file_path)
# 如果 MD5 值不一致(文件被修改),用备份文件替换原文件
if last_md5 is not None and current_md5 != last_md5:
print("检测到文件内容变化...")
replace_with_backup(file_path, backup_filename)
# 更新最后一次的 MD5 值
last_md5 = current_md5
else:
print(f"文件 {file_path} 不存在,等待下次检查。")
print(f"检查完成,等待 {interval} 秒后再次检查...")
time.sleep(interval)
if __name__ == "__main__":
if len(sys.argv) < 2:
print("用法: python periodic_check.py <文件名>")
sys.exit(1)
# 从命令行参数获取文件名
filename = sys.argv[1]
periodic_check(filename, 10)
sudo nohup python3 periodic_check.py 1001.share.conf
这样就可以了。但是如果要恢复系统的,就是网页端的控制的话就需要手动的执行一次重启,或者把这个进程先删掉,删掉了之后呢,把bak文件删掉,把1001.share.conf
文件里面的关于外接存储的删掉。这样再恢复网页程序控制才不容易出问题!
cat 1001.share.conf
[Photos]
path = /vol1/1001/Photos
browseable = yes
available = yes
writeable = yes
hide special files = yes
hide unreadable = yes
oplocks = yes
[学习]
path = /vol1/1001/学习
browseable = yes
available = yes
writeable = yes
hide special files = yes
hide unreadable = yes
oplocks = yes
ftp
这个我还没解决,等我再研究吧。要用它二进制的接口。主要是我怕搞坏
/usr/trim/bin$ ./trim_curlftpfs -h
usage: ./trim_curlftpfs <ftphost> <mountpoint>
CurlFtpFS options:
-o opt,[opt...] ftp options
-v --verbose make libcurl print verbose debug
-h --help print help
-V --version print version
FTP options:
ftpfs_debug print some debugging information
transform_symlinks prepend mountpoint to absolute symlink targets
disable_epsv use PASV, without trying EPSV first (default)
enable_epsv try EPSV before reverting to PASV
skip_pasv_ip skip the IP address for PASV
ftp_port=STR use PORT with address instead of PASV
disable_eprt use PORT, without trying EPRT first
ftp_method [multicwd/singlecwd] Control CWD usage
custom_list=STR Command used to list files. Defaults to "LIST -a"
tcp_nodelay use the TCP_NODELAY option
connect_timeout=N maximum time allowed for connection in seconds
ssl enable SSL/TLS for both control and data connections
ssl_control enable SSL/TLS only for control connection
ssl_try try SSL/TLS first but connect anyway
no_verify_hostname does not verify the hostname (SSL)
no_verify_peer does not verify the peer (SSL)
cert=STR client certificate file (SSL)
cert_type=STR certificate file type (DER/PEM/ENG) (SSL)
key=STR private key file name (SSL)
key_type=STR private key file type (DER/PEM/ENG) (SSL)
pass=STR pass phrase for the private key (SSL)
engine=STR crypto engine to use (SSL)
cacert=STR file with CA certificates to verify the peer (SSL)
capath=STR CA directory to verify peer against (SSL)
ciphers=STR SSL ciphers to use (SSL)
interface=STR specify network interface/address to use
krb4=STR enable krb4 with specified security level
proxy=STR use host:port HTTP proxy
proxytunnel operate through a HTTP proxy tunnel (using CONNECT)
proxy_anyauth pick "any" proxy authentication method
proxy_basic use Basic authentication on the proxy
proxy_digest use Digest authentication on the proxy
proxy_ntlm use NTLM authentication on the proxy
httpproxy use a HTTP proxy (default)
socks4 use a SOCKS4 proxy
socks5 use a SOCKS5 proxy
user=STR set server user and password
proxy_user=STR set proxy user and password
tlsv1 use TLSv1 (SSL)
sslv3 use SSLv3 (SSL)
ipv4 resolve name to IPv4 address
ipv6 resolve name to IPv6 address
utf8 try to transfer file list with utf-8 encoding
codepage=STR set the codepage the server uses
iocharset=STR set the charset used by the client
CurlFtpFS cache options:
cache=yes|no enable/disable cache (default: yes)
cache_timeout=SECS set timeout for stat, dir, link at once
default is 10 seconds
cache_stat_timeout=SECS set stat timeout
cache_dir_timeout=SECS set dir timeout
cache_link_timeout=SECS set link timeout
FUSE options:
-d -o debug enable debug output (implies -f)
-f foreground operation
-s disable multi-threaded operation
-o allow_other allow access to other users
-o allow_root allow access to root
-o auto_unmount auto unmount on process termination
-o nonempty allow mounts over non-empty file/dir
-o default_permissions enable permission checking by kernel
-o fsname=NAME set filesystem name
-o subtype=NAME set filesystem type
-o large_read issue large read requests (2.4 only)
-o max_read=N set maximum size of read requests
-o hard_remove immediate removal (don't hide files)
-o use_ino let filesystem set inode numbers
-o readdir_ino try to fill in d_ino in readdir
-o direct_io use direct I/O
-o kernel_cache cache files in kernel
-o [no]auto_cache enable caching based on modification times (off)
-o umask=M set file permissions (octal)
-o uid=N set file owner
-o gid=N set file group
-o entry_timeout=T cache timeout for names (1.0s)
-o negative_timeout=T cache timeout for deleted names (0.0s)
-o attr_timeout=T cache timeout for attributes (1.0s)
-o ac_attr_timeout=T auto cache timeout for attributes (attr_timeout)
-o noforget never forget cached inodes
-o remember=T remember cached inodes for T seconds (0s)
-o nopath don't supply path if not necessary
-o intr allow requests to be interrupted
-o intr_signal=NUM signal to send on interrupt (10)
-o modules=M1[:M2...] names of modules to push onto filesystem stack
-o max_write=N set maximum size of write requests
-o max_readahead=N set maximum readahead
-o max_background=N set number of maximum background requests
-o congestion_threshold=N set kernel's congestion threshold
-o async_read perform reads asynchronously (default)
-o sync_read perform reads synchronously
-o atomic_o_trunc enable atomic open+truncate support
-o big_writes enable larger than 4kB writes
-o no_remote_lock disable remote file locking
-o no_remote_flock disable remote file locking (BSD)
-o no_remote_posix_lock disable remove file locking (POSIX)
-o [no_]splice_write use splice to write to the fuse device
-o [no_]splice_move move data while splicing to the fuse device
-o [no_]splice_read use splice to read from the fuse device
Module options:
[iconv]
-o from_code=CHARSET original encoding of file names (default: UTF-8)
-o to_code=CHARSET new encoding of the file names (default: UTF-8)
[subdir]
-o subdir=DIR prepend this directory to all paths (mandatory)
-o [no]rellinks transform absolute symlinks to relative
/usr/trim/bin$ ./smbftpd -h
smbftpd (http://www.twbsd.org)
options:
-D Running smbftpd as a daemon
-s file Set the path of smbftpd.conf
-v Print the version of smbftpd
-h Print this help message
这个位置/usr/trim/var/share_service/1001
的文件ftp.conf文件虽然会变更,但是网页端查询是否挂载又不靠它提示,所以改了也没啥用。
ls
ftp.conf nfs.conf smb.conf webdav.conf
cat ftp.conf
{"updated":1735358252014,"encoding":"","mode":2,"items":[[],[]],"extended":{"mounted":[],"appShare":[],"external":["/vol00/SG_Flash"]}}
等我再研究或者官方更新吧。说实话感觉这几个协议的配置文件乱七八糟,有点费解。