#!/bin/sh # Description: Script to be executed before a liveburner upgrade. # It will mount a safezone to backup the configuration files BEFORE # the installation of the new firmware. EMMC_DEVICE="NOT_SUPPORTED" SAFEZONE_MOUNTPOINT="/tmp/safezeone" KEROS_CONFIGS_ARCHIVE="keros_config.tar.gz" ROOTFSRW_PATH="/" LOGFILE="${SAFEZONE_MOUNTPOINT}/upgrade.log" USER_LOGFILE=".upgrade.log" USER_LOGFILE_ARCHIVE="${SAFEZONE_MOUNTPOINT}/upgrade_logs.tar.gz" # SPF SPF_CONFILE="/user/spf/etc/global_conf.json" SPF2_CONFILE="/user/spf2/etc/config.json" # CPF LORAD_CONFDIR="/user/etc/lorad" LORAFWD_CONFDIR="/user/etc/lorafwd" CPF_CONFIGS_ARCHIVE="cpf_config.tar.gz" # BSCC BSCC_CONFFILE="/user/bscc/var.xml" # WMC WMC_CONFIGS_ARCHIVE="wmc_config.tar.gz" WMC_CONFIGS_CLIENT_OVPN="etc/openvpn/client-openvpn.conf" WMC_CONFIGS_FILES="\ etc/hosts \ ${WMC_CONFIGS_CLIENT_OVPN} \ etc/firewall.d/iptables_openvpn.rules \ " fatal_error() { echo "$1" touch /.need_reboot exit 1 } detect_emmc() { for i in $(seq 0 9); do if [ -b "/dev/mmcblk${i}" ]; then EMMC_DEVICE="mmcblk${i}" # Update RootFS RW path for system without overlayFS [ ${i} -eq 3 ] && ROOTFSRW_PATH="/user/rootfs_rw/" break fi done if [ "${EMMC_DEVICE}" == "NOT_SUPPORTED" ]; then fatal_error "Emmc device not found. Kernel version: ${KERNEL_VERSION}. Please fix." fi } # Backup the upgrade journal file # backup_previous_logs() { if [ -f /user/${USER_LOGFILE} ]; then echo "Saving /user/${USER_LOGFILE}..." tar -cvzf ${USER_LOGFILE_ARCHIVE} -C /user ${USER_LOGFILE} echo "/user/${USER_LOGFILE} saved" fi } # Initialize (format) the safezone and mount it in ${SAFEZONE_MOUNTPOINT} # create_safezone() { echo "Creating safezone" echo 0 > /sys/block/${EMMC_DEVICE}boot0/force_ro mkfs.ext2 -F /dev/${EMMC_DEVICE}boot0 || fatal_error "Error during safezone format" mkdir -p ${SAFEZONE_MOUNTPOINT} mount /dev/${EMMC_DEVICE}boot0 ${SAFEZONE_MOUNTPOINT} || fatal_error "Error during safezone mount" echo "Safezone created and ready to use" } # Show some information before starting the upgrade for the traces/logs. # get_product_description() { echo "Upgrade started at: $(date '+%f')" echo "Installed packages:" cat /.update/opkg/status } # Backup some KerOS configuration files in the safezone. # Only the /etc/network information are saved. # backup_keros_configurations() { echo "Backuping KerOS configurations in safezone..." if [ -f ${ROOTFSRW_PATH}/etc/sysupgrade.conf ]; then echo "Product support sysupgrade, using it" (find $(sed -ne "/^[[:space:]]*$/d; /^#/d; s@^/var/@@; p" \ /etc/sysupgrade.conf /etc/sysupgrade.d/*.conf 2>/dev/null\ ) /etc/sysupgrade.conf /etc/sysupgrade.d/*.conf \ -type f -o -type l 2>/dev/null; \ ) | sort -u > /tmp/keros_conffiles tar -cvzf ${SAFEZONE_MOUNTPOINT}/${KEROS_CONFIGS_ARCHIVE} -C ${ROOTFSRW_PATH} -T /tmp/keros_conffiles else echo "Product does not support sysupgrade, backuping network" tar -cvzf ${SAFEZONE_MOUNTPOINT}/${KEROS_CONFIGS_ARCHIVE} -C ${ROOTFSRW_PATH} etc/network fi echo "Backup finished" } # Backup SPF/CPF configuration filed in safezone, if present. # backup_lora_configurations() { local conf_found=false local cpf_list_files="" # SPF echo "Backuping SPF configurations in safezone..." for file_to_backup in ${SPF_CONFILE} ${SPF2_CONFILE} do if [ -f ${file_to_backup} ]; then echo "File ${file_to_backup} found in system. Backup it in safezone" cp -f ${file_to_backup} ${SAFEZONE_MOUNTPOINT}/ conf_found=true fi done if [ ${conf_found} = true ]; then echo "Backup SPF finished" else echo "No SPF configuration found. Nothing done." fi # CPF echo "Backuping CPF configurations in safezone..." for dir_to_backup in ${LORAD_CONFDIR} ${LORAFWD_CONFDIR} do if [ -d ${dir_to_backup} ]; then echo "Configuration dir ${dir_to_backup} found in system. Backup it in safezone" cpf_list_files="${cpf_list_files} ${dir_to_backup}" fi done if [ ! -z "${cpf_list_files}" ]; then tar -cvzf "${SAFEZONE_MOUNTPOINT}/${CPF_CONFIGS_ARCHIVE}" -C / ${cpf_list_files} echo "Backup CPF finished" else echo "No CPF configuration found. Nothing done." fi } # Backup BSCC configuration file in safezone, if present. # backup_bscc_configurations() { echo "Backuping BSCC configurations in safezone..." if [ -f ${BSCC_CONFFILE} ]; then echo "File ${BSCC_CONFFILE} found in system. Backup it in safezone" cp -f ${BSCC_CONFFILE} ${SAFEZONE_MOUNTPOINT}/ echo "Backup BSCC finished" else echo "No BSCC configuration found. Nothing done." fi } # Backup Prove&Core SecureStorage information # backup_securestorage() { echo "Backuping assets loaded in Prove&Core SecureStorage..." dd if=/dev/${EMMC_DEVICE} bs=512 skip=10240 count=32768 | gzip > ${SAFEZONE_MOUNTPOINT}/pcore_data.img.gz echo "Backup finished" } # Backup WMC configuration files # backup_wmc_configurations() { echo "Backuping WMC configurations in safezone..." if [ -f "${ROOTFSRW_PATH}/${WMC_CONFIGS_CLIENT_OVPN}" ]; then tar -cvzf ${SAFEZONE_MOUNTPOINT}/${WMC_CONFIGS_ARCHIVE} -C ${ROOTFSRW_PATH} ${WMC_CONFIGS_FILES} echo "Backup WMC configurations finished" else echo "No WMC configurations found. Nothing done." fi } #################### # MAIN KEROS PART #################### detect_emmc create_safezone backup_previous_logs echo -e "\n== START of PREINST script ==\n" 2>&1 | tee ${LOGFILE} get_product_description 2>&1 | tee -a ${LOGFILE} # KerOS configuration backup_keros_configurations 2>&1 | tee -a ${LOGFILE} # LoRa configuration backup_lora_configurations 2>&1 | tee -a ${LOGFILE} # BSCC configuration backup_bscc_configurations 2>&1 | tee -a ${LOGFILE} # SecureStorage information backup_securestorage 2>&1 | tee -a ${LOGFILE} # WMC configuration backup_wmc_configurations 2>&1 | tee -a ${LOGFILE} #################### # MAIN CUSTOMER PART #################### # Example: # cp ${ROOTFSRW_PATH}/etc/hosts ${SAFEZONE_MOUNTPOINT}/ echo -e "\n== End of PREINST script ==\n" 2>&1 | tee -a ${LOGFILE} sync