Skip to content

Commit 6c37e6d

Browse files
authored
Merge pull request containers#71 from slp/init-dev
Various fixes for init.c
2 parents a49db4e + 8791832 commit 6c37e6d

File tree

4 files changed

+39
-31
lines changed

4 files changed

+39
-31
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ LIBRARY_HEADER = include/libkrun.h
22
INIT_BINARY = init/init
33

44
ABI_VERSION=1
5-
FULL_VERSION=1.4.1
5+
FULL_VERSION=1.4.2
66

77
ifeq ($(SEV),1)
88
VARIANT = -sev

init/init.c

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -173,35 +173,25 @@ static int chroot_luks()
173173
}
174174
#endif
175175

176-
static int create_dirs()
176+
static int mount_filesystems()
177177
{
178-
char *const DIRS[] = {"/proc", "/sys", "/sys/fs", "/sys/fs/cgroup", "/dev/pts", "/dev/shm"};
178+
char *const DIRS_LEVEL1[] = {"/dev", "/proc", "/sys"};
179+
char *const DIRS_LEVEL2[] = {"/dev/pts", "/dev/shm"};
179180
int i;
180181

181-
if (access("/dev", F_OK) != 0) {
182-
if (mkdir("/dev", 0755) < 0 && errno != EEXIST) {
183-
printf("Error creating directory /dev\n");
184-
return -1;
185-
}
186-
if (mount("devtmpfs", "/dev", "devtmpfs",
187-
MS_RELATIME, NULL) < 0) {
188-
perror("mount(/dev)");
182+
for (i = 0; i < 3; ++i) {
183+
if (mkdir(DIRS_LEVEL1[i], 0755) < 0 && errno != EEXIST) {
184+
printf("Error creating directory (%s)\n", DIRS_LEVEL1[i]);
189185
return -1;
190186
}
191187
}
192188

193-
for (i = 0; i < 6; ++i) {
194-
if (mkdir(DIRS[i], 0755) < 0 && errno != EEXIST) {
195-
printf("Error creating directory (%s)\n", DIRS[i]);
196-
return -1;
197-
}
189+
if (mount("devtmpfs", "/dev", "devtmpfs",
190+
MS_RELATIME, NULL) < 0 && errno != EBUSY ) {
191+
perror("mount(/dev)");
192+
return -1;
198193
}
199194

200-
return 0;
201-
}
202-
203-
static int mount_filesystems()
204-
{
205195
if (mount("proc", "/proc", "proc",
206196
MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_RELATIME, NULL) < 0) {
207197
perror("mount(/proc)");
@@ -220,6 +210,13 @@ static int mount_filesystems()
220210
return -1;
221211
}
222212

213+
for (i = 0; i < 2; ++i) {
214+
if (mkdir(DIRS_LEVEL2[i], 0755) < 0 && errno != EEXIST) {
215+
printf("Error creating directory (%s)\n", DIRS_LEVEL2[i]);
216+
return -1;
217+
}
218+
}
219+
223220
if (mount("devpts", "/dev/pts", "devpts",
224221
MS_NOEXEC | MS_NOSUID | MS_RELATIME, NULL) < 0) {
225222
perror("mount(/dev/pts)");
@@ -260,7 +257,12 @@ static void config_parse_env(char *data, jsmntok_t *token)
260257
*env_val = '\0';
261258
env_val++;
262259

263-
setenv(env, env_val, 0);
260+
if ((strcmp(env, "HOME") == 0) ||
261+
(strcmp(env, "TERM") == 0)) {
262+
setenv(env, env_val, 1);
263+
} else {
264+
setenv(env, env_val, 0);
265+
}
264266
}
265267
}
266268

@@ -454,6 +456,8 @@ int main(int argc, char **argv)
454456
int sockfd;
455457
char localhost[] = "localhost\0";
456458
char *hostname;
459+
char *krun_home;
460+
char *krun_term;
457461
char *krun_init;
458462
char *config_workdir, *env_workdir;
459463
char *rlimits;
@@ -465,15 +469,9 @@ int main(int argc, char **argv)
465469
exit(-1);
466470
}
467471
#endif
468-
469-
if (create_dirs() < 0) {
470-
printf("Couldn't create support directories, bailing out\n");
471-
exit(-2);
472-
}
473-
474472
if (mount_filesystems() < 0) {
475473
printf("Couldn't mount filesystems, bailing out\n");
476-
exit(-3);
474+
exit(-2);
477475
}
478476

479477
setsid();
@@ -493,6 +491,16 @@ int main(int argc, char **argv)
493491

494492
config_parse_file(&config_argv, &config_workdir);
495493

494+
krun_home = getenv("KRUN_HOME");
495+
if (krun_home) {
496+
setenv("HOME", krun_home, 1);
497+
}
498+
499+
krun_term = getenv("KRUN_TERM");
500+
if (krun_term) {
501+
setenv("TERM", krun_term, 1);
502+
}
503+
496504
hostname = getenv("HOSTNAME");
497505
if (hostname) {
498506
sethostname(hostname, strlen(hostname));

src/libkrun/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "libkrun"
3-
version = "1.4.1"
3+
version = "1.4.2"
44
authors = ["Sergio Lopez <[email protected]>"]
55
edition = "2021"
66
build = "build.rs"

0 commit comments

Comments
 (0)