Skip to content

Commit 0dd8aae

Browse files
Merge pull request #165 from ExtremeFiretop/dev
Dev 1.0.8 Targeted as Next Stable Release
2 parents fddb4d1 + 082058e commit 0dd8aae

File tree

3 files changed

+127
-78
lines changed

3 files changed

+127
-78
lines changed

MerlinAU.sh

Lines changed: 119 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
#
55
# Original Creation Date: 2023-Oct-01 by @ExtremeFiretop.
66
# Official Co-Author: @Martinski W. - Date: 2023-Nov-01
7-
# Last Modified: 2024-Mar-03
7+
# Last Modified: 2024-Mar-16
88
###################################################################
99
set -u
1010

11-
readonly SCRIPT_VERSION=1.0.7
11+
readonly SCRIPT_VERSION=1.0.8
1212
readonly SCRIPT_NAME="MerlinAU"
1313

1414
##-------------------------------------##
@@ -77,8 +77,8 @@ cronCmd="$(which crontab) -l"
7777
##----------------------------------------------##
7878
inMenuMode=true
7979
isInteractive=false
80-
mainMenuReturnPromptStr="Press Enter to return to the Main Menu..."
81-
advnMenuReturnPromptStr="Press Enter to return to the Advanced Menu..."
80+
mainMenuReturnPromptStr="Press <Enter> to return to the Main Menu..."
81+
advnMenuReturnPromptStr="Press <Enter> to return to the Advanced Menu..."
8282

8383
[ -t 0 ] && ! tty | grep -qwi "NOT" && isInteractive=true
8484

@@ -132,7 +132,7 @@ _WaitForEnterKey_()
132132

133133
if [ $# -gt 0 ] && [ -n "$1" ]
134134
then promptStr="$1"
135-
else promptStr="Press Enter to continue..."
135+
else promptStr="Press <Enter> to continue..."
136136
fi
137137

138138
printf "\n$promptStr"
@@ -579,7 +579,7 @@ _SCRIPTUPDATE_()
579579
}
580580

