#!/bin/sh /etc/rc.common

#
# Copyright (C) 2021-2025  sirpdboy  <herboy2008@gmail.com> https://github.com/sirpdboy/partexp

# This is free software, licensed under the Apache License, Version 2.0 .
#

START=99
USE_PROCD=1

EXTRA_COMMANDS="autopart"

LOCK=/var/lock/partexp.lock
LOGD=/var/partexp
LOG=$LOGD/partexp.log
[ -d "$LOGD" ] || mkdir -p $LOGD

limit_log() {
	local logf=$1
	[ ! -f "$logf" ] && return
	local sc=100
	[ -n "$2" ] && sc=$2
	local count=$(grep -c "" $logf)
	if [ $count -gt $sc ];then
		let count=count-$sc
		sed -i "1,$count d" $logf
	fi
}

init_env() {
[ ! -f "$LOG" ] && echo " " > $LOG

}


gen_log()(
	log "---------------自动分区扩展挂载开始执行------------------ "
)

log(){
	echo -e  " $(date +'%Y-%m-%d %H:%M:%S') $*"
}

_uci_get() {
	local ret=$(uci get partexp.global.$1 2>/dev/null)
	echo ${ret:=$2}
}

uci_get() {
    case $# in
        2)
	local ret=$(uci get $1.$2 2>/dev/null)
	echo ${ret}
            ;;
        3)
	local ret=$(uci get $1.$2.$3 2>/dev/null)
	echo ${ret}
            ;;
        4)
	local ret=$(uci get $1.$2.$3 2>/dev/null)
	echo ${ret:=$4}
            ;;
    esac
}

uci_set() {
    case $# in
        3)
	uci set $1.$2=$3 2>/dev/null
	uci commit $1
            ;;
        4)
	uci set $1.$2.$3=$4 2>/dev/null
	uci commit $1
            ;;
    esac
}

uci_add(){
    uci add_list $1.$2.$3=$4 2>/dev/null
    uci commit $1
}

uci_del() {
    case $# in
        2)
            uci delete "$1.$2"
            ;;
        3)
            uci delete "$1.$2.$3"
            ;;
        4)
            uci delete "$1.$2.$3.$4"
            ;;
    esac
}

# 检查硬盘是否已挂载
is_disk_mounted() {
    DISK=$1
    if mount | grep -q "$DISK "; then
        return 0  # 已挂载
    else
        return 1  # 未挂载
    fi
}


mount_device() {
    local DEVICE=$1
    local MOUNT_POINT=$2
    local TYPE=$3
    # 检查设备是否存在
    if [ ! -e "$DEVICE" ]; then
        log "设备 $DEVICE 不存在"
        return 1
    fi

    # 检查挂载点是否存在
    if [ ! -d "$MOUNT_POINT" ]; then
        log "挂载点 $MOUNT_POINT 不存在，正在创建..."
        mkdir -p "$MOUNT_POINT"
        if [ $? -ne 0 ]; then
            log "无法创建挂载点 $MOUNT_POINT"
            return 1
        fi
    fi

    # 检查设备是否已挂载
    if mount | grep -q "$DEVICE"; then
        log "设备 $DEVICE 已挂载到其他位置"
        return 1
    fi

    if mount | grep -q "$MOUNT_POINT"; then
        log "挂载点 $MOUNT_POINT 已被其他设备占用"
        return 1
    fi

    # 挂载设备
    log "正在挂载 $DEVICE 到 $MOUNT_POINT..."
    # mount "$DEVICE" "$MOUNT_POINT"

    mount  $TYPE "$DEVICE" "$MOUNT_POINT" >/dev/null 2> /dev/null
    # 检查挂载是否成功
    if [ $? -eq 0 ]; then
        log "挂载成功: $DEVICE -> $MOUNT_POINT"
        return 0
    else
        log "挂载失败: $DEVICE -> $MOUNT_POINT"
        return 1
    fi
}

# 取消硬盘挂载
umount_disk() {
    DISK=$1
    MOUNT=''
    eval $(block info "$DISK" | grep -o -e "MOUNT=\S*")
    if [ "$MOUNT" ]; then
        umount $DISK 2>/dev/null
        if [ $? -eq 0 ]; then
            log "取消挂载成功:$DISK"
        else
            log "取消挂载失败:$DISK"
        fi
    else
        log "设备/dev/$DISK未挂载"
    fi
}

