发表于 2026年08月05日
armbian安装自动挂载smb记录
- 安装依赖
apt update apt install python3-flask cifs-utils -y - 创建程序
写入
mkdir -p /root/mpd-mount nano /root/mpd-mount/mpd_mount_web.py改完保存:#!/usr/bin/env python3 from flask import Flask,request,redirect,render_template_string import os import json import subprocess import threading import time import shutil BASE="/root/mpd-mount" CONFIG=BASE+"/mount.json" os.makedirs(BASE,exist_ok=True) if not os.path.exists(CONFIG): open(CONFIG,"w").write("[]") app=Flask(__name__) HTML=""" <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>MPD挂载管理</title> <style> body{ background:#111; color:white; font-family:Arial; padding:20px; } .box{ background:#222; padding:20px; margin-bottom:20px; border-radius:12px; } input{ width:100%; padding:10px; margin:5px 0; background:#333; color:white; } button{ width:100%; padding:12px; margin-top:8px; border:0; background:#00897b; color:white; } .red{ background:#c62828; } .blue{ background:#1565c0; } </style> </head> <body> <div class="box"> <h2>🎵 MPD NAS挂载</h2> <form action="/add" method="post"> 名称 <input name="name" value=""> NAS IP <input name="ip" value=""> 共享名 <input name="share" value=""> 子目录 <input name="subdir" value=""> 用户名 <input name="user"> 密码 <input name="password" type="password"> <button> 添加并挂载 </button> </form> </div> <div class="box"> <h3>挂载列表</h3> {% for m in mounts %} <hr> 名称: {{m.name}} <br> 目录: {{m.mount}} <br> 状态: {{m.status}} <form action="/mount/{{m.name}}" method="post"> <button class="blue"> 挂载 </button> </form> <form action="/umount/{{m.name}}" method="post"> <button> 卸载 </button> </form> <form action="/delete/{{m.name}}" method="post"> <button class="red"> 删除挂载+清理数据库 </button> </form> {% endfor %} </div> <div class="box"> <h3>MPD</h3> <form action="/update"> <button> 更新音乐库 </button> </form> <pre> {{info}} </pre> </div> </body> </html> """ def cmd(c): return subprocess.getoutput(c) def load(): with open(CONFIG) as f: return json.load(f) def save(x): with open(CONFIG,"w") as f: json.dump( x, f, indent=2, ensure_ascii=False ) def mount_one(m): os.makedirs( m["mount"], exist_ok=True ) cred=BASE+"/"+m["name"]+".cred" with open(cred,"w") as f: f.write( f"""username={m['user']} password={m['password']} """ ) os.chmod( cred, 600 ) source="//"+m["ip"]+"/"+m["share"] if m.get("subdir"): source+="/"+m["subdir"] cmd( f""" mountpoint -q {m['mount']} || \ mount -t cifs "{source}" "{m['mount']}" \ -o credentials={cred},vers=3.0,uid=mpd,gid=audio,iocharset=utf8,noperm """ ) time.sleep(1) cmd( "mpc update "+m["mount"] ) def umount_one(m): cmd( "umount -lf "+m["mount"] ) @app.route("/") def index(): arr=[] for m in load(): x=m.copy() x["status"]="已挂载" if os.path.ismount(m["mount"]) else "未挂载" arr.append(x) info=cmd( "mount | grep cifs;echo;mpc status" ) return render_template_string( HTML, mounts=arr, info=info ) @app.route("/add",methods=["POST"]) def add(): data=load() m={ "name":request.form["name"], "ip":request.form["ip"], "share":request.form["share"], "subdir":request.form["subdir"], "user":request.form["user"], "password":request.form["password"], "mount":"/media/music/"+request.form["name"] } data.append(m) save(data) mount_one(m) return redirect("/") @app.route("/mount/<name>",methods=["POST"]) def mount(name): for m in load(): if m["name"]==name: mount_one(m) return redirect("/") @app.route("/umount/<name>",methods=["POST"]) def umount(name): for m in load(): if m["name"]==name: umount_one(m) return redirect("/") @app.route("/delete/<name>",methods=["POST"]) def delete(name): old=load() data=[] target=None # 找到需要删除的挂载 for m in old: if m["name"]==name: target=m else: data.append(m) # 先删除配置 # 防止后台 auto_mount 再次挂回来 save(data) if target: # 卸载 umount_one(target) time.sleep(3) # 删除挂载目录 if os.path.exists(target["mount"]): shutil.rmtree( target["mount"], ignore_errors=True ) # 删除凭据 cred=BASE+"/"+name+".cred" if os.path.exists(cred): os.remove(cred) # 清理MPD数据库 cmd( "mpc rescan" ) return redirect("/") @app.route("/update") def update(): cmd("mpc update") return redirect("/") def auto_mount(): time.sleep(10) while True: for m in load(): if not os.path.ismount(m["mount"]): mount_one(m) time.sleep(60) if __name__=="__main__": threading.Thread( target=auto_mount, daemon=True ).start() app.run( host="0.0.0.0", port=9090 )chmod +x /root/mpd-mount/mpd_mount_web.py打开:python3 /root/mpd-mount/mpd_mount_web.py测试没问题 然后建立服务:http://你的IP:9090nano /etc/systemd/system/mpd-mount-web.service启动:[Unit] Description=MPD SMB Mount Web Manager After=network-online.target mpd.service Wants=network-online.target [Service] Type=simple WorkingDirectory=/root/mpd-mount ExecStart=/usr/bin/python3 /root/mpd-mount/mpd_mount_web.py Restart=always RestartSec=5 User=root [Install] WantedBy=multi-user.target查看:systemctl daemon-reload systemctl enable mpd-mount-web systemctl start mpd-mount-web打开:systemctl status mpd-mount-web测试没问题http://你的IP:9090