1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/utsname.h>
#include <sys/mount.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include "../log.h"
#include "init.h"
#define KB(x) (x * 1024)
#define ZRAM_MOD_PATH "/lib/modules/%s/zram.ko"
#define EXT4_MOD_PATH "/lib/modules/%s/ext4.ko"
static long
proc_meminfo(void)
{
FILE *fp;
char line[256];
char *key;
long val = KB(16);
fp = fopen("/proc/meminfo", "r");
if (fp == NULL) {
ERROR("Can't open /proc/meminfo: %s\n", strerror(errno));
return errno;
}
while (fgets(line, sizeof(line), fp)) {
key = strtok(line, ":");
if (strcasecmp(key, "MemTotal"))
continue;
val = atol(strtok(NULL, " kB\n"));
break;
}
fclose(fp);
if (val > KB(32))
val = KB(32);
return val;
}
static int
early_insmod(char *module)
{
pid_t pid = fork();
if (!pid) {
char *modprobe[] = { "/usr/sbin/modprobe", NULL, NULL };
char *path;
struct utsname ver;
uname(&ver);
path = alloca(sizeof(module) + strlen(ver.release) + 1);
sprintf(path, module, ver.release);
modprobe[1] = path;
execvp(modprobe[0], modprobe);
ERROR("Can't exec /usr/sbin/modprobe\n");
exit(-1);
}
if (pid <= 0) {
ERROR("Can't exec /usr/sbin/modprobe\n");
return -1;
} else {
waitpid(pid, NULL, 0);
}
return 0;
}
int
mount_zram_on_tmp(void)
{
char *mkfs[] = { "/usr/sbin/mkfs.ext4", "-b", "4096", "-F", "-L", "TEMP", "-m", "0", "/dev/zram0", NULL };
FILE *fp;
long zramsize;
pid_t pid;
int ret;
if (early_insmod(ZRAM_MOD_PATH) || early_insmod(EXT4_MOD_PATH)) {
ERROR("failed to insmod zram support\n");
return -1;
}
mkdev("*", 0600);
zramsize = proc_meminfo() / 2;
fp = fopen("/sys/block/zram0/disksize", "r+");
if (fp == NULL) {
ERROR("Can't open /sys/block/zram0/disksize: %s\n", strerror(errno));
return errno;
}
fprintf(fp, "%ld", KB(zramsize));
fclose(fp);
pid = fork();
if (!pid) {
execvp(mkfs[0], mkfs);
ERROR("Can't exec /sbin/mkfs.ext4\n");
exit(-1);
} else if (pid <= 0) {
ERROR("Can't exec /sbin/mkfs.ext4\n");
return -1;
} else {
waitpid(pid, NULL, 0);
}
ret = mount("/dev/zram0", "/tmp", "ext4", MS_NOSUID | MS_NODEV | MS_NOATIME, "errors=continue,noquota");
if (ret < 0) {
ERROR("Can't mount /dev/zram0 on /tmp: %s\n", strerror(errno));
return errno;
}
LOG("Using up to %ld kB of RAM as ZRAM storage on /mnt\n", zramsize);
return 0;
}
|