# 从 block info 中提取指定字段的值
get_block() {
    local DISK=$1
    local TYPE=$2
    local value
    value=`mount | grep $DISK |awk -F $TYPE '{print $2}' |awk '{print $1}' | head -1`
    # value=$(block info "/dev/$DISK" | grep -o -e "$TYPE=\S*" | cut -d\" -f2)
    echo $value
}
	
# 检查是否有共享挂载（如 Samba 或 NFS）
check_shared_mount() {
    DISK=$1
    if [ -f /etc/config/samba ]; then
        SHARED=$(grep -q "/dev/$DISK" /etc/config/samba)
        if [ $? -eq 0 ]; then
            log "检测到 Samba 共享挂载: /dev/$DISK"
            return 0
        fi
    fi
    # 检查是否有 NFS 共享
    if [ -f /etc/exports ]; then
        SHARED=$(grep -q "/dev/$DISK" /etc/exports)
        if [ $? -eq 0 ]; then
            log "检测到 NFS 共享挂载: /dev/$DISK"
            return 0
        fi
    fi
    return 1
}

usamba(){
s=$1
[ -e "/etc/config/$s" ] && {
	msum=$(grep -c "config sambashare" /etc/config/$s)
	for i in $(seq 0 $((msum)));do
		pdev=`uci_get $s @sambashare[$i] path `
		[ "$pdev" = "$2" ] && {
		uci_del $s @sambashare[$i] 
		log "分区/dev/$b被挂载$MOUNT共享使用，删除$s共享成功！"  
		sleep 1
		}
	done
}
    # 取消 Samba 共享
if [ -f /etc/config/$s ]; then
        sed -i "/\/dev\/$b/d" /etc/config/$s
        /etc/init.d/$s restart
fi
    # 取消 NFS 共享
if [ -f /etc/exports ]; then
        sed -i "/\/dev\/$b/d" /etc/exports
        /etc/init.d/nfs restart
fi
}


is_disk_partitioned() {
    PARTITION_COUNT=$(fdisk -l /dev/$1 2>/dev/null | grep -E "^/dev/$2" | wc -l)
    if [[ "$PARTITION_COUNT" -gt 0 ]]; then
        echo 1 
    else
        echo 0 
    fi
}

partednew(){
    DISK=$1
    parted -s /dev/$DISK mklabel gpt
    parted -s /dev/$DISK mkpart primary ext4 1MiB -1
}

fdisknew(){
    echo -e "n\np\n\n\n\nw" | fdisk /dev/$1   >/dev/null 2> /dev/null
}

fdisksave(){
    echo -e "n\w" | fdisk /dev/$1    >/dev/null 2> /dev/null
}

# 格式化磁盘函数 DISK=/dev/sda1 ;TYPE=btrfs
format_disk() {   
    local DISK=$1
    local TYPE=$2
    [ $TYPE == '0' ] && TYPE="ext4"
    log "正在格式化 $DISK "
    mkfs.$TYPE -F "$DISK" >/dev/null 2>/dev/null
    if [ $? -eq 0 ]; then
        log "格式化 $TYPE 成功 $DISK"
        return 0
    else
        log "格式化 $TYPE 失败 $DISK"
        return 1
    fi
}

