V1.1 重命名为AutoBuild_Tools.sh, 新增 Samba 一键共享/删除挂载点功能
This commit is contained in:
parent
c1c61e9d06
commit
364759e7e4
|
@ -1,42 +1,51 @@
|
|||
#!/bin/bash
|
||||
# https://github.com/Hyy2001X/AutoBuild-Actions
|
||||
# AutoBuild Module by Hyy2001
|
||||
# AutoExpand_Storage(BETA) for Openwrt
|
||||
# AutoBuild_Tools Tools for Openwrt
|
||||
|
||||
Version=V1.1-BETA
|
||||
|
||||
AutoBuild_Tools() {
|
||||
while :
|
||||
do
|
||||
clear
|
||||
echo -e "USB \ Samba 工具箱 ${Version}\n"
|
||||
echo "1.内部空间扩展"
|
||||
echo "2.Samba"
|
||||
echo -e "\nq.退出\n"
|
||||
read -p "请从上方选择一个操作:" Choose
|
||||
case $Choose in
|
||||
q)
|
||||
rm -rf /tmp/AutoExpand /tmp/AutoSamba
|
||||
clear
|
||||
exit
|
||||
;;
|
||||
1)
|
||||
|
||||
AutoExpand_UI
|
||||
;;
|
||||
2)
|
||||
Samba_UI
|
||||
;;
|
||||
esac
|
||||
done
|
||||
}
|
||||
|
||||
Version=V1.0-BETA
|
||||
|
||||
AutoExpand_UI() {
|
||||
uci set fstab.@global[0].auto_mount='0'
|
||||
uci set fstab.@global[0].auto_swap='0'
|
||||
uci commit fstab
|
||||
[ ! -d /tmp/autocheckUSB ] && mkdir -p /tmp/autocheckUSB
|
||||
rm -rf /tmp/autocheckUSB/*
|
||||
touch ${Disk_Processed_List}
|
||||
echo "$(block info)" > ${Block_Info}
|
||||
Check_Disk="$(cat ${Block_Info} | awk -F ':' '/sd/{print $1}')"
|
||||
clear
|
||||
echo -e "Newifi-D2 一键扩展内部空间 ${Version}\n"
|
||||
echo -e "Newifi-D2 一键扩展内部空间\n"
|
||||
USB_Check_Core
|
||||
if [ ! -z "${Check_Disk}" ];then
|
||||
echo "${Check_Disk}" > ${Disk_List}
|
||||
Disk_Number=$(sed -n '$=' ${Disk_List})
|
||||
echo -e "硬盘数量: ${Disk_Number}\n"
|
||||
for Disk_Name in $(cat ${Disk_List})
|
||||
do
|
||||
Mounted_on="$(df -h | grep "${Disk_Name}" | awk '{print $6}')"
|
||||
Disk_Available="$(df -m | grep "${Disk_Name}" | awk '{print $4}')"
|
||||
Disk_Format="$(cat ${Block_Info} | grep "${Disk_Name}" | egrep -o 'TYPE="[a-z].+' | awk -F '["]' '/TYPE/{print $2}')"
|
||||
if [ ! -z "$Mounted_on" ];then
|
||||
echo "${Disk_Name} ${Mounted_on} ${Disk_Format} ${Disk_Available}MB" >> ${Disk_Processed_List}
|
||||
else
|
||||
echo "${Disk_Name} ${Disk_Format}" >> ${Disk_Processed_List}
|
||||
fi
|
||||
done
|
||||
for ((i=1;i<=${Disk_Number};i++));
|
||||
do
|
||||
Disk_info=$(sed -n ${i}p ${Disk_Processed_List})
|
||||
List_Disk ${Disk_info}
|
||||
done
|
||||
echo -e "\nq.退出"
|
||||
echo -e "\nq.返回"
|
||||
echo "r.重新载入列表"
|
||||
else
|
||||
echo "未检测到外接硬盘!"
|
||||
|
@ -47,7 +56,7 @@ AutoExpand_UI() {
|
|||
echo ""
|
||||
case ${Choose} in
|
||||
q)
|
||||
exit
|
||||
break
|
||||
;;
|
||||
r)
|
||||
AutoExpand_UI
|
||||
|
@ -64,20 +73,48 @@ AutoExpand_UI() {
|
|||
esac
|
||||
}
|
||||
|
||||
USB_Check_Core() {
|
||||
block mount
|
||||
rm -rf ${AutoExpend_Tmp}/*
|
||||
echo "$(block info)" > ${Block_Info}
|
||||
Check_Disk="$(cat ${Block_Info} | awk -F ':' '/sd/{print $1}')"
|
||||
if [ ! -z "${Check_Disk}" ];then
|
||||
echo "${Check_Disk}" > ${Disk_List}
|
||||
Disk_Number=$(sed -n '$=' ${Disk_List})
|
||||
for Disk_Name in $(cat ${Disk_List})
|
||||
do
|
||||
Mounted_on="$(df -h | grep "${Disk_Name}" | awk '{print $6}')"
|
||||
Disk_Available="$(df -m | grep "${Disk_Name}" | awk '{print $4}')"
|
||||
Disk_Format="$(cat ${Block_Info} | grep "${Disk_Name}" | egrep -o 'TYPE="[a-z].+' | awk -F '["]' '/TYPE/{print $2}')"
|
||||
touch ${Disk_Processed_List}
|
||||
if [ ! -z "$Mounted_on" ];then
|
||||
echo "${Disk_Name} ${Mounted_on} ${Disk_Format} ${Disk_Available}MB" >> ${Disk_Processed_List}
|
||||
else
|
||||
echo "${Disk_Name} ${Disk_Format}" >> ${Disk_Processed_List}
|
||||
fi
|
||||
done
|
||||
fi
|
||||
}
|
||||
|
||||
AutoExpand_Core() {
|
||||
Choosed_Disk="$(sed -n ${Choose}p ${Disk_Processed_List} | awk '{print $1}')"
|
||||
echo "警告: 本次操作将把硬盘: '${Choosed_Disk}' 格式化为 'ext4' 格式,请做好数据备份工作!"
|
||||
echo "警告: 本次操作将把硬盘: '${Choosed_Disk}' 格式化为 'ext4' 格式,请提前做好数据备份工作!"
|
||||
read -p "是否继续本次操作?[Y/n]:" Choose
|
||||
if [ ${Choose} == Y ] || [ ${Choose} == y ];then
|
||||
sleep 3 && echo ""
|
||||
else
|
||||
sleep 3
|
||||
echo "用户已取消操作."
|
||||
exit
|
||||
break
|
||||
fi
|
||||
if [[ "$(mount)" =~ "${Choosed_Disk}" ]];then
|
||||
if [[ "$(mount)" =~ "${Choosed_Disk}" ]] > /dev/null 2>&1 ;then
|
||||
Choosed_Disk_Mounted="$(mount | grep "${Choosed_Disk}" | awk '{print $3}')"
|
||||
echo "取消挂载: '${Choosed_Disk}' on '${Choosed_Disk_Mounted}' ..."
|
||||
umount ${Choosed_Disk_Mounted} > /dev/null 2>&1
|
||||
umount -l ${Choosed_Disk_Mounted} > /dev/null 2>&1
|
||||
if [ "$(mount)" =~ "${Choosed_Disk_Mounted}" ];then
|
||||
echo "取消挂载: '${Choosed_Disk_Mounted}' 失败 !"
|
||||
exit
|
||||
fi
|
||||
fi
|
||||
if [[ ! "$(opkg list | awk '{print $1}')" =~ "e2fsprogs" ]];then
|
||||
echo "正在安装: 'e2fsprogs',请耐心等待 ..."
|
||||
|
@ -86,11 +123,11 @@ AutoExpand_Core() {
|
|||
fi
|
||||
echo "正在格式化硬盘: '${Choosed_Disk}' 为 'ext4' ..."
|
||||
mkfs.ext4 -F ${Choosed_Disk} > /dev/null 2>&1
|
||||
echo "格式化完成! 挂载 '${Choosed_Disk}' 到 ' /tmp/extroot' ..."
|
||||
echo "格式化完成! 挂载硬盘: '${Choosed_Disk}' 到 ' /tmp/extroot' ..."
|
||||
mkdir -p /tmp/introot && mkdir -p /tmp/extroot
|
||||
mount --bind / /tmp/introot
|
||||
mount ${Choosed_Disk} /tmp/extroot
|
||||
echo "正在备份系统文件,请耐心等待 ..."
|
||||
echo "正在备份系统文件到 硬盘: '${Choosed_Disk}',请耐心等待 ..."
|
||||
tar -C /tmp/introot -cf - . | tar -C /tmp/extroot -xf -
|
||||
echo "取消挂载: '/tmp/introot' '/tmp/extroot' ..."
|
||||
umount /tmp/introot && umount /tmp/extroot
|
||||
|
@ -106,9 +143,10 @@ AutoExpand_Core() {
|
|||
uci set fstab.@mount[${i}].enabled='1' > /dev/null 2>&1
|
||||
done
|
||||
uci commit fstab
|
||||
umount /mnt/bak
|
||||
umount -l /mnt/bak
|
||||
echo "操作结束,外接硬盘: '${Choosed_Disk}' 已挂载到 '/'."
|
||||
exit
|
||||
sleep 3
|
||||
break
|
||||
}
|
||||
|
||||
List_Disk() {
|
||||
|
@ -119,7 +157,71 @@ List_Disk() {
|
|||
fi
|
||||
}
|
||||
|
||||
Disk_List="/tmp/autocheckUSB/disk_list"
|
||||
Block_Info="/tmp/autocheckUSB/block_info"
|
||||
Disk_Processed_List="/tmp/autocheckUSB/disk_processed_list"
|
||||
AutoExpand_UI
|
||||
Samba_UI() {
|
||||
while :
|
||||
do
|
||||
clear
|
||||
echo -e "Samba 工具箱\n"
|
||||
echo "1.删除所有 Samba 挂载点"
|
||||
echo "2.自动共享所有已连接的硬盘"
|
||||
echo -e "\nq.返回\n"
|
||||
read -p "请从上方选择一个操作:" Choose
|
||||
case $Choose in
|
||||
1)
|
||||
Remove_Samba_Settings
|
||||
;;
|
||||
2)
|
||||
Mount_Samba_Devices
|
||||
;;
|
||||
q)
|
||||
break
|
||||
;;
|
||||
esac
|
||||
done
|
||||
}
|
||||
|
||||
Remove_Samba_Settings() {
|
||||
while :
|
||||
do
|
||||
Samba_config="$(grep "sambashare" /etc/config/samba | wc -l)"
|
||||
[ "${Samba_config}" -eq 0 ] && break
|
||||
uci delete samba.@sambashare[0]
|
||||
uci commit
|
||||
done
|
||||
}
|
||||
|
||||
Mount_Samba_Devices() {
|
||||
echo "$(cat /proc/mounts | awk -F ':' '/sd/{print $1}')" > ${Disk_List}
|
||||
Disk_Number=$(sed -n '$=' ${Disk_List})
|
||||
for ((i=1;i<=${Disk_Number};i++));
|
||||
do
|
||||
Disk_Name=$(sed -n ${i}p ${Disk_List} | awk '{print $1}')
|
||||
Disk_Mounted_on=$(sed -n ${i}p ${Disk_List} | awk '{print $2}')
|
||||
Samba_Name=${Disk_Mounted_on#*/mnt/}
|
||||
uci show 2>&1 | grep "sambashare" > ${UCI_Show_List}
|
||||
if [[ ! "$(cat ${UCI_Show_List})" =~ "${Disk_Name}" ]] > /dev/null 2>&1 ;then
|
||||
echo -e "\nconfig sambashare" >> ${Samba_Config_File}
|
||||
echo -e "\toption auto '1'" >> ${Samba_Config_File}
|
||||
echo -e "\toption name '${Samba_Name}'" >> ${Samba_Config_File}
|
||||
echo -e "\toption device '${Disk_Name}'" >> ${Samba_Config_File}
|
||||
echo -e "\toption path '${Disk_Mounted_on}'" >> ${Samba_Config_File}
|
||||
echo -e "\toption read_only 'no'" >> ${Samba_Config_File}
|
||||
echo -e "\toption guest_ok 'yes'" >> ${Samba_Config_File}
|
||||
echo -e "\toption create_mask '0666'" >> ${Samba_Config_File}
|
||||
echo -e "\toption dir_mask '0777'" >> ${Samba_Config_File}
|
||||
fi
|
||||
done
|
||||
/etc/init.d/samba restart
|
||||
}
|
||||
|
||||
AutoExpend_Tmp="/tmp/AutoExpand"
|
||||
Disk_List="${AutoExpend_Tmp}/Disk_List"
|
||||
Block_Info="${AutoExpend_Tmp}/Block_Info"
|
||||
Disk_Processed_List="${AutoExpend_Tmp}/Disk_Processed_List"
|
||||
[ ! -d ${AutoExpend_Tmp} ] && mkdir -p ${AutoExpend_Tmp}
|
||||
Samba_Config_File="/etc/config/samba"
|
||||
Samba_Tmp="/tmp/AutoSamba"
|
||||
Disk_List="${Samba_Tmp}/Disk_List"
|
||||
UCI_Show_List="${Samba_Tmp}/UCI_List"
|
||||
[ ! -d ${Samba_Tmp} ] && mkdir -p ${Samba_Tmp}
|
||||
AutoBuild_Tools
|
Loading…
Reference in New Issue