cros_sdk: inform and raise vm.max_map_count if too low

This adds a check to cros_sdk to ensure that vm.max_map_count is
high enough to handle large ThinLTO links. If not, the script
emits a notice and raises the limit.

BUG=chromium:1129937
TEST=(outside) $ sudo sysctl -w vm.max_map_count=65530
  vm.max_map_count = 65530
  (outside) $ cros_sdk --enter
  20:36:51: NOTICE: Raising vm.max_map_count from 65530 to 262144
  (cr) $ cat /proc/sys/vm/max_map_count
  262144

Change-Id: I697cdc534693e46095b19a023b8e210d174580a8
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/chromite/+/2466479
Reviewed-by: Manoj Gupta <manojgupta@chromium.org>
Reviewed-by: Chris McDonald <cjmcdonald@chromium.org>
Commit-Queue: Bob Haarman <inglorion@chromium.org>
Tested-by: Bob Haarman <inglorion@chromium.org>
diff --git a/scripts/cros_sdk.py b/scripts/cros_sdk.py
index 6b4cfcb..2cf59ab 100644
--- a/scripts/cros_sdk.py
+++ b/scripts/cros_sdk.py
@@ -247,7 +247,16 @@
     cmd.extend(additional_args)
 
   # ThinLTO opens lots of files at the same time.
-  resource.setrlimit(resource.RLIMIT_NOFILE, (32768, 32768))
+  # Set rlimit and vm.max_map_count to accommodate this.
+  file_limit = 262144
+  soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE)
+  resource.setrlimit(resource.RLIMIT_NOFILE,
+                     (max(soft, file_limit), max(hard, file_limit)))
+  max_map_count = int(open('/proc/sys/vm/max_map_count').read())
+  if max_map_count < file_limit:
+    logging.notice(
+        'Raising vm.max_map_count from %s to %s', max_map_count, file_limit)
+    open('/proc/sys/vm/max_map_count', 'w').write(f'{file_limit}\n')
   ret = cros_build_lib.dbg_run(cmd, check=False)
   # If we were in interactive mode, ignore the exit code; it'll be whatever
   # they last ran w/in the chroot and won't matter to us one way or another.