From 836f3703f604359f740e449180b001678d3760b5 Mon Sep 17 00:00:00 2001 From: eylenburg <84839316+eylenburg@users.noreply.github.com> Date: Mon, 23 Jun 2025 14:33:59 +0100 Subject: [PATCH 1/7] add TimeSync() function to script Signed-off-by: Oskar Manhart <52569953+oskardotglobal@users.noreply.github.com> --- bin/winapps | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/bin/winapps b/bin/winapps index 5779f3c..1ac8056 100755 --- a/bin/winapps +++ b/bin/winapps @@ -27,6 +27,8 @@ readonly CONFIG_PATH="${HOME}/.config/winapps/winapps.conf" readonly COMPOSE_PATH="${HOME}/.config/winapps/compose.yaml" # shellcheck disable=SC2155 # Silence warnings regarding masking return values through simultaneous declaration and assignment. readonly SCRIPT_DIR_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)" +readonly SLEEP_DETECT_PATH="${APPDATA_PATH}/last_activity" +readonly SLEEP_MARKER="${APPDATA_PATH}/sleep_marker" # OTHER readonly CONTAINER_NAME="WinApps" # FOR 'docker' AND 'podman' ONLY @@ -646,6 +648,48 @@ function waCheckIdle() { fi } +# Name: 'waTimeSync' +# Role: Detect if system went to sleep by comparing uptime progression, then sync time in Windows VM +function waTimeSync() { + local CURRENT_TIME=$(date +%s) + local CURRENT_UPTIME="$(awk '{print int($1)}' "/proc/uptime")" + local STORED_TIME=0 + local STORED_UPTIME=0 + local EXPECTED_UPTIME=0 + local UPTIME_DIFF=0 + + # Read stored values if file exists + if [ -f "$SLEEP_DETECT_PATH" ]; then + STORED_TIME=$(head -n1 "$SLEEP_DETECT_PATH" 2>/dev/null || echo 0) + STORED_UPTIME=$(tail -n1 "$SLEEP_DETECT_PATH" 2>/dev/null || echo 0) + fi + + if [ "$STORED_TIME" -gt 0 ] && [ "$STORED_UPTIME" -gt 0 ]; then + # Calculate what uptime should be now + EXPECTED_UPTIME=$((STORED_UPTIME + CURRENT_TIME - STORED_TIME)) + UPTIME_DIFF=$((EXPECTED_UPTIME - CURRENT_UPTIME)) + + dprint "UPTIME_DIFF: ${UPTIME_DIFF} seconds" + + # If uptime is significantly less than expected, system likely slept + if [[ "$UPTIME_DIFF" -gt 30 && ! -f "$SLEEP_MARKER" ]]; then + dprint "DETECTED SLEEP/WAKE CYCLE (uptime gap: ${UPTIME_DIFF}s). CREATING SLEEP MARKER TO SYNC WINDOWS TIME." + echo -e "Detected system sleep/wake cycle. Creating sleep marker to sync Windows time..." + + # Create sleep marker which will be monitored by Windows VM to trigger time sync + touch "$SLEEP_MARKER" + + dprint "CREATED SLEEP MARKER" + fi + fi + + # Store current values + { + echo "$CURRENT_TIME" + echo "$CURRENT_UPTIME" + } > "$SLEEP_DETECT_PATH" +} + ### MAIN LOGIC ### #set -x # Enable for debugging. dprint "START" @@ -675,6 +719,7 @@ else fi waCheckPortOpen +waTimeSync waRunCommand "$@" if [[ "$AUTOPAUSE" == "on" ]]; then From 3f909c5ec0685039d387630db59b6ffeb223ead8 Mon Sep 17 00:00:00 2001 From: eylenburg <84839316+eylenburg@users.noreply.github.com> Date: Mon, 23 Jun 2025 14:35:46 +0100 Subject: [PATCH 2/7] Update winapps Signed-off-by: Oskar Manhart <52569953+oskardotglobal@users.noreply.github.com> --- bin/winapps | 1 - 1 file changed, 1 deletion(-) diff --git a/bin/winapps b/bin/winapps index 1ac8056..6d12a2d 100755 --- a/bin/winapps +++ b/bin/winapps @@ -674,7 +674,6 @@ function waTimeSync() { # If uptime is significantly less than expected, system likely slept if [[ "$UPTIME_DIFF" -gt 30 && ! -f "$SLEEP_MARKER" ]]; then dprint "DETECTED SLEEP/WAKE CYCLE (uptime gap: ${UPTIME_DIFF}s). CREATING SLEEP MARKER TO SYNC WINDOWS TIME." - echo -e "Detected system sleep/wake cycle. Creating sleep marker to sync Windows time..." # Create sleep marker which will be monitored by Windows VM to trigger time sync touch "$SLEEP_MARKER" From 9708ca2825fa245d0c7e2bf417498f7a7bde7573 Mon Sep 17 00:00:00 2001 From: eylenburg <84839316+eylenburg@users.noreply.github.com> Date: Mon, 23 Jun 2025 14:37:54 +0100 Subject: [PATCH 3/7] Schedule timesync.ps1 in install.bat Signed-off-by: Oskar Manhart <52569953+oskardotglobal@users.noreply.github.com> --- oem/install.bat | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/oem/install.bat b/oem/install.bat index 018c510..9a1b208 100644 --- a/oem/install.bat +++ b/oem/install.bat @@ -23,3 +23,20 @@ if %ERRORLEVEL% equ 0 ( ) else ( echo Failed to create scheduled task. ) + +REM Create time sync task to be run by the user at login +copy %~dp0\TimeSync.ps1 %windir% +set "taskname2=TimeSync" +set "command2=powershell.exe -WindowStyle Hidden -ExecutionPolicy Bypass -File \"%windir%\TimeSync.ps1\"" + +schtasks /query /tn "%taskname2%" >nul +if %ERRORLEVEL% equ 0 ( + echo %DATE% %TIME% Task "%taskname2%" already exists, skipping creation. +) else ( + schtasks /create /tn "%taskname2%" /tr "%command2%" /sc onlogon /rl HIGHEST /f + if %ERRORLEVEL% equ 0 ( + echo %DATE% %TIME% Scheduled task "%taskname2%" created successfully. + ) else ( + echo %DATE% %TIME% Failed to create scheduled task %taskname2%. + ) +) From a04f5db39a0f94eb0abed6cdcc5bc93bf0ec8afa Mon Sep 17 00:00:00 2001 From: eylenburg <84839316+eylenburg@users.noreply.github.com> Date: Mon, 23 Jun 2025 14:39:49 +0100 Subject: [PATCH 4/7] Create TimeSync.ps1 Signed-off-by: Oskar Manhart <52569953+oskardotglobal@users.noreply.github.com> --- oem/TimeSync.ps1 | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 oem/TimeSync.ps1 diff --git a/oem/TimeSync.ps1 b/oem/TimeSync.ps1 new file mode 100644 index 0000000..8541d09 --- /dev/null +++ b/oem/TimeSync.ps1 @@ -0,0 +1,32 @@ +# Script to monitor if there is a sleep_marker created by WinApps (indicating the Linux host was suspended) in order to trigger a time sync as the time in the Windows VM will otherwise drift while Linux is suspended. + +# Define the path to monitor. Make sure this matches the location for the sleep_marker in the Winapps script (need to match the APPDATA path). +$filePath = "\\tsclient\home\.local\share\linoffice\sleep_marker" +$networkPath = "\\tsclient\home" + +# Function to check and handle file +function Monitor-File { + while ($true) { + # Check if network location is available + try { + $null = Test-Path -Path $networkPath -ErrorAction Stop + # Check if file exists + if (Test-Path -Path $filePath) { + # Run time resync silently + w32tm /resync /quiet + + # Remove the file + Remove-Item -Path $filePath -Force + } + } + catch { + # Network location not available, continue monitoring silently + } + + # Wait 5 minutes before next check + Start-Sleep -Seconds 3000 + } +} + +# Start monitoring silently +Monitor-File From fbb382e1e8ce9fe78bc765e1ac54b9c7020f091b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 23 Jun 2025 13:49:54 +0000 Subject: [PATCH 5/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Signed-off-by: Oskar Manhart <52569953+oskardotglobal@users.noreply.github.com> --- bin/winapps | 16 ++++++++-------- oem/TimeSync.ps1 | 4 ++-- oem/install.bat | 4 ++-- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/bin/winapps b/bin/winapps index 6d12a2d..01a8bf0 100755 --- a/bin/winapps +++ b/bin/winapps @@ -648,7 +648,7 @@ function waCheckIdle() { fi } -# Name: 'waTimeSync' +# Name: 'waTimeSync' # Role: Detect if system went to sleep by comparing uptime progression, then sync time in Windows VM function waTimeSync() { local CURRENT_TIME=$(date +%s) @@ -657,31 +657,31 @@ function waTimeSync() { local STORED_UPTIME=0 local EXPECTED_UPTIME=0 local UPTIME_DIFF=0 - + # Read stored values if file exists if [ -f "$SLEEP_DETECT_PATH" ]; then STORED_TIME=$(head -n1 "$SLEEP_DETECT_PATH" 2>/dev/null || echo 0) STORED_UPTIME=$(tail -n1 "$SLEEP_DETECT_PATH" 2>/dev/null || echo 0) fi - + if [ "$STORED_TIME" -gt 0 ] && [ "$STORED_UPTIME" -gt 0 ]; then # Calculate what uptime should be now EXPECTED_UPTIME=$((STORED_UPTIME + CURRENT_TIME - STORED_TIME)) UPTIME_DIFF=$((EXPECTED_UPTIME - CURRENT_UPTIME)) - + dprint "UPTIME_DIFF: ${UPTIME_DIFF} seconds" - + # If uptime is significantly less than expected, system likely slept if [[ "$UPTIME_DIFF" -gt 30 && ! -f "$SLEEP_MARKER" ]]; then dprint "DETECTED SLEEP/WAKE CYCLE (uptime gap: ${UPTIME_DIFF}s). CREATING SLEEP MARKER TO SYNC WINDOWS TIME." - + # Create sleep marker which will be monitored by Windows VM to trigger time sync touch "$SLEEP_MARKER" - + dprint "CREATED SLEEP MARKER" fi fi - + # Store current values { echo "$CURRENT_TIME" diff --git a/oem/TimeSync.ps1 b/oem/TimeSync.ps1 index 8541d09..3f475c9 100644 --- a/oem/TimeSync.ps1 +++ b/oem/TimeSync.ps1 @@ -14,7 +14,7 @@ function Monitor-File { if (Test-Path -Path $filePath) { # Run time resync silently w32tm /resync /quiet - + # Remove the file Remove-Item -Path $filePath -Force } @@ -22,7 +22,7 @@ function Monitor-File { catch { # Network location not available, continue monitoring silently } - + # Wait 5 minutes before next check Start-Sleep -Seconds 3000 } diff --git a/oem/install.bat b/oem/install.bat index 9a1b208..4281a1c 100644 --- a/oem/install.bat +++ b/oem/install.bat @@ -31,9 +31,9 @@ set "command2=powershell.exe -WindowStyle Hidden -ExecutionPolicy Bypass -File \ schtasks /query /tn "%taskname2%" >nul if %ERRORLEVEL% equ 0 ( - echo %DATE% %TIME% Task "%taskname2%" already exists, skipping creation. + echo %DATE% %TIME% Task "%taskname2%" already exists, skipping creation. ) else ( - schtasks /create /tn "%taskname2%" /tr "%command2%" /sc onlogon /rl HIGHEST /f + schtasks /create /tn "%taskname2%" /tr "%command2%" /sc onlogon /rl HIGHEST /f if %ERRORLEVEL% equ 0 ( echo %DATE% %TIME% Scheduled task "%taskname2%" created successfully. ) else ( From b53682be6c5f81bf6cda2aaabb6827f1470f8e82 Mon Sep 17 00:00:00 2001 From: eylenburg <84839316+eylenburg@users.noreply.github.com> Date: Mon, 23 Jun 2025 16:36:49 +0100 Subject: [PATCH 6/7] Update winapps Signed-off-by: Oskar Manhart <52569953+oskardotglobal@users.noreply.github.com> --- bin/winapps | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/bin/winapps b/bin/winapps index 01a8bf0..f8c30cc 100755 --- a/bin/winapps +++ b/bin/winapps @@ -651,13 +651,16 @@ function waCheckIdle() { # Name: 'waTimeSync' # Role: Detect if system went to sleep by comparing uptime progression, then sync time in Windows VM function waTimeSync() { - local CURRENT_TIME=$(date +%s) - local CURRENT_UPTIME="$(awk '{print int($1)}' "/proc/uptime")" + local CURRENT_TIME + local CURRENT_UPTIME local STORED_TIME=0 local STORED_UPTIME=0 local EXPECTED_UPTIME=0 local UPTIME_DIFF=0 + CURRENT_TIME=$(date +%s) + CURRENT_UPTIME=$(awk '{print int($1)}' /proc/uptime) + # Read stored values if file exists if [ -f "$SLEEP_DETECT_PATH" ]; then STORED_TIME=$(head -n1 "$SLEEP_DETECT_PATH" 2>/dev/null || echo 0) From cdb5c3ef7f5d57f991bb3988a9870a0d436c1c1a Mon Sep 17 00:00:00 2001 From: eylenburg <84839316+eylenburg@users.noreply.github.com> Date: Mon, 1 Sep 2025 11:16:17 +0100 Subject: [PATCH 7/7] Update TimeSync.ps1 Signed-off-by: Oskar Manhart <52569953+oskardotglobal@users.noreply.github.com> --- oem/TimeSync.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/oem/TimeSync.ps1 b/oem/TimeSync.ps1 index 3f475c9..c2519db 100644 --- a/oem/TimeSync.ps1 +++ b/oem/TimeSync.ps1 @@ -1,7 +1,7 @@ # Script to monitor if there is a sleep_marker created by WinApps (indicating the Linux host was suspended) in order to trigger a time sync as the time in the Windows VM will otherwise drift while Linux is suspended. # Define the path to monitor. Make sure this matches the location for the sleep_marker in the Winapps script (need to match the APPDATA path). -$filePath = "\\tsclient\home\.local\share\linoffice\sleep_marker" +$filePath = "\\tsclient\home\.local\share\winapps\sleep_marker" $networkPath = "\\tsclient\home" # Function to check and handle file