Skip to content

Commit 14a0990

Browse files
committed
Skip user creation on reboot when user already exists
On persistent disk images (qcow2/raw), useradd fails on the second boot because the user persists from the first boot. This causes rd-init to exit before configuring networking, which blocks systemd-networkd, lima-init, and sshd from starting. Check whether the user exists before calling useradd. The rest of the setup (authorized_keys, sudoers) still runs on every boot. Signed-off-by: Jan Dubois <jan.dubois@suse.com>
1 parent aea9a53 commit 14a0990

1 file changed

Lines changed: 18 additions & 15 deletions

File tree

src/rd-init/userdata.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -82,23 +82,26 @@ func LoadUserData(ctx context.Context) ([]string, error) {
8282

8383
// Process users
8484
for _, userEntry := range userData.Users {
85-
// Create user
86-
slog.InfoContext(ctx, "creating user", "user", userEntry.Name)
87-
err = runCommand(ctx, "/usr/sbin/useradd",
88-
"--home-dir", userEntry.HomeDir,
89-
"--create-home",
90-
"--comment", userEntry.GECOS,
91-
"--uid", userEntry.UID,
92-
"--shell", userEntry.Shell,
93-
userEntry.Name)
94-
if err != nil {
95-
return nil, fmt.Errorf("failed to create user %q: %w", userEntry.Name, err)
96-
}
97-
98-
// Look up the newly created user
9985
userInfo, err := user.LookupId(userEntry.UID)
10086
if err != nil {
101-
return nil, fmt.Errorf("failed to look up newly created user %q: %w", userEntry.Name, err)
87+
// User does not exist yet; create it.
88+
slog.InfoContext(ctx, "creating user", "user", userEntry.Name)
89+
err = runCommand(ctx, "/usr/sbin/useradd",
90+
"--home-dir", userEntry.HomeDir,
91+
"--create-home",
92+
"--comment", userEntry.GECOS,
93+
"--uid", userEntry.UID,
94+
"--shell", userEntry.Shell,
95+
userEntry.Name)
96+
if err != nil {
97+
return nil, fmt.Errorf("failed to create user %q: %w", userEntry.Name, err)
98+
}
99+
userInfo, err = user.LookupId(userEntry.UID)
100+
if err != nil {
101+
return nil, fmt.Errorf("failed to look up newly created user %q: %w", userEntry.Name, err)
102+
}
103+
} else {
104+
slog.InfoContext(ctx, "user already exists", "user", userEntry.Name)
102105
}
103106
uid, err := strconv.ParseInt(userInfo.Uid, 10, 32)
104107
if err != nil {

0 commit comments

Comments
 (0)