dcavalca / rpms / util-linux

Forked from rpms/util-linux 2 years ago
Clone

Blame SOURCES/0142-lsmem-chmem-backport-new-commands.patch

5f5089
From 4fde7a853f61fa025a9d42a7571161a69fdae162 Mon Sep 17 00:00:00 2001
5f5089
From: Karel Zak <kzak@redhat.com>
5f5089
Date: Mon, 23 Oct 2017 15:54:54 +0200
5f5089
Subject: [PATCH] lsmem, chmem: backport new commands
5f5089
5f5089
Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1496421
5f5089
Signed-off-by: Karel Zak <kzak@redhat.com>
5f5089
---
5f5089
 configure.ac            |  19 ++
5f5089
 include/c.h             |  10 +
5f5089
 include/path.h          |   5 +-
5f5089
 include/strutils.h      |   1 +
5f5089
 lib/path.c              |  36 ++-
5f5089
 lib/strutils.c          |  17 +-
5f5089
 sys-utils/Makemodule.am |  14 +
5f5089
 sys-utils/chmem.8       | 114 ++++++++
5f5089
 sys-utils/chmem.c       | 447 +++++++++++++++++++++++++++++++
5f5089
 sys-utils/lsmem.1       |  95 +++++++
5f5089
 sys-utils/lsmem.c       | 687 ++++++++++++++++++++++++++++++++++++++++++++++++
5f5089
 11 files changed, 1432 insertions(+), 13 deletions(-)
5f5089
 create mode 100644 sys-utils/chmem.8
5f5089
 create mode 100644 sys-utils/chmem.c
5f5089
 create mode 100644 sys-utils/lsmem.1
5f5089
 create mode 100644 sys-utils/lsmem.c
5f5089
5f5089
diff --git a/configure.ac b/configure.ac
5f5089
index 96c5838cf..d561e01d0 100644
5f5089
--- a/configure.ac
5f5089
+++ b/configure.ac
5f5089
@@ -1138,6 +1138,25 @@ UL_REQUIRES_SYSCALL_CHECK([pivot_root], [UL_CHECK_SYSCALL([pivot_root])])
5f5089
 AM_CONDITIONAL(BUILD_PIVOT_ROOT, test "x$build_pivot_root" = xyes)
5f5089
 
5f5089
 
5f5089
+AC_ARG_ENABLE([lsmem],
5f5089
+  AS_HELP_STRING([--disable-lsmem], [do not build lsmem]),
5f5089
+  [], enable_lsmem=check
5f5089
+)
5f5089
+UL_BUILD_INIT([lsmem])
5f5089
+UL_REQUIRES_LINUX([lsmem])
5f5089
+UL_REQUIRES_BUILD([lsmem], [libsmartcols])
5f5089
+AM_CONDITIONAL([BUILD_LSMEM], [test "x$build_lsmem" = xyes])
5f5089
+
5f5089
+
5f5089
+AC_ARG_ENABLE([chmem],
5f5089
+  AS_HELP_STRING([--disable-chmem], [do not build chmem]),
5f5089
+  [], enable_chmem=check
5f5089
+)
5f5089
+UL_BUILD_INIT([chmem])
5f5089
+UL_REQUIRES_LINUX([chmem])
5f5089
+AM_CONDITIONAL([BUILD_CHMEM], [test "x$build_chmem" = xyes])
5f5089
+
5f5089
+
5f5089
 AC_ARG_ENABLE([elvtune],
5f5089
   AS_HELP_STRING([--enable-elvtune], [build elvtune (only works with 2.2 and 2.4 kernels)]),
5f5089
   [], enable_elvtune=no
5f5089
diff --git a/include/c.h b/include/c.h
5f5089
index 8ff61b484..124035ea5 100644
5f5089
--- a/include/c.h
5f5089
+++ b/include/c.h
5f5089
@@ -302,6 +302,16 @@ static inline int usleep(useconds_t usec)
5f5089
 
5f5089
 #define UTIL_LINUX_VERSION _("%s from %s\n"), program_invocation_short_name, PACKAGE_STRING
5f5089
 
5f5089
+/* backported to RHEL */
5f5089
+#define USAGE_COLUMNS    _("\nAvailable output columns:\n")
5f5089
+#define USAGE_OPTSTR_HELP     _("display this help")
5f5089
+#define USAGE_OPTSTR_VERSION  _("display version")
5f5089
+#define USAGE_HELP_OPTIONS(marg_dsc) \
5f5089
+		"%-" #marg_dsc "s%s\n" \
5f5089
+		"%-" #marg_dsc "s%s\n" \
5f5089
+		, " -h, --help",    USAGE_OPTSTR_HELP \
5f5089
+		, " -V, --version", USAGE_OPTSTR_VERSION
5f5089
+
5f5089
 /*
5f5089
  * scanf modifiers for "strings allocation"
5f5089
  */
5f5089
diff --git a/include/path.h b/include/path.h
5f5089
index 45da692f8..1ab5724fc 100644
5f5089
--- a/include/path.h
5f5089
+++ b/include/path.h
5f5089
@@ -4,6 +4,10 @@
5f5089
 #include <stdio.h>
5f5089
 #include <stdint.h>
5f5089
 
5f5089
+extern int path_set_prefix(const char *);
5f5089
+
5f5089
+extern const char *path_get(const char *path, ...);
5f5089
+
5f5089
 extern char *path_strdup(const char *path, ...)
5f5089
 			__attribute__ ((__format__ (__printf__, 1, 2)));
5f5089
 extern FILE *path_fopen(const char *mode, int exit_on_err, const char *path, ...)
5f5089
@@ -27,7 +31,6 @@ extern cpu_set_t *path_read_cpuset(int, const char *path, ...)
5f5089
 			      __attribute__ ((__format__ (__printf__, 2, 3)));
5f5089
 extern cpu_set_t *path_read_cpulist(int, const char *path, ...)
5f5089
 			       __attribute__ ((__format__ (__printf__, 2, 3)));
5f5089
-extern void path_set_prefix(const char *);
5f5089
 #endif /* HAVE_CPU_SET_T */
5f5089
 
5f5089
 #endif /* UTIL_LINUX_PATH_H */
5f5089
diff --git a/include/strutils.h b/include/strutils.h
5f5089
index aa7b95f96..1f028e4ed 100644
5f5089
--- a/include/strutils.h
5f5089
+++ b/include/strutils.h
5f5089
@@ -25,6 +25,7 @@ extern uint32_t strtou32_or_err(const char *str, const char *errmesg);
5f5089
 
5f5089
 extern int64_t strtos64_or_err(const char *str, const char *errmesg);
5f5089
 extern uint64_t strtou64_or_err(const char *str, const char *errmesg);
5f5089
+extern uint64_t strtox64_or_err(const char *str, const char *errmesg);
5f5089
 
5f5089
 extern double strtod_or_err(const char *str, const char *errmesg);
5f5089
 
5f5089
diff --git a/lib/path.c b/lib/path.c
5f5089
index e47d31418..49ca9b5e0 100644
5f5089
--- a/lib/path.c
5f5089
+++ b/lib/path.c
5f5089
@@ -38,6 +38,20 @@
5f5089
 static size_t prefixlen;
5f5089
 static char pathbuf[PATH_MAX];
5f5089
 
5f5089
+int
5f5089
+path_set_prefix(const char *prefix)
5f5089
+{
5f5089
+	size_t len = strlen(prefix);
5f5089
+
5f5089
+	if (len >= sizeof(pathbuf) - 1) {
5f5089
+		errno = ENAMETOOLONG;
5f5089
+		return -1;
5f5089
+	}
5f5089
+	prefixlen = len;
5f5089
+	strcpy(pathbuf, prefix);
5f5089
+	return 0;
5f5089
+}
5f5089
+
5f5089
 static const char *
5f5089
 path_vcreate(const char *path, va_list ap)
5f5089
 {
5f5089
@@ -49,6 +63,19 @@ path_vcreate(const char *path, va_list ap)
5f5089
 	return pathbuf;
5f5089
 }
5f5089
 
5f5089
+const char *
5f5089
+path_get(const char *path, ...)
5f5089
+{
5f5089
+	const char *p;
5f5089
+	va_list ap;
5f5089
+
5f5089
+	va_start(ap, path);
5f5089
+	p = path_vcreate(path, ap);
5f5089
+	va_end(ap);
5f5089
+
5f5089
+	return p;
5f5089
+}
5f5089
+
5f5089
 char *
5f5089
 path_strdup(const char *path, ...)
5f5089
 {
5f5089
@@ -246,13 +273,6 @@ path_read_cpulist(int maxcpus, const char *path, ...)
5f5089
 
5f5089
 	return set;
5f5089
 }
5f5089
-
5f5089
 #endif /* HAVE_CPU_SET_T */
5f5089
 
5f5089
-void
5f5089
-path_set_prefix(const char *prefix)
5f5089
-{
5f5089
-	prefixlen = strlen(prefix);
5f5089
-	strncpy(pathbuf, prefix, sizeof(pathbuf));
5f5089
-	pathbuf[sizeof(pathbuf) - 1] = '\0';
5f5089
-}
5f5089
+
5f5089
diff --git a/lib/strutils.c b/lib/strutils.c
5f5089
index 4b8a8130d..2458a2c2f 100644
5f5089
--- a/lib/strutils.c
5f5089
+++ b/lib/strutils.c
5f5089
@@ -237,27 +237,36 @@ err:
5f5089
 	errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str);
5f5089
 }
5f5089
 
5f5089
-uint64_t strtou64_or_err(const char *str, const char *errmesg)
5f5089
+static uint64_t _strtou64_or_err(const char *str, const char *errmesg, int base)
5f5089
 {
5f5089
 	uintmax_t num;
5f5089
 	char *end = NULL;
5f5089
 
5f5089
+	errno = 0;
5f5089
 	if (str == NULL || *str == '\0')
5f5089
 		goto err;
5f5089
-	errno = 0;
5f5089
-	num = strtoumax(str, &end, 10);
5f5089
+	num = strtoumax(str, &end, base);
5f5089
 
5f5089
 	if (errno || str == end || (end && *end))
5f5089
 		goto err;
5f5089
 
5f5089
 	return num;
5f5089
 err:
5f5089
-	if (errno)
5f5089
+	if (errno == ERANGE)
5f5089
 		err(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str);
5f5089
 
5f5089
 	errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str);
