Index: scripts/decommission.sh =================================================================== diff -u -r49b21798fb65ce44a04ae4e771ef7c3c1c119fa6 -ra7693f04e693294338aa9829d14f4ed7aa99a7a5 --- scripts/decommission.sh (.../decommission.sh) (revision 49b21798fb65ce44a04ae4e771ef7c3c1c119fa6) +++ scripts/decommission.sh (.../decommission.sh) (revision a7693f04e693294338aa9829d14f4ed7aa99a7a5) @@ -1,4 +1,5 @@ #!/bin/sh + ########################################################################### # # Copyright (c) 2021-2023 Diality Inc. - All Rights Reserved. @@ -16,38 +17,139 @@ ############################################################################ # @details -# This file is called by UI Software to remove and reset the device to a decommissioned state +# This file is called by UI Software to decommission the device -LOC_PARTITION="/dev/mmcblk0p7" -LOC_SETTINGS_CONF="/home/root/scripts/settings.conf" #TODO update to reflect real location +if [ $# -eq 0 ]; then + currentFile=$(basename "$0") + echo "Usage: ./$currentFile " + exit 1 +fi + +ERR_FAILED_RM_WIFI_CONF=150 +ERR_FAILED_CLEAR_BT_CACHE=151 +ERR_FAILED_RESET_SETTINGS_CONF=152 +ERR_FAILED_CLEAR_LOGS=153 +ERR_FAILED_RM_SSH=154 +ERR_FAILED_CLEAR_CLOUDSYNC_TOKEN=155 + + +LOC_CLOUDSYNC_TOKEN=%1 +LOC_SETTINGS_CONF="/home/root/.config/Settings/System.conf" LOC_DEFAULT_SETTINGS_CONF="/home/root/scripts/default_settings.conf" #TODO update to reflect real location LOC_WIFI_CONF="/etc/wpa_supplicant/wpa_supplicant-wlan0.conf" -LOC_BLUETOOTH_CONF="/etc/bluetooth/main.conf" +LOC_BLUETOOTH_CACHE_FOLDER="/var/lib/bluetooth/" +LOC_SSH_FOLDER="$HOME/.ssh/" +LOC_LOG_PARENT_FOLDER="/media/sd-card/" +# removes all the files in the directory and subdirectory in a recursive manner +# exits with passed error code if either rm command fails or folder is not empty of files +# %1 - Directory path +# %2 - Error message string/msg +# %3 - Error code to return on exit in case of error +function clearFolderContent() { + if [ -f $1 ]; then + rm -rf "$1/*/*" + fi + + rmCommandReturn=$? + fileCount=$(find $1 -type f | wc -l) + + if [ "$fileCount" -ne 0 ] || [ "$rmCommandReturn" -ne 0 ]; then + echo "Error : $2 ($fileCount)" + exit $3 + fi +} + +# Removes a single file +# exits with passed error code if either rm command fails or if file was not removed +# %1 - path to file +# %2 - Error message string/msg +# %3 - Error code to return on exit in case of error +function removeSingleFile() { + if [ -f $1 ]; then + rm "$1" + fi + if [ -f $1 ]; then + echo "Error: $2" + # the file still exists, exit with error message + exit $3 + fi +} + +# ==== For encrypted partition ==== +ERR_CRYPTSETUP_USAGE=101 +ERR_CRYPTSETUP_PASSWORD=102 + +ERR_CRYPTSETUP_CREATE_MKDIR=121 +ERR_CRYPTSETUP_CREATE_FORMAT=122 +ERR_CRYPTSETUP_CREATE_OPEN=123 +ERR_CRYPTSETUP_CREATE_MKFS=124 +ERR_CRYPTSETUP_CREATE_MOUNT=125 +ERR_CRYPTSETUP_UNMOUNT_UNMOUNT=126 +ERR_CRYPTSETUP_UNMOUNT_CLOSE=127 + +LOC_DEV="/dev/mmcblk0p7" +LOC_DIR="configurations" +LOC_MAP="/dev/mapper/"$LOC_DIR +LOC_VAR="/var/"$LOC_DIR + +function checkPassword() { + if [ "$PASSWORD" == "" ]; then + echo "Error : Setup command missing password argument" + exit $ERR_CRYPTSETUP_PASSWORD + fi +} + +function checkOutput() { + if [ "$3" == "" ]; then + out=` eval "$1" 2>&1` + else + out=`echo $3 | eval "$1" 2>&1` + fi + if [ "$?" -ne 0 ]; then + echo $out + exit $2 + fi +} + +function unmount_luks_partition() +{ + checkOutput "umount $LOC_VAR " $ERR_CRYPTSETUP_UNMOUNT_UNMOUNT + checkOutput "cryptsetup luksClose $LOC_DIR " $ERR_CRYPTSETUP_UNMOUNT_CLOSE +} + +function create_luks_partition() +{ + umount_luks_partition + + checkOutput "mkdir -p $LOC_VAR " $ERR_CRYPTSETUP_CREATE_MKDIR + checkOutput "cryptsetup luksFormat $LOC_DEV " $ERR_CRYPTSETUP_CREATE_FORMAT $PASSWORD + checkOutput "cryptsetup luksOpen $LOC_DEV $LOC_DIR" $ERR_CRYPTSETUP_CREATE_OPEN $PASSWORD + checkOutput "mkfs.ext4 $LOC_MAP " $ERR_CRYPTSETUP_CREATE_MKFS + checkOutput "mount -t ext4 $LOC_MAP $LOC_VAR" $ERR_CRYPTSETUP_CREATE_MOUNT +} + # ------------------------------ Remove Wifi Conf -if [ -f $LOC_WIFI_CONF ]; then - rm "$LOC_WIFI_CONF" - echo "Deleted Wifi Conf" -fi +removeSingleFile $LOC_WIFI_CONF "Failed to remove WiFi conf file" $ERR_FAILED_RM_WIFI_CONF -# ------------------------------ Remove Bluetooth Conf -if [ -f $LOC_BLUETOOTH_CONF ]; then - rm "$LOC_BLUETOOTH_CONF" - echo "Deleted Bluetooth Conf" -fi +# ------------------------------ Remove Bluetooth cache +clearFolderContent $LOC_BLUETOOTH_CACHE_FOLDER "Failed to remove Bluetooth cache" $ERR_FAILED_CLEAR_BT_CACHE # ------------------------------ Reset Settings Conf cp $LOC_DEFAULT_SETTINGS_CONF $LOC_SETTINGS_CONF # ------------------------------ Remove Logs -rm -rf /media/sd-card/*/* +#TODO this will always fail due to the fact UI is not pausing on it's writing of log when we attempt a removal, causing the count to always be >0 +clearFolderContent $LOC_LOG_PARENT_FOLDER "Failed to remove logs" $ERR_FAILED_CLEAR_LOGS -# ------------------------------ Format Former Encrypted Partition +# ------------------------------ Format Encrypted Partition +checkPassword +create_luks_partition -# Assuming the partition is decrypted in advance -#mkfs.ext4 $LOC_PARTITION ## NEED TO Re-eval. since it might ask yes/no...and expect answer..can do force, but dangerous - # ------------------------------ Remove CloudSync Token -# TODO remove the cloudsync token ? Need additional info on how +clearFolderContent $LOC_CLOUDSYNC_TOKEN "Failed to remove CloudSync token" $ERR_FAILED_CLEAR_CLOUDSYNC_TOKEN +# ------------------------------ Remove ssh +clearFolderContent $LOC_SSH_FOLDER "Failed to remove ssh" $ERR_FAILED_RM_SSH + exit 0