Module Name: src
Committed By: martin
Date: Wed Dec 4 13:30:35 UTC 2013
Modified Files:
src/sbin/mount_tmpfs: mount_tmpfs.8 mount_tmpfs.c
Log Message:
Provide variants of the -s option to allow limiting the tmpfs dynamically
at mount time to 1/Nth or to N percent of the available ram.
To generate a diff of this commit:
cvs rdiff -u -r1.15 -r1.16 src/sbin/mount_tmpfs/mount_tmpfs.8
cvs rdiff -u -r1.25 -r1.26 src/sbin/mount_tmpfs/mount_tmpfs.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/sbin/mount_tmpfs/mount_tmpfs.8
diff -u src/sbin/mount_tmpfs/mount_tmpfs.8:1.15 src/sbin/mount_tmpfs/mount_tmpfs.8:1.16
--- src/sbin/mount_tmpfs/mount_tmpfs.8:1.15 Sun Jun 2 13:27:12 2013
+++ src/sbin/mount_tmpfs/mount_tmpfs.8 Wed Dec 4 13:30:35 2013
@@ -1,4 +1,4 @@
-.\" $NetBSD: mount_tmpfs.8,v 1.15 2013/06/02 13:27:12 wiz Exp $
+.\" $NetBSD: mount_tmpfs.8,v 1.16 2013/12/04 13:30:35 martin Exp $
.\"
.\" Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
.\" All rights reserved.
@@ -85,6 +85,24 @@ If zero is given (the default), the avai
main memory and swap space) will be used.
Note that four megabytes are always reserved for the system and cannot
be assigned to the file system.
+.Ar Size
+can alternatively be specified as a percentage of the available
+system ram by using the notation
+.Ar ram%n
+where
+.Ar n
+is a number between 1 and 100.
+Similarily it can be specified as a fraction of the available system
+ram by using
+.Ar ram/n
+where
+.Ar n
+is the divisor.
+(Using
+.Ar ram%25
+and
+.Ar ram/4
+will result in the same limit.)
.It Fl u Ar user
Specifies the user name or UID of the root inode of the file system.
Defaults to the mount point's UID.
Index: src/sbin/mount_tmpfs/mount_tmpfs.c
diff -u src/sbin/mount_tmpfs/mount_tmpfs.c:1.25 src/sbin/mount_tmpfs/mount_tmpfs.c:1.26
--- src/sbin/mount_tmpfs/mount_tmpfs.c:1.25 Sun Jun 2 13:27:20 2013
+++ src/sbin/mount_tmpfs/mount_tmpfs.c Wed Dec 4 13:30:35 2013
@@ -1,4 +1,5 @@
-/* $NetBSD: mount_tmpfs.c,v 1.25 2013/06/02 13:27:20 wiz Exp $ */
+
+/* $NetBSD: mount_tmpfs.c,v 1.26 2013/12/04 13:30:35 martin Exp $ */
/*
* Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
@@ -32,12 +33,13 @@
#include <sys/cdefs.h>
#ifndef lint
-__RCSID("$NetBSD: mount_tmpfs.c,v 1.25 2013/06/02 13:27:20 wiz Exp $");
+__RCSID("$NetBSD: mount_tmpfs.c,v 1.26 2013/12/04 13:30:35 martin Exp $");
#endif /* not lint */
#include <sys/param.h>
#include <sys/mount.h>
#include <sys/stat.h>
+#include <sys/sysctl.h>
#include <fs/tmpfs/tmpfs_args.h>
@@ -66,6 +68,63 @@ static const struct mntopt mopts[] = {
/* --------------------------------------------------------------------- */
static void usage(void) __dead;
+static int64_t ram_fract(const char *arg);
+static int64_t ram_percent(const char *arg);
+static int64_t ram_factor(float f);
+
+/* --------------------------------------------------------------------- */
+
+/* return f * available system ram */
+static int64_t
+ram_factor(float f)
+{
+ uint64_t ram;
+ size_t len;
+
+ len = sizeof(ram);
+ if (sysctlbyname("hw.physmem64", &ram, &len, NULL, 0))
+ err(EXIT_FAILURE, "can't get \"hw.physmem64\": %s", strerror(errno));
+
+ return (int64_t)((float)ram * f);
+}
+
+/* return fraction of available ram given by arg */
+static int64_t
+ram_fract(const char *arg)
+{
+ char *endp;
+ float f;
+
+ f = strtof(arg, &endp);
+ if (endp && *endp != 0)
+ err(EXIT_FAILURE, "syntax error in ram fraction: ram/%s"
+ " at %s", arg, endp);
+ if (f <= 0.0f)
+ err(EXIT_FAILURE, "ram fraction must be a positive number:"
+ " ram/%s", arg);
+
+ return ram_factor(1.0f/f);
+}
+
+/* --------------------------------------------------------------------- */
+
+/* return percentage of available ram given by arg */
+static int64_t
+ram_percent(const char *arg)
+{
+ char *endp;
+ float f;
+
+ f = strtof(arg, &endp);
+ if (endp && *endp != 0)
+ err(EXIT_FAILURE, "syntax error in ram percentage: ram%%%s"
+ " at %s", arg, endp);
+ if (f <= 0.0f || f >= 100.0f)
+ err(EXIT_FAILURE, "ram percentage must be a between 0 and 100"
+ " ram%%%s", arg);
+
+ return ram_factor(f/100.0f);
+}
/* --------------------------------------------------------------------- */
@@ -122,7 +181,11 @@ mount_tmpfs_parseargs(int argc, char *ar
break;
case 's':
- if (dehumanize_number(optarg, &tmpnumber) == -1)
+ if (strncmp(optarg, "ram/", 4) == 0)
+ tmpnumber = ram_fract(optarg+4);
+ else if (strncmp(optarg, "ram%", 4) == 0)
+ tmpnumber = ram_percent(optarg+4);
+ else if (dehumanize_number(optarg, &tmpnumber) == -1)
err(EXIT_FAILURE, "failed to parse size `%s'",
optarg);
args->ta_size_max = tmpnumber;