It is always a bad idea to mix up different languages
in one environment. Another point is, to bury awk in
base-files. There is no reason for it, these are only
quick hacks from the old days of whiterussian 8-).

now we call the function + get a value, and do not rely
on a global set var_name, which's name was unclear.

Signed-off-by: Bastian Bittorf <bittorf at bluebottle.com>

>From 5fde9d204cfd9aa0e40b93e1400efb96a8cd4e2f Mon Sep 17 00:00:00 2001
From: Bastian Bittorf <bitt...@bluebottle.com>
Date: Sun, 16 Jan 2011 11:55:25 +0100
Subject: [PATCH] refactoring calc_tmpfs_size() without awk

---
 .../base-files/files/lib/preinit/10_essential_fs   |   29 ++++++++++++++++---
 1 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/package/base-files/files/lib/preinit/10_essential_fs b/package/base-files/files/lib/preinit/10_essential_fs
index ab317dc..6f0193c 100644
--- a/package/base-files/files/lib/preinit/10_essential_fs
+++ b/package/base-files/files/lib/preinit/10_essential_fs
@@ -10,16 +10,35 @@ do_mount_sysfs() {
     mount sysfs /sys -t sysfs
 }
 
-calc_tmpfs_size() {
-    pi_size=$(awk '/MemTotal:/ {l=5242880;mt=($2*1024);print((s=mt/2)<l)&&(mt>l)?mt-l:s}' /proc/meminfo)
+calc_tmpfs_size()	# [bytes] - 50% of total mem, but at least 5mb must be untouched
+{
+	local mem_border=5242880
+	local line mem_total mem_untouched size_tmpfs
+
+	while read line; do {
+		case "$line" in
+			MemTotal:*)
+				set $line
+				mem_total=$(( $2 * 1024 ))
+				break
+			;;
+		esac
+	} done </proc/meminfo
+
+	size_tmpfs=$(( $mem_total / 2 ))
+	mem_untouched=$(( $mem_total - $size_tmpfs ))
+
+	[ $mem_untouched -lt $mem_border ] && {
+		size_tmpfs=$(( $mem_total - $mem_border ))
+	}
+
+	echo $size_tmpfs
 }
 
 do_mount_tmpfs() {
-    calc_tmpfs_size
-    mount tmpfs /tmp -t tmpfs -o size=$pi_size,nosuid,nodev,mode=1777
+    mount tmpfs /tmp -t tmpfs -o size=$( calc_tmpfs_size ),nosuid,nodev,mode=1777
 }
 
 boot_hook_add preinit_essential do_mount_procfs
 boot_hook_add preinit_essential do_mount_sysfs
 boot_hook_add preinit_essential do_mount_tmpfs
-
-- 
1.6.3.3

_______________________________________________
openwrt-devel mailing list
openwrt-devel@lists.openwrt.org
https://lists.openwrt.org/mailman/listinfo/openwrt-devel

Reply via email to