/*
* lsblk(8) - list block devices
*
- * Copyright (C) 2010,2011,2012 Red Hat, Inc. All rights reserved.
+ * Copyright (C) 2010-2018 Red Hat, Inc. All rights reserved.
* Written by Milan Broz <mbroz@redhat.com>
* Karel Zak <kzak@redhat.com>
*
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-
#include <stdio.h>
#include <errno.h>
#include <getopt.h>
#include "sysfs.h"
#include "closestream.h"
#include "optutils.h"
+#include "fileutils.h"
#include "lsblk.h"
COL_FSTYPE,
COL_FSUSED,
COL_FSUSEPERC,
+ COL_FSVERSION,
COL_TARGET,
COL_LABEL,
COL_UUID,
COL_PTUUID,
COL_PTTYPE,
COL_PARTTYPE,
+ COL_PARTTYPENAME,
COL_PARTLABEL,
COL_PARTUUID,
COL_PARTFLAGS,
double whint; /* width hint (N < 1 is in percent of termwidth) */
int flags; /* SCOLS_FL_* */
const char *help;
-
int type; /* COLTYPE_* */
};
/* columns descriptions */
static struct colinfo infos[] = {
- [COL_NAME] = { "NAME", 0.25, SCOLS_FL_TREE | SCOLS_FL_NOEXTREMES, N_("device name") },
+ [COL_NAME] = { "NAME", 0.25, SCOLS_FL_NOEXTREMES, N_("device name") },
[COL_KNAME] = { "KNAME", 0.3, 0, N_("internal kernel device name") },
[COL_PKNAME] = { "PKNAME", 0.3, 0, N_("internal parent kernel device name") },
[COL_PATH] = { "PATH", 0.3, 0, N_("path to the device node") },
[COL_FSTYPE] = { "FSTYPE", 0.1, SCOLS_FL_TRUNC, N_("filesystem type") },
[COL_FSUSED] = { "FSUSED", 5, SCOLS_FL_RIGHT, N_("filesystem size used") },
[COL_FSUSEPERC] = { "FSUSE%", 3, SCOLS_FL_RIGHT, N_("filesystem use percentage") },
+ [COL_FSVERSION] = { "FSVER", 0.1, SCOLS_FL_TRUNC, N_("filesystem version") },
[COL_TARGET] = { "MOUNTPOINT", 0.10, SCOLS_FL_TRUNC, N_("where the device is mounted") },
[COL_LABEL] = { "LABEL", 0.1, 0, N_("filesystem LABEL") },
[COL_PTUUID] = { "PTUUID", 36, 0, N_("partition table identifier (usually UUID)") },
[COL_PTTYPE] = { "PTTYPE", 0.1, 0, N_("partition table type") },
- [COL_PARTTYPE] = { "PARTTYPE", 36, 0, N_("partition type UUID") },
+ [COL_PARTTYPE] = { "PARTTYPE", 36, 0, N_("partition type code or UUID") },
+ [COL_PARTTYPENAME] = { "PARTTYPENAME", 0.1, 0, N_("partition type name") },
[COL_PARTLABEL] = { "PARTLABEL", 0.1, 0, N_("partition LABEL") },
[COL_PARTUUID] = { "PARTUUID", 36, 0, N_("partition UUID") },
[COL_PARTFLAGS] = { "PARTFLAGS", 36, 0, N_("partition flags") },
struct lsblk *lsblk; /* global handler */
-/* columns[] array specifies all currently wanted output column. The columns
+/*
+ * columns[] array specifies all currently wanted output column. The columns
* are defined by infos[] array and you can specify (on command line) each
* column twice. That's enough, dynamically allocated array of the columns is
- * unnecessary overkill and over-engineering in this case */
+ * unnecessary overkill and over-engineering in this case
+ */
static int columns[ARRAY_SIZE(infos) * 2];
static size_t ncolumns;
-
static inline void add_column(int id)
{
if (ncolumns >= ARRAY_SIZE(columns))
add_column(id);
}
+static void lsblk_init_debug(void)
+{
+ __UL_INIT_DEBUG_FROM_ENV(lsblk, LSBLK_DEBUG_, 0, LSBLK_DEBUG);
+}
+
+/*
+ * exclude/include devices filter based on major device numbers
+ */
static int excludes[256];
static size_t nexcludes;
static int includes[256];
static size_t nincludes;
-static void lsblk_init_debug(void)
-{
- __UL_INIT_DEBUG_FROM_ENV(lsblk, LSBLK_DEBUG_, 0, LSBLK_DEBUG);
-}
-
static int is_maj_excluded(int maj)
{
size_t i;
return 0;
}
-/* array with IDs of enabled columns */
+/* Converts column sequential number to column ID (COL_*) */
static int get_column_id(int num)
{
assert(num >= 0);
return columns[num];
}
+/* Returns column description for the column sequential number */
static struct colinfo *get_column_info(int num)
{
return &infos[ get_column_id(num) ];
}
+/* Converts column name (as defined in the infos[] to the column ID */
static int column_name_to_id(const char *name, size_t namesz)
{
size_t i;
return -1;
}
+/* Converts column ID (COL_*) to column sequential number */
static int column_id_to_number(int id)
{
size_t i;
return -1;
}
+/* Checks for DM prefix in the device name */
static int is_dm(const char *name)
{
return strncmp(name, "dm-", 3) ? 0 : 1;
}
-static struct dirent *xreaddir(DIR *dp)
-{
- struct dirent *d;
-
- assert(dp);
-
- while ((d = readdir(dp))) {
- if (!strcmp(d->d_name, ".") ||
- !strcmp(d->d_name, ".."))
- continue;
-
- /* blacklist here? */
- break;
- }
- return d;
-}
-
+/* Returns full pat to the device node (TODO: what about sysfs_blkdev_get_path()) */
static char *get_device_path(struct lsblk_device *dev)
{
char path[PATH_MAX];
{
char *res = NULL, *p;
- if (cxt->partition)
+ if (device_is_partition(dev))
return xstrdup("part");
if (is_dm(dev->name)) {
scols_cell_set_userdata(ce, data);
}
-static void set_sortdata_u64_from_string(struct libscols_line *ln, int col, const char *str)
+/* do not modify *data on any error */
+static void str2u64(const char *str, uint64_t *data)
{
- uint64_t x;
+ uintmax_t num;
+ char *end = NULL;
- if (!str || sscanf(str, "%"SCNu64, &x) != 1)
+ errno = 0;
+ if (str == NULL || *str == '\0')
return;
+ num = strtoumax(str, &end, 10);
- set_sortdata_u64(ln, col, x);
+ if (errno || str == end || (end && *end))
+ return;
+ *data = num;
}
static void unref_sortdata(struct libscols_table *tb)
static struct stat *device_get_stat(struct lsblk_device *dev)
{
- if (!dev->st.st_rdev)
- stat(dev->filename, &dev->st);
+ if (!dev->st.st_rdev
+ && stat(dev->filename, &dev->st) != 0)
+ return NULL;
return &dev->st;
}
-static void set_scols_data(
- struct lsblk_device *dev,
- struct lsblk_device *parent,
- int col,
- int id,
- struct libscols_line *ln)
+static int is_removable_device(struct lsblk_device *dev, struct lsblk_device *parent)
{
- struct lsblk_devprop *prop;
- int sort = 0;
- char *str = NULL;
+ struct path_cxt *pc;
+
+ if (dev->removable != -1)
+ goto done;
+ if (ul_path_scanf(dev->sysfs, "removable", "%d", &dev->removable) == 1)
+ goto done;
+
+ if (parent) {
+ pc = sysfs_blkdev_get_parent(dev->sysfs);
+ if (!pc)
+ goto done;
+
+ /* dev is partition and parent is whole-disk */
+ if (pc == parent->sysfs)
+ dev->removable = is_removable_device(parent, NULL);
+
+ /* parent is something else, use sysfs parent */
+ else if (ul_path_scanf(pc, "removable", "%d", &dev->removable) != 1)
+ dev->removable = 0;
+ }
+done:
+ if (dev->removable == -1)
+ dev->removable = 0;
+ return dev->removable;
+}
+
+static uint64_t device_get_discard_granularity(struct lsblk_device *dev)
+{
+ if (dev->discard_granularity == (uint64_t) -1
+ && ul_path_read_u64(dev->sysfs, &dev->discard_granularity,
+ "queue/discard_granularity") != 0)
+ dev->discard_granularity = 0;
+
+ return dev->discard_granularity;
+}
- if (lsblk->sort_id == id)
- sort = 1;
+/*
+ * Generates data (string) for column specified by column ID for specified device. If sortdata
+ * is not NULL then returns number usable to sort the column if the data are available for the
+ * column.
+ */
+static char *device_get_data(
+ struct lsblk_device *dev, /* device */
+ struct lsblk_device *parent, /* device parent as defined in the tree */
+ int id, /* column ID (COL_*) */
+ uint64_t *sortdata) /* returns sort data as number */
+{
+ struct lsblk_devprop *prop = NULL;
+ char *str = NULL;
switch(id) {
case COL_NAME:
str = xstrdup(dev->filename);
break;
case COL_OWNER:
- {
- struct stat *st = device_get_stat(dev);
- struct passwd *pw = st ? NULL : getpwuid(st->st_uid);
- if (pw)
- str = xstrdup(pw->pw_name);
+ if (lsblk->sysroot)
+ prop = lsblk_device_get_properties(dev);
+ if (prop && prop->owner) {
+ str = xstrdup(prop->owner);
+ } else {
+ struct stat *st = device_get_stat(dev);
+ struct passwd *pw = st ? getpwuid(st->st_uid) : NULL;
+ if (pw)
+ str = xstrdup(pw->pw_name);
+ }
break;
- }
case COL_GROUP:
- {
- struct stat *st = device_get_stat(dev);
- struct group *gr = st ? NULL : getgrgid(st->st_gid);
- if (gr)
- str = xstrdup(gr->gr_name);
+ if (lsblk->sysroot)
+ prop = lsblk_device_get_properties(dev);
+ if (prop && prop->group) {
+ str = xstrdup(prop->group);
+ } else {
+ struct stat *st = device_get_stat(dev);
+ struct group *gr = st ? getgrgid(st->st_gid) : NULL;
+ if (gr)
+ str = xstrdup(gr->gr_name);
+ }
break;
- }
case COL_MODE:
- {
- struct stat *st = device_get_stat(dev);
- char md[11] = { '\0' };
+ if (lsblk->sysroot)
+ prop = lsblk_device_get_properties(dev);
+ if (prop && prop->mode) {
+ str = xstrdup(prop->mode);
+ } else {
+ struct stat *st = device_get_stat(dev);
+ char md[11] = { '\0' };
- if (st)
- str = xstrdup(xstrmode(st->st_mode, md));
+ if (st)
+ str = xstrdup(xstrmode(st->st_mode, md));
+ }
break;
- }
case COL_MAJMIN:
if (is_parsable(lsblk))
xasprintf(&str, "%u:%u", dev->maj, dev->min);
else
xasprintf(&str, "%3u:%-3u", dev->maj, dev->min);
- if (sort)
- set_sortdata_u64(ln, col, makedev(dev->maj, dev->min));
+ if (sortdata)
+ *sortdata = makedev(dev->maj, dev->min);
break;
case COL_FSTYPE:
prop = lsblk_device_get_properties(dev);
case COL_FSUSEPERC:
str = get_vfs_attribute(dev, id);
break;
+ case COL_FSVERSION:
+ prop = lsblk_device_get_properties(dev);
+ if (prop && prop->fsversion)
+ str = xstrdup(prop->fsversion);
+ break;
case COL_TARGET:
- str = xstrdup(lsblk_device_get_mountpoint(dev));
+ {
+ char *s = lsblk_device_get_mountpoint(dev);
+ if (s)
+ str = xstrdup(s);
+ else
+ str = NULL;
break;
+ }
case COL_LABEL:
prop = lsblk_device_get_properties(dev);
if (prop && prop->label)
if (prop && prop->parttype)
str = xstrdup(prop->parttype);
break;
+ case COL_PARTTYPENAME:
+ prop = lsblk_device_get_properties(dev);
+ if (prop && prop->parttype && prop->pttype) {
+ const char *x = lsblk_parttype_code_to_string(
+ prop->parttype, prop->pttype);
+ if (x)
+ str = xstrdup(x);
+ }
+ break;
case COL_PARTLABEL:
prop = lsblk_device_get_properties(dev);
if (prop && prop->partlabel)
break;
case COL_RA:
ul_path_read_string(dev->sysfs, &str, "queue/read_ahead_kb");
- if (sort)
- set_sortdata_u64_from_string(ln, col, str);
+ if (sortdata)
+ str2u64(str, sortdata);
break;
case COL_RO:
str = xstrdup(is_readonly_device(dev) ? "1" : "0");
break;
case COL_RM:
- ul_path_read_string(dev->sysfs, &str, "removable");
- if (!str && sysfs_blkdev_get_parent(dev->sysfs))
- ul_path_read_string(sysfs_blkdev_get_parent(dev->sysfs),
- &str,
- "removable");
+ str = xstrdup(is_removable_device(dev, parent) ? "1" : "0");
break;
case COL_HOTPLUG:
str = sysfs_blkdev_is_hotpluggable(dev->sysfs) ? xstrdup("1") : xstrdup("0");
ul_path_read_string(dev->sysfs, &str, "queue/add_random");
break;
case COL_MODEL:
- if (!dev->partition && dev->nslaves == 0) {
+ if (!device_is_partition(dev) && dev->nslaves == 0) {
prop = lsblk_device_get_properties(dev);
if (prop && prop->model)
str = xstrdup(prop->model);
}
break;
case COL_SERIAL:
- if (!dev->partition && dev->nslaves == 0) {
+ if (!device_is_partition(dev) && dev->nslaves == 0) {
prop = lsblk_device_get_properties(dev);
if (prop && prop->serial)
str = xstrdup(prop->serial);
}
break;
case COL_REV:
- if (!dev->partition && dev->nslaves == 0)
+ if (!device_is_partition(dev) && dev->nslaves == 0)
ul_path_read_string(dev->sysfs, &str, "device/rev");
break;
case COL_VENDOR:
- if (!dev->partition && dev->nslaves == 0)
+ if (!device_is_partition(dev) && dev->nslaves == 0)
ul_path_read_string(dev->sysfs, &str, "device/vendor");
break;
case COL_SIZE:
xasprintf(&str, "%ju", dev->size);
else
str = size_to_human_string(SIZE_SUFFIX_1LETTER, dev->size);
- if (sort)
- set_sortdata_u64(ln, col, dev->size);
+ if (sortdata)
+ *sortdata = dev->size;
break;
case COL_STATE:
- if (!dev->partition && !dev->dm_name)
+ if (!device_is_partition(dev) && !dev->dm_name)
ul_path_read_string(dev->sysfs, &str, "device/state");
else if (dev->dm_name) {
int x = 0;
break;
case COL_ALIOFF:
ul_path_read_string(dev->sysfs, &str, "alignment_offset");
- if (sort)
- set_sortdata_u64_from_string(ln, col, str);
+ if (sortdata)
+ str2u64(str, sortdata);
break;
case COL_MINIO:
ul_path_read_string(dev->sysfs, &str, "queue/minimum_io_size");
- if (sort)
- set_sortdata_u64_from_string(ln, col, str);
+ if (sortdata)
+ str2u64(str, sortdata);
break;
case COL_OPTIO:
ul_path_read_string(dev->sysfs, &str, "queue/optimal_io_size");
- if (sort)
- set_sortdata_u64_from_string(ln, col, str);
+ if (sortdata)
+ str2u64(str, sortdata);
break;
case COL_PHYSEC:
ul_path_read_string(dev->sysfs, &str, "queue/physical_block_size");
- if (sort)
- set_sortdata_u64_from_string(ln, col, str);
+ if (sortdata)
+ str2u64(str, sortdata);
break;
case COL_LOGSEC:
ul_path_read_string(dev->sysfs, &str, "queue/logical_block_size");
- if (sort)
- set_sortdata_u64_from_string(ln, col, str);
+ if (sortdata)
+ str2u64(str, sortdata);
break;
case COL_SCHED:
str = get_scheduler(dev);
break;
case COL_RQ_SIZE:
ul_path_read_string(dev->sysfs, &str, "queue/nr_requests");
- if (sort)
- set_sortdata_u64_from_string(ln, col, str);
+ if (sortdata)
+ str2u64(str, sortdata);
break;
case COL_TYPE:
str = get_type(dev);
str = get_subsystems(dev);
break;
case COL_DALIGN:
- if (dev->discard)
+ if (device_get_discard_granularity(dev) > 0)
ul_path_read_string(dev->sysfs, &str, "discard_alignment");
if (!str)
str = xstrdup("0");
- if (sort)
- set_sortdata_u64_from_string(ln, col, str);
+ if (sortdata)
+ str2u64(str, sortdata);
break;
case COL_DGRAN:
if (lsblk->bytes) {
ul_path_read_string(dev->sysfs, &str, "queue/discard_granularity");
- if (sort)
- set_sortdata_u64_from_string(ln, col, str);
+ if (sortdata)
+ str2u64(str, sortdata);
} else {
- uint64_t x;
- if (ul_path_read_u64(dev->sysfs, &x, "queue/discard_granularity") == 0) {
- str = size_to_human_string(SIZE_SUFFIX_1LETTER, x);
- if (sort)
- set_sortdata_u64(ln, col, x);
- }
+ uint64_t x = device_get_discard_granularity(dev);
+ str = size_to_human_string(SIZE_SUFFIX_1LETTER, x);
+ if (sortdata)
+ *sortdata = x;
}
break;
case COL_DMAX:
if (lsblk->bytes) {
ul_path_read_string(dev->sysfs, &str, "queue/discard_max_bytes");
- if (sort)
- set_sortdata_u64_from_string(ln, col, str);
+ if (sortdata)
+ str2u64(str, sortdata);
} else {
uint64_t x;
if (ul_path_read_u64(dev->sysfs, &x, "queue/discard_max_bytes") == 0) {
str = size_to_human_string(SIZE_SUFFIX_1LETTER, x);
- if (sort)
- set_sortdata_u64(ln, col, x);
+ if (sortdata)
+ *sortdata = x;
}
}
break;
case COL_DZERO:
- if (dev->discard)
+ if (device_get_discard_granularity(dev) > 0)
ul_path_read_string(dev->sysfs, &str, "queue/discard_zeroes_data");
if (!str)
str = xstrdup("0");
case COL_WSAME:
if (lsblk->bytes) {
ul_path_read_string(dev->sysfs, &str, "queue/write_same_max_bytes");
- if (sort)
- set_sortdata_u64_from_string(ln, col, str);
+ if (sortdata)
+ str2u64(str, sortdata);
} else {
uint64_t x;
if (ul_path_read_u64(dev->sysfs, &x, "queue/write_same_max_bytes") == 0) {
str = size_to_human_string(SIZE_SUFFIX_1LETTER, x);
- if (sort)
- set_sortdata_u64(ln, col, x);
+ if (sortdata)
+ *sortdata = x;
}
}
if (!str)
break;
};
- if (str && scols_line_refer_data(ln, col, str))
- err(EXIT_FAILURE, _("failed to add output data"));
+ return str;
}
-static void device_to_scols(struct lsblk_device *dev, struct lsblk_device *parent, struct libscols_table *tab)
+/*
+ * Adds data for all wanted columns about the device to the smartcols table
+ */
+static void device_to_scols(
+ struct lsblk_device *dev,
+ struct lsblk_device *parent,
+ struct libscols_table *tab,
+ struct libscols_line *parent_line)
{
size_t i;
+ struct libscols_line *ln;
struct lsblk_iter itr;
struct lsblk_device *child = NULL;
+ int link_group = 0;
+
+
+ DBG(DEV, ul_debugobj(dev, "add '%s' to scols", dev->name));
+ ON_DBG(DEV, if (ul_path_isopen_dirfd(dev->sysfs)) ul_debugobj(dev, " %s ---> is open!", dev->name));
+
+ if (!parent && dev->wholedisk)
+ parent = dev->wholedisk;
- dev->scols_line = scols_table_new_line(tab, parent ? parent->scols_line : NULL);
- if (!dev->scols_line)
+ /* Do not print device more than one in --list mode */
+ if (!(lsblk->flags & LSBLK_TREE) && dev->is_printed)
+ return;
+
+ if (lsblk->merge && list_count_entries(&dev->parents) > 1) {
+ if (!lsblk_device_is_last_parent(dev, parent))
+ return;
+ link_group = 1;
+ }
+
+ ln = scols_table_new_line(tab, link_group ? NULL : parent_line);
+ if (!ln)
err(EXIT_FAILURE, _("failed to allocate output line"));
- for (i = 0; i < ncolumns; i++)
- set_scols_data(dev, parent, i, get_column_id(i), dev->scols_line);
+ dev->is_printed = 1;
+
+ if (link_group) {
+ struct lsblk_device *p;
+ struct libscols_line *gr = parent_line;
+
+ /* Merge all my parents to the one group */
+ DBG(DEV, ul_debugobj(dev, " grouping parents [--merge]"));
+ lsblk_reset_iter(&itr, LSBLK_ITER_FORWARD);
+ while (lsblk_device_next_parent(dev, &itr, &p) == 0) {
+ if (!p->scols_line) {
+ DBG(DEV, ul_debugobj(dev, " *** ignore '%s' no scols line yet", p->name));
+ continue;
+ }
+ DBG(DEV, ul_debugobj(dev, " group '%s'", p->name));
+ scols_table_group_lines(tab, p->scols_line, gr, 0);
+ }
+
+ /* Link the group -- this makes group->child connection */
+ DBG(DEV, ul_debugobj(dev, " linking the group [--merge]"));
+ scols_line_link_group(ln, gr, 0);
+ }
+
+ /* read column specific data and set it to smartcols table line */
+ for (i = 0; i < ncolumns; i++) {
+ char *data;
+ int id = get_column_id(i);
+
+ if (lsblk->sort_id != id)
+ data = device_get_data(dev, parent, id, NULL);
+ else {
+ uint64_t sortdata = (uint64_t) -1;
+
+ data = device_get_data(dev, parent, id, &sortdata);
+ if (data && sortdata != (uint64_t) -1)
+ set_sortdata_u64(ln, i, sortdata);
+ }
+ DBG(DEV, ul_debugobj(dev, " refer data[%zu]=\"%s\"", i, data));
+ if (data && scols_line_refer_data(ln, i, data))
+ err(EXIT_FAILURE, _("failed to add output data"));
+ }
+
+ dev->scols_line = ln;
+
+ if (dev->npartitions == 0)
+ /* For partitions we often read from parental whole-disk sysfs,
+ * otherwise we can close */
+ ul_path_close_dirfd(dev->sysfs);
lsblk_reset_iter(&itr, LSBLK_ITER_FORWARD);
+ while (lsblk_device_next_child(dev, &itr, &child) == 0) {
+ DBG(DEV, ul_debugobj(dev, "%s -> continue to child", dev->name));
+ device_to_scols(child, dev, tab, ln);
+ DBG(DEV, ul_debugobj(dev, "%s <- child done", dev->name));
+ }
- while (lsblk_device_next_child(dev, &itr, &child) == 0)
- device_to_scols(child, dev, tab);
+ /* Let's be careful with number of open files */
+ ul_path_close_dirfd(dev->sysfs);
}
+/*
+ * Walks on tree and adds one line for each device to the smartcols table
+ */
static void devtree_to_scols(struct lsblk_devtree *tr, struct libscols_table *tab)
{
struct lsblk_iter itr;
lsblk_reset_iter(&itr, LSBLK_ITER_FORWARD);
while (lsblk_devtree_next_root(tr, &itr, &dev) == 0)
- device_to_scols(dev, NULL, tab);
+ device_to_scols(dev, NULL, tab, NULL);
}
-static int set_device(struct lsblk_device *dev,
+/*
+ * Reads very basic information about the device from sysfs into the device struct
+ */
+static int initialize_device(struct lsblk_device *dev,
struct lsblk_device *wholedisk,
const char *name)
{
dev_t devno;
- DBG(DEV, ul_debugobj(dev, "setting context for %s [wholedisk=%p]", name, wholedisk));
+ DBG(DEV, ul_debugobj(dev, "initialize %s [wholedisk=%p %s]",
+ name, wholedisk, wholedisk ? wholedisk->name : ""));
dev->name = xstrdup(name);
- dev->partition = wholedisk != NULL;
+
+ if (wholedisk) {
+ dev->wholedisk = wholedisk;
+ lsblk_ref_device(wholedisk);
+ }
dev->filename = get_device_path(dev);
if (!dev->filename) {
DBG(DEV, ul_debugobj(dev, "%s: filename=%s", dev->name, dev->filename));
devno = __sysfs_devname_to_devno(lsblk->sysroot, dev->name, wholedisk ? wholedisk->name : NULL);
-
if (!devno) {
DBG(DEV, ul_debugobj(dev, "%s: unknown device name", dev->name));
return -1;
if (ul_path_read_u64(dev->sysfs, &dev->size, "size") == 0) /* in sectors */
dev->size <<= 9; /* in bytes */
- if (ul_path_read_s32(dev->sysfs, &dev->discard,
- "queue/discard_granularity") != 0)
- dev->discard = 0;
-
/* Ignore devices of zero size */
if (!lsblk->all_devices && dev->size == 0) {
DBG(DEV, ul_debugobj(dev, "zero size device -- ignore"));
struct lsblk_device *dev = lsblk_devtree_get_device(tr, name);
if (!dev) {
- dev = lsblk_new_device(tr);
+ dev = lsblk_new_device();
if (!dev)
err(EXIT_FAILURE, _("failed to allocate device"));
- if (set_device(dev, disk, name) != 0) {
+ if (initialize_device(dev, disk, name) != 0) {
lsblk_unref_device(dev);
return NULL;
}
* Do not process further if there are no partitions for
* this device or the device itself is a partition.
*/
- if (!disk->npartitions || disk->partition)
+ if (!disk->npartitions || device_is_partition(disk))
return -EINVAL;
DBG(DEV, ul_debugobj(disk, "%s: probe whole-disk for partitions", disk->name));
if (lsblk_device_new_dependence(disk, part) == 0)
process_dependencies(tr, part, 0);
+
+ ul_path_close_dirfd(part->sysfs);
}
+ /* For partitions we need parental (whole-disk) sysfs directory pretty
+ * often, so close it now when all is done */
+ ul_path_close_dirfd(disk->sysfs);
+
DBG(DEV, ul_debugobj(disk, "probe whole-disk for partitions -- done"));
closedir(dir);
return 0;
}
/*
- * List device dependencies: partitions, holders (inverse = 0) or slaves (inverse = 1).
+ * Reads slaves/holders and partitions for specified device into device tree
*/
static int process_dependencies(
struct lsblk_devtree *tr,
assert(dev);
+ if (lsblk->nodeps)
+ return 0;
+
/* read all or specified partition */
if (do_partitions && dev->npartitions)
process_partitions(tr, dev);
- if (lsblk->nodeps)
- return 0;
-
DBG(DEV, ul_debugobj(dev, "%s: reading dependencies", dev->name));
if (!(lsblk->inverse ? dev->nslaves : dev->nholders)) {
DBG(DEV, ul_debugobj(dev, " ignore (no slaves/holders directory)"));
return 0;
}
+ ul_path_close_dirfd(dev->sysfs);
DBG(DEV, ul_debugobj(dev, " %s: checking for '%s' dependence", dev->name, depname));
while ((d = xreaddir(dir))) {
- struct lsblk_device *dep;
+ struct lsblk_device *dep = NULL;
+ struct lsblk_device *disk = NULL;
/* Is the dependency a partition? */
if (sysfs_blkdev_is_partition_dirent(dir, d, NULL)) {
char buf[PATH_MAX];
char *diskname;
- struct lsblk_device *disk = NULL;
DBG(DEV, ul_debugobj(dev, " %s: dependence is partition", d->d_name));
disk = devtree_get_device_or_new(tr, NULL, diskname);
if (!disk) {
DBG(DEV, ul_debugobj(dev, " ignore no wholedisk ???"));
- continue;
+ goto next;
}
dep = devtree_get_device_or_new(tr, disk, d->d_name);
if (!dep)
- continue;
+ goto next;
if (lsblk_device_new_dependence(dev, dep) == 0)
process_dependencies(tr, dep, 1);
if (lsblk->inverse
&& lsblk_device_new_dependence(dep, disk) == 0)
process_dependencies(tr, disk, 0);
-
}
/* The dependency is a whole device. */
else {
dep = devtree_get_device_or_new(tr, NULL, d->d_name);
if (!dep)
- continue;
+ goto next;
if (lsblk_device_new_dependence(dev, dep) == 0)
/* For inverse tree we don't want to show partitions
* if the dependence is on whole-disk */
process_dependencies(tr, dep, lsblk->inverse ? 0 : 1);
}
+next:
+ if (dep && dep->sysfs)
+ ul_path_close_dirfd(dep->sysfs);
+ if (disk && disk->sysfs)
+ ul_path_close_dirfd(disk->sysfs);
}
closedir(dir);
return 0;
}
-
-/* Iterate devices in sysfs */
-
-static int process_one_device(struct lsblk_devtree *tr, char *devname)
+/*
+ * Defines the device as root node in the device tree and walks on all dependencies of the device.
+ */
+static int __process_one_device(struct lsblk_devtree *tr, char *devname, dev_t devno)
{
struct lsblk_device *dev = NULL;
- struct stat st;
+ struct lsblk_device *disk = NULL;
char buf[PATH_MAX + 1], *name = NULL, *diskname = NULL;
- dev_t disk = 0;
int real_part = 0, rc = -EINVAL;
- DBG(DEV, ul_debug("%s: reading alone device", devname));
+ if (devno == 0 && devname) {
+ struct stat st;
- if (stat(devname, &st) || !S_ISBLK(st.st_mode)) {
- warnx(_("%s: not a block device"), devname);
- goto leave;
+ DBG(DEV, ul_debug("%s: reading alone device", devname));
+
+ if (stat(devname, &st) || !S_ISBLK(st.st_mode)) {
+ warnx(_("%s: not a block device"), devname);
+ goto leave;
+ }
+ devno = st.st_rdev;
+ } else if (devno) {
+ DBG(DEV, ul_debug("%d:%d: reading alone device", major(devno), minor(devno)));
+ } else {
+ assert(devno || devname);
+ return -EINVAL;
}
/* TODO: sysfs_devno_to_devname() internally initializes path_cxt, it
* would be better to use ul_new_sysfs_path() + sysfs_blkdev_get_name()
- * and reuse path_cxt for set_device()
+ * and reuse path_cxt for initialize_device()
*/
- name = sysfs_devno_to_devname(st.st_rdev, buf, sizeof(buf));
+ name = sysfs_devno_to_devname(devno, buf, sizeof(buf));
if (!name) {
- warn(_("%s: failed to get sysfs name"), devname);
+ if (devname)
+ warn(_("%s: failed to get sysfs name"), devname);
goto leave;
}
name = xstrdup(name);
/* dm mapping is never a real partition! */
real_part = 0;
} else {
- if (blkid_devno_to_wholedisk(st.st_rdev, buf, sizeof(buf), &disk)) {
- warn(_("%s: failed to get whole-disk device number"), devname);
+ dev_t diskno = 0;
+
+ if (blkid_devno_to_wholedisk(devno, buf, sizeof(buf), &diskno)) {
+ warn(_("%s: failed to get whole-disk device number"), name);
goto leave;
}
diskname = buf;
- real_part = st.st_rdev != disk;
+ real_part = devno != diskno;
}
if (!real_part) {
/*
* Partition, read sysfs name of the disk device
*/
- struct lsblk_device *disk;
-
DBG(DEV, ul_debug(" partition"));
disk = devtree_get_device_or_new(tr, NULL, diskname);
if (lsblk->inverse
&& lsblk_device_new_dependence(dev, disk) == 0)
process_dependencies(tr, disk, 0);
+ else
+ ul_path_close_dirfd(disk->sysfs);
}
rc = 0;
leave:
+ if (dev && dev->sysfs)
+ ul_path_close_dirfd(dev->sysfs);
+ if (disk && disk->sysfs)
+ ul_path_close_dirfd(disk->sysfs);
free(name);
return rc;
}
-static int process_all_devices(struct lsblk_devtree *tr)
+static int process_one_device(struct lsblk_devtree *tr, char *devname)
+{
+ assert(devname);
+ return __process_one_device(tr, devname, 0);
+}
+
+/*
+ * The /sys/block contains only root devices, and no partitions. It seems more
+ * simple to scan /sys/dev/block where are all devices without exceptions to get
+ * top-level devices for the reverse tree.
+ */
+static int process_all_devices_inverse(struct lsblk_devtree *tr)
{
DIR *dir;
struct dirent *d;
- struct path_cxt *pc = ul_new_path(_PATH_SYS_BLOCK);
+ struct path_cxt *pc = ul_new_path(_PATH_SYS_DEVBLOCK);
+
+ assert(lsblk->inverse);
if (!pc)
err(EXIT_FAILURE, _("failed to allocate /sys handler"));
ul_path_set_prefix(pc, lsblk->sysroot);
-
- /* TODO: reuse @pc in set_device(), etc. */
dir = ul_path_opendir(pc, NULL);
if (!dir)
goto done;
- DBG(DEV, ul_debug("iterate on " _PATH_SYS_BLOCK "%s", lsblk->inverse ? " [inverse]" : ""));
+ DBG(DEV, ul_debug("iterate on " _PATH_SYS_DEVBLOCK));
while ((d = xreaddir(dir))) {
- struct lsblk_device *dev;
+ dev_t devno;
+ int maj, min;
DBG(DEV, ul_debug(" %s dentry", d->d_name));
+ if (sscanf(d->d_name, "%d:%d", &maj, &min) != 2)
+ continue;
+ devno = makedev(maj, min);
+
+ if (is_maj_excluded(maj) || !is_maj_included(maj))
+ continue;
+ if (ul_path_countf_dirents(pc, "%s/holders", d->d_name) != 0)
+ continue;
+ if (sysfs_devno_count_partitions(devno) != 0)
+ continue;
+ __process_one_device(tr, NULL, devno);
+ }
+
+ closedir(dir);
+done:
+ ul_unref_path(pc);
+ DBG(DEV, ul_debug("iterate on " _PATH_SYS_DEVBLOCK " -- done"));
+ return 0;
+}
+
+/*
+ * Reads root nodes (devices) from /sys/block into devices tree
+ */
+static int process_all_devices(struct lsblk_devtree *tr)
+{
+ DIR *dir;
+ struct dirent *d;
+ struct path_cxt *pc;
+
+ assert(lsblk->inverse == 0);
+
+ pc = ul_new_path(_PATH_SYS_BLOCK);
+ if (!pc)
+ err(EXIT_FAILURE, _("failed to allocate /sys handler"));
+
+ ul_path_set_prefix(pc, lsblk->sysroot);
+ dir = ul_path_opendir(pc, NULL);
+ if (!dir)
+ goto done;
+
+ DBG(DEV, ul_debug("iterate on " _PATH_SYS_BLOCK));
+
+ while ((d = xreaddir(dir))) {
+ struct lsblk_device *dev = NULL;
+
+ DBG(DEV, ul_debug(" %s dentry", d->d_name));
dev = devtree_get_device_or_new(tr, NULL, d->d_name);
if (!dev)
- continue;
+ goto next;
/* remove unwanted devices */
if (is_maj_excluded(dev->maj) || !is_maj_included(dev->maj)) {
DBG(DEV, ul_debug(" %s: ignore (by filter)", d->d_name));
lsblk_devtree_remove_device(tr, dev);
- continue;
+ dev = NULL;
+ goto next;
}
- /*
- * ignore devices in the midle of the tree
- */
- if (!lsblk->inverse) {
- if (dev->nslaves) {
- DBG(DEV, ul_debug(" %s: ignore (in-middle)", d->d_name));
- continue;
- }
- lsblk_devtree_add_root(tr, dev);
- process_dependencies(tr, dev, 1);
- } else {
- /* not implemented yet */
- ;
+ if (dev->nslaves) {
+ DBG(DEV, ul_debug(" %s: ignore (in-middle)", d->d_name));
+ goto next;
}
+
+ lsblk_devtree_add_root(tr, dev);
+ process_dependencies(tr, dev, 1);
+next:
+ /* Let's be careful with number of open files */
+ if (dev && dev->sysfs)
+ ul_path_close_dirfd(dev->sysfs);
}
closedir(dir);
return 0;
}
+/*
+ * Parses major numbers as specified on lsblk command line
+ */
static void parse_excludes(const char *str0)
{
const char *str = str0;
}
}
+/*
+ * Parses major numbers as specified on lsblk command line
+ * (TODO: what about refactor and merge parse_excludes() and parse_includes().)
+ */
static void parse_includes(const char *str0)
{
const char *str = str0;
return *adata == *bdata ? 0 : *adata >= *bdata ? 1 : -1;
}
+static void device_set_dedupkey(
+ struct lsblk_device *dev,
+ struct lsblk_device *parent,
+ int id)
+{
+ struct lsblk_iter itr;
+ struct lsblk_device *child = NULL;
+
+ dev->dedupkey = device_get_data(dev, parent, id, NULL);
+ if (dev->dedupkey)
+ DBG(DEV, ul_debugobj(dev, "%s: de-duplication key: %s", dev->name, dev->dedupkey));
+
+ if (dev->npartitions == 0)
+ /* For partitions we often read from parental whole-disk sysfs,
+ * otherwise we can close */
+ ul_path_close_dirfd(dev->sysfs);
+
+ lsblk_reset_iter(&itr, LSBLK_ITER_FORWARD);
+
+ while (lsblk_device_next_child(dev, &itr, &child) == 0)
+ device_set_dedupkey(child, dev, id);
+
+ /* Let's be careful with number of open files */
+ ul_path_close_dirfd(dev->sysfs);
+}
+
+static void devtree_set_dedupkeys(struct lsblk_devtree *tr, int id)
+{
+ struct lsblk_iter itr;
+ struct lsblk_device *dev = NULL;
+
+ lsblk_reset_iter(&itr, LSBLK_ITER_FORWARD);
+
+ while (lsblk_devtree_next_root(tr, &itr, &dev) == 0)
+ device_set_dedupkey(dev, NULL, id);
+}
+
static void __attribute__((__noreturn__)) usage(void)
{
FILE *out = stdout;
fputs(_("List information about block devices.\n"), out);
fputs(USAGE_OPTIONS, out);
+ fputs(_(" -D, --discard print discard capabilities\n"), out);
+ fputs(_(" -E, --dedup <column> de-duplicate output by <column>\n"), out);
+ fputs(_(" -I, --include <list> show only devices with specified major numbers\n"), out);
+ fputs(_(" -J, --json use JSON output format\n"), out);
+ fputs(_(" -O, --output-all output all columns\n"), out);
+ fputs(_(" -P, --pairs use key=\"value\" output format\n"), out);
+ fputs(_(" -S, --scsi output info about SCSI devices\n"), out);
+ fputs(_(" -T, --tree[=<column>] use tree format output\n"), out);
fputs(_(" -a, --all print all devices\n"), out);
fputs(_(" -b, --bytes print SIZE in bytes rather than in human readable format\n"), out);
fputs(_(" -d, --nodeps don't print slaves or holders\n"), out);
- fputs(_(" -D, --discard print discard capabilities\n"), out);
- fputs(_(" -z, --zoned print zone model\n"), out);
fputs(_(" -e, --exclude <list> exclude devices by major number (default: RAM disks)\n"), out);
fputs(_(" -f, --fs output info about filesystems\n"), out);
fputs(_(" -i, --ascii use ascii characters only\n"), out);
- fputs(_(" -I, --include <list> show only devices with specified major numbers\n"), out);
- fputs(_(" -J, --json use JSON output format\n"), out);
fputs(_(" -l, --list use list format output\n"), out);
- fputs(_(" -T, --tree use tree format output\n"), out);
+ fputs(_(" -M, --merge group parents of sub-trees (usable for RAIDs, Multi-path)\n"), out);
fputs(_(" -m, --perms output info about permissions\n"), out);
fputs(_(" -n, --noheadings don't print headings\n"), out);
fputs(_(" -o, --output <list> output columns\n"), out);
- fputs(_(" -O, --output-all output all columns\n"), out);
fputs(_(" -p, --paths print complete device path\n"), out);
- fputs(_(" -P, --pairs use key=\"value\" output format\n"), out);
fputs(_(" -r, --raw use raw output format\n"), out);
fputs(_(" -s, --inverse inverse dependencies\n"), out);
- fputs(_(" -S, --scsi output info about SCSI devices\n"), out);
fputs(_(" -t, --topology output info about topology\n"), out);
+ fputs(_(" -z, --zoned print zone model\n"), out);
fputs(_(" -x, --sort <column> sort output by <column>\n"), out);
fputs(_(" --sysroot <dir> use specified directory as system root\n"), out);
fputs(USAGE_SEPARATOR, out);
int main(int argc, char *argv[])
{
- struct lsblk _ls = { .sort_id = -1, .flags = LSBLK_TREE };
+ struct lsblk _ls = {
+ .sort_id = -1,
+ .dedup_id = -1,
+ .flags = LSBLK_TREE,
+ .tree_id = COL_NAME
+ };
struct lsblk_devtree *tr = NULL;
int c, status = EXIT_FAILURE;
char *outarg = NULL;
size_t i;
- int force_tree = 0;
+ int force_tree = 0, has_tree_col = 0;
enum {
OPT_SYSROOT = CHAR_MAX + 1
{ "bytes", no_argument, NULL, 'b' },
{ "nodeps", no_argument, NULL, 'd' },
{ "discard", no_argument, NULL, 'D' },
+ { "dedup", required_argument, NULL, 'E' },
{ "zoned", no_argument, NULL, 'z' },
{ "help", no_argument, NULL, 'h' },
{ "json", no_argument, NULL, 'J' },
{ "output", required_argument, NULL, 'o' },
{ "output-all", no_argument, NULL, 'O' },
+ { "merge", no_argument, NULL, 'M' },
{ "perms", no_argument, NULL, 'm' },
{ "noheadings", no_argument, NULL, 'n' },
{ "list", no_argument, NULL, 'l' },
{ "scsi", no_argument, NULL, 'S' },
{ "sort", required_argument, NULL, 'x' },
{ "sysroot", required_argument, NULL, OPT_SYSROOT },
- { "tree", no_argument, NULL, 'T' },
+ { "tree", optional_argument, NULL, 'T' },
{ "version", no_argument, NULL, 'V' },
{ NULL, 0, NULL, 0 },
};
{ 'O','S' },
{ 'O','f' },
{ 'O','m' },
+ { 'O','o' },
{ 'O','t' },
{ 'P','T', 'l','r' },
{ 0 }
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
- atexit(close_stdout);
+ close_stdout_atexit();
lsblk = &_ls;
lsblk_init_debug();
while((c = getopt_long(argc, argv,
- "abdDze:fhJlnmo:OpPiI:rstVSTx:", longopts, NULL)) != -1) {
+ "abdDzE:e:fhJlnMmo:OpPiI:rstVST:x:", longopts, NULL)) != -1) {
err_exclusive_options(c, longopts, excl, excl_st);
case 'e':
parse_excludes(optarg);
break;
- case 'h':
- usage();
- break;
case 'J':
lsblk->flags |= LSBLK_JSON;
break;
case 'l':
lsblk->flags &= ~LSBLK_TREE; /* disable the default */
break;
+ case 'M':
+ lsblk->merge = 1;
+ break;
case 'n':
lsblk->flags |= LSBLK_NOHEADINGS;
break;
case 'f':
add_uniq_column(COL_NAME);
add_uniq_column(COL_FSTYPE);
+ add_uniq_column(COL_FSVERSION);
add_uniq_column(COL_LABEL);
add_uniq_column(COL_UUID);
add_uniq_column(COL_FSAVAIL);
break;
case 'T':
force_tree = 1;
+ if (optarg)
+ lsblk->tree_id = column_name_to_id(optarg, strlen(optarg));
break;
-
case OPT_SYSROOT:
lsblk->sysroot = optarg;
break;
- case 'V':
- printf(UTIL_LINUX_VERSION);
- return EXIT_SUCCESS;
+ case 'E':
+ lsblk->dedup_id = column_name_to_id(optarg, strlen(optarg));
+ if (lsblk->dedup_id >= 0)
+ break;
+ errtryhelp(EXIT_FAILURE);
+ break;
case 'x':
lsblk->flags &= ~LSBLK_TREE; /* disable the default */
lsblk->sort_id = column_name_to_id(optarg, strlen(optarg));
if (lsblk->sort_id >= 0)
break;
- /* fallthrough */
+ errtryhelp(EXIT_FAILURE);
+ break;
+
+ case 'h':
+ usage();
+ case 'V':
+ print_version(EXIT_SUCCESS);
default:
errtryhelp(EXIT_FAILURE);
}
lsblk->sort_hidden = 1;
}
+ if (lsblk->dedup_id >= 0 && column_id_to_number(lsblk->dedup_id) < 0) {
+ /* the deduplication column is not between output columns -- add as hidden */
+ add_column(lsblk->dedup_id);
+ lsblk->dedup_hidden = 1;
+ }
+
lsblk_mnt_init();
scols_init_debug(0);
ul_path_init_debug();
struct libscols_column *cl;
int id = get_column_id(i), fl = ci->flags;
- if (!(lsblk->flags & LSBLK_TREE) && id == COL_NAME)
- fl &= ~SCOLS_FL_TREE;
+ if ((lsblk->flags & LSBLK_TREE)
+ && has_tree_col == 0
+ && id == lsblk->tree_id) {
+ fl |= SCOLS_FL_TREE;
+ fl &= ~SCOLS_FL_RIGHT;
+ has_tree_col = 1;
+ }
+
if (lsblk->sort_hidden && lsblk->sort_id == id)
fl |= SCOLS_FL_HIDDEN;
+ if (lsblk->dedup_hidden && lsblk->dedup_id == id)
+ fl |= SCOLS_FL_HIDDEN;
+
+ if (force_tree
+ && lsblk->flags & LSBLK_JSON
+ && has_tree_col == 0
+ && i + 1 == ncolumns)
+ /* The "--tree --json" specified, but no column with
+ * SCOLS_FL_TREE yet; force it for the last column
+ */
+ fl |= SCOLS_FL_TREE;
cl = scols_table_new_column(lsblk->table, ci->name, ci->whint, fl);
if (!cl) {
if (!tr)
err(EXIT_FAILURE, _("failed to allocate device tree"));
- if (optind == argc)
- status = process_all_devices(tr) == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
- else {
+ if (optind == argc) {
+ int rc = lsblk->inverse ?
+ process_all_devices_inverse(tr) :
+ process_all_devices(tr);
+
+ status = rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
+ } else {
int cnt = 0, cnt_err = 0;
while (optind < argc) {
EXIT_SUCCESS; /* all success */
}
+ if (lsblk->dedup_id > -1) {
+ devtree_set_dedupkeys(tr, lsblk->dedup_id);
+ lsblk_devtree_deduplicate_devices(tr);
+ }
+
devtree_to_scols(tr, lsblk->table);
if (lsblk->sort_col)