openhands.runtime.utils.runtime_init
init_user_and_working_directory
def init_user_and_working_directory(username: str, user_id: int,
initial_pwd: str) -> int | None
Create working directory and user if not exists. It performs the following steps effectively:
- Creates the Working Directory:
- Uses mkdir -p to create the directory.
- Sets ownership to username:root.
- Adjusts permissions to be readable and writable by group and others.
- User Verification and Creation:
- Checks if the user exists using id -u.
- If the user exists with the correct UID, it skips creation.
- If the UID differs, it logs a warning and return an updated user_id.
- If the user doesn't exist, it proceeds to create the user.
- Sudo Configuration:
- Appends %sudo ALL=(ALL) NOPASSWD:ALL to /etc/sudoers to grant passwordless sudo access to the sudo group.
- Adds the user to the sudo group with the useradd command, handling UID conflicts by incrementing the UID if necessary.
Arguments:
username
str - The username to create.user_id
int - The user ID to assign to the user.initial_pwd
str - The initial working directory to create.
Returns:
int | None: The user ID if it was updated, None otherwise.