cros_sdk: Make fstrim run in background
Chroot images created with --use-image take unnecessary space, since
deleting files inside of chroot doesn't automatically return space to
the OS. cros_sdk calls fstrim to return the unused space to the OS
when amount of wasted space exceeds 20 GiBs, however, fstrim call blocks
cros_sdk and may take a while.
This change makes fstrim run in background. Output of fstrim is lost,
as we don't wait for it to finish, and fstrim will keep running after
cros_sdk exits.
BUG=chromium:1093131
TEST=verified manually
Change-Id: I52189bdb72f70567924f4d4c17795793c9868c83
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/chromite/+/2585948
Tested-by: Sergey Frolov <sfrolov@google.com>
Reviewed-by: Mike Frysinger <vapier@chromium.org>
Commit-Queue: Sergey Frolov <sfrolov@google.com>
diff --git a/scripts/cros_sdk.py b/scripts/cros_sdk.py
index 79c6b35..11cfb21 100644
--- a/scripts/cros_sdk.py
+++ b/scripts/cros_sdk.py
@@ -21,6 +21,7 @@
import random
import re
import resource
+import subprocess
import sys
from six.moves import urllib
@@ -1146,14 +1147,19 @@
extra_gbs = (img_used_bytes - mount_used_bytes) // 2**30
if extra_gbs > MAX_UNUSED_IMAGE_GBS:
logging.notice('%s is using %s GiB more than needed. Running '
- 'fstrim.', img_path, extra_gbs)
- cmd = ['fstrim', options.chroot]
- try:
- cros_build_lib.dbg_run(cmd)
- except cros_build_lib.RunCommandError as e:
- logging.warning(
- 'Running fstrim failed. Consider running fstrim on '
- 'your chroot manually.\n%s', e)
+ 'fstrim in background.', img_path, extra_gbs)
+ pid = os.fork()
+ if pid == 0:
+ try:
+ # Directly call Popen to run fstrim concurrently.
+ cmd = ['fstrim', options.chroot]
+ subprocess.Popen(cmd, close_fds=True, shell=False)
+ except subprocess.SubprocessError as e:
+ logging.warning(
+ 'Running fstrim failed. Consider running fstrim on '
+ 'your chroot manually.\n%s', e)
+ os._exit(0) # pylint: disable=protected-access
+ os.waitpid(pid, 0)
# Enter a new set of namespaces. Everything after here cannot directly affect
# the hosts's mounts or alter LVM volumes.