fdiskB(){
    target_function=$( _uci_get target_function "/opt" )
    format_type=$( _uci_get format_type)
    keep_config=$( _uci_get keep_config "1" )
    uci_set fstab @global[0] anon_mount '0'
    uci_set fstab @global[0] auto_mount '0'
	a=$( _uci_get target_disk "/sda3" )
	local b=$a$1
	log "开始检测目标$a信息"
   	log "检测/dev/$b是否需要分区和格式化$format_type"

	   if [ $target_function = '/opt' ] ;then
		/etc/init.d/dockerd stop   >/dev/null 2> /dev/null
		amount=`mount |grep /opt | awk '{print $1}'`
		if [ -n $amount ] ;then
		        umount $amount   >/dev/null 2> /dev/null
         		log "取消/opt之前的挂载$amount成功！"
		fi
		 for OPT in $(mount |grep /opt | awk '{print $3}');do
		    umount $OPT   >/dev/null 2> /dev/null
		    log "取消/opt之前的挂载$OPT成功！"
		done
	   fi
	[ -e "/etc/config/fstab" ] || block detect > /etc/config/fstab
	[ -d "/mnt/$b" ] || mkdir -p /mnt/$b
	if is_disk_mounted "/dev/$b"; then
 	   log "设备 /dev/$b 已挂载，尝试取消挂载..."

	   	   if check_shared_mount $b; then
	   	      usamba samba4 $MOUNT
	  	      usamba samba $MOUNT
	  	      sleep 5
	           fi
 	           umount_disk "/dev/$b" || umount_disk "/mnt/$b"
	else
  	   log "设备/dev/$b未挂载"
	   isfdisk=0
	   isP=$(is_disk_partitioned $a $b)
	   if [ "$isP" == '0' ] ;then
 		fdisksave $a  
 		fdisknew $a
		sleep 2
		isfdisk=1
	   fi
	   isP=$(is_disk_partitioned $a $b)
	   if [ $isP == '1' -a "$isfdisk" == 1 ] ;then
	       log "分区$b建立成功！"
	   elif  [ $isP == '1'  -a "$isfdisk" = 0 ] ;then
	       log "检测目标分区$b已存在."
	   else
	       log "分区$b建立失败，请检查$b硬盘空间！"
	       expquit 1
	   fi
	   sleep 1
	fi
	if is_disk_mounted "/dev/$b"; then
		umount /dev/$b  >/dev/null 2> /dev/null
		[ $? -ne 0 ] && block umount /dev/$b   >/dev/null 2> /dev/null
	fi
	if [ "$target_function" = "/" -o "$target_function" = "/overlay" ] ; then
	    format_disk "/dev/$b" $format_type
	elif [ "$format_type" != "0" -o "$isfdisk" = "1" ] ; then
	    format_disk "/dev/$b" $format_type
	else
	    log  "设备/dev/$b如果未格式化,可能无法正常使用."
	fi

	
	TYPE='';eval $(blkid  "/dev/$b" | grep -o -e "TYPE=\S*")

	log "检测设备/dev/$b分区$TYPE格式！"

	if [ "$TYPE" = "ntfs" ];then
		if  [ `which ntfs-3g ` ]  ;then
		   if  is_disk_mounted "/mnt/$b" ;then
		      mount_device /dev/$b /mnt/$b "-t ntfs-3g"
		   fi
	       else 
		   if  is_disk_mounted "/mnt/$b" ;then
		      mount_device /dev/$b /mnt/$b "-t ntfs3"
		   fi
	       fi
	else
	   mount /dev/$b  /mnt/$b  >/dev/null 2> /dev/null
	fi
	UUID='';eval $(block info /dev/$b | grep -o -e "UUID=\S*")
	if [ ! "$UUID" ] ; then 
	     log "获取/dev/$b设备UUID信息失败！"
	     expquit 1
	else
	     log "获取/dev/$b设备UUID信息:$UUID成功" 
	fi

        case "$target_function" in

	"/overlay") 
	 	if [ "$keep_config" = "1" ] ; then
			    # cp -a -f /overlay/*  /mnt/$b/  || cp -a -f /rom/overlay/*  /mnt/$b/
			    tar -C /overlay -cvf - . | tar -C /mnt/$b/ -xf -  || tar -C /rom/overlay -cvf - . | tar -C /mnt/$b/ -xf - 
			    umount /dev/$b  >/dev/null 2> /dev/null 
			    [ $? -ne 0 ] && umount /mnt/$b  >/dev/null 2> /dev/null 
			    [ $? -ne 0 ] && block umount /dev/$b   >/dev/null 2> /dev/null 
			    block detect > /etc/config/fstab
			    OVERLAY=`uci -q get fstab.@mount[0].target `
			    if [ "$OVERLAY" == "/overlay" -o "$OVERLAY" == "/dev/loop0" ] ;then
			         uci -q set fstab.@mount[0].uuid="${UUID}"
			         uci -q set fstab.@mount[0].target='/overlay'
			         uci -q set fstab.@mount[0].enabled='0'
			    fi
			    msum=$(grep -c "'mount'" /etc/config/fstab)
			    for i in $(seq 0 $((msum-1)))
			    do
				zuuid=`uci -q get fstab.@mount[$i].uuid `
				[ $? -ne 0 ] && break
				if [ "$zuuid" = "$UUID" ] ; then
				    uci -q set fstab.@mount[$i].target="/overlay"
				    uci -q set fstab.@mount[$i].enabled='1'
			    	 fi
			    done
			    uci set fstab.@global[0].delay_root="15"
			    uci commit fstab
			    log "保留数据overlay扩展/dev/$b成功！"
			    sleep 3
			    log "设备重启才能生效"
			    expquit 2
		else
			    umount /dev/$b  >/dev/null 2> /dev/null 
			    [ $? -ne 0 ] && umount /mnt/$b  >/dev/null 2> /dev/null 
			    [ $? -ne 0 ] && block umount /dev/$b   >/dev/null 2> /dev/null 
			    block detect > /etc/config/fstab
			    OVERLAY=`uci -q get fstab.@mount[0].target `
			    if [ "$OVERLAY" == "/overlay" -o "$OVERLAY" == "/dev/loop0" ] ;then
			         uci -q set fstab.@mount[0].uuid="${UUID}"
			         uci -q set fstab.@mount[0].target='/overlay'
			         uci -q set fstab.@mount[0].enabled='0'
			    fi
			    msum=$(grep -c "'mount'" /etc/config/fstab)
			    for i in $(seq 0 $((msum-1)))
			    do
				zuuid=`uci -q get fstab.@mount[$i].uuid `
				[ $? -ne 0 ] && break
				if [ "$zuuid" = "$UUID" ] ; then
				    uci -q set fstab.@mount[$i].target="/overlay"
				    uci -q set fstab.@mount[$i].enabled='1'
			    	 fi
			    done
			    uci set fstab.@global[0].delay_root="15"
			    uci commit fstab
			    log "不保留数据overlay扩展/dev/$b成功！"
			    sleep 3
			    log "设备重启才能生效"
			    expquit 2
		fi

	;;
	"/opt")  
		umount /dev/$b  >/dev/null 2> /dev/null 
		[ $? -ne 0 ] && umount /mnt/$b  >/dev/null 2> /dev/null 
		[ $? -ne 0 ] && block umount /dev/$b   >/dev/null 2> /dev/null 

		block detect > /etc/config/fstab
		mkdir -p $target_function
		msum=$(grep -c "'mount'" /etc/config/fstab)
		mount_device /dev/$b "$target_function"
		for i in $(seq 0 $((msum-1)))
		do
			zuuid=`uci -q get fstab.@mount[$i].uuid `
			[ $? -ne 0 ] && break
			if [ "$zuuid" = "$UUID" ] ; then
			    uci -q set fstab.@mount[$i].target="$target_function"
			    uci -q set fstab.@mount[$i].enabled='1'
			 fi
		done
		uci commit fstab
		#  ln -sf /mnt/$b /overlay
		if is_disk_mounted "/opt"; then
		    log "/dev/$b分区扩容和挂载到$target_function成功！" 
		    log "如果没生效，请重启设备"
		    expquit 2
		else
		    log "/dev/$b分区扩容和挂载到$target_function失败！" 
		fi
	;;
	"/") 
	    if [ "$keep_config" = "1" ] ; then
		mkdir -p /tmp/introot
		mount --bind / /tmp/introot
		tar -C /tmp/introot -cvf - . | tar -C /mnt/$b -xf -
		umount /tmp/introot || block umount /tmp/introot
		umount /dev/$b  >/dev/null 2> /dev/null 
		[ $? -ne 0 ] && umount /mnt/$b  >/dev/null 2> /dev/null 
		[ $? -ne 0 ] && block umount /dev/$b   >/dev/null 2> /dev/null 

		block detect > /etc/config/fstab
		OVERLAY=`uci -q get fstab.@mount[0].target `
		if [ "$OVERLAY" == "/overlay" -o "$OVERLAY" == "/dev/loop0" ] ;then
			         uci -q set fstab.@mount[0].uuid="${UUID}"
			         uci -q set fstab.@mount[0].target='/'
			         uci -q set fstab.@mount[0].enabled='1'
		fi
		log "保留数据根目录扩展/dev/$b成功！"
		sleep 3
		log "设备重启才能生效"
		expquit 2
	   else
		mkdir -p /tmp/introot
		mount --bind / /tmp/introot
		umount /tmp/introot || block umount /tmp/introot
		umount /dev/$b  >/dev/null 2> /dev/null 
		[ $? -ne 0 ] && umount /mnt/$b  >/dev/null 2> /dev/null 
		[ $? -ne 0 ] && block umount /dev/$b   >/dev/null 2> /dev/null 
		block detect > /etc/config/fstab
		OVERLAY=`uci -q get fstab.@mount[0].target `
		if [ "$OVERLAY" == "/overlay" -o "$OVERLAY" == "/dev/loop0" ] ;then
			         uci -q set fstab.@mount[0].uuid="${UUID}"
			         uci -q set fstab.@mount[0].target='/'
			         uci -q set fstab.@mount[0].enabled='1'
		fi
		log "保留数据根目录扩展/dev/$b成功！"
		sleep 3
		log "设备重启才能生效"
		expquit 2
	   fi
	;;
	*) 
		umount /dev/$b  >/dev/null 2> /dev/null 
		[ $? -ne 0 ] && umount /mnt/$b  >/dev/null 2> /dev/null 
		[ $? -ne 0 ] && block umount /dev/$b   >/dev/null 2> /dev/null 
		block detect > /etc/config/fstab
		mkdir -p $target_function
		msum=$(grep -c "'mount'" /etc/config/fstab)
		mount_device /dev/$b /mnt/$b
		for i in $(seq 0 $((msum-1)))
		do
			zuuid=`uci -q get fstab.@mount[$i].uuid `
			[ $? -ne 0 ] && break
			if [ "$zuuid" = "$UUID" ] ; then
			    uci -q set fstab.@mount[$i].target="/mnt/$b"
			    uci -q set fstab.@mount[$i].enabled='1'
			 fi
		done
		uci commit fstab
		if is_disk_mounted /mnt/$b ; then
		    log "/dev/$b分区扩容和挂载到/mnt/$b成功！" 
		    log "如果没生效，请重启设备"
		    expquit 2
		else

		    log "/dev/$b分区扩容和挂载到/mnt/$b失败！" 
		fi
	    ;;
    esac

}

