You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ublinux-init/ublinux/rc.halt.pre/89-save-clean

94 lines
4.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/usr/bin/env bash
ENABLED=yes
[[ ${ENABLED} == "yes" ]] || exit 0
DEBUGMODE=no
[[ -d /usr/lib/ublinux ]] && { ROOTFS= ; CMD_CHROOT= ; } || { [[ -d /sysroot ]] && ROOTFS="/sysroot" || ROOTFS="."; CMD_CHROOT="chroot ${ROOTFS}"; }
SOURCE=${ROOTFS}/usr/lib/ublinux/functions; [[ -f ${SOURCE} ]] && . ${SOURCE} 2>/dev/null || exit 0
SOURCE=${ROOTFS}/usr/lib/ublinux/default; [[ -f ${SOURCE} ]] && . ${SOURCE} 2>/dev/null || exit 0
debug_mode "$0" "$@"
SYSCONF="${ROOTFS}${SYSCONF}"
SOURCE=${SYSCONF}/config; [[ -f ${SOURCE} ]] && . ${SOURCE} 2>/dev/null
SOURCE=${SYSCONF}/save; [[ -f ${SOURCE} ]] && . ${SOURCE} 2>/dev/null
## Удалить указанные изменения, после очистки параметр удаляется
## SAVE_CLEAN=all|changes,rootcopy,homes
## all # Удалить все изменения из /ublinux-data/changes/ /ublinux-data/rootcopy/ /ublinux-data/homes/
## changes # Удалить только системные изменения /ublinux-data/changes/
## rootcopy # Удалить только /ublinux-data/rootcopy/
## homes # Удалить только /ublinux-data/homes/
## SAVE_CLEAN=changes,rootcopy
exec_save_clean(){
find_path_changes(){
PATH_CHANGES=$(find /memory/layer-base/*/ -maxdepth 1 -type d -name "${NAME_CHANGES}" | head -1)
[[ -n ${PATH_CHANGES} ]] || PATH_CHANGES="$(find /memory/layer-base/*/ -maxdepth 1 -type f -name "ublinux-data*.sgn" | head -1)"
[[ -n ${PATH_CHANGES} ]] && PATH_CHANGES="${PATH_CHANGES%/*}/${NAME_CHANGES}" || return 1
}
find_path_rootcopy(){
PATH_ROOTCOPY=$(find /memory/layer-base/*/ -maxdepth 1 -type d -name "${NAME_ROOTCOPY}" | head -1)
[[ -n ${PATH_ROOTCOPY} ]] || PATH_ROOTCOPY="$(find /memory/layer-base/*/ -maxdepth 1 -type f -name "ublinux-data*.sgn" | head -1)"
[[ -n ${PATH_ROOTCOPY} ]] && PATH_ROOTCOPY="${PATH_ROOTCOPY%/*}/${NAME_ROOTCOPY}" || return 1
}
find_path_homes(){
PATH_HOMES=$(find /memory/layer-base/*/ -maxdepth 1 -type d -name "${NAME_HOMES}" | head -1)
[[ -n ${PATH_HOMES} ]] || PATH_HOMES="$(find /memory/layer-base/*/ -maxdepth 1 -type f -name "ublinux-data*.sgn" | head -1)"
[[ -n ${PATH_HOMES} ]] && PATH_HOMES="${PATH_HOMES%/*}/${NAME_HOMES}" || return 1
}
check_clean_changes(){
[[ -n $(ls -A ${PATH_CHANGES} 2>/dev/null) ]] && NEED_REBOOT=yes || return 1
}
check_clean_rootcopy(){
[[ -n $(ls -A ${PATH_ROOTCOPY} 2>/dev/null) ]] && NEED_REBOOT=yes || return 1
}
check_clean_homes(){
local STATUS=
find ${PATH_HOMES} -maxdepth 2 -type d -name ".config" | while read -r PATH_USER_HOME; do
[[ -n $(ls -A ${PATH_USER_HOME%/*} 2>/dev/null) ]] && NEED_REBOOT=yes && STATUS=1
done
[[ -z ${STATUS} ]] || return 1
}
clean_changes(){
find ${PATH_CHANGES}/{..?*,.[!.]*,*} -delete 2>/dev/null
}
clean_rootcopy(){
find ${PATH_ROOTCOPY}/{..?*,.[!.]*,*} -delete 2>/dev/null
}
clean_homes(){
find ${PATH_HOMES} -maxdepth 2 -type d -name ".config" | while read -r PATH_USER_HOME; do
find ${PATH_USER_HOME%/*} -delete 2>/dev/null
done
}
# Если задан парметр, то использовать как значение
[[ -n $@ ]] && SAVE_CLEAN=$@
if [[ -n ${SAVE_CLEAN} ]]; then
local NAME_CHANGES="changes"
local NAME_ROOTCOPY="rootcopy"
local NAME_HOMES="homes"
local NEED_REBOOT=
if [[ ${SAVE_CLEAN} =~ 'all' ]]; then
find_path_changes && check_clean_changes && clean_changes
find_path_rootcopy && check_clean_rootcopy && clean_rootcopy
find_path_homes && check_clean_homes && clean_homes
else
[[ ${SAVE_CLEAN} =~ 'changes' ]] && find_path_changes && check_clean_changes && clean_changes
[[ ${SAVE_CLEAN} =~ 'rootcopy' ]] && find_path_rootcopy && check_clean_rootcopy && clean_rootcopy
[[ ${SAVE_CLEAN} =~ 'homes' ]] && find_path_homes && check_clean_homes && clean_homes
fi
${CMD_CHROOT} /usr/bin/ubconfig --quiet --target global remove [save] SAVE_CLEAN
sync; sync; sync; echo 3 > /proc/sys/vm/drop_caches
[[ -n ${NEED_REBOOT} ]] || return 1
fi
}
################
##### MAIN #####
################
# Если файл подключен как ресурс с функциями, то выйти
return 0 2>/dev/null && return 0
exec_save_clean $@