From ee238ef656c06cd77e988943e3c4282b5bfd8051 Mon Sep 17 00:00:00 2001 From: Tom Mortensen Date: Wed, 20 Aug 2025 08:26:30 -0700 Subject: [PATCH] chore: refactor file sysem check scripts again fix: re-arrange raidz vdev selections and establish better defaults --- emhttp/plugins/dynamix/DeviceInfo.page | 18 +-- .../dynamix/include/FileSystemStatus.php | 4 +- emhttp/plugins/dynamix/scripts/check.source | 105 +++++++----------- 3 files changed, 51 insertions(+), 76 deletions(-) diff --git a/emhttp/plugins/dynamix/DeviceInfo.page b/emhttp/plugins/dynamix/DeviceInfo.page index fe33e9e3d..7f68f0be2 100644 --- a/emhttp/plugins/dynamix/DeviceInfo.page +++ b/emhttp/plugins/dynamix/DeviceInfo.page @@ -317,7 +317,7 @@ function selectDiskFsWidthZFS(slots,init) { value: 1, text: _(sprintf('%s '+label,slots)) })); - if (selected_width == 0) selected_width = 1; + selected_width = 1; } else if ($('#diskFsProfile').val() == 'mirror') { var width; for (width=2; width<=Math.min(slots,4); width++) { @@ -336,7 +336,7 @@ function selectDiskFsWidthZFS(slots,init) { if ($('#diskFsProfile').val() == 'raidz1') min_width = 2; else if ($('#diskFsProfile').val() == 'raidz2') min_width = 3; else if ($('#diskFsProfile').val() == 'raidz3') min_width = 4; - for (width=min_width; width<=slots; width++) { + for (width=slots; width>=min_width; width--) { if ((slots % width) == 0) { var groups = slots / width; var label = (groups == 1) ? "vdev" : "vdevs"; @@ -344,9 +344,9 @@ function selectDiskFsWidthZFS(slots,init) { value: width, text: _(sprintf('%s '+label+' of %s devices',groups,width)), })); - if (selected_width == 0) selected_width = width; } } + selected_width = slots; } $('#diskFsWidth').val(selected_width); } @@ -565,7 +565,7 @@ function btrfsScrub(path) { } function btrfsCheck(path) { $.post('/webGui/include/FileSystemStatus.php',{cmd:'btrfs-check',path:path},function(data) { - $('#btrfs-check').text(data); + $('#btrfs-check').text(data.replace(/\0$/, '')); if (data.slice(-1)!='\0') { setTimeout(function(){btrfsCheck(path);},1000); } else { @@ -615,7 +615,7 @@ function zfsExpansion(path) { } function reiserfsCheck(path) { $.post('/webGui/include/FileSystemStatus.php',{cmd:'reiserfs-check',path:path},function(data) { - $('#reiserfs-check').text(data); + $('#reiserfs-check').text(data.replace(/\0$/, '')); if (data.slice(-1)!='\0') { setTimeout(function(){reiserfsCheck(path);},1000); } else { @@ -627,7 +627,7 @@ function reiserfsCheck(path) { } function xfsCheck(path) { $.post('/webGui/include/FileSystemStatus.php',{cmd:'xfs-check',path:path},function(data) { - $('#xfs-check').text(data); + $('#xfs-check').text(data.replace(/\0$/, '')); if (data.slice(-1)!='\0') { setTimeout(function(){xfsCheck(path);},1000); } else { @@ -639,7 +639,7 @@ function xfsCheck(path) { } function extCheck(path) { $.post('/webGui/include/FileSystemStatus.php',{cmd:'ext-check',path:path},function(data) { - $('#ext-check').text(data); + $('#ext-check').text(data.replace(/\0$/, '')); if (data.slice(-1)!='\0') { setTimeout(function(){extCheck(path);},1000); } else { @@ -651,7 +651,7 @@ function extCheck(path) { } function ntfsCheck(path) { $.post('/webGui/include/FileSystemStatus.php',{cmd:'ntfs-check',path:path},function(data) { - $('#ntfs-check').text(data); + $('#ntfs-check').text(data.replace(/\0$/, '')); if (data.slice(-1)!='\0') { setTimeout(function(){ntfsCheck(path);},1000); } else { @@ -663,7 +663,7 @@ function ntfsCheck(path) { } function exfatCheck(path) { $.post('/webGui/include/FileSystemStatus.php',{cmd:'exfat-check',path:path},function(data) { - $('#exfat-check').text(data); + $('#exfat-check').text(data.replace(/\0$/, '')); if (data.slice(-1)!='\0') { setTimeout(function(){exfatCheck(path);},1000); } else { diff --git a/emhttp/plugins/dynamix/include/FileSystemStatus.php b/emhttp/plugins/dynamix/include/FileSystemStatus.php index 0b32594d1..29ae6e4f9 100644 --- a/emhttp/plugins/dynamix/include/FileSystemStatus.php +++ b/emhttp/plugins/dynamix/include/FileSystemStatus.php @@ -44,8 +44,8 @@ default: case 'exfat-check': case 'reiserfs-check': $fs = explode('-',$cmd)[0]; - [$dev,$id] = array_pad(explode(' ',$path),2,''); - exec("$docroot/webGui/scripts/{$fs}_check status $dev $id", $status, $retval); + [$dev,$id] = array_pad(explode(' ',trim($path,"'")),2,''); + passthru("$docroot/webGui/scripts/{$fs}_check status $dev $id", $retval); if ($retval != 9) echo "\0"; } } diff --git a/emhttp/plugins/dynamix/scripts/check.source b/emhttp/plugins/dynamix/scripts/check.source index 388825826..074dc92cb 100644 --- a/emhttp/plugins/dynamix/scripts/check.source +++ b/emhttp/plugins/dynamix/scripts/check.source @@ -1,90 +1,65 @@ # source this file in "xxx_check" scripts # variable FSCK should be set to the file sysem check program +# $FSCK start +# $FSCK status +# $FSCK cancel + # Exit codes: -# 0 - No corruption detected or process not found. +# 0 - No corruption detected # N - refer to individual file system check programs -# 8 - No check has been run yet. +# 8 - No check has been run yet or previous check cancelled # 9 - Process is still running. -OUTDIR=/var/lib/check -mkdir -p $OUTDIR +DEV="$2" +ID="$3" +OPTIONS="$4" + +START_CMD="$(basename $0) start $2 $3" +OUTDIR="/var/lib/check" +mkdir -p "$OUTDIR" case "$1" in 'start') # Start the process in the background and log output - rm -f $OUTDIR/check.status.$3 - ( $FSCK $4 $2 &> $OUTDIR/check.status.$3 ; echo $? > $OUTDIR/check.status.$3.exit )& - pid=$! - echo $pid > $OUTDIR/check.pid.$3 + rm -f "${OUTDIR}/check.status.${ID}" + rm -f "${OUTDIR}/check.status.${ID}.exit" + ( + $FSCK $OPTIONS $DEV &> "${OUTDIR}/check.status.${ID}" + echo $? > "${OUTDIR}/check.status.${ID}.exit" + + ) /dev/null & exit 0 ;; 'status') - # Check if the process is still running - if [ -f $OUTDIR/check.pid.$3 ]; then - pid=$(cat $OUTDIR/check.pid.$3) - if kill -0 $pid 2>/dev/null; then - # Process is running, return status and exit code 9 - cat $OUTDIR/check.status.$3 - exit 9 - else - # Process is not running, read the exit status if available - if [ -f $OUTDIR/check.status.$3.exit ]; then - exit_status=$(cat $OUTDIR/check.status.$3.exit) - cat $OUTDIR/check.status.$3 - rm -f $OUTDIR/check.pid.$3 - if [[ $exit_status -eq 0 || $exit_status -eq 1 || $exit_status -eq 2 || $exit_status -eq 4 ]]; then - exit $exit_status - else - exit 0 - fi - else - # Exit status file does not exist, but return status file if available - if [ -f $OUTDIR/check.status.$3 ]; then - cat $OUTDIR/check.status.$3 - fi - exit 8 - fi - fi + # return status file if available + if [[ -f "${OUTDIR}/check.status.${ID}" ]]; then + cat "${OUTDIR}/check.status.${ID}" else - # No PID file found, check for existing status - if [ -f $OUTDIR/check.status.$3 ]; then - cat $OUTDIR/check.status.$3 - # If no exit status file, assume process completed successfully - if [ -f $OUTDIR/check.status.$3.exit ]; then - exit_status=$(cat $OUTDIR/check.status.$3.exit) - if [[ $exit_status -eq 0 || $exit_status -eq 1 || $exit_status -eq 2 || $exit_status -eq 4 ]]; then - exit $exit_status - else - exit 0 - fi - else - exit 8 - fi - else - # No status file found - echo "Not available" - exit 8 + echo "Not available" + fi + # Check if the process is still running + if pgrep -f "$START_CMD" &> /dev/null ; then + # Process is running, return exit code 9 + exit 9 + fi + # Process is not running, read the exit status if available + if [[ -f "${OUTDIR}/check.status.${ID}.exit" ]]; then + exit_status=$(cat "${OUTDIR}/check.status.${ID}.exit") + if [[ $exit_status -lt 8 ]]; then + exit $exit_status fi fi + exit 8 ;; 'cancel') - # Cancel the ntfsfix process - if [ -f $OUTDIR/check.pid.$3 ]; then - pid=$(cat $OUTDIR/check.pid.$3) - kill $pid - while kill -0 $pid 2>/dev/null; do - sleep 1 - done - echo -e "\nCancelled" >> $OUTDIR/check.status.$3 - rm -f $OUTDIR/check.pid.$3 - else - echo "No process to cancel" + # Cancel the process + if pkill -f "$START_CMD" &> /dev/null ; then + echo -e "\nCancelled" >> "${OUTDIR}/check.status.${ID}" fi - - exit 0 + exit 8 ;; *)