get_system_disk() {
     SYSTEM_DISK=$( df -h | grep boot |awk -F ' ' '{print $1}' |head -1)
    [ -z ${SYSTEM_DISK} ]  &&  SYSTEM_DISK=$(mount | grep 'on /overlay' | awk '{print $1}' | sed 's/[0-9]*$//' |head -1)
    echo "$SYSTEM_DISK"
}

get_all_disks() {
    DISKS=`find /dev -regex '.*/\(sd[a-z]\|mmcblk[0-9]\+\|sata[a-z]\|nvme[0-9]\+n[0-9]\+\|vd[a-z]\)$'`
    echo "$DISKS"
}

check_free_space() {
    DISK=$1 
    PARTED_OUTPUT=$(echo i | parted /dev/$DISK unit GB print free 2>/dev/null)
    FREE_SPACE=$(echo "$PARTED_OUTPUT" | grep "Free Space" | awk '{print $3}' )
    echo $FREE_SPACE |awk -F '.' '{print $1}' | sed 's/[A-Za-z]//g'

}

# 获取下一个新的可用的分区号
get_next_partition_number() {
    DISK=$1
    PARTITIONS=$(fdisk -l /dev/$DISK 2>/dev/null | grep -v boot  | grep -E "^/dev/$DISK" | awk '{print $1}' | sed 's/\/dev\/[a-z]*//g' | awk -F '[^0-9]+' '{print $NF}')
    MAX_PARTITION=$(echo "$PARTITIONS" | sort -n | tail -n 1)
    NEXT_PARTITION=$(awk -v n="$MAX_PARTITION" 'BEGIN { print n + 1 }')
    #NEXT_PARTITION=$((MAX_PARTITION + 1))
    echo "$NEXT_PARTITION"
}

