380fe0ea75cfd134232c64706449d5448170bcf8
8 #include <sys/utsname.h>
10 #include <sys/types.h>
15 #include "../container.h"
19 #define KB(x) (x * 1024)
21 #define ZRAM_MOD_PATH "/lib/modules/%s/zram.ko"
22 #define EXT4_MOD_PATH "/lib/modules/%s/ext4.ko"
32 fp
= fopen("/proc/meminfo", "r");
34 ERROR("Can't open /proc/meminfo: %m\n");
38 while (fgets(line
, sizeof(line
), fp
)) {
39 key
= strtok(line
, ":");
40 if (strcasecmp(key
, "MemTotal"))
42 val
= atol(strtok(NULL
, " kB\n"));
54 early_insmod(char *module
)
57 char *modprobe
[] = { "/sbin/modprobe", NULL
, NULL
};
64 path
= alloca(strlen(module
) + strlen(ver
.release
) + 1);
65 sprintf(path
, module
, ver
.release
);
67 execvp(modprobe
[0], modprobe
);
68 ERROR("Can't exec %s: %m\n", modprobe
[0]);
73 ERROR("Can't exec %s: %m\n", modprobe
[0]);
76 waitpid(pid
, NULL
, 0);
84 mount_zram_on_tmp(void)
86 char *mkfs
[] = { "/usr/sbin/mkfs.ext4", "-b", "4096", "-F", "-L", "TEMP", "-m", "0", "/dev/zram0", NULL
};
92 if (early_insmod(ZRAM_MOD_PATH
) || early_insmod(EXT4_MOD_PATH
)) {
93 ERROR("failed to insmod zram support\n");
99 zramsize
= proc_meminfo() / 2;
100 fp
= fopen("/sys/block/zram0/disksize", "r+");
102 ERROR("Can't open /sys/block/zram0/disksize: %m\n");
105 fprintf(fp
, "%ld", KB(zramsize
));
110 execvp(mkfs
[0], mkfs
);
111 ERROR("Can't exec %s: %m\n", mkfs
[0]);
113 } else if (pid
<= 0) {
114 ERROR("Can't exec %s: %m\n", mkfs
[0]);
117 waitpid(pid
, NULL
, 0);
120 if (!is_container()) {
121 ret
= mount("/dev/zram0", "/tmp", "ext4", MS_NOSUID
| MS_NODEV
| MS_NOATIME
, "errors=continue,noquota");
123 ERROR("Can't mount /dev/zram0 on /tmp: %m\n");
128 LOG("Using up to %ld kB of RAM as ZRAM storage on /mnt\n", zramsize
);
130 ret
= chmod("/tmp", 01777);
132 ERROR("Can't set /tmp mode to 1777: %m\n");