Merge pull request #1288 from bergware/master

TRIM enhancements
This commit is contained in:
tom mortensen
2023-03-18 08:14:00 -07:00
committed by GitHub
2 changed files with 28 additions and 19 deletions

View File

@@ -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 = "";
}

View File

@@ -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,30 +60,20 @@ 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);
write("$target: ... <i class='fa fa-spin fa-circle-o-notch'></i>\r");
write(exec("fstrim -v $target")." on $source\r","\n");
write(exec("fstrim -v $target 2>/dev/null")." 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);
$trim = exec("zpool status -t $pool|grep -Pom1 '\d+% trimmed'");
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("/mnt/$pool: ... <i class='fa fa-spin fa-circle-o-notch'></i>\r");
exec("zpool trim -w $pool 2>/dev/null");
write(zfs_info($pool)."\r","\n");
}
write(_("Finished")."\n",'_DONE_','');
?>