8 #include <sys/utsname.h>
10 #include <sys/types.h>
18 #define KB(x) (x * 1024)
20 #define ZRAM_MOD_PATH "/lib/modules/%s/zram.ko"
21 #define EXT4_MOD_PATH "/lib/modules/%s/ext4.ko"
31 fp = fopen("/proc/meminfo", "r");
33 ERROR("Can't open /proc/meminfo: %s\n", strerror(errno));
37 while (fgets(line, sizeof(line), fp)) {
38 key = strtok(line, ":");
39 if (strcasecmp(key, "MemTotal"))
41 val = atol(strtok(NULL, " kB\n"));
53 early_insmod(char *module)
56 char *modprobe[] = { "/sbin/modprobe", NULL, NULL };
63 path = alloca(sizeof(module) + strlen(ver.release) + 1);
64 sprintf(path, module, ver.release);
66 execvp(modprobe[0], modprobe);
67 ERROR("Can't exec %s\n", modprobe[0]);
72 ERROR("Can't exec %s\n", modprobe[0]);
75 waitpid(pid, NULL, 0);
83 mount_zram_on_tmp(void)
85 char *mkfs[] = { "/usr/sbin/mkfs.ext4", "-b", "4096", "-F", "-L", "TEMP", "-m", "0", "/dev/zram0", NULL };
91 if (early_insmod(ZRAM_MOD_PATH) || early_insmod(EXT4_MOD_PATH)) {
92 ERROR("failed to insmod zram support\n");
98 zramsize = proc_meminfo() / 2;
99 fp = fopen("/sys/block/zram0/disksize", "r+");
101 ERROR("Can't open /sys/block/zram0/disksize: %s\n", strerror(errno));
104 fprintf(fp, "%ld", KB(zramsize));
109 execvp(mkfs[0], mkfs);
110 ERROR("Can't exec %s\n", mkfs[0]);
112 } else if (pid <= 0) {
113 ERROR("Can't exec %s\n", mkfs[0]);
116 waitpid(pid, NULL, 0);
119 ret = mount("/dev/zram0", "/tmp", "ext4", MS_NOSUID | MS_NODEV | MS_NOATIME, "errors=continue,noquota");
121 ERROR("Can't mount /dev/zram0 on /tmp: %s\n", strerror(errno));
125 LOG("Using up to %ld kB of RAM as ZRAM storage on /mnt\n", zramsize);
127 ret = chmod("/tmp", 01777);
129 ERROR("Can't set /tmp mode to 1777: %s\n", strerror(errno));