Skip to content
This repository was archived by the owner on Sep 3, 2022. It is now read-only.

Commit 9b93098

Browse files
authored
Ensure that home directories have the right owner at startup. (#2067)
Apparently, Container Optimized OS stores the users database in a temp filesystem, causing it to get lost and recreated every time an instance is restarted. Among the many important things recorded in that database is the mapping from user names (e.g. `datalab`) to user numeric IDs (e.g. `2000`). By recreating the users database on every restart, that mapping can change seemingly randomly. For instance, the `datalab` user can have an ID of `2000` on one boot, with the `logger` user having an ID of `2001`, and after rebooting the instance those numbers could be reversed: `datalab` having a user ID of `2001` and `logger` having a user ID of `2000`. Since file ownership is defined in terms of user ID, this means that the owner of files under each home directory can change randomly every time an instance is rebooted. That, in turn, causes `datalab connect` calls to fail, as the SSH tunnel cannot be created if the `datalab` user cannot log in. This change fixes that problem by making the file ownership of the `/home/datalab` and `/home/logger` directories stable. That is done by attempting to assign those two users consistent UIDs, and then forcing the file ownership to match the corresponding users even if the UID has changed. Changing the startup script in the `create.py` file is sufficient to do this for both regular and gpu-enabled instances, as GPU instances no longer have their own startup-script extensions. This change also removes the structure that was previously used for startup-script extensions in order to make clear the fact that they are no longer used. This fixes #2014
1 parent 2bccee3 commit 9b93098

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

tools/cli/commands/create.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,20 @@
5959

6060
_DATALAB_NOTEBOOKS_REPOSITORY = 'datalab-notebooks'
6161

62-
_DATALAB_BASE_STARTUP_SCRIPT = """#!/bin/bash
62+
_DATALAB_STARTUP_SCRIPT = """#!/bin/bash
6363
64-
# First, make sure the `datalab` user exists with their
65-
# home directory setup correctly.
66-
useradd datalab
64+
# First, make sure the `datalab` and `logger` users exist with their
65+
# home directories setup correctly.
66+
useradd datalab -u 2000 || useradd datalab
67+
useradd logger -u 2001 || useradd logger
68+
69+
# In case the instance has started before, the `/home/datalab` directory
70+
# may already exist, but with the incorrect user ID (since `/etc/passwd`
71+
# is saved in a tmpfs and changes after restarts). To account for that,
72+
# we should force the file ownership under `/home/datalab` to match
73+
# the current UID for the `datalab` user.
74+
chown -R datalab /home/datalab
75+
chown -R logger /home/logger
6776
6877
PERSISTENT_DISK_DEV="/dev/disk/by-id/google-datalab-pd"
6978
MOUNT_DIR="/mnt/disks/datalab-pd"
@@ -214,9 +223,6 @@
214223
find "${{tmpdir}}/" -mindepth 1 -delete
215224
}}
216225
217-
"""
218-
219-
_DATALAB_STARTUP_SCRIPT = _DATALAB_BASE_STARTUP_SCRIPT + """
220226
download_docker_image
221227
mount_and_prepare_disk
222228
configure_swap

0 commit comments

Comments
 (0)