581581
##----------------------------------------##
582-
## Modified by Martinski W. [2024-Jan-27] ##
582+
## Modified by Martinski W. [2024-Mar-14] ##
583583
##----------------------------------------##
584584
#-------------------------------------------------------------#
585585
# Since a list of current mount points can have a different
@@ -593,7 +593,7 @@ _ValidateUSBMountPoint_()
593593
{
594594
if [ $# -eq 0 ] || [ -z "$1" ] ; then return 1 ; fi
595595

596-
local mounPointPaths expectedPath
596+
local mounPointPaths expectedPath mountPointList
597597
local symblPath realPath1 realPath2 foundPathOK
598598
local mountPointRegExp="^/dev/sd.* /tmp/mnt/.*"
599599

@@ -611,13 +611,21 @@ _ValidateUSBMountPoint_()
611611
expectedPath="$(/usr/bin/dirname "$realPath1")"
612612
fi
613613

614+
mountPointList=""
614615
foundPathOK=false
616+
615617
for thePATH in $mounPointPaths
616618
do
617619
if echo "${expectedPath}/" | grep -qE "^${thePATH}/"
618620
then foundPathOK=true ; break ; fi
621+
mountPointList="$mountPointList $thePATH"
619622
done
620-
"$foundPathOK" && return 0 || return 1
623+
"$foundPathOK" && return 0
624+
625+
## Report found Mount Points on failure ##
626+
if [ $# -gt 1 ] && [ "$2" -eq 1 ] && [ -n "$mountPointList" ]
627+
then Say "Mount points found:$mountPointList" ; fi
628+
return 1
621629
}
622630

623631
##----------------------------------------##
@@ -1803,35 +1811,76 @@ _DoCleanUp_()
18031811
}
18041812

18051813
##----------------------------------------##
1806-
## Modified by Martinski W. [2024-Jan-06] ##
1814+
## Modified by Martinski W. [2024-Mar-16] ##
18071815
##----------------------------------------##
1808-
check_memory_and_prompt_reboot() {
1816+
check_memory_and_prompt_reboot()
1817+
{
18091818
local required_space_kb="$1"
18101819
local availableRAM_kb="$2"
18111820

1812-
if [ "$availableRAM_kb" -lt "$required_space_kb" ]; then
1821+
if [ "$availableRAM_kb" -lt "$required_space_kb" ]
1822+
then
18131823
Say "Insufficient RAM available."
18141824

18151825
# Attempt to clear PageCache #
18161826
Say "Attempting to free up memory..."
18171827
sync; echo 1 > /proc/sys/vm/drop_caches
1828+
sleep 2
18181829

18191830
# Check available memory again #
1820-
availableRAM_kb=$(_GetAvailableRAM_KB_)
1821-
if [ "$availableRAM_kb" -lt "$required_space_kb" ]; then
1822-
# In an interactive shell session, ask user to confirm reboot #
1823-
if "$isInteractive" && _WaitForYESorNO_ "Reboot router now"
1831+
availableRAM_kb="$(_GetAvailableRAM_KB_)"
1832+
if [ "$availableRAM_kb" -lt "$required_space_kb" ]
1833+
then
1834+
freeRAM_kb="$(get_free_ram)"
1835+
Say "Required RAM: ${required_space_kb} KB - RAM Free: ${freeRAM_kb} KB - RAM Available: ${availableRAM_kb} KB"
1836+
1837+
# Attempt to clear dentries and inodes. #
1838+
Say "Attempting to free up memory again more aggressively..."
1839+
sync; echo 2 > /proc/sys/vm/drop_caches
1840+
sleep 2
1841+
1842+
# Check available memory again #
1843+
availableRAM_kb="$(_GetAvailableRAM_KB_)"
1844+
if [ "$availableRAM_kb" -lt "$required_space_kb" ]
18241845
then
1825-
_AddPostRebootRunScriptHook_
1826-
Say "Rebooting router..."
1827-
_ReleaseLock_
1828-
/sbin/service reboot
1829-
exit 1 # Although the reboot command should end the script, it's good practice to exit after.
1846+
freeRAM_kb="$(get_free_ram)"
1847+
Say "Required RAM: ${required_space_kb} KB - RAM Free: ${freeRAM_kb} KB - RAM Available: ${availableRAM_kb} KB"
1848+
1849+
# Attempt to clear clears pagecache, dentries, and inodes after shutting down services
1850+
Say "Attempting to free up memory once more even more aggressively..."
1851+
1852+
# Stop Entware services before F/W flash #
1853+
_EntwareServicesHandler_ stop
1854+
1855+
sync; echo 3 > /proc/sys/vm/drop_caches
1856+
sleep 2
1857+
1858+
# Check available memory again #
1859+
availableRAM_kb="$(_GetAvailableRAM_KB_)"
1860+
if [ "$availableRAM_kb" -lt "$required_space_kb" ]
1861+
then
1862+
# In an interactive shell session, ask user to confirm reboot #
1863+
if "$isInteractive" && _WaitForYESorNO_ "Reboot router now"
1864+
then
1865+
_AddPostRebootRunScriptHook_
1866+
Say "Rebooting router..."
1867+
_ReleaseLock_
1868+
/sbin/service reboot
1869+
exit 1 # Although the reboot command should end the script, it's good practice to exit after.
1870+
else
1871+
# Exit script if non-interactive or if user answers NO #
1872+
Say "Insufficient memory to continue. Exiting script."
1873+
# Restart Entware services #
1874+
_EntwareServicesHandler_ start
1875+
1876+
_DoCleanUp_ 1 "$keepZIPfile"
1877+
_DoExit_ 1
1878+
fi
1879+
else
1880+
Say "Successfully freed up memory. Available: ${availableRAM_kb}KB."
1881+
fi
18301882
else
1831-
# Exit script if non-interactive or if user answers NO #
1832-
Say "Insufficient memory to continue. Exiting script."
1833-
_DoCleanUp_ 1 "$keepZIPfile"
1834-
_DoExit_ 1
1883+
Say "Successfully freed up memory. Available: ${availableRAM_kb}KB."
18351884
fi
18361885
else
18371886
Say "Successfully freed up memory. Available: ${availableRAM_kb}KB."
@@ -2812,37 +2861,41 @@ _Toggle_FW_UpdateCheckSetting_()
28122861
_WaitForEnterKey_ "$mainMenuReturnPromptStr"
28132862
}
28142863

2815-
##-------------------------------------##
2816-
## Added by Martinski W. [2024-Jan-25] ##
2817-
##-------------------------------------##
2864+
##----------------------------------------##
2865+
## Modified by Martinski W. [2024-Mar-16] ##
2866+
##----------------------------------------##
28182867
_EntwareServicesHandler_()
28192868
{
28202869
if [ $# -eq 0 ] || [ -z "$1" ] ; then return 1 ; fi
28212870

2822-
local fileCount entwOPT_init entwOPT_unslung actionStr=""
2823-
2824-
entwOPT_init="/opt/etc/init.d"
2825-
entwOPT_unslung="${entwOPT_init}/rc.unslung"
2826-
2827-
if [ ! -x /opt/bin/opkg ] || [ ! -x "$entwOPT_unslung" ]
2828-
then return 0 ; fi ## Entware is not found ##
2829-
2830-
fileCount="$(/usr/bin/find -L "$entwOPT_init" -name "S*" -exec ls -1 {} \; 2>/dev/null | /bin/grep -cE "${entwOPT_init}/S[0-9]+")"
2831-
[ "$fileCount" -eq 0 ] && return 0
2871+
local serviceCnt entwOPT_init entwOPT_unslung actionStr="" divAction=""
28322872

28332873
case "$1" in
2834-
stop) actionStr="Stopping" ;;
2835-
start) actionStr="Restarting" ;;
2874+
stop) actionStr="Stopping" ; divAction="unmount" ;;
2875+
start) actionStr="Restarting" ; divAction="mount" ;;
28362876
*) return 1 ;;
28372877
esac
28382878

