From 6b80ce8f5742747de431a069cce80326c2feff96 Mon Sep 17 00:00:00 2001 From: bergware Date: Sat, 18 Mar 2023 15:46:51 +0100 Subject: [PATCH 1/3] TRIM enhancements --- plugins/dynamix/include/update.trim.php | 4 +--- plugins/dynamix/scripts/ssd_trim | 32 ++++++++++++++++++------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/plugins/dynamix/include/update.trim.php b/plugins/dynamix/include/update.trim.php index 2d7235d32..b845e4fa4 100644 --- a/plugins/dynamix/include/update.trim.php +++ b/plugins/dynamix/include/update.trim.php @@ -19,9 +19,7 @@ if ($_POST['mode']>0) { $min = isset($_POST['min']) ? $_POST['min'] : '*'; $dotm = isset($_POST['dotm']) ? $_POST['dotm'] : '*'; $day = isset($_POST['day']) ? $_POST['day'] : '*'; - $cron = "# Generated ssd trim schedule:\n$min $hour $dotm * $day /sbin/fstrim -a -v | logger &> /dev/null\n"; - exec("/usr/sbin/zpool status|grep -Po 'pool: \K.+'",$zfs_pools); - foreach ($zfs_pools as $pool) if ($pool) $cron .= "$min $hour $dotm * $day /usr/sbin/zpool trim $pool 2> /dev/null\n"; + $cron = "# Generated TRIM schedule:\n$min $hour $dotm * $day /usr/local/emhttp/plugins/dynamix/scripts/ssd_trim cron|logger &> /dev/null\n"; } else { $cron = ""; } diff --git a/plugins/dynamix/scripts/ssd_trim b/plugins/dynamix/scripts/ssd_trim index 500856823..4d0630cfa 100755 --- a/plugins/dynamix/scripts/ssd_trim +++ b/plugins/dynamix/scripts/ssd_trim @@ -17,11 +17,32 @@ $docroot = $docroot ?? $_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp'; require_once "$docroot/webGui/include/Wrappers.php"; extract(parse_plugin_cfg('dynamix',true)); +// cron operation +if ($argc==2 && $argv[1]=='cron') { + // trim btrfs + echo shell_exec("fstrim -va 2>/dev/null"); + // trim zfs + exec("zpool status|grep -Po 'pool: \K.+'",$zfs_pools); + foreach ($zfs_pools as $pool) if ($pool) { + exec("zpool trim -w $pool 2>/dev/null"); + echo zfs_info($pool)."\n"; + } + exit(0); +} + // add translations $_SERVER['REQUEST_URI'] = 'settings'; $login_locale = _var($display,'locale'); require_once "$docroot/webGui/include/Translations.php"; +function zfs_info($pool) { + $trim = preg_replace('/(.$)/',' $1',exec("zfs list -Ho used $pool"))."iB"; + $bytes = exec("zfs list -Hpo used $pool"); + exec("zpool status -tP $pool|grep -Po '^\s+\K.+% trimmed'",$devs); + $devs = implode(', ',array_map(function($d){return strtok($d,' ');},$devs)); + return "/mnt/$pool: $trim ($bytes bytes) trimmed on $devs"; +} + function write(...$messages){ $com = curl_init(); curl_setopt_array($com,[ @@ -39,7 +60,7 @@ function write(...$messages){ write(_("TRIM operation started").".\n","\n","\n"); -// trim btrfs pools +// trim btrfs file system exec("findmnt -lnt btrfs|sort -rk2|awk '$1!~\"btrfs\"{print $1,$2}'",$mounts); foreach ($mounts as $mount) { [$target,$source] = explode(' ',$mount); @@ -47,10 +68,9 @@ foreach ($mounts as $mount) { write(exec("fstrim -v $target")." on $source\r","\n"); } -// trim zfs pools +// trim zfs file system exec("zpool status|grep -Po 'pool: \K.+'",$zfs_pools); foreach ($zfs_pools as $pool) if ($pool) { - unset($devs); exec("zpool trim $pool"); while (true) { sleep(1); @@ -58,11 +78,7 @@ foreach ($zfs_pools as $pool) if ($pool) { write("/mnt/$pool: $trim\r"); if (substr($trim,0,3)==100) break; } - $trim = preg_replace('/(.$)/',' $1',exec("zfs list $pool|grep -Pom1 '^$pool\s+\K\S+'"))."iB"; - $bytes = exec("zfs list -p $pool|grep -Pom1 '^$pool\s+\K\d+'"); - exec("zpool status -t $pool|grep -Po '^\s+\K.+100%'",$devs); - $devs = implode(', ',array_map(function($dev){return "/dev/".strtok($dev,' ');},$devs)); - write("/mnt/$pool: $trim ($bytes bytes) trimmed on $devs\r","\n"); + write(zfs_info($pool)."\r","\n"); } write(_("Finished")."\n",'_DONE_',''); ?> From 683e1ba901c4ea633794bb256a60d12bd0226b09 Mon Sep 17 00:00:00 2001 From: bergware Date: Sat, 18 Mar 2023 15:53:01 +0100 Subject: [PATCH 2/3] TRIM enhancements --- plugins/dynamix/scripts/ssd_trim | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/plugins/dynamix/scripts/ssd_trim b/plugins/dynamix/scripts/ssd_trim index 4d0630cfa..87cfb1cef 100755 --- a/plugins/dynamix/scripts/ssd_trim +++ b/plugins/dynamix/scripts/ssd_trim @@ -71,13 +71,8 @@ foreach ($mounts as $mount) { // trim zfs file system exec("zpool status|grep -Po 'pool: \K.+'",$zfs_pools); foreach ($zfs_pools as $pool) if ($pool) { - exec("zpool trim $pool"); - while (true) { - sleep(1); - $trim = exec("zpool status -t $pool|grep -Pom1 '\d+% trimmed'"); - write("/mnt/$pool: $trim\r"); - if (substr($trim,0,3)==100) break; - } + write("/mnt/$pool: ... \r"); + exec("zpool trim -w $pool"); write(zfs_info($pool)."\r","\n"); } write(_("Finished")."\n",'_DONE_',''); From 3a8cc3981b6b9b8be51a984e1f8d811d9d37355c Mon Sep 17 00:00:00 2001 From: bergware Date: Sat, 18 Mar 2023 15:55:54 +0100 Subject: [PATCH 3/3] TRIM enhancements --- plugins/dynamix/scripts/ssd_trim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/dynamix/scripts/ssd_trim b/plugins/dynamix/scripts/ssd_trim index 87cfb1cef..82a4d55c4 100755 --- a/plugins/dynamix/scripts/ssd_trim +++ b/plugins/dynamix/scripts/ssd_trim @@ -65,14 +65,14 @@ exec("findmnt -lnt btrfs|sort -rk2|awk '$1!~\"btrfs\"{print $1,$2}'",$mounts); foreach ($mounts as $mount) { [$target,$source] = explode(' ',$mount); write("$target: ... \r"); - write(exec("fstrim -v $target")." on $source\r","\n"); + write(exec("fstrim -v $target 2>/dev/null")." on $source\r","\n"); } // trim zfs file system exec("zpool status|grep -Po 'pool: \K.+'",$zfs_pools); foreach ($zfs_pools as $pool) if ($pool) { write("/mnt/$pool: ... \r"); - exec("zpool trim -w $pool"); + exec("zpool trim -w $pool 2>/dev/null"); write(zfs_info($pool)."\r","\n"); } write(_("Finished")."\n",'_DONE_','');