# 获取最后一个可用的分区号
get_last_partition_number() {
    DISK=$1
    PARTITIONS=$(fdisk -l /dev/$DISK 2>/dev/null | grep -v boot  | grep -E "^/dev/$DISK" | awk '{print $1}' | sed 's/\/dev\/[a-z]*//g' | awk -F '[^0-9]+' '{print $NF}')
    MAX_PARTITION=$(echo "$PARTITIONS" | sort -n | tail -n 1)
    echo "$MAX_PARTITION"
}


# 获取硬盘分区数
get_partition_number() {
    DISK=$1
    PARTITIONS=$(fdisk -l /dev/$DISK 2>/dev/null | grep -v boot  | grep -E "^/dev/$DISK" | awk '{print $1}' | sed 's/\/dev\/[a-z]*//g' | wc -l)
    echo "$PARTITIONS"
}

get_config() {
	config_get target_function $1 target_function 1
	config_get target_disk $1 target_disk 1
	config_get_bool keep_config $1 keep_config 1
	config_get format_type $1 format_type
}

autopart() {
    #config_load partexp
    #config_foreach get_config global
    touch $LOCK
    init_env
    gen_log
    target_function=$( _uci_get target_function "/opt" )
    format_type=$( _uci_get format_type)
    DISK=$( _uci_get target_disk "/sda3" )
    keep_config=$( _uci_get keep_config "1" )
    NEXTPART=1  #目标分区
    DISKSALL=$(get_all_disks)  #硬盘名列表
    DISK_COUNT=$(echo "$DISKSALL" | wc -l)  #所有硬盘数量
    log "系统中检测到的硬盘数量: $DISK_COUNT"
    log "硬盘信息列表:" $DISKSALL
    SYSTEM_DISK=$(get_system_disk)
    log "系统盘: "$SYSTEM_DISK
    if [[ $SYSTEM_DISK =~ ^/dev/$DISK ]] ;then
        fdisksave /dev/$DISK
        log "此次执行操作功能：$target_function ，目标盘是系统盘：/dev/$DISK"
	PARTITIONSUM=$(get_partition_number $DISK)
	log "目标盘 $DISK 一共有分区数: $PARTITIONSUM个"
	if  [[ "$PARTITIONSUM" -gt 3 ]];then
	         FREE_SPACE=$(check_free_space $(basename $DISK))
	         log "目标盘 $DISK 有剩余空间: $FREE_SPACE Gb"
	         if [[ "$FREE_SPACE" -gt 2 ]]; then
		        NEXTPART=$(get_next_partition_number $DISK)
	         else
		        NEXTPART=$(get_last_partition_number  $DISK)
		 fi
	else 
		 FREE_SPACE=$(check_free_space $(basename $DISK))
	         log "目标盘 $DISK 有剩余空间: $FREE_SPACE Gb"
	         if [[ "$FREE_SPACE" -gt 2 ]]; then
		        NEXTPART=$(get_next_partition_number $DISK)
		 else

		        log "目标盘 $SYSTEM_DISK 没有足够的剩余空间！"
			expquit 1
		 fi
	fi
    else
        log "此次执行操作功能：$target_function ，目标盘是：/dev/$DISK"
	PARTITIONSUM=$(get_partition_number $DISK)
	log "目标盘 $DISK 一共有分区数: $PARTITIONSUM个"
	if  [[ "$PARTITIONSUM" -gt 1 ]];then
	         FREE_SPACE=$(check_free_space $(basename $DISK))
	         log "目标盘 $DISK 有剩余空间: $FREE_SPACE Gb"
		 
	         [[ $FREE_SPACE -gt 2 ]] && NEXTPART=$(get_next_partition_number $DISK) || NEXTPART=$(get_last_partition_number  $DISK)
	else 
		 NEXTPART=1
	fi
    fi
    log  "定位到操作目标设备分区:/dev/$DISK$NEXTPART"
    case "$DISK" in
    vd*) fdiskB  $NEXTPART;;
    sd*) fdiskB  $NEXTPART;;
    nvme*)  fdiskB  p$NEXTPART;;
    mmc*)  fdiskB  p$NEXTPART;;
    *)
	    log "目标设备/dev/$DISK暂不支持！请联系作者sirpdboy！"
	    ;;
    esac

   expquit 1
}

start() {

    [ -f $LOCK ] && exit
    [ x$xBOOT = x1 ] || autopart
    rm -f $LOCK 2>/dev/null

}

boot() {
xBOOT=1
}

expquit() {
    rm -f $LOCK
    uci_set fstab @global[0] anon_mount '1'
    uci_set fstab @global[0] auto_mount '1'
    [ -e "/etc/config/dockerd" ] && /etc/init.d/dockerd restart   >/dev/null 2> /dev/null 
    sleep 2
    [ $1 == 2 ] && log "重启中...\n" &&reboot 
    exit $1
}