2839-
if [ -n "$actionStr" ]
2879+
if [ -f /opt/bin/diversion ]
28402880
then
2841-
"$isInteractive" && \
2842-
printf "\n${actionStr} Entware services... Please wait.\n"
2843-
$entwOPT_unslung "$1" ; sleep 5
2844-
printf "\nDone.\n"
2881+
Say "${actionStr} Diversion service..."
2882+
/opt/bin/diversion "$divAction"
2883+
sleep 1
28452884
fi
2885+
2886+
entwOPT_init="/opt/etc/init.d"
2887+
entwOPT_unslung="${entwOPT_init}/rc.unslung"
2888+
2889+
if [ ! -x /opt/bin/opkg ] || [ ! -x "$entwOPT_unslung" ]
2890+
then return 0 ; fi ## Entware is NOT found ##
2891+
2892+
serviceCnt="$(/usr/bin/find -L "$entwOPT_init" -name "S*" -exec ls -1 {} \; 2>/dev/null | /bin/grep -cE "${entwOPT_init}/S[0-9]+")"
2893+
[ "$serviceCnt" -eq 0 ] && return 0
2894+
2895+
Say "${actionStr} Entware services..."
2896+
"$isInteractive" && printf "\nPlease wait.\n"
2897+
$entwOPT_unslung "$1" ; sleep 5
2898+
"$isInteractive" && printf "\nDone.\n"
28462899
}
28472900