5f5089
 }
5f5089
 
5f5089
+uint64_t strtou64_or_err(const char *str, const char *errmesg)
5f5089
+{
5f5089
+	return _strtou64_or_err(str, errmesg, 10);
5f5089
+}
5f5089
+
5f5089
+uint64_t strtox64_or_err(const char *str, const char *errmesg)
5f5089
+{
5f5089
+	return _strtou64_or_err(str, errmesg, 16);
5f5089
+}
5f5089
 
5f5089
 double strtod_or_err(const char *str, const char *errmesg)
5f5089
 {
5f5089
diff --git a/sys-utils/Makemodule.am b/sys-utils/Makemodule.am
5f5089
index 0496b84e3..21585ce80 100644
5f5089
--- a/sys-utils/Makemodule.am
5f5089
+++ b/sys-utils/Makemodule.am
5f5089
@@ -1,3 +1,17 @@
5f5089
+if BUILD_LSMEM
5f5089
+usrbin_exec_PROGRAMS += lsmem
5f5089
+dist_man_MANS += sys-utils/lsmem.1
5f5089
+lsmem_SOURCES = sys-utils/lsmem.c
5f5089
+lsmem_LDADD = $(LDADD) libcommon.la libsmartcols.la
5f5089
+lsmem_CFLAGS = $(AM_CFLAGS) -I$(ul_libsmartcols_incdir)
5f5089
+endif
5f5089
+
5f5089
+if BUILD_CHMEM
5f5089
+usrbin_exec_PROGRAMS += chmem
5f5089
+dist_man_MANS += sys-utils/chmem.8
5f5089
+chmem_SOURCES = sys-utils/chmem.c
5f5089
+chmem_LDADD = $(LDADD) libcommon.la
5f5089
+endif
5f5089
 
5f5089
 usrbin_exec_PROGRAMS += flock
5f5089
 dist_man_MANS += sys-utils/flock.1
5f5089
diff --git a/sys-utils/chmem.8 b/sys-utils/chmem.8
5f5089
new file mode 100644
5f5089
index 000000000..dae7413d4
5f5089
--- /dev/null
5f5089
+++ b/sys-utils/chmem.8
5f5089
@@ -0,0 +1,114 @@
5f5089
+.TH CHMEM 8 "October 2016" "util-linux" "System Administration"
5f5089
+.SH NAME
5f5089
+chmem \- configure memory
5f5089
+.SH SYNOPSIS
5f5089
+.B chmem
5f5089
+.RB [ \-h "] [" \-V "] [" \-v "] [" \-e | \-d "]"
5f5089
+[\fISIZE\fP|\fIRANGE\fP|\fB\-b\fP \fIBLOCKRANGE\fP]
5f5089
+[-z ZONE]
5f5089
+.SH DESCRIPTION
5f5089
+The chmem command sets a particular size or range of memory online or offline.
5f5089
+.
5f5089
+.IP "\(hy" 2
5f5089
+Specify \fISIZE\fP as <size>[m|M|g|G]. With m or M, <size> specifies the memory
5f5089
+size in MiB (1024 x 1024 bytes). With g or G, <size> specifies the memory size
5f5089
+in GiB (1024 x 1024 x 1024 bytes). The default unit is MiB.
5f5089
+.
5f5089
+.IP "\(hy" 2
5f5089
+Specify \fIRANGE\fP in the form 0x<start>-0x<end> as shown in the output of the
5f5089
+\fBlsmem\fP command. <start> is the hexadecimal address of the first byte and <end>
5f5089
+is the hexadecimal address of the last byte in the memory range.
5f5089
+.
5f5089
+.IP "\(hy" 2
5f5089
+Specify \fIBLOCKRANGE\fP in the form <first>-<last> or <block> as shown in the
5f5089
+output of the \fBlsmem\fP command. <first> is the number of the first memory block
5f5089
+and <last> is the number of the last memory block in the memory
5f5089
+range. Alternatively a single block can be specified. \fIBLOCKRANGE\fP requires
5f5089
+the \fB--blocks\fP option.
5f5089
+.
5f5089
+.IP "\(hy" 2
5f5089
+Specify \fIZONE\fP as the name of a memory zone, as shown in the output of the
5f5089
+\fBlsmem -o +ZONES\fP command. The output shows one or more valid memory zones
5f5089
+for each memory range. If multiple zones are shown, then the memory range
5f5089
+currently belongs to the first zone. By default, chmem will set memory online
5f5089
+to the zone Movable, if this is among the valid zones. This default can be
5f5089
+changed by specifying the \fB--zone\fP option with another valid zone.
5f5089
+For memory ballooning, it is recommended to select the zone Movable for memory
5f5089
+online and offline, if possible. Memory in this zone is much more likely to be
5f5089
+able to be offlined again, but it cannot be used for arbitrary kernel
5f5089
+allocations, only for migratable pages (e.g. anonymous and page cache pages).
5f5089
+Use the \fB\-\-help\fR option to see all available zones.
5f5089
+.
5f5089
+.PP
5f5089
+\fISIZE\fP and \fIRANGE\fP must be aligned to the Linux memory block size, as
5f5089
+shown in the output of the \fBlsmem\fP command.
5f5089
+
5f5089
+Setting memory online can fail for various reasons. On virtualized systems it
5f5089
+can fail if the hypervisor does not have enough memory left, for example
5f5089
+because memory was overcommitted. Setting memory offline can fail if Linux
5f5089
+cannot free the memory. If only part of the requested memory can be set online
5f5089
+or offline, a message tells you how much memory was set online or offline
5f5089
+instead of the requested amount.
5f5089
+
5f5089
+When setting memory online \fBchmem\fP starts with the lowest memory block
5f5089
+numbers. When setting memory offline \fBchmem\fP starts with the highest memory
5f5089
+block numbers.
5f5089
+.SH OPTIONS
5f5089
+.TP
5f5089
+.BR \-b ", " \-\-blocks
5f5089
+Use a \fIBLOCKRANGE\fP parameter instead of \fIRANGE\fP or \fISIZE\fP for the
5f5089
+\fB--enable\fP and \fB--disable\fP options.
5f5089
+.TP
5f5089
+.BR \-d ", " \-\-disable
5f5089
+Set the specified \fIRANGE\fP, \fISIZE\fP, or \fIBLOCKRANGE\fP of memory offline.
5f5089
+.TP
5f5089
+.BR \-e ", " \-\-enable
5f5089
+Set the specified \fIRANGE\fP, \fISIZE\fP, or \fIBLOCKRANGE\fP of memory online.
5f5089
+.TP
5f5089
+.BR \-z ", " \-\-zone
5f5089
+Select the memory \fIZONE\fP where to set the specified \fIRANGE\fP, \fISIZE\fP,
5f5089
+or \fIBLOCKRANGE\fP of memory online or offline. By default, memory will be set
5f5089
+online to the zone Movable, if possible.
5f5089
+.TP
5f5089
+.BR \-h ", " \-\-help
5f5089
+Print a short help text, then exit.
5f5089
+.TP
5f5089
+.BR \-v ", " \-\-verbose
5f5089
+Verbose mode. Causes \fBchmem\fP to print debugging messages about it's
5f5089
+progress.
5f5089
+.TP
5f5089
+.BR \-V ", " \-\-version
5f5089
+Print the version number, then exit.
5f5089
+.SH RETURN CODES
5f5089
+.B chmem
5f5089
+has the following return codes:
5f5089
+.TP
5f5089
+.BR 0
5f5089
+success
5f5089
+.TP
5f5089
+.BR 1
5f5089
+failure
5f5089
+.TP
5f5089
+.BR 64
5f5089
+partial success
5f5089
+.SH EXAMPLES
5f5089
+.TP
5f5089
+.B chmem --enable 1024
5f5089
+This command requests 1024 MiB of memory to be set online.
5f5089
+.TP
5f5089
+.B chmem -e 2g
5f5089
+This command requests 2 GiB of memory to be set online.
5f5089
+.TP
5f5089
+.B chmem --disable 0x00000000e4000000-0x00000000f3ffffff
5f5089
+This command requests the memory range starting with 0x00000000e4000000
5f5089
+and ending with 0x00000000f3ffffff to be set offline.
5f5089
+.TP
5f5089
+.B chmem -b -d 10
5f5089
+This command requests the memory block number 10 to be set offline.
5f5089
+.SH SEE ALSO
5f5089
+.BR lsmem (1)
5f5089
+.SH AVAILABILITY
5f5089
+The \fBchmem\fP command is part of the util-linux package and is available from
5f5089
+.UR https://\:www.kernel.org\:/pub\:/linux\:/utils\:/util-linux/
5f5089
+Linux Kernel Archive
5f5089
+.UE .
5f5089
diff --git a/sys-utils/chmem.c b/sys-utils/chmem.c
5f5089
new file mode 100644
5f5089
index 000000000..0e5e84727
5f5089
--- /dev/null
5f5089
+++ b/sys-utils/chmem.c
5f5089
@@ -0,0 +1,447 @@
5f5089
+/*
5f5089
+ * chmem - Memory configuration tool
5f5089
+ *
5f5089
+ * Copyright IBM Corp. 2016
5f5089
+ *
5f5089
+ * This program is free software; you can redistribute it and/or modify
5f5089
+ * it under the terms of the GNU General Public License as published by
5f5089
+ * the Free Software Foundation; either version 2 of the License, or
5f5089
+ * (at your option) any later version.
5f5089
+ *
5f5089
+ * This program is distributed in the hope that it would be useful,
5f5089
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
5f5089
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5f5089
+ * GNU General Public License for more details.
5f5089
+ *
5f5089
+ * You should have received a copy of the GNU General Public License along
5f5089
+ * with this program; if not, write to the Free Software Foundation, Inc.,
5f5089
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
5f5089
+ */
5f5089
+#include <stdio.h>
5f5089
+#include <unistd.h>
5f5089
+#include <stdlib.h>
5f5089
+#include <getopt.h>
5f5089
+#include <assert.h>
5f5089
+#include <dirent.h>
5f5089
+#include <ctype.h>
5f5089
+
5f5089
+#include "c.h"
5f5089
+#include "nls.h"
5f5089
+#include "path.h"
5f5089
+#include "strutils.h"
5f5089
+#include "strv.h"
5f5089
+#include "optutils.h"
5f5089
+#include "closestream.h"
5f5089
+#include "xalloc.h"
5f5089
+
5f5089
+/* partial success, otherwise we return regular EXIT_{SUCCESS,FAILURE} */
5f5089
+#define CHMEM_EXIT_SOMEOK		64
5f5089
+
5f5089
+#define _PATH_SYS_MEMORY		"/sys/devices/system/memory"
5f5089
+#define _PATH_SYS_MEMORY_BLOCK_SIZE	_PATH_SYS_MEMORY "/block_size_bytes"
5f5089
+
5f5089
+struct chmem_desc {
5f5089
+	struct dirent	**dirs;
5f5089
+	int		ndirs;
5f5089
+	uint64_t	block_size;
5f5089
+	uint64_t	start;
5f5089
+	uint64_t	end;
5f5089
+	uint64_t	size;
5f5089
+	unsigned int	use_blocks : 1;
5f5089
+	unsigned int	is_size	   : 1;
5f5089
+	unsigned int	verbose	   : 1;
5f5089
+	unsigned int	have_zones : 1;
5f5089
+};
5f5089
+
5f5089
+enum {
5f5089
+	CMD_MEMORY_ENABLE = 0,
5f5089
+	CMD_MEMORY_DISABLE,
5f5089
+	CMD_NONE
5f5089
+};
5f5089
+
5f5089
+enum zone_id {
5f5089
+	ZONE_DMA = 0,
5f5089
+	ZONE_DMA32,
5f5089
+	ZONE_NORMAL,
5f5089
+	ZONE_HIGHMEM,
5f5089
+	ZONE_MOVABLE,
5f5089
+	ZONE_DEVICE,
5f5089
+};
5f5089
+
5f5089
+static char *zone_names[] = {
5f5089
+	[ZONE_DMA]	= "DMA",
5f5089
+	[ZONE_DMA32]	= "DMA32",
5f5089
+	[ZONE_NORMAL]	= "Normal",
5f5089
+	[ZONE_HIGHMEM]	= "Highmem",
5f5089
+	[ZONE_MOVABLE]	= "Movable",
5f5089
+	[ZONE_DEVICE]	= "Device",
5f5089
+};
5f5089
+
5f5089
+/*
5f5089
+ * name must be null-terminated
5f5089
+ */
5f5089
+static int zone_name_to_id(const char *name)
5f5089
+{
5f5089
+	size_t i;
5f5089
+
5f5089
+	for (i = 0; i < ARRAY_SIZE(zone_names); i++) {
5f5089
+		if (!strcasecmp(name, zone_names[i]))
5f5089
+			return i;
5f5089
+	}
5f5089
+	return -1;
5f5089
+}
5f5089
+
5f5089
+static void idxtostr(struct chmem_desc *desc, uint64_t idx, char *buf, size_t bufsz)
5f5089
+{
5f5089
+	uint64_t start, end;
5f5089
+
5f5089
+	start = idx * desc->block_size;
5f5089
+	end = start + desc->block_size - 1;
5f5089
+	snprintf(buf, bufsz,
5f5089
+		 _("Memory Block %"PRIu64" (0x%016"PRIx64"-0x%016"PRIx64")"),
5f5089
+		 idx, start, end);
5f5089
+}
5f5089
+
5f5089
+static int chmem_size(struct chmem_desc *desc, int enable, int zone_id)
5f5089
+{
5f5089
+	char *name, *onoff, line[BUFSIZ], str[BUFSIZ];
5f5089
+	uint64_t size, index;
5f5089
+	const char *zn;
5f5089
+	int i, rc;
5f5089
+
5f5089
+	size = desc->size;
5f5089
+	onoff = enable ? "online" : "offline";
5f5089
+	i = enable ? 0 : desc->ndirs - 1;
5f5089
+
5f5089
+	if (enable && zone_id >= 0) {
5f5089
+		if (zone_id == ZONE_MOVABLE)
5f5089
+			onoff = "online_movable";
5f5089
+		else
5f5089
+			onoff = "online_kernel";
5f5089
+	}
5f5089
+
5f5089
+	for (; i >= 0 && i < desc->ndirs && size; i += enable ? 1 : -1) {
5f5089
+		name = desc->dirs[i]->d_name;
5f5089
+		index = strtou64_or_err(name + 6, _("Failed to parse index"));
5f5089
+		path_read_str(line, sizeof(line), _PATH_SYS_MEMORY "/%s/state", name);
5f5089
+		if (strncmp(onoff, line, 6) == 0)
5f5089
+			continue;
5f5089
+
5f5089
+		if (desc->have_zones) {
5f5089
+			path_read_str(line, sizeof(line),
5f5089
+				      _PATH_SYS_MEMORY "/%s/valid_zones", name);
5f5089
+			if (zone_id >= 0) {
5f5089
+				zn = zone_names[zone_id];
5f5089
+				if (enable && !strcasestr(line, zn))
5f5089
+					continue;
5f5089
+				if (!enable && strncasecmp(line, zn, strlen(zn)))
5f5089
+					continue;
5f5089
+			} else if (enable) {
5f5089
+				/* By default, use zone Movable for online, if valid */
5f5089
+				if (strcasestr(line, zone_names[ZONE_MOVABLE]))
5f5089
+					onoff = "online_movable";
5f5089
+				else
5f5089
+					onoff = "online";
5f5089
+			}
5f5089
+		}
5f5089
+
5f5089
+		idxtostr(desc, index, str, sizeof(str));
5f5089
+		rc = path_write_str(onoff, _PATH_SYS_MEMORY"/%s/state", name);
5f5089
+		if (rc == -1 && desc->verbose) {
5f5089
+			if (enable)
5f5089
+				fprintf(stdout, _("%s enable failed\n"), str);
5f5089
+			else
5f5089
+				fprintf(stdout, _("%s disable failed\n"), str);
5f5089
+		} else if (rc == 0 && desc->verbose) {
5f5089
+			if (enable)
5f5089
+				fprintf(stdout, _("%s enabled\n"), str);
5f5089
+			else
5f5089
+				fprintf(stdout, _("%s disabled\n"), str);
5f5089
+		}
5f5089
+		if (rc == 0)
5f5089
+			size--;
5f5089
+	}
5f5089
+	if (size) {
5f5089
+		uint64_t bytes;
5f5089
+		char *sizestr;
5f5089
+
5f5089
+		bytes = (desc->size - size) * desc->block_size;
5f5089
+		sizestr = size_to_human_string(SIZE_SUFFIX_1LETTER, bytes);
5f5089
+		if (enable)
5f5089
+			warnx(_("Could only enable %s of memory"), sizestr);
5f5089
+		else
5f5089
+			warnx(_("Could only disable %s of memory"), sizestr);
5f5089
+		free(sizestr);
5f5089
+	}
5f5089
+	return size == 0 ? 0 : size == desc->size ? -1 : 1;
5f5089
+}
5f5089
+
5f5089
+static int chmem_range(struct chmem_desc *desc, int enable, int zone_id)
5f5089
+{
5f5089
+	char *name, *onoff, line[BUFSIZ], str[BUFSIZ];
5f5089
+	uint64_t index, todo;
5f5089
+	const char *zn;
5f5089
+	int i, rc;
5f5089
+
5f5089
+	todo = desc->end - desc->start + 1;
5f5089
+	onoff = enable ? "online" : "offline";
5f5089
+
5f5089
+	if (enable && zone_id >= 0) {
5f5089
+		if (zone_id == ZONE_MOVABLE)
5f5089
+			onoff = "online_movable";
5f5089
+		else
5f5089
+			onoff = "online_kernel";
5f5089
+	}
5f5089
+
5f5089
+	for (i = 0; i < desc->ndirs; i++) {
5f5089
+		name = desc->dirs[i]->d_name;
5f5089
+		index = strtou64_or_err(name + 6, _("Failed to parse index"));
5f5089
+		if (index < desc->start)
5f5089
+			continue;
5f5089
+		if (index > desc->end)
5f5089
+			break;
5f5089
+		idxtostr(desc, index, str, sizeof(str));
5f5089
+		path_read_str(line, sizeof(line), _PATH_SYS_MEMORY "/%s/state", name);
5f5089
+		if (strncmp(onoff, line, 6) == 0) {
5f5089
+			if (desc->verbose && enable)
5f5089
+				fprintf(stdout, _("%s already enabled\n"), str);
5f5089
+			else if (desc->verbose && !enable)
5f5089
+				fprintf(stdout, _("%s already disabled\n"), str);
5f5089
+			todo--;
5f5089
+			continue;
5f5089
+		}
5f5089
+
5f5089
+		if (desc->have_zones) {
5f5089
+			path_read_str(line, sizeof(line),
5f5089
+				      _PATH_SYS_MEMORY "/%s/valid_zones", name);
5f5089
+			if (zone_id >= 0) {
5f5089
+				zn = zone_names[zone_id];
5f5089
+				if (enable && !strcasestr(line, zn)) {
5f5089
+					warnx(_("%s enable failed: Zone mismatch"), str);
5f5089
+					continue;
5f5089
+				}
5f5089
+				if (!enable && strncasecmp(line, zn, strlen(zn))) {
5f5089
+					warnx(_("%s disable failed: Zone mismatch"), str);
5f5089
+					continue;
5f5089
+				}
5f5089
+			} else if (enable) {
5f5089
+				/* By default, use zone Movable for online, if valid */
5f5089
+				if (strcasestr(line, zone_names[ZONE_MOVABLE]))
5f5089
+					onoff = "online_movable";
5f5089
+				else
5f5089
+					onoff = "online";
5f5089
+			}
5f5089
+		}
5f5089
+
5f5089
+		rc = path_write_str(onoff, _PATH_SYS_MEMORY"/%s/state", name);
5f5089
+		if (rc == -1) {
5f5089
+			if (enable)
5f5089
+				warn(_("%s enable failed"), str);
5f5089
+			else
5f5089
+				warn(_("%s disable failed"), str);
5f5089
+		} else if (desc->verbose) {
5f5089
+			if (enable)
5f5089
+				fprintf(stdout, _("%s enabled\n"), str);
5f5089
+			else
5f5089
+				fprintf(stdout, _("%s disabled\n"), str);
5f5089
+		}
5f5089
+		if (rc == 0)
5f5089
+			todo--;
5f5089
+	}
5f5089
+	return todo == 0 ? 0 : todo == desc->end - desc->start + 1 ? -1 : 1;
5f5089
+}
5f5089
+
5f5089
+static int filter(const struct dirent *de)
5f5089
+{
5f5089
+	if (strncmp("memory", de->d_name, 6))
5f5089
+		return 0;
5f5089
+	return isdigit_string(de->d_name + 6);
5f5089
+}
5f5089
+
5f5089
+static void read_info(struct chmem_desc *desc)
5f5089
+{
5f5089
+	char line[BUFSIZ];
5f5089
+
5f5089
+	desc->ndirs = scandir(_PATH_SYS_MEMORY, &desc->dirs, filter, versionsort);
5f5089
+	if (desc->ndirs <= 0)
5f5089
+		err(EXIT_FAILURE, _("Failed to read %s"), _PATH_SYS_MEMORY);
5f5089
+	path_read_str(line, sizeof(line), _PATH_SYS_MEMORY_BLOCK_SIZE);
5f5089
+	desc->block_size = strtoumax(line, NULL, 16);
5f5089
+}
5f5089
+
5f5089
+static void parse_single_param(struct chmem_desc *desc, char *str)
5f5089
+{
5f5089
+	if (desc->use_blocks) {
5f5089
+		desc->start = strtou64_or_err(str, _("Failed to parse block number"));
5f5089
+		desc->end = desc->start;
5f5089
+		return;
5f5089
+	}
5f5089
+	desc->is_size = 1;
5f5089
+	desc->size = strtosize_or_err(str, _("Failed to parse size"));
5f5089
+	if (isdigit(str[strlen(str) - 1]))
5f5089
+		desc->size *= 1024*1024;
5f5089
+	if (desc->size % desc->block_size) {
5f5089
+		errx(EXIT_FAILURE, _("Size must be aligned to memory block size (%s)"),
5f5089
+		     size_to_human_string(SIZE_SUFFIX_1LETTER, desc->block_size));
5f5089
+	}
5f5089
+	desc->size /= desc->block_size;
5f5089
+}
5f5089
+
5f5089
+static void parse_range_param(struct chmem_desc *desc, char *start, char *end)
5f5089
+{
5f5089
+	if (desc->use_blocks) {
5f5089
+		desc->start = strtou64_or_err(start, _("Failed to parse start"));
5f5089
+		desc->end = strtou64_or_err(end, _("Failed to parse end"));
5f5089
+		return;
5f5089
+	}
5f5089
+	if (strlen(start) < 2 || start[1] != 'x')
5f5089
+		errx(EXIT_FAILURE, _("Invalid start address format: %s"), start);
5f5089
+	if (strlen(end) < 2 || end[1] != 'x')
5f5089
+		errx(EXIT_FAILURE, _("Invalid end address format: %s"), end);
5f5089
+	desc->start = strtox64_or_err(start, _("Failed to parse start address"));
5f5089
+	desc->end = strtox64_or_err(end, _("Failed to parse end address"));
5f5089
+	if (desc->start % desc->block_size || (desc->end + 1) % desc->block_size) {
5f5089
+		errx(EXIT_FAILURE,
5f5089
+		     _("Start address and (end address + 1) must be aligned to "
5f5089
+		       "memory block size (%s)"),
5f5089
+		     size_to_human_string(SIZE_SUFFIX_1LETTER, desc->block_size));
5f5089
+	}
5f5089
+	desc->start /= desc->block_size;
5f5089
+	desc->end /= desc->block_size;
5f5089
+}
5f5089
+
5f5089
+static void parse_parameter(struct chmem_desc *desc, char *param)
5f5089
+{
5f5089
+	char **split;
5f5089
+
5f5089
+	split = strv_split(param, "-");
5f5089
+	if (strv_length(split) > 2)
5f5089
+		errx(EXIT_FAILURE, _("Invalid parameter: %s"), param);
5f5089
+	if (strv_length(split) == 1)
5f5089
+		parse_single_param(desc, split[0]);
5f5089
+	else
5f5089
+		parse_range_param(desc, split[0], split[1]);
5f5089
+	strv_free(split);
5f5089
+	if (desc->start > desc->end)
5f5089
+		errx(EXIT_FAILURE, _("Invalid range: %s"), param);
5f5089
+}
5f5089
+
5f5089
+static void __attribute__((__noreturn__)) usage(void)
5f5089
+{
5f5089
+	FILE *out = stdout;
5f5089
+	size_t i;
5f5089
+
5f5089
+	fputs(USAGE_HEADER, out);
5f5089
+	fprintf(out, _(" %s [options] [SIZE|RANGE|BLOCKRANGE]\n"), program_invocation_short_name);
5f5089
+
5f5089
+	fputs(USAGE_SEPARATOR, out);
5f5089
+	fputs(_("Set a particular size or range of memory online or offline.\n"), out);
5f5089
+
5f5089
+	fputs(USAGE_OPTIONS, out);
5f5089
+	fputs(_(" -e, --enable       enable memory\n"), out);
5f5089
+	fputs(_(" -d, --disable      disable memory\n"), out);
5f5089
+	fputs(_(" -b, --blocks       use memory blocks\n"), out);
5f5089
+	fputs(_(" -z, --zone <name>  select memory zone (see below)\n"), out);
5f5089
+	fputs(_(" -v, --verbose      verbose output\n"), out);
5f5089
+	printf(USAGE_HELP_OPTIONS(20));
5f5089
+
5f5089
+	fputs(_("\nSupported zones:\n"), out);
5f5089
+	for (i = 0; i < ARRAY_SIZE(zone_names); i++)
5f5089
+		fprintf(out, " %s\n", zone_names[i]);
5f5089
+
5f5089
+	printf(USAGE_MAN_TAIL("chmem(8)"));
5f5089
+
5f5089
+	exit(EXIT_SUCCESS);
5f5089
+}
5f5089
+
5f5089
+int main(int argc, char **argv)
5f5089
+{
5f5089
+	struct chmem_desc _desc = { }, *desc = &_desc;
5f5089
+	int cmd = CMD_NONE, zone_id = -1;
5f5089
+	char *zone = NULL;
5f5089
+	int c, rc;
5f5089
+
5f5089
+	static const struct option longopts[] = {
5f5089
+		{"block",	no_argument,		NULL, 'b'},
5f5089
+		{"disable",	no_argument,		NULL, 'd'},
5f5089
+		{"enable",	no_argument,		NULL, 'e'},
5f5089
+		{"help",	no_argument,		NULL, 'h'},
5f5089
+		{"verbose",	no_argument,		NULL, 'v'},
5f5089
+		{"version",	no_argument,		NULL, 'V'},
5f5089
+		{"zone",	required_argument,	NULL, 'z'},
5f5089
+		{NULL,		0,			NULL, 0}
5f5089
+	};
5f5089
+
5f5089
+	static const ul_excl_t excl[] = {	/* rows and cols in ASCII order */
5f5089
+		{ 'd','e' },
5f5089
+		{ 0 }
5f5089
+	};
5f5089
+	int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
5f5089
+
5f5089
+	setlocale(LC_ALL, "");
5f5089
+	bindtextdomain(PACKAGE, LOCALEDIR);
5f5089
+	textdomain(PACKAGE);
5f5089
+	atexit(close_stdout);
5f5089
+
5f5089
+	read_info(desc);
5f5089
+
5f5089
+	while ((c = getopt_long(argc, argv, "bdehvVz:", longopts, NULL)) != -1) {
5f5089
+
5f5089
+		err_exclusive_options(c, longopts, excl, excl_st);
5f5089
+
5f5089
+		switch (c) {
5f5089
+		case 'd':
5f5089
+			cmd = CMD_MEMORY_DISABLE;
5f5089
+			break;
5f5089
+		case 'e':
5f5089
+			cmd = CMD_MEMORY_ENABLE;
5f5089
+			break;
5f5089
+		case 'b':
5f5089
+			desc->use_blocks = 1;
5f5089
+			break;
5f5089
+		case 'h':
5f5089
+			usage();
5f5089
+			break;
5f5089
+		case 'v':
5f5089
+			desc->verbose = 1;
5f5089
+			break;
5f5089
+		case 'V':
5f5089
+			printf(UTIL_LINUX_VERSION);
5f5089
+			return EXIT_SUCCESS;
5f5089
+		case 'z':
5f5089
+			zone = xstrdup(optarg);
5f5089
+			break;
5f5089
+		default:
5f5089
+			errtryhelp(EXIT_FAILURE);
5f5089
+		}
5f5089
+	}
5f5089
+
5f5089
+	if ((argc == 1) || (argc != optind + 1) || (cmd == CMD_NONE)) {
5f5089
+		warnx(_("bad usage"));
5f5089
+		errtryhelp(EXIT_FAILURE);
5f5089
+	}
5f5089
+
5f5089
+	parse_parameter(desc, argv[optind]);
5f5089
+
5f5089
+	/* The valid_zones sysfs attribute was introduced with kernel 3.18 */
5f5089
+	if (path_exist(_PATH_SYS_MEMORY "/memory0/valid_zones"))
5f5089
+		desc->have_zones = 1;
5f5089
+	else if (zone)
5f5089
+		warnx(_("zone ignored, no valid_zones sysfs attribute present"));
5f5089
+
5f5089
+	if (zone && desc->have_zones) {
5f5089
+		zone_id = zone_name_to_id(zone);
5f5089
+		if (zone_id == -1) {
5f5089
+			warnx(_("unknown memory zone: %s"), zone);
5f5089
+			errtryhelp(EXIT_FAILURE);
5f5089
+		}
5f5089
+	}
5f5089
+
5f5089
+	if (desc->is_size)
5f5089
+		rc = chmem_size(desc, cmd == CMD_MEMORY_ENABLE ? 1 : 0, zone_id);
5f5089
+	else
5f5089
+		rc = chmem_range(desc, cmd == CMD_MEMORY_ENABLE ? 1 : 0, zone_id);
5f5089
+
5f5089
+	return rc == 0 ? EXIT_SUCCESS :
5f5089
+		rc < 0 ? EXIT_FAILURE : CHMEM_EXIT_SOMEOK;
5f5089
+}
5f5089
diff --git a/sys-utils/lsmem.1 b/sys-utils/lsmem.1
5f5089
new file mode 100644
5f5089
index 000000000..3f5cd7d4b
5f5089
--- /dev/null
5f5089
+++ b/sys-utils/lsmem.1
5f5089
@@ -0,0 +1,95 @@
5f5089
+.TH LSMEM 1 "October 2016" "util-linux" "User Commands"
5f5089
+.SH NAME
5f5089
+lsmem \- list the ranges of available memory with their online status
5f5089
+.SH SYNOPSIS
5f5089
+.B lsmem
5f5089
+[options]
5f5089
+.SH DESCRIPTION
5f5089
+The \fBlsmem\fP command lists the ranges of available memory with their online
5f5089
+status. The listed memory blocks correspond to the memory block representation
5f5089
+in sysfs. The command also shows the memory block size and the amount of memory
5f5089
+in online and offline state.
5f5089
+
5f5089
+The default output compatible with original implementation from s390-tools, but
5f5089
+it's strongly recommended to avoid using default outputs in your scripts.
5f5089
+Always explicitly define expected columns by using the \fB\-\-output\fR option
5f5089
+together with a columns list in environments where a stable output is required.
5f5089
+
5f5089
+The \fBlsmem\fP command lists a new memory range always when the current memory
5f5089
+block distinguish from the previous block by STATE, REMOVABLE, NODE or ZONES
5f5089
+attribute.  This default behavior is possible to override by the
5f5089
+\fB\-\-split\fR option (e.g. \fBlsmem \-\-split=STATE,ZONES\fR).  The special
5f5089
+word "none" may be used to ignore all differences between memory blocks and to
5f5089
+create as large as possible continuous ranges.  The opposite semantic is
5f5089
+\fB\-\-all\fR to list individual memory blocks.  The default split policy is
5f5089
+subject to change.  Always explicitly use \fB\-\-split\fR in environments where
5f5089
+a stable output is required.
5f5089
+
5f5089
+Note that some output columns may provide inaccurate information if a split policy
5f5089
+forces \fBlsmem\fP to ignore diffrences in some attributes. For example if you
5f5089
+merge removable and non-removable memory blocks to the one range than all
5f5089
+the range will be marked as non-removable on \fBlsmem\fP output.
5f5089
+
5f5089
+Not all columns are supported on all systems.  If an unsupported column is
5f5089
+specified, \fBlsmem\fP prints the column but does not provide any data for it.
5f5089
+
5f5089
+Use the \fB\-\-help\fR option to see the columns description.
5f5089
+
5f5089
+.SH OPTIONS
5f5089
+.TP
5f5089
+.BR \-a ", " \-\-all
5f5089
+List each individual memory block, instead of combining memory blocks with
5f5089
+similar attributes.
5f5089
+.TP
5f5089
+.BR \-b , " \-\-bytes"
5f5089
+Print the SIZE column in bytes rather than in a human-readable format.
5f5089
+.TP
5f5089
+.BR \-h ", " \-\-help
5f5089
+Display help text and exit.
5f5089
+.TP
5f5089
+.BR \-n , " \-\-noheadings"
5f5089
+Do not print a header line.
5f5089
+.TP
5f5089
+.BR \-o , " \-\-output " \fIlist\fP
5f5089
+Specify which output columns to print.  Use \fB\-\-help\fR
5f5089
+to get a list of all supported columns.
5f5089
+The default list of columns may be extended if \fIlist\fP is
5f5089
+specified in the format \fB+\fIlist\fP (e.g. \fBlsmem \-o +NODE\fP).
5f5089
+.TP
5f5089
+.BR \-P , " \-\-pairs"
5f5089
+Produce output in the form of key="value" pairs.
5f5089
+All potentially unsafe characters are hex-escaped (\\x).
5f5089
+.TP
5f5089
+.BR \-r , " \-\-raw"
5f5089
+Produce output in raw format.  All potentially unsafe characters are hex-escaped
5f5089
+(\\x).
5f5089
+.TP
5f5089
+.BR \-S , " \-\-split " \fIlist\fP
5f5089
+Specify which columns (attributes) use to split memory blocks to ranges.  The
5f5089
+supported columns are STATE, REMOVABLE, NODE and ZONES, or "none". The another columns are
5f5089
+silently ignored. For more details see DESCRIPTION above.
5f5089
+.TP
5f5089
+.BR \-s , " \-\-sysroot " \fIdirectory\fP
5f5089
+Gather memory data for a Linux instance other than the instance from which the
5f5089
+\fBlsmem\fP command is issued.  The specified \fIdirectory\fP is the system
5f5089
+root of the Linux instance to be inspected.
5f5089
+.TP
5f5089
+.BR \-V ", " \-\-version
5f5089
+Display version information and exit.
5f5089
+.TP
5f5089
+\fB\-\-summary\fR[=\fIwhen\fR]
5f5089
+This option controls summary lines output.  The optional argument \fIwhen\fP can be
5f5089
+\fBnever\fR, \fBalways\fR or \fBonly\fR.  If the \fIwhen\fR argument is
5f5089
+omitted, it defaults to \fB"only"\fR. The summary output is suppresed for
5f5089
+\fB\-\-raw\fR and \fB\-\-pairs\fR.
5f5089
+.SH AUTHOR
5f5089
+.B lsmem
5f5089
+was originally written by Gerald Schaefer for s390-tools in Perl. The C version
5f5089
+for util-linux was written by Clemens von Mann, Heiko Carstens and Karel Zak.
5f5089
+.SH SEE ALSO
5f5089
+.BR chmem (8)
5f5089
+.SH AVAILABILITY
5f5089
+The \fBlsmem\fP command is part of the util-linux package and is available from
5f5089
+.UR https://\:www.kernel.org\:/pub\:/linux\:/utils\:/util-linux/
5f5089
+Linux Kernel Archive
5f5089
+.UE .
5f5089
diff --git a/sys-utils/lsmem.c b/sys-utils/lsmem.c
5f5089
new file mode 100644
5f5089
index 000000000..34a2847af
5f5089
--- /dev/null
5f5089
+++ b/sys-utils/lsmem.c
5f5089
@@ -0,0 +1,687 @@
5f5089
+/*
5f5089
+ * lsmem - Show memory configuration
5f5089
+ *
5f5089
+ * Copyright IBM Corp. 2016
5f5089
+ * Copyright (C) 2016 Karel Zak <kzak@redhat.com>
5f5089
+ *
5f5089
+ * This program is free software; you can redistribute it and/or modify
5f5089
+ * it under the terms of the GNU General Public License as published by
5f5089
+ * the Free Software Foundation; either version 2 of the License, or
5f5089
+ * (at your option) any later version.
5f5089
+ *
5f5089
+ * This program is distributed in the hope that it would be useful,
5f5089
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
5f5089
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5f5089
+ * GNU General Public License for more details.
5f5089
+ *
5f5089
+ * You should have received a copy of the GNU General Public License along
5f5089
+ * with this program; if not, write to the Free Software Foundation, Inc.,
5f5089
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
5f5089
+ */
5f5089
+#include <c.h>
5f5089
+#include <nls.h>
5f5089
+#include <path.h>
5f5089
+#include <strutils.h>
5f5089
+#include <closestream.h>
5f5089
+#include <xalloc.h>
5f5089
+#include <getopt.h>
5f5089
+#include <stdio.h>
5f5089
+#include <stdlib.h>
5f5089
+#include <dirent.h>
5f5089
+#include <fcntl.h>
5f5089
+#include <inttypes.h>
5f5089
+#include <assert.h>
5f5089
+#include <optutils.h>
5f5089
+#include <libsmartcols.h>
5f5089
+
5f5089
+#define _PATH_SYS_MEMORY		"/sys/devices/system/memory"
5f5089
+#define _PATH_SYS_MEMORY_BLOCK_SIZE	_PATH_SYS_MEMORY "/block_size_bytes"
5f5089
+
5f5089
+#define MEMORY_STATE_ONLINE		0
5f5089
+#define MEMORY_STATE_OFFLINE		1
5f5089
+#define MEMORY_STATE_GOING_OFFLINE	2
5f5089
+#define MEMORY_STATE_UNKNOWN		3
5f5089
+
5f5089
+enum zone_id {
5f5089
+	ZONE_DMA = 0,
5f5089
+	ZONE_DMA32,
5f5089
+	ZONE_NORMAL,
5f5089
+	ZONE_HIGHMEM,
5f5089
+	ZONE_MOVABLE,
5f5089
+	ZONE_DEVICE,
5f5089
+	ZONE_NONE,
5f5089
+	ZONE_UNKNOWN,
5f5089
+	MAX_NR_ZONES,
5f5089
+};
5f5089
+
5f5089
+struct memory_block {
5f5089
+	uint64_t	index;
5f5089
+	uint64_t	count;
5f5089
+	int		state;
5f5089
+	int		node;
5f5089
+	int		nr_zones;
5f5089
+	int		zones[MAX_NR_ZONES];
5f5089
+	unsigned int	removable:1;
5f5089
+};
5f5089
+
5f5089
+struct lsmem {
5f5089
+	struct dirent		**dirs;
5f5089
+	int			ndirs;
5f5089
+	struct memory_block	*blocks;
5f5089
+	int			nblocks;
5f5089
+	uint64_t		block_size;
5f5089
+	uint64_t		mem_online;
5f5089
+	uint64_t		mem_offline;
5f5089
+
5f5089
+	struct libscols_table	*table;
5f5089
+	unsigned int		have_nodes : 1,
5f5089
+				raw : 1,
5f5089
+				export : 1,
5f5089
+				noheadings : 1,
5f5089
+				summary : 1,
5f5089
+				list_all : 1,
5f5089
+				bytes : 1,
5f5089
+				want_summary : 1,
5f5089
+				want_table : 1,
5f5089
+				split_by_node : 1,
5f5089
+				split_by_state : 1,
5f5089
+				split_by_removable : 1,
5f5089
+				split_by_zones : 1,
5f5089
+				have_zones : 1;
5f5089
+};
5f5089
+
5f5089
+
5f5089
+enum {
5f5089
+	COL_RANGE,
5f5089
+	COL_SIZE,
5f5089
+	COL_STATE,
5f5089
+	COL_REMOVABLE,
5f5089
+	COL_BLOCK,
5f5089
+	COL_NODE,
5f5089
+	COL_ZONES,
5f5089
+};
5f5089
+
5f5089
+static char *zone_names[] = {
5f5089
+	[ZONE_DMA]	= "DMA",
5f5089
+	[ZONE_DMA32]	= "DMA32",
5f5089
+	[ZONE_NORMAL]	= "Normal",
5f5089
+	[ZONE_HIGHMEM]	= "Highmem",
5f5089
+	[ZONE_MOVABLE]	= "Movable",
5f5089
+	[ZONE_DEVICE]	= "Device",
5f5089
+	[ZONE_NONE]	= "None",	/* block contains more than one zone, can't be offlined */
5f5089
+	[ZONE_UNKNOWN]	= "Unknown",
5f5089
+};
5f5089
+
5f5089
+/* column names */
5f5089
+struct coldesc {
5f5089
+	const char	*name;		/* header */
5f5089
+	double		whint;		/* width hint (N < 1 is in percent of termwidth) */
5f5089
+	int		flags;		/* SCOLS_FL_* */
5f5089
+	const char      *help;
5f5089
+
5f5089
+	int	sort_type;		/* SORT_* */
5f5089
+};
5f5089
+
5f5089
+/* columns descriptions */
5f5089
+static struct coldesc coldescs[] = {
5f5089
+	[COL_RANGE]	= { "RANGE", 0, 0, N_("start and end address of the memory range")},
5f5089
+	[COL_SIZE]	= { "SIZE", 5, SCOLS_FL_RIGHT, N_("size of the memory range")},
5f5089
+	[COL_STATE]	= { "STATE", 0, SCOLS_FL_RIGHT, N_("online status of the memory range")},
5f5089
+	[COL_REMOVABLE]	= { "REMOVABLE", 0, SCOLS_FL_RIGHT, N_("memory is removable")},
5f5089
+	[COL_BLOCK]	= { "BLOCK", 0, SCOLS_FL_RIGHT, N_("memory block number or blocks range")},
5f5089
+	[COL_NODE]	= { "NODE", 0, SCOLS_FL_RIGHT, N_("numa node of memory")},
5f5089
+	[COL_ZONES]	= { "ZONES", 0, SCOLS_FL_RIGHT, N_("valid zones for the memory range")},
5f5089
+};
5f5089
+
5f5089
+/* columns[] array specifies all currently wanted output column. The columns
5f5089
+ * are defined by coldescs[] array and you can specify (on command line) each
5f5089
+ * column twice. That's enough, dynamically allocated array of the columns is
5f5089
+ * unnecessary overkill and over-engineering in this case */
5f5089
+static int columns[ARRAY_SIZE(coldescs) * 2];
5f5089
+static size_t ncolumns;
5f5089
+
5f5089
+static inline size_t err_columns_index(size_t arysz, size_t idx)
5f5089
+{
5f5089
+	if (idx >= arysz)
5f5089
+		errx(EXIT_FAILURE, _("too many columns specified, "
5f5089
+				     "the limit is %zu columns"),
5f5089
+				arysz - 1);
5f5089
+	return idx;
5f5089
+}
5f5089
+
5f5089
+/*
5f5089
+ * name must be null-terminated
5f5089
+ */
5f5089
+static int zone_name_to_id(const char *name)
5f5089
+{
5f5089
+	size_t i;
5f5089
+
5f5089
+	for (i = 0; i < ARRAY_SIZE(zone_names); i++) {
5f5089
+		if (!strcasecmp(name, zone_names[i]))
5f5089
+			return i;
5f5089
+	}
5f5089
+	return ZONE_UNKNOWN;
5f5089
+}
5f5089
+
5f5089
+#define add_column(ary, n, id)	\
5f5089
+		((ary)[ err_columns_index(ARRAY_SIZE(ary), (n)) ] = (id))
5f5089
+
5f5089
+static int column_name_to_id(const char *name, size_t namesz)
5f5089
+{
5f5089
+	size_t i;
5f5089
+
5f5089
+	for (i = 0; i < ARRAY_SIZE(coldescs); i++) {
5f5089
+		const char *cn = coldescs[i].name;
5f5089
+
5f5089
+		if (!strncasecmp(name, cn, namesz) && !*(cn + namesz))
5f5089
+			return i;
5f5089
+	}
5f5089
+	warnx(_("unknown column: %s"), name);
5f5089
+	return -1;
5f5089
+}
5f5089
+
5f5089
+static inline int get_column_id(int num)
5f5089
+{
5f5089
+	assert(num >= 0);
5f5089
+	assert((size_t) num < ncolumns);
5f5089
+	assert(columns[num] < (int) ARRAY_SIZE(coldescs));
5f5089
+
5f5089
+	return columns[num];
5f5089
+}
5f5089
+
5f5089
+static inline struct coldesc *get_column_desc(int num)
5f5089
+{
5f5089
+	return &coldescs[ get_column_id(num) ];
5f5089
+}
5f5089
+
5f5089
+static inline void reset_split_policy(struct lsmem *l, int enable)
5f5089
+{
5f5089
+	l->split_by_state = enable;
5f5089
+	l->split_by_node = enable;
5f5089
+	l->split_by_removable = enable;
5f5089
+	l->split_by_zones = enable;
5f5089
+}
5f5089
+
5f5089
+static void add_scols_line(struct lsmem *lsmem, struct memory_block *blk)
5f5089
+{
5f5089
+	size_t i;
5f5089
+	struct libscols_line *line;
5f5089
+
5f5089
+	line = scols_table_new_line(lsmem->table, NULL);
5f5089
+	if (!line)
5f5089
+		err_oom();
5f5089
+
5f5089
+	for (i = 0; i < ncolumns; i++) {
5f5089
+		char *str = NULL;
5f5089
+
5f5089
+		switch (get_column_id(i)) {
5f5089
+		case COL_RANGE:
5f5089
+		{
5f5089
+			uint64_t start = blk->index * lsmem->block_size;
5f5089
+			uint64_t size = blk->count * lsmem->block_size;
5f5089
+			xasprintf(&str, "0x%016"PRIx64"-0x%016"PRIx64, start, start + size - 1);
5f5089
+			break;
5f5089
+		}
5f5089
+		case COL_SIZE:
5f5089
+			if (lsmem->bytes)
5f5089
+				xasprintf(&str, "%"PRId64, (uint64_t) blk->count * lsmem->block_size);
5f5089
+			else
5f5089
+				str = size_to_human_string(SIZE_SUFFIX_1LETTER,
5f5089
+						(uint64_t) blk->count * lsmem->block_size);
5f5089
+			break;
5f5089
+		case COL_STATE:
5f5089
+			str = xstrdup(
5f5089
+				blk->state == MEMORY_STATE_ONLINE ? _("online") :
5f5089
+				blk->state == MEMORY_STATE_OFFLINE ? _("offline") :
5f5089
+				blk->state == MEMORY_STATE_GOING_OFFLINE ? _("on->off") :
5f5089
+				"?");
5f5089
+			break;
5f5089
+		case COL_REMOVABLE:
5f5089
+			if (blk->state == MEMORY_STATE_ONLINE)
5f5089
+				str = xstrdup(blk->removable ? _("yes") : _("no"));
5f5089
+			else
5f5089
+				str = xstrdup("-");
5f5089
+			break;
5f5089
+		case COL_BLOCK:
5f5089
+			if (blk->count == 1)
5f5089
+				xasprintf(&str, "%"PRId64, blk->index);
5f5089
+			else
5f5089
+				xasprintf(&str, "%"PRId64"-%"PRId64,
5f5089
+					 blk->index, blk->index + blk->count - 1);
5f5089
+			break;
5f5089
+		case COL_NODE:
5f5089
+			if (lsmem->have_nodes)
5f5089
+				xasprintf(&str, "%d", blk->node);
5f5089
+			else
5f5089
+				str = xstrdup("-");
5f5089
+			break;
5f5089
+		case COL_ZONES:
5f5089
+			if (lsmem->have_zones) {
5f5089
+				char valid_zones[BUFSIZ];
5f5089
+				int j, zone_id;
5f5089
+
5f5089
+				valid_zones[0] = '\0';
5f5089
+				for (j = 0; j < blk->nr_zones; j++) {
5f5089
+					zone_id = blk->zones[j];
5f5089
+					if (strlen(valid_zones) +
5f5089
+					    strlen(zone_names[zone_id]) > BUFSIZ - 2)
5f5089
+						break;
5f5089
+					strcat(valid_zones, zone_names[zone_id]);
5f5089
+					if (j + 1 < blk->nr_zones)
5f5089
+						strcat(valid_zones, "/");
5f5089
+				}
5f5089
+				str = xstrdup(valid_zones);
5f5089
+			} else
5f5089
+				str = xstrdup("-");
5f5089
+			break;
5f5089
+		}
5f5089
+
5f5089
+		if (str && scols_line_refer_data(line, i, str) != 0)
5f5089
+			err_oom();
5f5089
+	}
5f5089
+}
5f5089
+
5f5089
+static void fill_scols_table(struct lsmem *lsmem)
5f5089
+{
5f5089
+	int i;
5f5089
+
5f5089
+	for (i = 0; i < lsmem->nblocks; i++)
5f5089
+		add_scols_line(lsmem, &lsmem->blocks[i]);
5f5089
+}
5f5089
+
5f5089
+static void print_summary(struct lsmem *lsmem)
5f5089
+{
5f5089
+	if (lsmem->bytes) {
5f5089
+		printf("%-23s %15"PRId64"\n",_("Memory block size:"), lsmem->block_size);
5f5089
+		printf("%-23s %15"PRId64"\n",_("Total online memory:"), lsmem->mem_online);
5f5089
+		printf("%-23s %15"PRId64"\n",_("Total offline memory:"), lsmem->mem_offline);
5f5089
+	} else {
5f5089
+		printf("%-23s %5s\n",_("Memory block size:"),
5f5089
+			size_to_human_string(SIZE_SUFFIX_1LETTER, lsmem->block_size));
5f5089
+		printf("%-23s %5s\n",_("Total online memory:"),
5f5089
+			size_to_human_string(SIZE_SUFFIX_1LETTER, lsmem->mem_online));
5f5089
+		printf("%-23s %5s\n",_("Total offline memory:"),
5f5089
+			size_to_human_string(SIZE_SUFFIX_1LETTER, lsmem->mem_offline));
5f5089
+	}
5f5089
+}
5f5089
+
5f5089
+static int memory_block_get_node(char *name)
5f5089
+{
5f5089
+	struct dirent *de;
5f5089
+	const char *path;
5f5089
+	DIR *dir;
5f5089
+	int node;
5f5089
+
5f5089
+	path = path_get(_PATH_SYS_MEMORY"/%s", name);
5f5089
+	if (!path || !(dir= opendir(path)))
5f5089
+		err(EXIT_FAILURE, _("Failed to open %s"), path ? path : name);
5f5089
+
5f5089
+	node = -1;
5f5089
+	while ((de = readdir(dir)) != NULL) {
5f5089
+		if (strncmp("node", de->d_name, 4))
5f5089
+			continue;
5f5089
+		if (!isdigit_string(de->d_name + 4))
5f5089
+			continue;
5f5089
+		node = strtol(de->d_name + 4, NULL, 10);
5f5089
+		break;
5f5089
+	}
5f5089
+	closedir(dir);
5f5089
+	return node;
5f5089
+}
5f5089
+
5f5089
+static void memory_block_read_attrs(struct lsmem *lsmem, char *name,
5f5089
+				    struct memory_block *blk)
5f5089
+{
5f5089
+	char *token = NULL;
5f5089
+	char line[BUFSIZ];
5f5089
+	int i;
5f5089
+
5f5089
+	blk->count = 1;
5f5089
+	blk->index = strtoumax(name + 6, NULL, 10); /* get <num> of "memory<num>" */
5f5089
+	blk->removable = path_read_u64(_PATH_SYS_MEMORY"/%s/removable", name);
5f5089
+	blk->state = MEMORY_STATE_UNKNOWN;
5f5089
+
5f5089
+	path_read_str(line, sizeof(line), _PATH_SYS_MEMORY"/%s/state", name);
5f5089
+	if (strcmp(line, "offline") == 0)
5f5089
+		blk->state = MEMORY_STATE_OFFLINE;
5f5089
+	else if (strcmp(line, "online") == 0)
5f5089
+		blk->state = MEMORY_STATE_ONLINE;
5f5089
+	else if (strcmp(line, "going-offline") == 0)
5f5089
+		blk->state = MEMORY_STATE_GOING_OFFLINE;
5f5089
+
5f5089
+	if (lsmem->have_nodes)
5f5089
+		blk->node = memory_block_get_node(name);
5f5089
+
5f5089
+	blk->nr_zones = 0;
5f5089
+	if (lsmem->have_zones) {
5f5089
+		path_read_str(line, sizeof(line), _PATH_SYS_MEMORY"/%s/valid_zones", name);
5f5089
+		token = strtok(line, " ");
5f5089
+	}
5f5089
+	for (i = 0; i < MAX_NR_ZONES; i++) {
5f5089
+		if (token) {
5f5089
+			blk->zones[i] = zone_name_to_id(token);
5f5089
+			blk->nr_zones++;
5f5089
+			token = strtok(NULL, " ");
5f5089
+		}
5f5089
+	}
5f5089
+}
5f5089
+
5f5089
+static int is_mergeable(struct lsmem *lsmem, struct memory_block *blk)
5f5089
+{
5f5089
+	struct memory_block *curr;
5f5089
+	int i;
5f5089
+
5f5089
+	if (!lsmem->nblocks)
5f5089
+		return 0;
5f5089
+	curr = &lsmem->blocks[lsmem->nblocks - 1];
5f5089
+	if (lsmem->list_all)
5f5089
+		return 0;
5f5089
+	if (curr->index + curr->count != blk->index)
5f5089
+		return 0;
5f5089
+	if (lsmem->split_by_state && curr->state != blk->state)
5f5089
+		return 0;
5f5089
+	if (lsmem->split_by_removable && curr->removable != blk->removable)
5f5089
+		return 0;
5f5089
+	if (lsmem->split_by_node && lsmem->have_nodes) {
5f5089
+		if (curr->node != blk->node)
5f5089
+			return 0;
5f5089
+	}
5f5089
+	if (lsmem->split_by_zones && lsmem->have_zones) {
5f5089
+		if (curr->nr_zones != blk->nr_zones)
5f5089
+			return 0;
5f5089
+		for (i = 0; i < curr->nr_zones; i++) {
5f5089
+			if (curr->zones[i] == ZONE_UNKNOWN ||
5f5089
+			    curr->zones[i] != blk->zones[i])
5f5089
+				return 0;
5f5089
+		}
5f5089
+	}
5f5089
+	return 1;
5f5089
+}
5f5089
+
5f5089
+static void read_info(struct lsmem *lsmem)
5f5089
+{
5f5089
+	struct memory_block blk;
5f5089
+	char line[BUFSIZ];
5f5089
+	int i;
5f5089
+
5f5089
+	path_read_str(line, sizeof(line), _PATH_SYS_MEMORY_BLOCK_SIZE);
5f5089
+	lsmem->block_size = strtoumax(line, NULL, 16);
5f5089
+
5f5089
+	for (i = 0; i < lsmem->ndirs; i++) {
5f5089
+		memory_block_read_attrs(lsmem, lsmem->dirs[i]->d_name, &blk);
5f5089
+		if (is_mergeable(lsmem, &blk)) {
5f5089
+			lsmem->blocks[lsmem->nblocks - 1].count++;
5f5089
+			continue;
5f5089
+		}
5f5089
+		lsmem->nblocks++;
5f5089
+		lsmem->blocks = xrealloc(lsmem->blocks, lsmem->nblocks * sizeof(blk));
5f5089
+		*&lsmem->blocks[lsmem->nblocks - 1] = blk;
5f5089
+	}
5f5089
+	for (i = 0; i < lsmem->nblocks; i++) {
5f5089
+		if (lsmem->blocks[i].state == MEMORY_STATE_ONLINE)
5f5089
+			lsmem->mem_online += lsmem->block_size * lsmem->blocks[i].count;
5f5089
+		else
5f5089
+			lsmem->mem_offline += lsmem->block_size * lsmem->blocks[i].count;
5f5089
+	}
5f5089
+}
5f5089
+
5f5089
+static int memory_block_filter(const struct dirent *de)
5f5089
+{
5f5089
+	if (strncmp("memory", de->d_name, 6))
5f5089
+		return 0;
5f5089
+	return isdigit_string(de->d_name + 6);
5f5089
+}
5f5089
+
5f5089
+static void read_basic_info(struct lsmem *lsmem)
5f5089
+{
5f5089
+	const char *dir;
5f5089
+
5f5089
+	if (!path_exist(_PATH_SYS_MEMORY_BLOCK_SIZE))
5f5089
+		errx(EXIT_FAILURE, _("This system does not support memory blocks"));
5f5089
+
5f5089
+	dir = path_get(_PATH_SYS_MEMORY);
5f5089
+	if (!dir)
5f5089
+		err(EXIT_FAILURE, _("Failed to read %s"), _PATH_SYS_MEMORY);
5f5089
+
5f5089
+	lsmem->ndirs = scandir(dir, &lsmem->dirs, memory_block_filter, versionsort);
5f5089
+	if (lsmem->ndirs <= 0)
5f5089
+		err(EXIT_FAILURE, _("Failed to read %s"), _PATH_SYS_MEMORY);
5f5089
+
5f5089
+	if (memory_block_get_node(lsmem->dirs[0]->d_name) != -1)
5f5089
+		lsmem->have_nodes = 1;
5f5089
+
5f5089
+	/* The valid_zones sysfs attribute was introduced with kernel 3.18 */
5f5089
+	if (path_exist(_PATH_SYS_MEMORY "/memory0/valid_zones"))
5f5089
+		lsmem->have_zones = 1;
5f5089
+}
5f5089
+
5f5089
+static void __attribute__((__noreturn__)) usage(void)
5f5089
+{
5f5089
+	FILE *out = stdout;
5f5089
+	size_t i;
5f5089
+
5f5089
+	fputs(USAGE_HEADER, out);
5f5089
+	fprintf(out, _(" %s [options]\n"), program_invocation_short_name);
5f5089
+
5f5089
+	fputs(USAGE_SEPARATOR, out);
5f5089
+	fputs(_("List the ranges of available memory with their online status.\n"), out);
5f5089
+
5f5089
+	fputs(USAGE_OPTIONS, out);
5f5089
+	fputs(_(" -P, --pairs          use key=\"value\" output format\n"), out);
5f5089
+	fputs(_(" -a, --all            list each individual memory block\n"), out);
5f5089
+	fputs(_(" -b, --bytes          print SIZE in bytes rather than in human readable format\n"), out);
5f5089
+	fputs(_(" -n, --noheadings     don't print headings\n"), out);
5f5089
+	fputs(_(" -o, --output <list>  output columns\n"), out);
5f5089
+	fputs(_(" -r, --raw            use raw output format\n"), out);
5f5089
+	fputs(_(" -S, --split <list>   split ranges by specified columns\n"), out);
5f5089
+	fputs(_(" -s, --sysroot <dir>  use the specified directory as system root\n"), out);
5f5089
+	fputs(_("     --summary[=when] print summary information (never,always or only)\n"), out);
5f5089
+
5f5089
+	fputs(USAGE_SEPARATOR, out);
5f5089
+	printf(USAGE_HELP_OPTIONS(22));
5f5089
+
5f5089
+	fputs(USAGE_COLUMNS, out);
5f5089
+	for (i = 0; i < ARRAY_SIZE(coldescs); i++)
5f5089
+		fprintf(out, " %10s  %s\n", coldescs[i].name, _(coldescs[i].help));
5f5089
+
5f5089
+	printf(USAGE_MAN_TAIL("lsmem(1)"));
5f5089
+
5f5089
+	exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
5f5089
+}
5f5089
+
5f5089
+int main(int argc, char **argv)
5f5089
+{
5f5089
+	struct lsmem _lsmem = {
5f5089
+			.want_table = 1,
5f5089
+			.want_summary = 1
5f5089
+		}, *lsmem = &_lsmem;
5f5089
+
5f5089
+	const char *outarg = NULL, *splitarg = NULL;
5f5089
+	int c;
5f5089
+	size_t i;
5f5089
+
5f5089
+	enum {
5f5089
+		LSMEM_OPT_SUMARRY = CHAR_MAX + 1
5f5089
+	};
5f5089
+
5f5089
+	static const struct option longopts[] = {
5f5089
+		{"all",		no_argument,		NULL, 'a'},
5f5089
+		{"bytes",	no_argument,		NULL, 'b'},
5f5089
+		{"help",	no_argument,		NULL, 'h'},
5f5089
+		{"noheadings",	no_argument,		NULL, 'n'},
5f5089
+		{"output",	required_argument,	NULL, 'o'},
5f5089
+		{"pairs",	no_argument,		NULL, 'P'},
5f5089
+		{"raw",		no_argument,		NULL, 'r'},
5f5089
+		{"sysroot",	required_argument,	NULL, 's'},
5f5089
+		{"split",       required_argument,      NULL, 'S'},
5f5089
+		{"version",	no_argument,		NULL, 'V'},
5f5089
+		{"summary",     optional_argument,	NULL, LSMEM_OPT_SUMARRY },
5f5089
+		{NULL,		0,			NULL, 0}
5f5089
+	};
5f5089
+	static const ul_excl_t excl[] = {	/* rows and cols in ASCII order */
5f5089
+		{ 'J', 'P', 'r' },
5f5089
+		{ 'S', 'a' },
5f5089
+		{ 0 }
5f5089
+	};
5f5089
+	int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
5f5089
+
5f5089
+	setlocale(LC_ALL, "");
5f5089
+	bindtextdomain(PACKAGE, LOCALEDIR);
5f5089
+	textdomain(PACKAGE);
5f5089
+	atexit(close_stdout);
5f5089
+
5f5089
+	while ((c = getopt_long(argc, argv, "abhJno:PrS:s:V", longopts, NULL)) != -1) {
5f5089
+
5f5089
+		err_exclusive_options(c, longopts, excl, excl_st);
5f5089
+
5f5089
+		switch (c) {
5f5089
+		case 'a':
5f5089
+			lsmem->list_all = 1;
5f5089
+			break;
5f5089
+		case 'b':
5f5089
+			lsmem->bytes = 1;
5f5089
+			break;
5f5089
+		case 'h':
5f5089
+			usage();
5f5089
+			break;
5f5089
+		case 'n':
5f5089
+			lsmem->noheadings = 1;
5f5089
+			break;
5f5089
+		case 'o':
5f5089
+			outarg = optarg;
5f5089
+			break;
5f5089
+		case 'P':
5f5089
+			lsmem->export = 1;
5f5089
+			lsmem->want_summary = 0;
5f5089
+			break;
5f5089
+		case 'r':
5f5089
+			lsmem->raw = 1;
5f5089
+			lsmem->want_summary = 0;
5f5089
+			break;
5f5089
+		case 's':
5f5089
+			if(path_set_prefix(optarg))
5f5089
+				err(EXIT_FAILURE, _("invalid argument to %s"), "--sysroot");
5f5089
+			break;
5f5089
+		case 'S':
5f5089
+			splitarg = optarg;
5f5089
+			break;
5f5089
+		case 'V':
5f5089
+			printf(UTIL_LINUX_VERSION);
5f5089
+			return 0;
5f5089
+		case LSMEM_OPT_SUMARRY:
5f5089
+			if (optarg) {
5f5089
+				if (strcmp(optarg, "never") == 0)
5f5089
+					lsmem->want_summary = 0;
5f5089
+				else if (strcmp(optarg, "only") == 0)
5f5089
+					lsmem->want_table = 0;
5f5089
+				else if (strcmp(optarg, "always") == 0)
5f5089
+					lsmem->want_summary = 1;
5f5089
+				else
5f5089
+					errx(EXIT_FAILURE, _("unsupported --summary argument"));
5f5089
+			} else
5f5089
+				lsmem->want_table = 0;
5f5089
+			break;
5f5089
+		default:
5f5089
+			errtryhelp(EXIT_FAILURE);
5f5089
+		}
5f5089
+	}
5f5089
+
5f5089
+	if (argc != optind) {
5f5089
+		warnx(_("bad usage"));
5f5089
+		errtryhelp(EXIT_FAILURE);
5f5089
+	}
5f5089
+
5f5089
+	if (lsmem->want_table + lsmem->want_summary == 0)
5f5089
+		errx(EXIT_FAILURE, _("options --{raw,pairs} and --summary=only are mutually exclusive"));
5f5089
+
5f5089
+	/* Shortcut to avoid scols machinery on --summary=only */
5f5089
+	if (lsmem->want_table == 0 && lsmem->want_summary) {
5f5089
+		read_basic_info(lsmem);
5f5089
+		read_info(lsmem);
5f5089
+		print_summary(lsmem);
5f5089
+		return EXIT_SUCCESS;
5f5089
+	}
5f5089
+
5f5089
+	/*
5f5089
+	 * Default columns
5f5089
+	 */
5f5089
+	if (!ncolumns) {
5f5089
+		add_column(columns, ncolumns++, COL_RANGE);
5f5089
+		add_column(columns, ncolumns++, COL_SIZE);
5f5089
+		add_column(columns, ncolumns++, COL_STATE);
5f5089
+		add_column(columns, ncolumns++, COL_REMOVABLE);
5f5089
+		add_column(columns, ncolumns++, COL_BLOCK);
5f5089
+	}
5f5089
+
5f5089
+	if (outarg && string_add_to_idarray(outarg, columns, ARRAY_SIZE(columns),
5f5089
+					 (int *) &ncolumns, column_name_to_id) < 0)
5f5089
+		return EXIT_FAILURE;
5f5089
+
5f5089
+	/*
5f5089
+	 * Initialize output
5f5089
+	 */
5f5089
+	scols_init_debug(0);
5f5089
+
5f5089
+	if (!(lsmem->table = scols_new_table()))
5f5089
+		errx(EXIT_FAILURE, _("failed to initialize output table"));
5f5089
+	scols_table_enable_raw(lsmem->table, lsmem->raw);
5f5089
+	scols_table_enable_export(lsmem->table, lsmem->export);
5f5089
+	scols_table_enable_noheadings(lsmem->table, lsmem->noheadings);
5f5089
+
5f5089
+	for (i = 0; i < ncolumns; i++) {
5f5089
+		struct coldesc *ci = get_column_desc(i);
5f5089
+		if (!scols_table_new_column(lsmem->table, ci->name, ci->whint, ci->flags))
5f5089
+			err(EXIT_FAILURE, _("Failed to initialize output column"));
5f5089
+	}
5f5089
+
5f5089
+	if (splitarg) {
5f5089
+		int split[ARRAY_SIZE(coldescs)] = { 0 };
5f5089
+		static size_t nsplits = 0;
5f5089
+
5f5089
+		reset_split_policy(lsmem, 0);	/* disable all */
5f5089
+
5f5089
+		if (strcasecmp(splitarg, "none") == 0)
5f5089
+			;
5f5089
+		else if (string_add_to_idarray(splitarg, split, ARRAY_SIZE(split),
5f5089
+					(int *) &nsplits, column_name_to_id) < 0)
5f5089
+			return EXIT_FAILURE;
5f5089
+
5f5089
+		for (i = 0; i < nsplits; i++) {
5f5089
+			switch (split[i]) {
5f5089
+			case COL_STATE:
5f5089
+				lsmem->split_by_state = 1;
5f5089
+				break;
5f5089
+			case COL_NODE:
5f5089
+				lsmem->split_by_node = 1;
5f5089
+				break;
5f5089
+			case COL_REMOVABLE:
5f5089
+				lsmem->split_by_removable = 1;
5f5089
+				break;
5f5089
+			case COL_ZONES:
5f5089
+				lsmem->split_by_zones = 1;
5f5089
+				break;
5f5089
+			}
5f5089
+		}
5f5089
+	} else
5f5089
+		reset_split_policy(lsmem, 1); /* enable all */
5f5089
+
5f5089
+	/*
5f5089
+	 * Read data and print output
5f5089
+	 */
5f5089
+	read_basic_info(lsmem);
5f5089
+	read_info(lsmem);
5f5089
+
5f5089
+	if (lsmem->want_table) {
5f5089
+		fill_scols_table(lsmem);
5f5089
+		scols_print_table(lsmem->table);
5f5089
+
5f5089
+		if (lsmem->want_summary)
5f5089
+			fputc('\n', stdout);
5f5089
+	}
5f5089
+
5f5089
+	if (lsmem->want_summary)
5f5089
+		print_summary(lsmem);
5f5089
+
5f5089
+	scols_unref_table(lsmem->table);
5f5089
+	return 0;
5f5089
+}
5f5089
-- 
5f5089
2.13.6
5f5089