#!/usr/bin/sh

#说明：该脚本清除除了当前正在运行的docker之外的所有lvm以及mount磁盘空间。
#


#修改环境变量
host_name=$( cat /tmp/.efuse_sn )
case "$host_name" in
  E* | OC* | FX*)
      # oe和wky需要执行环境变量
      ulimit -v unlimited;export PATH=/app/system/miner.plugin-dockerd.ipk/bin/:$PATH
      cd /app/system/miner.plugin-deps.ipk/lvm-plugin/;source env.sh;
      cd /
      ;;
  *)
    # 其余case
    # echo "other host."
    ;;
esac

tmp_log_path="/root/tmp_remove.log"


baseon_appid() {
	#清空相应的存储
	echo "111111111" > /root/disk.txt
	> /root/mount.txt
	> /root/umount.txt
	> /root/lvremove.txt
	> /root/aftermount.txt

	docker_list=$( docker ps | grep -v dhcp | grep -v COMMAND)
	if [  -n "$docker_list" ];then
		# 将字符串拆分成多行数组，并获取所有的docker的缓存信息
		echo "$docker_list" | while IFS= read -r line; do
	#		echo "Read line: $line"
			docker_id=$(echo "$line" |awk -F " " '{print $1}')
			if [ -n "$docker_id" ];then
	#			echo " docker id : $docker_id"
				storge_path=$(docker inspect $docker_id | grep -E "storage\"" | grep Source | awk -F "\"" '{print $4}')
				if [ -n "$storge_path" ];then
					echo "$storge_path" >> /root/disk.txt
				fi
			fi
		done
	fi

	# 获取当前mount的磁盘信息
	#lsblk| grep mnt| awk '{print $NF}'|grep -v vg >/root/mount.txt
	df -P| grep storage| awk '{print $NF}'|grep -v vg >/root/mount.txt
	# 获取当前在运行的但是不需要的缓存，umount掉。
	grep -vwf /root/disk.txt /root/mount.txt >/root/umount.txt
	# 进行umount以及lvremove操作
	for disk in $(cat /root/umount.txt)
	do
		if [ -n "$disk" ];then
			# 增加一个检测，针对wky上一些分配的莫名其妙的存储，不进行释放
			tmp_len=$(echo "$disk" | awk -F "task_" '{print $NF}' | awk -F "/" '{print $1}' | awk -F "." '{print $1}')
			if [ "${#tmp_len}" -eq 32 ]; then
				# 根据umount的路径生成lvremove文件
				removepath=$( mount | grep "$disk" | awk -F " " '{print $1}' )
				umount $disk >>"$tmp_log_path"
				sleep 0.5
				lvremove -yf "$removepath"  >>"$tmp_log_path"
				sleep 0.5
				#echo "$disk"
				echo "$removepath"
			fi
		fi
	done

	# 获取当前已经umount掉但是没有删除的lvm
	#lsblk| grep _lv |awk -F " " '{if(NF<7){print $1}}' | awk -F "-" '{printf("%s/%s\n",$(NF-1),$NF)}' >/root/aftermount.txt
	# 为了兼容EA这种内置盘，使用其他方式查询
	lvdisplay | grep Path | awk '{print $NF}' |awk -F "/" '{print $NF}'|grep -v vg >/root/aftermount.txt
	for disk in $(cat /root/aftermount.txt)
	do
		if [ -n "$disk" ];then
			# 增加一个检测，针对wky上一些分配的莫名其妙的存储，不进行释放
			tmp_len=$(echo "$disk" | awk -F "_" '{print $1}' | awk -F "." '{print $1}')
			if [ "${#tmp_len}" -eq 32 ]; then
				# 遍历所有lvm，查询是否已经umount掉
				mountpath=$(df | grep "$disk")
				if [ -z "$mountpath" ];then
					#如果为空则说明已经umount掉了，则直接lvremove
					removepath=$( lvdisplay | grep "$disk" | grep Path | awk '{print $NF}' | awk -F "/" '{printf("/dev/mapper/%s-%s",$(NF-1),$NF)}')
					lvremove -yf "$removepath"  >>"$tmp_log_path"
					sleep 0.5
					#echo "$disk"
					echo "$removepath"
				fi
			fi
		fi
	done
}

init_logfile() {
	# 检测文件是否存在
	if [ -e "$tmp_log_path" ]; then
		# 清空文件内容
		> "$tmp_log_path"
	else
		# 创建新文件
		touch "$tmp_log_path"
#		echo "创建新文件成功"
	fi
}
restart_agent() {
case "$host_name" in
  E*)
        # 重启agent
#        /app/system/miner.plugin-magent.ipk/stop.sh
        ubus call xyipk stop '{"package": "plugin-magent"}' >/dev/null
        sleep 0.5
#       /app/system/miner.plugin-magent.ipk/start.sh
        ubus call xyipk start '{"package": "plugin-magent"}' >/dev/null
      ;;
  XRVD*)
       # 重启agent
#        /xyapp/system/miner.plugin-agent.ipk/stop.sh
        xycall stop plugin-agent >/dev/null
        sleep 0.5
#       /xyapp/system/miner.plugin-agent.ipk/start.sh
        xycall start plugin-agent >/dev/null
      ;;
  *)
    # 其余case
    # echo "other host."
    ;;
esac
}

main() {
	init_logfile
	
	# 输出参数值
#	echo "UID: $para_uid"
#	echo "AppID: $para_appid"

	# 确定使用的过滤字符串
	baseon_appid
	# 重启agent
	restart_agent
}

#脚本入口
main "$@"