28482901
##----------------------------------------##
@@ -2884,7 +2937,7 @@ Please manually update to version $minimum_supported_version or higher to use th
28842937
# Make a special case when USB drive has Entware installed.
28852938
#---------------------------------------------------------------#
28862939
if echo "$FW_ZIP_BASE_DIR" | grep -qE "^(/tmp/mnt/|/tmp/opt/|/opt/)" && \
2887-
! _ValidateUSBMountPoint_ "$FW_ZIP_BASE_DIR"
2940+
! _ValidateUSBMountPoint_ "$FW_ZIP_BASE_DIR" 1
28882941
then
28892942
Say "Expected directory path $FW_ZIP_BASE_DIR is NOT found."
28902943
Say "${REDct}**ERROR**${NOct}: Required USB storage device is not connected or not mounted correctly."
@@ -2996,7 +3049,8 @@ Please manually update to version $minimum_supported_version or higher to use th
29963049
# Get the required space for the firmware download and extraction
29973050
required_space_kb=$(get_required_space "$release_link")
29983051
if ! _HasRouterMoreThan256MBtotalRAM_ && [ "$required_space_kb" -gt 51200 ]; then
2999-
if ! _ValidateUSBMountPoint_ "$FW_ZIP_BASE_DIR"; then
3052+
if ! _ValidateUSBMountPoint_ "$FW_ZIP_BASE_DIR" 1
3053+
then
30003054
Say "${REDct}**ERROR**${NOct}: A USB drive is required for the F/W update due to limited RAM."
30013055
"$inMenuMode" && _WaitForEnterKey_ "$mainMenuReturnPromptStr"
30023056
return 1
@@ -3135,7 +3189,7 @@ EOT
31353189
then
31363190
# It's not on a USB drive, so it's safe to delete it #
31373191
rm -f "$FW_ZIP_FPATH"
3138-
elif ! _ValidateUSBMountPoint_ "$FW_ZIP_BASE_DIR"
3192+
elif ! _ValidateUSBMountPoint_ "$FW_ZIP_BASE_DIR" 1
31393193
then
31403194
#-------------------------------------------------------------#
31413195
# This should not happen because we already checked for it
@@ -3171,17 +3225,19 @@ EOT
31713225
cd "$FW_BIN_DIR"
31723226

31733227
##----------------------------------------##
3174-
## Modified by Martinski W. [2024-Feb-10] ##
3228+
## Modified by Martinski W. [2024-Mar-16] ##
31753229
##----------------------------------------##
31763230
local checkChangeLogSetting="$(Get_Custom_Setting "CheckChangeLog")"
31773231

3178-
if [ "$checkChangeLogSetting" = "ENABLED" ]; then
3179-
# Files matching the pattern 'Changelog-*.txt' #
3180-
changelog_file="$(/usr/bin/find -L "${FW_BIN_DIR}" -name "Changelog-*.txt" -print | head -n 1)"
3232+
if [ "$checkChangeLogSetting" = "ENABLED" ]
3233+
then
3234+
# Get the correct Changelog filename (Changelog-[386|NG].txt) based on the "build number" #
3235+
changeLogTag="$(echo "$(nvram get buildno)" | grep -qE "^386[.]" && echo "386" || echo "NG")"
3236+
changeLogFile="$(/usr/bin/find -L "${FW_BIN_DIR}" -name "Changelog-${changeLogTag}.txt" -print)"
31813237

3182-
# Check if the log file exists
3183-
if [ ! -f "$changelog_file" ]; then
3184-
Say "Change-log file does not exist at $changelog_file"
3238+
if [ ! -f "$changeLogFile" ]
3239+
then
3240+
Say "Change-log file [$changeLogFile] does NOT exist."
31853241
_DoCleanUp_
31863242
"$inMenuMode" && _WaitForEnterKey_ "$mainMenuReturnPromptStr"
31873243
return 1
@@ -3207,32 +3263,32 @@ EOT
32073263
current_version_regex="$formatted_current_version \([0-9]{1,2}-[A-Za-z]{3}-[0-9]{4}\)"
32083264

