[OpenWrt-Devel] [PATCH] ZRAM: enhacements including /tmp on ZRAM for Barrier Breaker

Fernando Frediani fhfrediani at gmail.com
Wed Oct 22 10:07:40 EDT 2014


By the way. Has anyone compiled and used BB 14.07 for devices with 16MB 
of RAM that went unsupported with AA release because of lack of ZRAM ?

Fernando

On 22/10/2014 08:20, Tomasz Wasiak wrote:
> Devices with less memory are still common so why limit ZRAM usage just to swap
> when it could be very useful as for /tmp storage.
>
> This patch changes 3 things:
>   - sets default number of ZRAM devices to 2 (1st for /tmp, 2nd for swap)
>   - adds ZRAM (default) support to procd's init (TMPFS is used when ZRAM is not
>     available
>   - changes zram-swap so it will use /dev/zram1 for 1st CPU, /dev/zram2 for 2nd
>     and so on as /dev/zram0 is in use for /tmp.
>
> Signed-off-by: Tomasz Wasiak <tjwasiak at gmail.com>
> ---
>   package/system/procd/patches/procd-support_for_tmp_on_zram.patch           |  185 ++++++++++
>   package/system/zram-swap/files/zram.init                                   |   15
>   target/linux/generic/patches-3.10/998_zram_make_2_devices_by_default.patch |   11
>   3 files changed, 203 insertions(+), 8 deletions(-)
>
> --- a/package/system/procd/patches/procd-support_for_tmo_on_zram.patch
> +++ b/package/system/procd/patches/procd-support_for_tmp_on_zram.patch
> @@ -0,0 +1,185 @@
> +--- a/initd/early.c
> ++++ b/initd/early.c
> +@@ -12,34 +12,130 @@
> +  * GNU General Public License for more details.
> +  */
> +
> +-#include <sys/mount.h>
> +-#include <sys/types.h>
> +-#include <sys/stat.h>
> +-
> +-#include <stdio.h>
> ++#include <errno.h>
> + #include <fcntl.h>
> +-#include <unistd.h>
> ++#include <stdio.h>
> + #include <stdlib.h>
> ++#include <string.h>
> ++#include <strings.h>
> ++#include <unistd.h>
> ++#include <sys/mount.h>
> ++#include <sys/stat.h>
> ++#include <sys/types.h>
> ++#include <sys/wait.h>
> +
> + #include "../log.h"
> + #include "init.h"
> +
> ++static long
> ++check_ramsize(void)
> ++{
> ++	FILE *fp;
> ++	char line[256];
> ++	char *key;
> ++	long val = 0;
> ++
> ++	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, ":");
> ++		val = atol(strtok(NULL, " kB\n"));
> ++
> ++		if (!key || !val)
> ++			continue;
> ++
> ++		if (!strcasecmp(key, "MemTotal"))
> ++			break;
> ++	}
> ++
> ++	fclose(fp);
> ++
> ++	return val;
> ++}
> ++
> ++static int
> ++mount_zram_on_tmp(void)
> ++{
> ++	FILE *fp;
> ++	long zramsize = (check_ramsize() / 2);
> ++	pid_t pid;
> ++
> ++	if(!zramsize)
> ++	{
> ++		ERROR("Can't read size of RAM. Assuming 16 MB.\n");
> ++		zramsize = 8192;
> ++	}
> ++
> ++	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", (zramsize * 1024));
> ++
> ++	fclose(fp);
> ++
> ++	pid = fork();
> ++
> ++	if (!pid)
> ++	{
> ++		char *mkfs[] = { "/sbin/mke2fs", "-b", "4096", "-F", "-L", "TEMP", "-m", "0", "/dev/zram0", NULL };
> ++		int fd = open("/dev/null", O_RDWR);
> ++
> ++		if (fd > -1) {
> ++			dup2(fd, STDIN_FILENO);
> ++			dup2(fd, STDOUT_FILENO);
> ++			dup2(fd, STDERR_FILENO);
> ++			if (fd > STDERR_FILENO)
> ++				close(fd);
> ++		}
> ++
> ++		execvp(mkfs[0], mkfs);
> ++		ERROR("Can't exec /sbin/mke2fs\n");
> ++		exit(-1);
> ++	}
> ++
> ++	if (pid <= 0)
> ++	{
> ++		ERROR("Can't exec /sbin/mke2fs\n");
> ++		return -1;
> ++	} else {
> ++		waitpid(pid, NULL, 0);
> ++	}
> ++
> ++	if(mount("/dev/zram0", "/tmp", "ext2", MS_NOSUID | MS_NODEV | MS_NOATIME, "check=none,errors=continue,noquota") < 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;
> ++}
> ++
> + static void
> +-early_mounts(void)
> ++mount_tmpfs_on_tmp(void)
> + {
> +-	mount("proc", "/proc", "proc", MS_NOATIME, 0);
> +-	mount("sysfs", "/sys", "sysfs", MS_NOATIME, 0);
> ++	char line[256];
> ++	long tmpfssize = (check_ramsize() / 2);
> +
> +-	mount("tmpfs", "/tmp", "tmpfs", MS_NOSUID | MS_NODEV | MS_NOATIME, NULL);
> +-	mkdir("/tmp/run", 0777);
> +-	mkdir("/tmp/lock", 0777);
> +-	mkdir("/tmp/state", 0777);
> +-	symlink("/tmp", "/var");
> ++	if(!tmpfssize)
> ++	{
> ++		ERROR("Can't read size of RAM. Assuming 16 MB.\n");
> ++		tmpfssize = 8192;
> ++	}
> +
> +-	mount("tmpfs", "/dev", "tmpfs", MS_NOATIME, "mode=0755,size=512K");
> +-	mkdir("/dev/shm", 0755);
> +-	mkdir("/dev/pts", 0755);
> +-	mount("devpts", "/dev/pts", "devpts", MS_NOATIME, "mode=600");
> ++	snprintf(line, 256, "size=%ldk", tmpfssize);
> ++	mount("tmpfs", "/tmp", "tmpfs", MS_NOSUID | MS_NODEV | MS_NOATIME, line);
> ++	LOG("Using up to %ld kB of RAM as TMPFS storage on /tmp\n", tmpfssize);
> + }
> +
> + static void
> +@@ -50,6 +146,25 @@ early_dev(void)
> + }
> +
> + static void
> ++early_mounts(void)
> ++{
> ++	mount("proc", "/proc", "proc", MS_NOATIME, 0);
> ++	mount("sysfs", "/sys", "sysfs", MS_NOATIME, 0);
> ++	mount("tmpfs", "/dev", "tmpfs", MS_NOATIME, "mode=0755,size=512K");
> ++	mkdir("/dev/shm", 0755);
> ++	mkdir("/dev/pts", 0755);
> ++	mount("devpts", "/dev/pts", "devpts", MS_NOATIME, "mode=600");
> ++	early_dev();
> ++
> ++	if(mount_zram_on_tmp() !=0)
> ++		mount_tmpfs_on_tmp();
> ++	mkdir("/tmp/run", 0777);
> ++	mkdir("/tmp/lock", 0777);
> ++	mkdir("/tmp/state", 0777);
> ++	symlink("/tmp", "/var");
> ++}
> ++
> ++static void
> + early_console(const char *dev)
> + {
> + 	struct stat s;
> +@@ -87,7 +202,6 @@ early(void)
> + 		return;
> +
> + 	early_mounts();
> +-	early_dev();
> + 	early_env();
> + 	early_console("/dev/console");
> +
> --- a/package/system/zram-swap/files/zram.init
> +++ b/package/system/zram-swap/files/zram.init
> @@ -15,10 +15,10 @@ zram_size()	# in megabytes
>   	local ram_size="$( ram_size )"
>   
>   	if [ -z "$zram_size" ]; then
> -		# e.g. 6mb for 16mb-routers or 61mb for 128mb-routers
> -		echo $(( $ram_size / 2048 ))
> +		# e.g. 3200kb for 16mb-routers or 25600kb for 128mb-routers
> +		echo $(( $ram_size * 2 / 10 ))
>   	else
> -		echo "$zram_size"
> +		echo $(( $zram_size * 1024 ))
>   	fi
>   }
>   
> @@ -54,9 +54,9 @@ zram_applicable()
>   
>   zram_dev()
>   {
> -	local core="$1"
> +	local core=$(( $1 + 1 ))
>   
> -	echo "/dev/zram${core:-0}"
> +	echo "/dev/zram${core:-1}"
>   }
>   
>   zram_reset()
> @@ -95,10 +95,10 @@ start()
>   		zram_dev="$( zram_dev "$core" )"
>   		zram_applicable "$zram_dev" || return 1
>   
> -		logger -s -t zram_start -p daemon.debug "activating '$zram_dev' for swapping ($zram_size MegaBytes)"
> +		logger -s -t zram_start -p daemon.debug "activating '$zram_dev' for swapping ($zram_size KiloBytes)"
>   
>   		zram_reset "$zram_dev" "enforcing defaults"
> -		echo $(( $zram_size * 1024 * 1024 )) >"/sys/block/$( basename $zram_dev )/disksize"
> +		echo $(( $zram_size * 1024 )) >"/sys/block/$( basename $zram_dev )/disksize"
>   		mkswap "$zram_dev"
>   		swapon "$zram_dev"
>   	} done
> @@ -120,4 +120,3 @@ stop()
>   		zram_reset "$zram_dev" "claiming memory back"
>   	} done
>   }
> -
> --- a/target/linux/generic/patches-3.10/998_zram_make_2_devices_by_default.patch
> +++ b/target/linux/generic/patches-3.10/998_zram_make_2_devices_by_default.patch
> @@ -0,0 +1,11 @@
> +--- a/drivers/staging/zram/zram_drv.c
> ++++ b/drivers/staging/zram/zram_drv.c
> +@@ -40,7 +40,7 @@ static int zram_major;
> + struct zram *zram_devices;
> +
> + /* Module params (documentation at end) */
> +-static unsigned int num_devices = 1;
> ++static unsigned int num_devices = 2;
> +
> + static void zram_stat64_add(struct zram *zram, u64 *v, u64 inc)
> + {
> _______________________________________________
> openwrt-devel mailing list
> openwrt-devel at lists.openwrt.org
> https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel
_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel



More information about the openwrt-devel mailing list