32093265
# Check if the current version is present in the changelog
3210-
if ! grep -Eq "$current_version_regex" "$changelog_file"; then
3266+
if ! grep -Eq "$current_version_regex" "$changeLogFile"; then
32113267
Say "Current version not found in change-log. Bypassing change-log verification for this run."
32123268
else
32133269
# Extract log contents between two firmware versions
3214-
changelog_contents="$(awk "/$release_version_regex/,/$current_version_regex/" "$changelog_file")"
3270+
changelog_contents="$(awk "/$release_version_regex/,/$current_version_regex/" "$changeLogFile")"
32153271
# Define high-risk terms as a single string separated by '|'
32163272
high_risk_terms="factory default reset|features are disabled|break backward compatibility|must be manually|strongly recommended"
32173273

32183274
# Search for high-risk terms in the extracted log contents
32193275
if echo "$changelog_contents" | grep -Eiq "$high_risk_terms"; then
32203276
if [ "$inMenuMode" = true ]; then
3221-
printf "\n ${REDct}Warning: Found high-risk phrases in the change-logs.${NOct}"
3277+
printf "\n ${REDct}Warning: Found high-risk phrases in the change-log.${NOct}"
32223278
printf "\n ${REDct}Would you like to continue anyways?${NOct}"
32233279
if ! _WaitForYESorNO_ ; then
32243280
Say "Exiting for change-log review."
32253281
_DoCleanUp_ 1 ; return 1
32263282
fi
32273283
else
3228-
Say "Warning: Found high-risk phrases in the change-logs."
3284+
Say "Warning: Found high-risk phrases in the change-log."
32293285
Say "Please run script interactively to approve the upgrade."
32303286
_SendEMailNotification_ STOP_FW_UPDATE_APPROVAL
32313287
_DoCleanUp_ 1
32323288
_DoExit_ 1
32333289
fi
32343290
else
3235-
Say "No high-risk phrases found in the change-logs."
3291+
Say "No high-risk phrases found in the change-log."
32363292
fi
32373293
fi
32383294
fi
@@ -3319,7 +3375,7 @@ EOT
33193375
fi
33203376

33213377
##----------------------------------------##
3322-
## Modified by Martinski W. [2024-Jan-06] ##
3378+
## Modified by Martinski W. [2024-Mar-16] ##
33233379
##----------------------------------------##
33243380
freeRAM_kb="$(get_free_ram)"
33253381
availableRAM_kb="$(_GetAvailableRAM_KB_)"
@@ -3332,7 +3388,7 @@ EOT
33323388
if "$isInteractive"
33333389
then
33343390
printf "${GRNct}**IMPORTANT**:${NOct}\nThe firmware flash is about to start.\n"
3335-
printf "Press Enter to stop now, or type ${GRNct}Y${NOct} to continue.\n"
3391+
printf "Press <Enter> to stop now, or type ${GRNct}Y${NOct} to continue.\n"
33363392
printf "Once started, the flashing process CANNOT be interrupted.\n"
33373393
if ! _WaitForYESorNO_ "Continue?"
33383394
then
@@ -3353,18 +3409,11 @@ EOT
33533409
# Send last email notification before F/W flash #
33543410
_SendEMailNotification_ START_FW_UPDATE_STATUS
33553411

3356-
# Check if '/opt/bin/diversion' exists #
3357-
if [ -f /opt/bin/diversion ]; then
3358-
# Stop Diversion services before flash #
3359-
Say "Stopping Diversion service..."
3360-
/opt/bin/diversion unmount
3361-
fi
3362-
3363-
# Stop entware services before F/W flash #
3412+
# Stop Entware services before F/W flash #
33643413
_EntwareServicesHandler_ stop
33653414

33663415
##------------------------------------------##
3367-
## Modified by ExtremeFiretop [2024-Feb-28] ##
3416+
## Modified by ExtremeFiretop [2024-Mar-15] ##
33683417
##------------------------------------------##
33693418

33703419
curl_response="$(curl -k "${routerURLstr}/login.cgi" \

0 commit comments

Comments
 (0)