2010-11-24 10:06:39 -06:00
|
|
|
/*
|
2010-11-24 11:16:44 -06:00
|
|
|
* lsblk(8) - list block devices
|
2010-11-24 10:06:39 -06:00
|
|
|
*
|
2018-10-17 03:54:59 -05:00
|
|
|
* Copyright (C) 2010-2018 Red Hat, Inc. All rights reserved.
|
2010-11-24 10:06:39 -06:00
|
|
|
* Written by Milan Broz <mbroz@redhat.com>
|
|
|
|
* Karel Zak <kzak@redhat.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it would be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
2012-02-22 15:28:48 -06:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2010-11-24 10:06:39 -06:00
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <locale.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <grp.h>
|
2011-02-24 10:41:33 -06:00
|
|
|
#include <ctype.h>
|
2018-09-19 04:25:08 -05:00
|
|
|
#include <assert.h>
|
2010-11-24 10:06:39 -06:00
|
|
|
|
|
|
|
#include <blkid.h>
|
|
|
|
|
2012-03-07 07:28:52 -06:00
|
|
|
#include "c.h"
|
2010-11-24 10:06:39 -06:00
|
|
|
#include "pathnames.h"
|
|
|
|
#include "blkdev.h"
|
|
|
|
#include "canonicalize.h"
|
|
|
|
#include "nls.h"
|
|
|
|
#include "xalloc.h"
|
|
|
|
#include "strutils.h"
|
2011-05-16 08:24:26 -05:00
|
|
|
#include "sysfs.h"
|
2012-04-04 12:56:48 -05:00
|
|
|
#include "closestream.h"
|
2012-06-14 14:46:16 -05:00
|
|
|
#include "optutils.h"
|
2019-04-11 06:11:53 -05:00
|
|
|
#include "fileutils.h"
|
2020-09-09 05:18:07 -05:00
|
|
|
#include "loopdev.h"
|
2010-11-24 10:06:39 -06:00
|
|
|
|
2018-09-14 08:36:59 -05:00
|
|
|
#include "lsblk.h"
|
2015-04-01 05:06:15 -05:00
|
|
|
|
2018-09-14 08:36:59 -05:00
|
|
|
UL_DEBUG_DEFINE_MASK(lsblk);
|
2015-04-01 05:06:15 -05:00
|
|
|
UL_DEBUG_DEFINE_MASKNAMES(lsblk) = UL_DEBUG_EMPTY_MASKNAMES;
|
|
|
|
|
2015-01-29 05:35:26 -06:00
|
|
|
#define LSBLK_EXIT_SOMEOK 64
|
|
|
|
#define LSBLK_EXIT_ALLFAILED 32
|
|
|
|
|
2017-04-19 07:28:16 -05:00
|
|
|
static int column_id_to_number(int id);
|
|
|
|
|
2010-11-24 10:06:39 -06:00
|
|
|
/* column IDs */
|
|
|
|
enum {
|
|
|
|
COL_NAME = 0,
|
|
|
|
COL_KNAME,
|
2018-07-10 03:33:18 -05:00
|
|
|
COL_PATH,
|
2010-11-24 10:06:39 -06:00
|
|
|
COL_MAJMIN,
|
2018-09-13 05:11:06 -05:00
|
|
|
COL_FSAVAIL,
|
|
|
|
COL_FSSIZE,
|
2010-11-24 10:06:39 -06:00
|
|
|
COL_FSTYPE,
|
2018-09-13 05:11:06 -05:00
|
|
|
COL_FSUSED,
|
|
|
|
COL_FSUSEPERC,
|
2019-11-12 06:55:55 -06:00
|
|
|
COL_FSVERSION,
|
2010-11-24 10:06:39 -06:00
|
|
|
COL_TARGET,
|
|
|
|
COL_LABEL,
|
|
|
|
COL_UUID,
|
2018-06-19 05:33:20 -05:00
|
|
|
COL_PTUUID,
|
|
|
|
COL_PTTYPE,
|
2014-01-14 15:19:50 -06:00
|
|
|
COL_PARTTYPE,
|
2019-12-04 05:38:57 -06:00
|
|
|
COL_PARTTYPENAME,
|
2012-04-19 07:39:44 -05:00
|
|
|
COL_PARTLABEL,
|
|
|
|
COL_PARTUUID,
|
2014-03-07 04:14:46 -06:00
|
|
|
COL_PARTFLAGS,
|
2012-07-24 09:34:33 -05:00
|
|
|
COL_RA,
|
2010-11-24 10:06:39 -06:00
|
|
|
COL_RO,
|
2011-01-10 04:18:51 -06:00
|
|
|
COL_RM,
|
2014-12-02 04:09:04 -06:00
|
|
|
COL_HOTPLUG,
|
2010-11-24 10:06:39 -06:00
|
|
|
COL_MODEL,
|
2013-05-13 06:33:41 -05:00
|
|
|
COL_SERIAL,
|
2010-11-24 10:06:39 -06:00
|
|
|
COL_SIZE,
|
2011-07-05 16:29:30 -05:00
|
|
|
COL_STATE,
|
2010-11-24 10:06:39 -06:00
|
|
|
COL_OWNER,
|
|
|
|
COL_GROUP,
|
|
|
|
COL_MODE,
|
|
|
|
COL_ALIOFF,
|
|
|
|
COL_MINIO,
|
|
|
|
COL_OPTIO,
|
|
|
|
COL_PHYSEC,
|
|
|
|
COL_LOGSEC,
|
|
|
|
COL_ROTA,
|
|
|
|
COL_SCHED,
|
2011-07-09 08:24:04 -05:00
|
|
|
COL_RQ_SIZE,
|
2011-02-24 10:41:33 -06:00
|
|
|
COL_TYPE,
|
2011-05-04 10:16:05 -05:00
|
|
|
COL_DALIGN,
|
|
|
|
COL_DGRAN,
|
|
|
|
COL_DMAX,
|
|
|
|
COL_DZERO,
|
2012-12-20 05:42:23 -06:00
|
|
|
COL_WSAME,
|
2012-08-14 11:14:35 -05:00
|
|
|
COL_WWN,
|
2012-08-17 13:25:26 -05:00
|
|
|
COL_RAND,
|
2012-10-19 07:10:39 -05:00
|
|
|
COL_PKNAME,
|
2012-10-19 07:10:40 -05:00
|
|
|
COL_HCTL,
|
2012-10-19 07:10:41 -05:00
|
|
|
COL_TRANSPORT,
|
2014-12-02 04:38:15 -06:00
|
|
|
COL_SUBSYS,
|
2012-11-02 12:56:29 -05:00
|
|
|
COL_REV,
|
2017-04-18 19:25:58 -05:00
|
|
|
COL_VENDOR,
|
|
|
|
COL_ZONED,
|
2020-05-05 09:31:44 -05:00
|
|
|
COL_DAX
|
2010-11-24 10:06:39 -06:00
|
|
|
};
|
|
|
|
|
2014-03-27 10:29:13 -05:00
|
|
|
/* basic table settings */
|
|
|
|
enum {
|
|
|
|
LSBLK_ASCII = (1 << 0),
|
|
|
|
LSBLK_RAW = (1 << 1),
|
|
|
|
LSBLK_NOHEADINGS = (1 << 2),
|
2014-04-02 06:52:08 -05:00
|
|
|
LSBLK_EXPORT = (1 << 3),
|
|
|
|
LSBLK_TREE = (1 << 4),
|
2015-06-04 08:49:52 -05:00
|
|
|
LSBLK_JSON = (1 << 5),
|
2014-03-27 10:29:13 -05:00
|
|
|
};
|
|
|
|
|
2018-04-03 08:10:21 -05:00
|
|
|
/* Types used for qsort() and JSON */
|
2014-05-20 08:08:08 -05:00
|
|
|
enum {
|
2018-04-03 08:10:21 -05:00
|
|
|
COLTYPE_STR = 0, /* default */
|
|
|
|
COLTYPE_NUM = 1, /* always u64 number */
|
|
|
|
COLTYPE_SORTNUM = 2, /* string on output, u64 for qsort() */
|
|
|
|
COLTYPE_SIZE = 3, /* srring by default, number when --bytes */
|
|
|
|
COLTYPE_BOOL = 4 /* 0 or 1 */
|
2014-05-20 08:08:08 -05:00
|
|
|
};
|
|
|
|
|
2010-11-24 10:06:39 -06:00
|
|
|
/* column names */
|
|
|
|
struct colinfo {
|
|
|
|
const char *name; /* header */
|
|
|
|
double whint; /* width hint (N < 1 is in percent of termwidth) */
|
2014-03-27 10:29:13 -05:00
|
|
|
int flags; /* SCOLS_FL_* */
|
2010-11-24 10:06:39 -06:00
|
|
|
const char *help;
|
2018-04-03 08:10:21 -05:00
|
|
|
int type; /* COLTYPE_* */
|
2010-11-24 10:06:39 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/* columns descriptions */
|
2011-10-26 08:58:18 -05:00
|
|
|
static struct colinfo infos[] = {
|
2019-02-20 05:49:24 -06:00
|
|
|
[COL_NAME] = { "NAME", 0.25, SCOLS_FL_NOEXTREMES, N_("device name") },
|
2011-01-31 04:44:42 -06:00
|
|
|
[COL_KNAME] = { "KNAME", 0.3, 0, N_("internal kernel device name") },
|
2018-09-13 05:11:06 -05:00
|
|
|
[COL_PKNAME] = { "PKNAME", 0.3, 0, N_("internal parent kernel device name") },
|
2018-07-10 03:33:18 -05:00
|
|
|
[COL_PATH] = { "PATH", 0.3, 0, N_("path to the device node") },
|
2018-04-03 08:10:21 -05:00
|
|
|
[COL_MAJMIN] = { "MAJ:MIN", 6, 0, N_("major:minor device number"), COLTYPE_SORTNUM },
|
2018-09-13 05:11:06 -05:00
|
|
|
|
|
|
|
[COL_FSAVAIL] = { "FSAVAIL", 5, SCOLS_FL_RIGHT, N_("filesystem size available") },
|
|
|
|
[COL_FSSIZE] = { "FSSIZE", 5, SCOLS_FL_RIGHT, N_("filesystem size") },
|
|
|
|
[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") },
|
2019-11-12 06:55:55 -06:00
|
|
|
[COL_FSVERSION] = { "FSVER", 0.1, SCOLS_FL_TRUNC, N_("filesystem version") },
|
2018-09-13 05:11:06 -05:00
|
|
|
|
2014-03-27 10:29:13 -05:00
|
|
|
[COL_TARGET] = { "MOUNTPOINT", 0.10, SCOLS_FL_TRUNC, N_("where the device is mounted") },
|
2010-11-24 10:06:39 -06:00
|
|
|
[COL_LABEL] = { "LABEL", 0.1, 0, N_("filesystem LABEL") },
|
|
|
|
[COL_UUID] = { "UUID", 36, 0, N_("filesystem UUID") },
|
2012-04-19 07:39:44 -05:00
|
|
|
|
2018-06-19 05:33:20 -05:00
|
|
|
[COL_PTUUID] = { "PTUUID", 36, 0, N_("partition table identifier (usually UUID)") },
|
|
|
|
[COL_PTTYPE] = { "PTTYPE", 0.1, 0, N_("partition table type") },
|
|
|
|
|
2019-12-04 05:38:57 -06:00
|
|
|
[COL_PARTTYPE] = { "PARTTYPE", 36, 0, N_("partition type code or UUID") },
|
|
|
|
[COL_PARTTYPENAME] = { "PARTTYPENAME", 0.1, 0, N_("partition type name") },
|
2012-04-19 07:39:44 -05:00
|
|
|
[COL_PARTLABEL] = { "PARTLABEL", 0.1, 0, N_("partition LABEL") },
|
|
|
|
[COL_PARTUUID] = { "PARTUUID", 36, 0, N_("partition UUID") },
|
2014-03-07 04:14:46 -06:00
|
|
|
[COL_PARTFLAGS] = { "PARTFLAGS", 36, 0, N_("partition flags") },
|
2012-04-19 07:39:44 -05:00
|
|
|
|
2018-04-03 08:10:21 -05:00
|
|
|
[COL_RA] = { "RA", 3, SCOLS_FL_RIGHT, N_("read-ahead of the device"), COLTYPE_NUM },
|
|
|
|
[COL_RO] = { "RO", 1, SCOLS_FL_RIGHT, N_("read-only device"), COLTYPE_BOOL },
|
|
|
|
[COL_RM] = { "RM", 1, SCOLS_FL_RIGHT, N_("removable device"), COLTYPE_BOOL },
|
|
|
|
[COL_HOTPLUG]= { "HOTPLUG", 1, SCOLS_FL_RIGHT, N_("removable or hotplug device (usb, pcmcia, ...)"), COLTYPE_BOOL },
|
|
|
|
[COL_ROTA] = { "ROTA", 1, SCOLS_FL_RIGHT, N_("rotational device"), COLTYPE_BOOL },
|
|
|
|
[COL_RAND] = { "RAND", 1, SCOLS_FL_RIGHT, N_("adds randomness"), COLTYPE_BOOL },
|
2014-03-27 10:29:13 -05:00
|
|
|
[COL_MODEL] = { "MODEL", 0.1, SCOLS_FL_TRUNC, N_("device identifier") },
|
|
|
|
[COL_SERIAL] = { "SERIAL", 0.1, SCOLS_FL_TRUNC, N_("disk serial number") },
|
2018-04-03 08:10:21 -05:00
|
|
|
[COL_SIZE] = { "SIZE", 5, SCOLS_FL_RIGHT, N_("size of the device"), COLTYPE_SIZE },
|
2014-03-27 10:29:13 -05:00
|
|
|
[COL_STATE] = { "STATE", 7, SCOLS_FL_TRUNC, N_("state of the device") },
|
|
|
|
[COL_OWNER] = { "OWNER", 0.1, SCOLS_FL_TRUNC, N_("user name"), },
|
|
|
|
[COL_GROUP] = { "GROUP", 0.1, SCOLS_FL_TRUNC, N_("group name") },
|
2010-11-24 10:06:39 -06:00
|
|
|
[COL_MODE] = { "MODE", 10, 0, N_("device node permissions") },
|
2018-04-03 08:10:21 -05:00
|
|
|
[COL_ALIOFF] = { "ALIGNMENT", 6, SCOLS_FL_RIGHT, N_("alignment offset"), COLTYPE_NUM },
|
|
|
|
[COL_MINIO] = { "MIN-IO", 6, SCOLS_FL_RIGHT, N_("minimum I/O size"), COLTYPE_NUM },
|
|
|
|
[COL_OPTIO] = { "OPT-IO", 6, SCOLS_FL_RIGHT, N_("optimal I/O size"), COLTYPE_NUM },
|
|
|
|
[COL_PHYSEC] = { "PHY-SEC", 7, SCOLS_FL_RIGHT, N_("physical sector size"), COLTYPE_NUM },
|
|
|
|
[COL_LOGSEC] = { "LOG-SEC", 7, SCOLS_FL_RIGHT, N_("logical sector size"), COLTYPE_NUM },
|
2011-02-24 10:41:33 -06:00
|
|
|
[COL_SCHED] = { "SCHED", 0.1, 0, N_("I/O scheduler name") },
|
2018-04-03 08:10:21 -05:00
|
|
|
[COL_RQ_SIZE]= { "RQ-SIZE", 5, SCOLS_FL_RIGHT, N_("request queue size"), COLTYPE_NUM },
|
2011-05-04 10:16:05 -05:00
|
|
|
[COL_TYPE] = { "TYPE", 4, 0, N_("device type") },
|
2018-04-03 08:10:21 -05:00
|
|
|
[COL_DALIGN] = { "DISC-ALN", 6, SCOLS_FL_RIGHT, N_("discard alignment offset"), COLTYPE_NUM },
|
|
|
|
[COL_DGRAN] = { "DISC-GRAN", 6, SCOLS_FL_RIGHT, N_("discard granularity"), COLTYPE_SIZE },
|
|
|
|
[COL_DMAX] = { "DISC-MAX", 6, SCOLS_FL_RIGHT, N_("discard max bytes"), COLTYPE_SIZE },
|
|
|
|
[COL_DZERO] = { "DISC-ZERO", 1, SCOLS_FL_RIGHT, N_("discard zeroes data"), COLTYPE_BOOL },
|
|
|
|
[COL_WSAME] = { "WSAME", 6, SCOLS_FL_RIGHT, N_("write same max bytes"), COLTYPE_SIZE },
|
2012-08-14 11:14:35 -05:00
|
|
|
[COL_WWN] = { "WWN", 18, 0, N_("unique storage identifier") },
|
2012-10-19 07:10:40 -05:00
|
|
|
[COL_HCTL] = { "HCTL", 10, 0, N_("Host:Channel:Target:Lun for SCSI") },
|
2012-10-19 07:10:41 -05:00
|
|
|
[COL_TRANSPORT] = { "TRAN", 6, 0, N_("device transport type") },
|
2014-12-02 04:38:15 -06:00
|
|
|
[COL_SUBSYS] = { "SUBSYSTEMS", 0.1, SCOLS_FL_NOEXTREMES, N_("de-duplicated chain of subsystems") },
|
2014-03-27 10:29:13 -05:00
|
|
|
[COL_REV] = { "REV", 4, SCOLS_FL_RIGHT, N_("device revision") },
|
|
|
|
[COL_VENDOR] = { "VENDOR", 0.1, SCOLS_FL_TRUNC, N_("device vendor") },
|
2017-04-18 19:25:58 -05:00
|
|
|
[COL_ZONED] = { "ZONED", 0.3, 0, N_("zone model") },
|
2020-05-05 09:31:44 -05:00
|
|
|
[COL_DAX] = { "DAX", 1, SCOLS_FL_RIGHT, N_("dax-capable device"), COLTYPE_BOOL },
|
2010-11-24 10:06:39 -06:00
|
|
|
};
|
|
|
|
|
2018-09-14 08:36:59 -05:00
|
|
|
struct lsblk *lsblk; /* global handler */
|
2011-10-26 08:58:18 -05:00
|
|
|
|
2018-10-17 03:54:59 -05:00
|
|
|
/*
|
|
|
|
* columns[] array specifies all currently wanted output column. The columns
|
2014-07-17 10:17:46 -05:00
|
|
|
* 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
|
2018-10-17 03:54:59 -05:00
|
|
|
* unnecessary overkill and over-engineering in this case
|
|
|
|
*/
|
2014-07-17 10:17:46 -05:00
|
|
|
static int columns[ARRAY_SIZE(infos) * 2];
|
2015-04-22 04:08:05 -05:00
|
|
|
static size_t ncolumns;
|
2014-07-17 10:17:46 -05:00
|
|
|
|
2017-04-19 07:28:16 -05:00
|
|
|
static inline void add_column(int id)
|
2014-07-17 10:17:46 -05:00
|
|
|
{
|
2017-04-19 07:28:16 -05:00
|
|
|
if (ncolumns >= ARRAY_SIZE(columns))
|
2014-07-17 10:17:46 -05:00
|
|
|
errx(EXIT_FAILURE, _("too many columns specified, "
|
2014-08-06 15:35:06 -05:00
|
|
|
"the limit is %zu columns"),
|
2017-04-19 07:28:16 -05:00
|
|
|
ARRAY_SIZE(columns) - 1);
|
|
|
|
columns[ ncolumns++ ] = id;
|
2014-07-17 10:17:46 -05:00
|
|
|
}
|
|
|
|
|
2017-04-19 07:28:16 -05:00
|
|
|
static inline void add_uniq_column(int id)
|
|
|
|
{
|
|
|
|
if (column_id_to_number(id) < 0)
|
|
|
|
add_column(id);
|
|
|
|
}
|
2010-11-24 10:06:39 -06:00
|
|
|
|
2018-10-17 03:54:59 -05:00
|
|
|
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
|
|
|
|
*/
|
2012-06-15 08:40:35 -05:00
|
|
|
static int excludes[256];
|
|
|
|
static size_t nexcludes;
|
2010-11-24 10:06:39 -06:00
|
|
|
|
2012-06-19 09:06:22 -05:00
|
|
|
static int includes[256];
|
|
|
|
static size_t nincludes;
|
|
|
|
|
2010-11-24 10:06:39 -06:00
|
|
|
static int is_maj_excluded(int maj)
|
|
|
|
{
|
2011-08-01 05:40:03 -05:00
|
|
|
size_t i;
|
2010-11-24 10:06:39 -06:00
|
|
|
|
|
|
|
assert(ARRAY_SIZE(excludes) > nexcludes);
|
|
|
|
|
2012-06-19 09:06:22 -05:00
|
|
|
if (!nexcludes)
|
2016-05-29 16:11:53 -05:00
|
|
|
return 0; /* filter not enabled, device not excluded */
|
2012-06-19 09:06:22 -05:00
|
|
|
|
2015-04-01 05:06:15 -05:00
|
|
|
for (i = 0; i < nexcludes; i++) {
|
|
|
|
if (excludes[i] == maj) {
|
|
|
|
DBG(FILTER, ul_debug("exclude: maj=%d", maj));
|
2010-11-24 10:06:39 -06:00
|
|
|
return 1;
|
2015-04-01 05:06:15 -05:00
|
|
|
}
|
|
|
|
}
|
2010-11-24 10:06:39 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-06-19 09:06:22 -05:00
|
|
|
static int is_maj_included(int maj)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
assert(ARRAY_SIZE(includes) > nincludes);
|
|
|
|
|
|
|
|
if (!nincludes)
|
|
|
|
return 1; /* filter not enabled, device is included */
|
|
|
|
|
2015-04-01 05:06:15 -05:00
|
|
|
for (i = 0; i < nincludes; i++) {
|
|
|
|
if (includes[i] == maj) {
|
|
|
|
DBG(FILTER, ul_debug("include: maj=%d", maj));
|
2012-06-19 09:06:22 -05:00
|
|
|
return 1;
|
2015-04-01 05:06:15 -05:00
|
|
|
}
|
|
|
|
}
|
2012-06-19 09:06:22 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-17 03:54:59 -05:00
|
|
|
/* Converts column sequential number to column ID (COL_*) */
|
2010-11-24 10:06:39 -06:00
|
|
|
static int get_column_id(int num)
|
|
|
|
{
|
2015-04-22 04:08:05 -05:00
|
|
|
assert(num >= 0);
|
|
|
|
assert((size_t) num < ncolumns);
|
2014-07-17 10:17:46 -05:00
|
|
|
assert(columns[num] < (int) ARRAY_SIZE(infos));
|
2010-11-24 10:06:39 -06:00
|
|
|
return columns[num];
|
|
|
|
}
|
|
|
|
|
2018-10-17 03:54:59 -05:00
|
|
|
/* Returns column description for the column sequential number */
|
2010-11-24 10:06:39 -06:00
|
|
|
static struct colinfo *get_column_info(int num)
|
|
|
|
{
|
|
|
|
return &infos[ get_column_id(num) ];
|
|
|
|
}
|
|
|
|
|
2018-10-17 03:54:59 -05:00
|
|
|
/* Converts column name (as defined in the infos[] to the column ID */
|
2010-11-24 10:06:39 -06:00
|
|
|
static int column_name_to_id(const char *name, size_t namesz)
|
|
|
|
{
|
2011-10-26 08:58:18 -05:00
|
|
|
size_t i;
|
2010-11-24 10:06:39 -06:00
|
|
|
|
2014-07-17 08:35:52 -05:00
|
|
|
for (i = 0; i < ARRAY_SIZE(infos); i++) {
|
2010-11-24 10:06:39 -06:00
|
|
|
const char *cn = infos[i].name;
|
|
|
|
|
|
|
|
if (!strncasecmp(name, cn, namesz) && !*(cn + namesz))
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
warnx(_("unknown column: %s"), name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-10-17 03:54:59 -05:00
|
|
|
/* Converts column ID (COL_*) to column sequential number */
|
2014-05-20 08:08:08 -05:00
|
|
|
static int column_id_to_number(int id)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
2015-04-22 04:08:05 -05:00
|
|
|
for (i = 0; i < ncolumns; i++)
|
2014-05-20 08:08:08 -05:00
|
|
|
if (columns[i] == id)
|
|
|
|
return i;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-10-17 03:54:59 -05:00
|
|
|
/* Checks for DM prefix in the device name */
|
2010-11-24 10:06:39 -06:00
|
|
|
static int is_dm(const char *name)
|
|
|
|
{
|
|
|
|
return strncmp(name, "dm-", 3) ? 0 : 1;
|
|
|
|
}
|
|
|
|
|
2018-10-17 04:14:40 -05:00
|
|
|
/* Returns full pat to the device node (TODO: what about sysfs_blkdev_get_path()) */
|
2018-10-09 05:46:28 -05:00
|
|
|
static char *get_device_path(struct lsblk_device *dev)
|
2010-11-24 10:06:39 -06:00
|
|
|
{
|
|
|
|
char path[PATH_MAX];
|
|
|
|
|
2018-10-09 05:46:28 -05:00
|
|
|
assert(dev);
|
|
|
|
assert(dev->name);
|
2010-11-24 10:06:39 -06:00
|
|
|
|
2018-10-09 05:46:28 -05:00
|
|
|
if (is_dm(dev->name))
|
|
|
|
return __canonicalize_dm_name(lsblk->sysroot, dev->name);
|
2010-11-24 10:06:39 -06:00
|
|
|
|
2018-10-09 05:46:28 -05:00
|
|
|
snprintf(path, sizeof(path), "/dev/%s", dev->name);
|
2015-05-29 03:55:01 -05:00
|
|
|
sysfs_devname_sys_to_dev(path);
|
2010-11-24 10:06:39 -06:00
|
|
|
return xstrdup(path);
|
|
|
|
}
|
|
|
|
|
2018-10-09 05:46:28 -05:00
|
|
|
static int is_readonly_device(struct lsblk_device *dev)
|
2010-11-24 10:06:39 -06:00
|
|
|
{
|
|
|
|
int fd, ro = 0;
|
|
|
|
|
2018-10-09 05:46:28 -05:00
|
|
|
if (ul_path_scanf(dev->sysfs, "ro", "%d", &ro) == 1)
|
2010-11-24 10:06:39 -06:00
|
|
|
return ro;
|
|
|
|
|
|
|
|
/* fallback if "ro" attribute does not exist */
|
2018-10-09 05:46:28 -05:00
|
|
|
fd = open(dev->filename, O_RDONLY);
|
2010-11-24 10:06:39 -06:00
|
|
|
if (fd != -1) {
|
2012-06-21 04:58:39 -05:00
|
|
|
if (ioctl(fd, BLKROGET, &ro) != 0)
|
|
|
|
ro = 0;
|
2010-11-24 10:06:39 -06:00
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
return ro;
|
|
|
|
}
|
|
|
|
|
2018-10-09 05:46:28 -05:00
|
|
|
static char *get_scheduler(struct lsblk_device *dev)
|
2010-11-24 10:06:39 -06:00
|
|
|
{
|
2018-06-15 04:59:29 -05:00
|
|
|
char buf[128];
|
2010-11-24 10:06:39 -06:00
|
|
|
char *p, *res = NULL;
|
|
|
|
|
2018-10-09 05:46:28 -05:00
|
|
|
if (ul_path_read_buffer(dev->sysfs, buf, sizeof(buf), "queue/scheduler") == 0)
|
2010-11-24 10:06:39 -06:00
|
|
|
return NULL;
|
2018-06-15 04:59:29 -05:00
|
|
|
p = strchr(buf, '[');
|
2010-11-24 10:06:39 -06:00
|
|
|
if (p) {
|
|
|
|
res = p + 1;
|
|
|
|
p = strchr(res, ']');
|
|
|
|
if (p) {
|
|
|
|
*p = '\0';
|
|
|
|
res = xstrdup(res);
|
|
|
|
} else
|
|
|
|
res = NULL;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-10-09 05:46:28 -05:00
|
|
|
static char *get_type(struct lsblk_device *dev)
|
2011-02-24 10:41:33 -06:00
|
|
|
{
|
|
|
|
char *res = NULL, *p;
|
|
|
|
|
2018-12-07 05:29:50 -06:00
|
|
|
if (device_is_partition(dev))
|
2018-10-03 07:39:57 -05:00
|
|
|
return xstrdup("part");
|
|
|
|
|
2018-10-09 05:46:28 -05:00
|
|
|
if (is_dm(dev->name)) {
|
2018-06-15 04:59:29 -05:00
|
|
|
char *dm_uuid = NULL;
|
2011-02-24 10:41:33 -06:00
|
|
|
|
|
|
|
/* The DM_UUID prefix should be set to subsystem owning
|
|
|
|
* the device - LVM, CRYPT, DMRAID, MPATH, PART */
|
2018-10-09 05:46:28 -05:00
|
|
|
if (ul_path_read_string(dev->sysfs, &dm_uuid, "dm/uuid") > 0
|
2018-06-15 04:59:29 -05:00
|
|
|
&& dm_uuid) {
|
2011-02-24 10:41:33 -06:00
|
|
|
char *tmp = dm_uuid;
|
|
|
|
char *dm_uuid_prefix = strsep(&tmp, "-");
|
|
|
|
|
|
|
|
if (dm_uuid_prefix) {
|
|
|
|
/* kpartx hack to remove partition number */
|
|
|
|
if (strncasecmp(dm_uuid_prefix, "part", 4) == 0)
|
|
|
|
dm_uuid_prefix[4] = '\0';
|
|
|
|
|
|
|
|
res = xstrdup(dm_uuid_prefix);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(dm_uuid);
|
|
|
|
if (!res)
|
|
|
|
/* No UUID or no prefix - just mark it as DM device */
|
|
|
|
res = xstrdup("dm");
|
|
|
|
|
2018-10-09 05:46:28 -05:00
|
|
|
} else if (!strncmp(dev->name, "loop", 4)) {
|
2011-02-24 10:41:33 -06:00
|
|
|
res = xstrdup("loop");
|
|
|
|
|
2018-10-09 05:46:28 -05:00
|
|
|
} else if (!strncmp(dev->name, "md", 2)) {
|
2018-06-15 04:59:29 -05:00
|
|
|
char *md_level = NULL;
|
|
|
|
|
2018-10-09 05:46:28 -05:00
|
|
|
ul_path_read_string(dev->sysfs, &md_level, "md/level");
|
2011-02-24 10:41:33 -06:00
|
|
|
res = md_level ? md_level : xstrdup("md");
|
|
|
|
|
|
|
|
} else {
|
2012-09-21 07:23:32 -05:00
|
|
|
const char *type = NULL;
|
2011-05-18 04:57:17 -05:00
|
|
|
int x = 0;
|
2012-06-15 06:13:40 -05:00
|
|
|
|
2018-10-09 05:46:28 -05:00
|
|
|
if (ul_path_read_s32(dev->sysfs, &x, "device/type") == 0)
|
2012-09-21 07:23:32 -05:00
|
|
|
type = blkdev_scsi_type_to_name(x);
|
2012-06-15 06:13:40 -05:00
|
|
|
if (!type)
|
2018-10-03 07:39:57 -05:00
|
|
|
type = "disk";
|
2012-06-15 06:13:40 -05:00
|
|
|
res = xstrdup(type);
|
2011-02-24 10:41:33 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
for (p = res; p && *p; p++)
|
|
|
|
*p = tolower((unsigned char) *p);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2012-10-19 07:10:41 -05:00
|
|
|
/* Thanks to lsscsi code for idea of detection logic used here */
|
2018-10-09 05:46:28 -05:00
|
|
|
static char *get_transport(struct lsblk_device *dev)
|
2012-10-19 07:10:41 -05:00
|
|
|
{
|
2018-10-09 05:46:28 -05:00
|
|
|
struct path_cxt *sysfs = dev->sysfs;
|
2012-10-23 05:21:37 -05:00
|
|
|
char *attr = NULL;
|
|
|
|
const char *trans = NULL;
|
2012-10-19 07:10:41 -05:00
|
|
|
|
2018-06-15 04:59:29 -05:00
|
|
|
|
2012-11-02 12:56:31 -05:00
|
|
|
/* SCSI - Serial Peripheral Interface */
|
2018-06-15 04:59:29 -05:00
|
|
|
if (sysfs_blkdev_scsi_host_is(sysfs, "spi"))
|
2012-10-23 05:21:37 -05:00
|
|
|
trans = "spi";
|
2012-10-19 07:10:41 -05:00
|
|
|
|
|
|
|
/* FC/FCoE - Fibre Channel / Fibre Channel over Ethernet */
|
2018-06-15 04:59:29 -05:00
|
|
|
else if (sysfs_blkdev_scsi_host_is(sysfs, "fc")) {
|
|
|
|
attr = sysfs_blkdev_scsi_host_strdup_attribute(sysfs, "fc", "symbolic_name");
|
2012-10-23 04:32:55 -05:00
|
|
|
if (!attr)
|
2012-10-19 07:10:41 -05:00
|
|
|
return NULL;
|
2012-10-23 05:21:37 -05:00
|
|
|
trans = strstr(attr, " over ") ? "fcoe" : "fc";
|
|
|
|
free(attr);
|
2012-10-19 07:10:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* SAS - Serial Attached SCSI */
|
2018-06-15 04:59:29 -05:00
|
|
|
else if (sysfs_blkdev_scsi_host_is(sysfs, "sas") ||
|
|
|
|
sysfs_blkdev_scsi_has_attribute(sysfs, "sas_device"))
|
2012-10-23 05:21:37 -05:00
|
|
|
trans = "sas";
|
2012-10-19 07:10:41 -05:00
|
|
|
|
|
|
|
|
|
|
|
/* SBP - Serial Bus Protocol (FireWire) */
|
2018-06-15 04:59:29 -05:00
|
|
|
else if (sysfs_blkdev_scsi_has_attribute(sysfs, "ieee1394_id"))
|
2012-10-23 05:21:37 -05:00
|
|
|
trans = "sbp";
|
2012-10-19 07:10:41 -05:00
|
|
|
|
|
|
|
/* iSCSI */
|
2018-06-15 04:59:29 -05:00
|
|
|
else if (sysfs_blkdev_scsi_host_is(sysfs, "iscsi"))
|
2012-10-23 05:21:37 -05:00
|
|
|
trans ="iscsi";
|
2012-10-19 07:10:41 -05:00
|
|
|
|
|
|
|
/* USB - Universal Serial Bus */
|
2018-06-15 04:59:29 -05:00
|
|
|
else if (sysfs_blkdev_scsi_path_contains(sysfs, "usb"))
|
2012-10-23 05:21:37 -05:00
|
|
|
trans = "usb";
|
2012-10-19 07:10:41 -05:00
|
|
|
|
|
|
|
/* ATA, SATA */
|
2018-06-15 04:59:29 -05:00
|
|
|
else if (sysfs_blkdev_scsi_host_is(sysfs, "scsi")) {
|
|
|
|
attr = sysfs_blkdev_scsi_host_strdup_attribute(sysfs, "scsi", "proc_name");
|
2012-10-23 04:32:55 -05:00
|
|
|
if (!attr)
|
2012-10-19 07:10:41 -05:00
|
|
|
return NULL;
|
2012-10-23 05:21:37 -05:00
|
|
|
if (!strncmp(attr, "ahci", 4) || !strncmp(attr, "sata", 4))
|
|
|
|
trans = "sata";
|
|
|
|
else if (strstr(attr, "ata"))
|
|
|
|
trans = "ata";
|
|
|
|
free(attr);
|
2016-06-02 07:41:08 -05:00
|
|
|
|
2018-10-09 05:46:28 -05:00
|
|
|
} else if (strncmp(dev->name, "nvme", 4) == 0)
|
2016-06-02 07:41:08 -05:00
|
|
|
trans = "nvme";
|
2012-10-19 07:10:41 -05:00
|
|
|
|
2012-10-23 05:21:37 -05:00
|
|
|
return trans ? xstrdup(trans) : NULL;
|
2012-10-19 07:10:41 -05:00
|
|
|
}
|
|
|
|
|
2018-10-09 05:46:28 -05:00
|
|
|
static char *get_subsystems(struct lsblk_device *dev)
|
2014-12-02 04:38:15 -06:00
|
|
|
{
|
|
|
|
char path[PATH_MAX];
|
|
|
|
char *sub, *chain, *res = NULL;
|
|
|
|
size_t len = 0, last = 0;
|
|
|
|
|
2018-10-09 05:46:28 -05:00
|
|
|
chain = sysfs_blkdev_get_devchain(dev->sysfs, path, sizeof(path));
|
2014-12-02 04:38:15 -06:00
|
|
|
if (!chain)
|
|
|
|
return NULL;
|
|
|
|
|
2018-10-09 05:46:28 -05:00
|
|
|
while (sysfs_blkdev_next_subsystem(dev->sysfs, chain, &sub) == 0) {
|
2014-12-02 04:38:15 -06:00
|
|
|
size_t sz;
|
|
|
|
|
|
|
|
/* don't create "block:scsi:scsi", but "block:scsi" */
|
2015-08-25 11:27:41 -05:00
|
|
|
if (len && strcmp(res + last, sub) == 0) {
|
|
|
|
free(sub);
|
2014-12-02 04:38:15 -06:00
|
|
|
continue;
|
2015-08-25 11:27:41 -05:00
|
|
|
}
|
2014-12-02 04:38:15 -06:00
|
|
|
|
|
|
|
sz = strlen(sub);
|
|
|
|
res = xrealloc(res, len + sz + 2);
|
|
|
|
if (len)
|
|
|
|
res[len++] = ':';
|
|
|
|
|
|
|
|
memcpy(res + len, sub, sz + 1);
|
|
|
|
last = len;
|
|
|
|
len += sz;
|
|
|
|
free(sub);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-27 10:29:13 -05:00
|
|
|
#define is_parsable(_l) (scols_table_is_raw((_l)->table) || \
|
2015-06-04 08:49:52 -05:00
|
|
|
scols_table_is_export((_l)->table) || \
|
|
|
|
scols_table_is_json((_l)->table))
|
2012-03-08 09:04:45 -06:00
|
|
|
|
2013-03-29 11:16:02 -05:00
|
|
|
static char *mk_name(const char *name)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
if (!name)
|
|
|
|
return NULL;
|
|
|
|
if (lsblk->paths)
|
|
|
|
xasprintf(&p, "/dev/%s", name);
|
|
|
|
else
|
|
|
|
p = xstrdup(name);
|
2015-05-29 03:55:01 -05:00
|
|
|
if (p)
|
|
|
|
sysfs_devname_sys_to_dev(p);
|
2013-03-29 11:16:02 -05:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *mk_dm_name(const char *name)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
if (!name)
|
|
|
|
return NULL;
|
|
|
|
if (lsblk->paths)
|
|
|
|
xasprintf(&p, "/dev/mapper/%s", name);
|
|
|
|
else
|
|
|
|
p = xstrdup(name);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2014-05-20 08:08:08 -05:00
|
|
|
/* stores data to scols cell userdata (invisible and independent on output)
|
|
|
|
* to make the original values accessible for sort functions
|
|
|
|
*/
|
|
|
|
static void set_sortdata_u64(struct libscols_line *ln, int col, uint64_t x)
|
|
|
|
{
|
|
|
|
struct libscols_cell *ce = scols_line_get_cell(ln, col);
|
|
|
|
uint64_t *data;
|
|
|
|
|
|
|
|
if (!ce)
|
|
|
|
return;
|
|
|
|
data = xmalloc(sizeof(uint64_t));
|
|
|
|
*data = x;
|
|
|
|
scols_cell_set_userdata(ce, data);
|
|
|
|
}
|
|
|
|
|
2018-10-18 05:39:41 -05:00
|
|
|
/* do not modify *data on any error */
|
|
|
|
static void str2u64(const char *str, uint64_t *data)
|
2014-05-20 08:08:08 -05:00
|
|
|
{
|
2018-10-18 05:39:41 -05:00
|
|
|
uintmax_t num;
|
|
|
|
char *end = NULL;
|
2014-05-20 08:08:08 -05:00
|
|
|
|
2018-10-18 05:39:41 -05:00
|
|
|
errno = 0;
|
|
|
|
if (str == NULL || *str == '\0')
|
2014-05-20 08:08:08 -05:00
|
|
|
return;
|
2018-10-18 05:39:41 -05:00
|
|
|
num = strtoumax(str, &end, 10);
|
2014-05-20 08:08:08 -05:00
|
|
|
|
2018-10-18 05:39:41 -05:00
|
|
|
if (errno || str == end || (end && *end))
|
|
|
|
return;
|
|
|
|
*data = num;
|
2014-05-20 08:08:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void unref_sortdata(struct libscols_table *tb)
|
|
|
|
{
|
|
|
|
struct libscols_iter *itr;
|
|
|
|
struct libscols_line *ln;
|
|
|
|
|
|
|
|
if (!tb || !lsblk->sort_col)
|
|
|
|
return;
|
|
|
|
itr = scols_new_iter(SCOLS_ITER_FORWARD);
|
|
|
|
if (!itr)
|
|
|
|
return;
|
|
|
|
while (scols_table_next_line(tb, itr, &ln) == 0) {
|
|
|
|
struct libscols_cell *ce = scols_line_get_column_cell(ln,
|
|
|
|
lsblk->sort_col);
|
|
|
|
void *data = scols_cell_get_userdata(ce);
|
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
scols_free_iter(itr);
|
|
|
|
}
|
|
|
|
|
2018-10-09 05:46:28 -05:00
|
|
|
static char *get_vfs_attribute(struct lsblk_device *dev, int id)
|
2018-09-13 05:11:06 -05:00
|
|
|
{
|
|
|
|
char *sizestr;
|
|
|
|
uint64_t vfs_attr = 0;
|
|
|
|
char *mnt;
|
|
|
|
|
2018-10-09 05:46:28 -05:00
|
|
|
if (!dev->fsstat.f_blocks) {
|
|
|
|
mnt = lsblk_device_get_mountpoint(dev);
|
|
|
|
if (!mnt || dev->is_swap)
|
2018-09-13 05:11:06 -05:00
|
|
|
return NULL;
|
2018-10-09 05:46:28 -05:00
|
|
|
if (statvfs(mnt, &dev->fsstat) != 0)
|
2018-09-13 05:11:06 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(id) {
|
|
|
|
case COL_FSSIZE:
|
2018-10-09 05:46:28 -05:00
|
|
|
vfs_attr = dev->fsstat.f_frsize * dev->fsstat.f_blocks;
|
2018-09-13 05:11:06 -05:00
|
|
|
break;
|
|
|
|
case COL_FSAVAIL:
|
2018-10-09 05:46:28 -05:00
|
|
|
vfs_attr = dev->fsstat.f_frsize * dev->fsstat.f_bavail;
|
2018-09-13 05:11:06 -05:00
|
|
|
break;
|
|
|
|
case COL_FSUSED:
|
2018-10-09 05:46:28 -05:00
|
|
|
vfs_attr = dev->fsstat.f_frsize * (dev->fsstat.f_blocks - dev->fsstat.f_bfree);
|
2018-09-13 05:11:06 -05:00
|
|
|
break;
|
|
|
|
case COL_FSUSEPERC:
|
2018-10-09 05:46:28 -05:00
|
|
|
if (dev->fsstat.f_blocks == 0)
|
2018-09-13 05:11:06 -05:00
|
|
|
return xstrdup("-");
|
|
|
|
|
|
|
|
xasprintf(&sizestr, "%.0f%%",
|
2018-10-09 05:46:28 -05:00
|
|
|
(double)(dev->fsstat.f_blocks - dev->fsstat.f_bfree) /
|
|
|
|
dev->fsstat.f_blocks * 100);
|
2018-09-13 05:11:06 -05:00
|
|
|
return sizestr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!vfs_attr)
|
|
|
|
sizestr = xstrdup("0");
|
|
|
|
else if (lsblk->bytes)
|
|
|
|
xasprintf(&sizestr, "%ju", vfs_attr);
|
|
|
|
else
|
|
|
|
sizestr = size_to_human_string(SIZE_SUFFIX_1LETTER, vfs_attr);
|
|
|
|
|
|
|
|
return sizestr;
|
|
|
|
}
|
|
|
|
|
2018-10-09 05:46:28 -05:00
|
|
|
static struct stat *device_get_stat(struct lsblk_device *dev)
|
2018-09-14 09:27:26 -05:00
|
|
|
{
|
2019-05-23 05:28:13 -05:00
|
|
|
if (!dev->st.st_rdev
|
|
|
|
&& stat(dev->filename, &dev->st) != 0)
|
|
|
|
return NULL;
|
2018-09-14 09:27:26 -05:00
|
|
|
|
2018-10-09 05:46:28 -05:00
|
|
|
return &dev->st;
|
2018-09-14 09:27:26 -05:00
|
|
|
}
|
2018-09-13 05:11:06 -05:00
|
|
|
|
2018-10-17 06:48:43 -05:00
|
|
|
static int is_removable_device(struct lsblk_device *dev, struct lsblk_device *parent)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2019-05-23 05:31:49 -05:00
|
|
|
/* dev is partition and parent is whole-disk */
|
2018-10-17 06:48:43 -05:00
|
|
|
if (pc == parent->sysfs)
|
|
|
|
dev->removable = is_removable_device(parent, NULL);
|
2019-05-23 05:31:49 -05:00
|
|
|
|
|
|
|
/* parent is something else, use sysfs parent */
|
|
|
|
else if (ul_path_scanf(pc, "removable", "%d", &dev->removable) != 1)
|
|
|
|
dev->removable = 0;
|
2018-10-17 06:48:43 -05:00
|
|
|
}
|
|
|
|
done:
|
|
|
|
if (dev->removable == -1)
|
|
|
|
dev->removable = 0;
|
|
|
|
return dev->removable;
|
|
|
|
}
|
|
|
|
|
2018-10-17 07:10:27 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-10-17 03:54:59 -05:00
|
|
|
/*
|
2018-10-18 05:39:41 -05:00
|
|
|
* 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.
|
2018-10-17 03:54:59 -05:00
|
|
|
*/
|
2018-10-18 05:39:41 -05:00
|
|
|
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 */
|
2010-11-24 10:06:39 -06:00
|
|
|
{
|
2019-10-15 06:08:09 -05:00
|
|
|
struct lsblk_devprop *prop = NULL;
|
2014-04-02 06:52:08 -05:00
|
|
|
char *str = NULL;
|
2010-11-24 10:06:39 -06:00
|
|
|
|
|
|
|
switch(id) {
|
|
|
|
case COL_NAME:
|
2018-10-09 05:46:28 -05:00
|
|
|
str = dev->dm_name ? mk_dm_name(dev->dm_name) : mk_name(dev->name);
|
2013-03-29 11:16:02 -05:00
|
|
|
break;
|
2010-11-24 10:06:39 -06:00
|
|
|
case COL_KNAME:
|
2018-10-09 05:46:28 -05:00
|
|
|
str = mk_name(dev->name);
|
2010-11-24 10:06:39 -06:00
|
|
|
break;
|
2012-10-19 07:10:39 -05:00
|
|
|
case COL_PKNAME:
|
2018-10-16 07:20:29 -05:00
|
|
|
if (parent)
|
|
|
|
str = mk_name(parent->name);
|
2012-10-19 07:10:39 -05:00
|
|
|
break;
|
2018-07-10 03:33:18 -05:00
|
|
|
case COL_PATH:
|
2018-10-09 05:46:28 -05:00
|
|
|
if (dev->filename)
|
|
|
|
str = xstrdup(dev->filename);
|
2018-07-10 03:33:18 -05:00
|
|
|
break;
|
2010-11-24 10:06:39 -06:00
|
|
|
case COL_OWNER:
|
2019-10-15 06:08:09 -05:00
|
|
|
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);
|
|
|
|
}
|
2010-11-24 10:06:39 -06:00
|
|
|
break;
|
|
|
|
case COL_GROUP:
|
2019-10-15 06:08:09 -05:00
|
|
|
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);
|
|
|
|
}
|
2010-11-24 10:06:39 -06:00
|
|
|
break;
|
|
|
|
case COL_MODE:
|
2019-10-15 06:08:09 -05:00
|
|
|
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));
|
|
|
|
}
|
2010-11-24 10:06:39 -06:00
|
|
|
break;
|
|
|
|
case COL_MAJMIN:
|
2012-03-08 09:04:45 -06:00
|
|
|
if (is_parsable(lsblk))
|
2018-10-09 05:46:28 -05:00
|
|
|
xasprintf(&str, "%u:%u", dev->maj, dev->min);
|
2010-11-24 10:06:39 -06:00
|
|
|
else
|
2018-10-09 05:46:28 -05:00
|
|
|
xasprintf(&str, "%3u:%-3u", dev->maj, dev->min);
|
2018-10-18 05:39:41 -05:00
|
|
|
if (sortdata)
|
|
|
|
*sortdata = makedev(dev->maj, dev->min);
|
2010-11-24 10:06:39 -06:00
|
|
|
break;
|
|
|
|
case COL_FSTYPE:
|
2018-10-09 05:46:28 -05:00
|
|
|
prop = lsblk_device_get_properties(dev);
|
2018-09-19 04:04:35 -05:00
|
|
|
if (prop && prop->fstype)
|
|
|
|
str = xstrdup(prop->fstype);
|
2010-11-24 10:06:39 -06:00
|
|
|
break;
|
2018-09-13 05:11:06 -05:00
|
|
|
case COL_FSSIZE:
|
|
|
|
case COL_FSAVAIL:
|
|
|
|
case COL_FSUSED:
|
|
|
|
case COL_FSUSEPERC:
|
2018-10-09 05:46:28 -05:00
|
|
|
str = get_vfs_attribute(dev, id);
|
2018-09-13 05:11:06 -05:00
|
|
|
break;
|
2019-11-12 06:55:55 -06:00
|
|
|
case COL_FSVERSION:
|
|
|
|
prop = lsblk_device_get_properties(dev);
|
|
|
|
if (prop && prop->fsversion)
|
|
|
|
str = xstrdup(prop->fsversion);
|
|
|
|
break;
|
2010-11-24 10:06:39 -06:00
|
|
|
case COL_TARGET:
|
2019-07-12 16:28:10 -05:00
|
|
|
{
|
|
|
|
char *s = lsblk_device_get_mountpoint(dev);
|
|
|
|
if (s)
|
|
|
|
str = xstrdup(s);
|
|
|
|
else
|
|
|
|
str = NULL;
|
2010-11-24 10:06:39 -06:00
|
|
|
break;
|
2019-07-12 16:28:10 -05:00
|
|
|
}
|
2010-11-24 10:06:39 -06:00
|
|
|
case COL_LABEL:
|
2018-10-09 05:46:28 -05:00
|
|
|
prop = lsblk_device_get_properties(dev);
|
2018-09-19 04:04:35 -05:00
|
|
|
if (prop && prop->label)
|
|
|
|
str = xstrdup(prop->label);
|
2010-11-24 10:06:39 -06:00
|
|
|
break;
|
|
|
|
case COL_UUID:
|
2018-10-09 05:46:28 -05:00
|
|
|
prop = lsblk_device_get_properties(dev);
|
2018-09-19 04:04:35 -05:00
|
|
|
if (prop && prop->uuid)
|
|
|
|
str = xstrdup(prop->uuid);
|
2010-11-24 10:06:39 -06:00
|
|
|
break;
|
2018-06-19 05:33:20 -05:00
|
|
|
case COL_PTUUID:
|
2018-10-09 05:46:28 -05:00
|
|
|
prop = lsblk_device_get_properties(dev);
|
2018-09-19 04:04:35 -05:00
|
|
|
if (prop && prop->ptuuid)
|
|
|
|
str = xstrdup(prop->ptuuid);
|
2018-06-19 05:33:20 -05:00
|
|
|
break;
|
|
|
|
case COL_PTTYPE:
|
2018-10-09 05:46:28 -05:00
|
|
|
prop = lsblk_device_get_properties(dev);
|
2018-09-19 04:04:35 -05:00
|
|
|
if (prop && prop->pttype)
|
|
|
|
str = xstrdup(prop->pttype);
|
2018-06-19 05:33:20 -05:00
|
|
|
break;
|
2014-01-14 15:19:50 -06:00
|
|
|
case COL_PARTTYPE:
|
2018-10-09 05:46:28 -05:00
|
|
|
prop = lsblk_device_get_properties(dev);
|
2018-09-19 04:04:35 -05:00
|
|
|
if (prop && prop->parttype)
|
|
|
|
str = xstrdup(prop->parttype);
|
2014-01-14 15:19:50 -06:00
|
|
|
break;
|
2019-12-04 05:38:57 -06:00
|
|
|
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;
|
2012-04-19 07:39:44 -05:00
|
|
|
case COL_PARTLABEL:
|
2018-10-09 05:46:28 -05:00
|
|
|
prop = lsblk_device_get_properties(dev);
|
2018-09-19 04:04:35 -05:00
|
|
|
if (prop && prop->partlabel)
|
|
|
|
str = xstrdup(prop->partlabel);
|
2012-04-19 07:39:44 -05:00
|
|
|
break;
|
|
|
|
case COL_PARTUUID:
|
2018-10-09 05:46:28 -05:00
|
|
|
prop = lsblk_device_get_properties(dev);
|
2018-09-19 04:04:35 -05:00
|
|
|
if (prop && prop->partuuid)
|
|
|
|
str = xstrdup(prop->partuuid);
|
2012-04-19 07:39:44 -05:00
|
|
|
break;
|
2014-03-07 04:14:46 -06:00
|
|
|
case COL_PARTFLAGS:
|
2018-10-09 05:46:28 -05:00
|
|
|
prop = lsblk_device_get_properties(dev);
|
2018-09-19 04:04:35 -05:00
|
|
|
if (prop && prop->partflags)
|
|
|
|
str = xstrdup(prop->partflags);
|
2014-03-07 04:14:46 -06:00
|
|
|
break;
|
2012-08-14 11:14:35 -05:00
|
|
|
case COL_WWN:
|
2018-10-09 05:46:28 -05:00
|
|
|
prop = lsblk_device_get_properties(dev);
|
2018-09-19 04:04:35 -05:00
|
|
|
if (prop && prop->wwn)
|
|
|
|
str = xstrdup(prop->wwn);
|
2012-08-14 11:14:35 -05:00
|
|
|
break;
|
2012-07-24 09:34:33 -05:00
|
|
|
case COL_RA:
|
2018-10-09 05:46:28 -05:00
|
|
|
ul_path_read_string(dev->sysfs, &str, "queue/read_ahead_kb");
|
2018-10-18 05:39:41 -05:00
|
|
|
if (sortdata)
|
|
|
|
str2u64(str, sortdata);
|
2012-07-24 09:34:33 -05:00
|
|
|
break;
|
2010-11-24 10:06:39 -06:00
|
|
|
case COL_RO:
|
2018-10-09 05:46:28 -05:00
|
|
|
str = xstrdup(is_readonly_device(dev) ? "1" : "0");
|
2010-11-24 10:06:39 -06:00
|
|
|
break;
|
2011-01-10 04:18:51 -06:00
|
|
|
case COL_RM:
|
2018-10-17 06:48:43 -05:00
|
|
|
str = xstrdup(is_removable_device(dev, parent) ? "1" : "0");
|
2010-11-24 10:06:39 -06:00
|
|
|
break;
|
2014-12-02 04:09:04 -06:00
|
|
|
case COL_HOTPLUG:
|
2018-10-09 05:46:28 -05:00
|
|
|
str = sysfs_blkdev_is_hotpluggable(dev->sysfs) ? xstrdup("1") : xstrdup("0");
|
2014-12-02 04:09:04 -06:00
|
|
|
break;
|
2010-11-24 10:06:39 -06:00
|
|
|
case COL_ROTA:
|
2018-10-09 05:46:28 -05:00
|
|
|
ul_path_read_string(dev->sysfs, &str, "queue/rotational");
|
2010-11-24 10:06:39 -06:00
|
|
|
break;
|
2012-08-17 13:25:26 -05:00
|
|
|
case COL_RAND:
|
2018-10-09 05:46:28 -05:00
|
|
|
ul_path_read_string(dev->sysfs, &str, "queue/add_random");
|
2012-08-17 13:25:26 -05:00
|
|
|
break;
|
2010-11-24 10:06:39 -06:00
|
|
|
case COL_MODEL:
|
2018-10-18 06:57:46 -05:00
|
|
|
if (!device_is_partition(dev) && dev->nslaves == 0) {
|
2018-10-09 05:46:28 -05:00
|
|
|
prop = lsblk_device_get_properties(dev);
|
2018-09-19 04:04:35 -05:00
|
|
|
if (prop && prop->model)
|
|
|
|
str = xstrdup(prop->model);
|
2018-09-10 03:29:37 -05:00
|
|
|
else
|
2018-10-09 05:46:28 -05:00
|
|
|
ul_path_read_string(dev->sysfs, &str, "device/model");
|
2018-09-10 03:29:37 -05:00
|
|
|
}
|
2012-11-02 12:56:29 -05:00
|
|
|
break;
|
2013-05-13 06:33:41 -05:00
|
|
|
case COL_SERIAL:
|
2018-10-18 06:57:46 -05:00
|
|
|
if (!device_is_partition(dev) && dev->nslaves == 0) {
|
2018-10-09 05:46:28 -05:00
|
|
|
prop = lsblk_device_get_properties(dev);
|
2018-09-19 04:04:35 -05:00
|
|
|
if (prop && prop->serial)
|
|
|
|
str = xstrdup(prop->serial);
|
2016-06-02 07:41:08 -05:00
|
|
|
else
|
2018-10-09 05:46:28 -05:00
|
|
|
ul_path_read_string(dev->sysfs, &str, "device/serial");
|
2013-05-13 06:33:41 -05:00
|
|
|
}
|
|
|
|
break;
|
2012-11-02 12:56:29 -05:00
|
|
|
case COL_REV:
|
2018-10-18 06:57:46 -05:00
|
|
|
if (!device_is_partition(dev) && dev->nslaves == 0)
|
2018-10-09 05:46:28 -05:00
|
|
|
ul_path_read_string(dev->sysfs, &str, "device/rev");
|
2012-11-02 12:56:30 -05:00
|
|
|
break;
|
|
|
|
case COL_VENDOR:
|
2018-10-18 06:57:46 -05:00
|
|
|
if (!device_is_partition(dev) && dev->nslaves == 0)
|
2018-10-09 05:46:28 -05:00
|
|
|
ul_path_read_string(dev->sysfs, &str, "device/vendor");
|
2010-11-24 10:06:39 -06:00
|
|
|
break;
|
|
|
|
case COL_SIZE:
|
2014-04-02 06:52:08 -05:00
|
|
|
if (lsblk->bytes)
|
2018-10-09 05:46:28 -05:00
|
|
|
xasprintf(&str, "%ju", dev->size);
|
2014-04-02 06:52:08 -05:00
|
|
|
else
|
2018-10-09 05:46:28 -05:00
|
|
|
str = size_to_human_string(SIZE_SUFFIX_1LETTER, dev->size);
|
2018-10-18 05:39:41 -05:00
|
|
|
if (sortdata)
|
|
|
|
*sortdata = dev->size;
|
2010-11-24 10:06:39 -06:00
|
|
|
break;
|
2011-07-05 16:29:30 -05:00
|
|
|
case COL_STATE:
|
2018-10-18 06:57:46 -05:00
|
|
|
if (!device_is_partition(dev) && !dev->dm_name)
|
2018-10-09 05:46:28 -05:00
|
|
|
ul_path_read_string(dev->sysfs, &str, "device/state");
|
|
|
|
else if (dev->dm_name) {
|
2011-07-05 16:29:30 -05:00
|
|
|
int x = 0;
|
2018-10-09 05:46:28 -05:00
|
|
|
if (ul_path_read_s32(dev->sysfs, &x, "dm/suspended") == 0)
|
2014-04-02 06:52:08 -05:00
|
|
|
str = xstrdup(x ? "suspended" : "running");
|
2011-07-05 16:29:30 -05:00
|
|
|
}
|
|
|
|
break;
|
2010-11-24 10:06:39 -06:00
|
|
|
case COL_ALIOFF:
|
2018-10-09 05:46:28 -05:00
|
|
|
ul_path_read_string(dev->sysfs, &str, "alignment_offset");
|
2018-10-18 05:39:41 -05:00
|
|
|
if (sortdata)
|
|
|
|
str2u64(str, sortdata);
|
2010-11-24 10:06:39 -06:00
|
|
|
break;
|
|
|
|
case COL_MINIO:
|
2018-10-09 05:46:28 -05:00
|
|
|
ul_path_read_string(dev->sysfs, &str, "queue/minimum_io_size");
|
2018-10-18 05:39:41 -05:00
|
|
|
if (sortdata)
|
|
|
|
str2u64(str, sortdata);
|
2010-11-24 10:06:39 -06:00
|
|
|
break;
|
|
|
|
case COL_OPTIO:
|
2018-10-09 05:46:28 -05:00
|
|
|
ul_path_read_string(dev->sysfs, &str, "queue/optimal_io_size");
|
2018-10-18 05:39:41 -05:00
|
|
|
if (sortdata)
|
|
|
|
str2u64(str, sortdata);
|
2010-11-24 10:06:39 -06:00
|
|
|
break;
|
|
|
|
case COL_PHYSEC:
|
2018-10-09 05:46:28 -05:00
|
|
|
ul_path_read_string(dev->sysfs, &str, "queue/physical_block_size");
|
2018-10-18 05:39:41 -05:00
|
|
|
if (sortdata)
|
|
|
|
str2u64(str, sortdata);
|
2010-11-24 10:06:39 -06:00
|
|
|
break;
|
|
|
|
case COL_LOGSEC:
|
2018-10-09 05:46:28 -05:00
|
|
|
ul_path_read_string(dev->sysfs, &str, "queue/logical_block_size");
|
2018-10-18 05:39:41 -05:00
|
|
|
if (sortdata)
|
|
|
|
str2u64(str, sortdata);
|
2010-11-24 10:06:39 -06:00
|
|
|
break;
|
|
|
|
case COL_SCHED:
|
2018-10-09 05:46:28 -05:00
|
|
|
str = get_scheduler(dev);
|
2010-11-24 10:06:39 -06:00
|
|
|
break;
|
2011-07-09 08:24:04 -05:00
|
|
|
case COL_RQ_SIZE:
|
2018-10-09 05:46:28 -05:00
|
|
|
ul_path_read_string(dev->sysfs, &str, "queue/nr_requests");
|
2018-10-18 05:39:41 -05:00
|
|
|
if (sortdata)
|
|
|
|
str2u64(str, sortdata);
|
2011-07-09 08:24:04 -05:00
|
|
|
break;
|
2011-02-24 10:41:33 -06:00
|
|
|
case COL_TYPE:
|
2018-10-09 05:46:28 -05:00
|
|
|
str = get_type(dev);
|
2011-02-24 10:41:33 -06:00
|
|
|
break;
|
2012-10-19 07:10:40 -05:00
|
|
|
case COL_HCTL:
|
|
|
|
{
|
|
|
|
int h, c, t, l;
|
2018-10-09 05:46:28 -05:00
|
|
|
if (sysfs_blkdev_scsi_get_hctl(dev->sysfs, &h, &c, &t, &l) == 0)
|
2014-04-02 06:52:08 -05:00
|
|
|
xasprintf(&str, "%d:%d:%d:%d", h, c, t, l);
|
2012-10-19 07:10:40 -05:00
|
|
|
break;
|
|
|
|
}
|
2012-10-19 07:10:41 -05:00
|
|
|
case COL_TRANSPORT:
|
2018-10-09 05:46:28 -05:00
|
|
|
str = get_transport(dev);
|
2012-10-19 07:10:41 -05:00
|
|
|
break;
|
2014-12-02 04:38:15 -06:00
|
|
|
case COL_SUBSYS:
|
2018-10-09 05:46:28 -05:00
|
|
|
str = get_subsystems(dev);
|
2014-12-02 04:38:15 -06:00
|
|
|
break;
|
2011-05-04 10:16:05 -05:00
|
|
|
case COL_DALIGN:
|
2018-10-17 07:10:27 -05:00
|
|
|
if (device_get_discard_granularity(dev) > 0)
|
2018-10-09 05:46:28 -05:00
|
|
|
ul_path_read_string(dev->sysfs, &str, "discard_alignment");
|
2014-04-02 06:52:08 -05:00
|
|
|
if (!str)
|
|
|
|
str = xstrdup("0");
|
2018-10-18 05:39:41 -05:00
|
|
|
if (sortdata)
|
|
|
|
str2u64(str, sortdata);
|
2011-05-04 10:16:05 -05:00
|
|
|
break;
|
|
|
|
case COL_DGRAN:
|
2014-05-20 08:08:08 -05:00
|
|
|
if (lsblk->bytes) {
|
2018-10-09 05:46:28 -05:00
|
|
|
ul_path_read_string(dev->sysfs, &str, "queue/discard_granularity");
|
2018-10-18 05:39:41 -05:00
|
|
|
if (sortdata)
|
|
|
|
str2u64(str, sortdata);
|
2014-05-20 08:08:08 -05:00
|
|
|
} else {
|
2018-10-17 07:10:27 -05:00
|
|
|
uint64_t x = device_get_discard_granularity(dev);
|
|
|
|
str = size_to_human_string(SIZE_SUFFIX_1LETTER, x);
|
2018-10-18 05:39:41 -05:00
|
|
|
if (sortdata)
|
|
|
|
*sortdata = x;
|
2011-05-18 05:17:43 -05:00
|
|
|
}
|
2011-05-04 10:16:05 -05:00
|
|
|
break;
|
|
|
|
case COL_DMAX:
|
2014-05-20 08:08:08 -05:00
|
|
|
if (lsblk->bytes) {
|
2018-10-09 05:46:28 -05:00
|
|
|
ul_path_read_string(dev->sysfs, &str, "queue/discard_max_bytes");
|
2018-10-18 05:39:41 -05:00
|
|
|
if (sortdata)
|
|
|
|
str2u64(str, sortdata);
|
2014-05-20 08:08:08 -05:00
|
|
|
} else {
|
2011-05-18 05:17:43 -05:00
|
|
|
uint64_t x;
|
2018-10-09 05:46:28 -05:00
|
|
|
if (ul_path_read_u64(dev->sysfs, &x, "queue/discard_max_bytes") == 0) {
|
2014-04-02 06:52:08 -05:00
|
|
|
str = size_to_human_string(SIZE_SUFFIX_1LETTER, x);
|
2018-10-18 05:39:41 -05:00
|
|
|
if (sortdata)
|
|
|
|
*sortdata = x;
|
2014-05-20 08:08:08 -05:00
|
|
|
}
|
2011-05-18 05:17:43 -05:00
|
|
|
}
|
2011-05-04 10:16:05 -05:00
|
|
|
break;
|
|
|
|
case COL_DZERO:
|
2018-10-17 07:10:27 -05:00
|
|
|
if (device_get_discard_granularity(dev) > 0)
|
2018-10-09 05:46:28 -05:00
|
|
|
ul_path_read_string(dev->sysfs, &str, "queue/discard_zeroes_data");
|
2014-04-02 06:52:08 -05:00
|
|
|
if (!str)
|
|
|
|
str = xstrdup("0");
|
2011-05-04 10:16:05 -05:00
|
|
|
break;
|
2012-12-20 05:42:23 -06:00
|
|
|
case COL_WSAME:
|
2014-05-20 08:08:08 -05:00
|
|
|
if (lsblk->bytes) {
|
2018-10-09 05:46:28 -05:00
|
|
|
ul_path_read_string(dev->sysfs, &str, "queue/write_same_max_bytes");
|
2018-10-18 05:39:41 -05:00
|
|
|
if (sortdata)
|
|
|
|
str2u64(str, sortdata);
|
2014-05-20 08:08:08 -05:00
|
|
|
} else {
|
2012-12-20 05:42:23 -06:00
|
|
|
uint64_t x;
|
|
|
|
|
2018-10-09 05:46:28 -05:00
|
|
|
if (ul_path_read_u64(dev->sysfs, &x, "queue/write_same_max_bytes") == 0) {
|
2014-04-02 06:52:08 -05:00
|
|
|
str = size_to_human_string(SIZE_SUFFIX_1LETTER, x);
|
2018-10-18 05:39:41 -05:00
|
|
|
if (sortdata)
|
|
|
|
*sortdata = x;
|
2014-05-20 08:08:08 -05:00
|
|
|
}
|
2012-12-20 05:42:23 -06:00
|
|
|
}
|
2014-04-02 06:52:08 -05:00
|
|
|
if (!str)
|
|
|
|
str = xstrdup("0");
|
2012-12-20 05:42:23 -06:00
|
|
|
break;
|
2017-04-18 19:25:58 -05:00
|
|
|
case COL_ZONED:
|
2018-10-09 05:46:28 -05:00
|
|
|
ul_path_read_string(dev->sysfs, &str, "queue/zoned");
|
2017-04-18 19:25:58 -05:00
|
|
|
break;
|
2020-05-05 09:31:44 -05:00
|
|
|
case COL_DAX:
|
|
|
|
ul_path_read_string(dev->sysfs, &str, "queue/dax");
|
|
|
|
break;
|
2010-11-24 10:06:39 -06:00
|
|
|
};
|
2014-04-02 06:52:08 -05:00
|
|
|
|
2018-10-18 05:39:41 -05:00
|
|
|
return str;
|
2010-11-24 10:06:39 -06:00
|
|
|
}
|
|
|
|
|
2018-10-17 03:54:59 -05:00
|
|
|
/*
|
|
|
|
* Adds data for all wanted columns about the device to the smartcols table
|
|
|
|
*/
|
2018-10-18 05:39:41 -05:00
|
|
|
static void device_to_scols(
|
|
|
|
struct lsblk_device *dev,
|
|
|
|
struct lsblk_device *parent,
|
|
|
|
struct libscols_table *tab,
|
|
|
|
struct libscols_line *parent_line)
|
2010-11-24 10:06:39 -06:00
|
|
|
{
|
2015-04-22 04:08:05 -05:00
|
|
|
size_t i;
|
2018-10-18 05:39:41 -05:00
|
|
|
struct libscols_line *ln;
|
2018-10-15 06:55:08 -05:00
|
|
|
struct lsblk_iter itr;
|
|
|
|
struct lsblk_device *child = NULL;
|
2018-12-07 05:29:50 -06:00
|
|
|
int link_group = 0;
|
2010-11-24 10:06:39 -06:00
|
|
|
|
2019-05-02 08:51:48 -05:00
|
|
|
|
|
|
|
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));
|
2018-10-17 06:19:50 -05:00
|
|
|
|
2019-06-27 02:22:18 -05:00
|
|
|
if (!parent && dev->wholedisk)
|
|
|
|
parent = dev->wholedisk;
|
|
|
|
|
2020-02-25 08:31:23 -06:00
|
|
|
/* Do not print device more than once on --list if tree order is not requested */
|
|
|
|
if (!(lsblk->flags & LSBLK_TREE) && !lsblk->force_tree_order && dev->is_printed)
|
2018-12-07 05:29:50 -06:00
|
|
|
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);
|
2018-10-18 05:39:41 -05:00
|
|
|
if (!ln)
|
2017-05-18 04:39:34 -05:00
|
|
|
err(EXIT_FAILURE, _("failed to allocate output line"));
|
2010-11-24 10:06:39 -06:00
|
|
|
|
2018-12-07 05:29:50 -06:00
|
|
|
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 */
|
2019-05-02 08:51:48 -05:00
|
|
|
DBG(DEV, ul_debugobj(dev, " grouping parents [--merge]"));
|
2018-12-07 05:29:50 -06:00
|
|
|
lsblk_reset_iter(&itr, LSBLK_ITER_FORWARD);
|
|
|
|
while (lsblk_device_next_parent(dev, &itr, &p) == 0) {
|
2019-05-02 08:51:48 -05:00
|
|
|
if (!p->scols_line) {
|
|
|
|
DBG(DEV, ul_debugobj(dev, " *** ignore '%s' no scols line yet", p->name));
|
2018-12-07 05:29:50 -06:00
|
|
|
continue;
|
2019-05-02 08:51:48 -05:00
|
|
|
}
|
|
|
|
DBG(DEV, ul_debugobj(dev, " group '%s'", p->name));
|
|
|
|
scols_table_group_lines(tab, p->scols_line, gr, 0);
|
2018-12-07 05:29:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Link the group -- this makes group->child connection */
|
2019-05-02 08:51:48 -05:00
|
|
|
DBG(DEV, ul_debugobj(dev, " linking the group [--merge]"));
|
2018-12-07 05:29:50 -06:00
|
|
|
scols_line_link_group(ln, gr, 0);
|
|
|
|
}
|
|
|
|
|
2018-10-18 05:39:41 -05:00
|
|
|
/* 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);
|
|
|
|
}
|
2019-05-02 08:51:48 -05:00
|
|
|
DBG(DEV, ul_debugobj(dev, " refer data[%zu]=\"%s\"", i, data));
|
2018-10-18 05:39:41 -05:00
|
|
|
if (data && scols_line_refer_data(ln, i, data))
|
|
|
|
err(EXIT_FAILURE, _("failed to add output data"));
|
|
|
|
}
|
2018-10-15 06:55:08 -05:00
|
|
|
|
2018-12-07 05:29:50 -06:00
|
|
|
dev->scols_line = ln;
|
|
|
|
|
2018-10-17 06:19:50 -05:00
|
|
|
if (dev->npartitions == 0)
|
|
|
|
/* For partitions we often read from parental whole-disk sysfs,
|
|
|
|
* otherwise we can close */
|
|
|
|
ul_path_close_dirfd(dev->sysfs);
|
|
|
|
|
2018-12-07 05:29:50 -06:00
|
|
|
lsblk_reset_iter(&itr, LSBLK_ITER_FORWARD);
|
2019-05-02 08:51:48 -05:00
|
|
|
while (lsblk_device_next_child(dev, &itr, &child) == 0) {
|
|
|
|
DBG(DEV, ul_debugobj(dev, "%s -> continue to child", dev->name));
|
2018-10-18 05:39:41 -05:00
|
|
|
device_to_scols(child, dev, tab, ln);
|
2019-05-02 08:51:48 -05:00
|
|
|
DBG(DEV, ul_debugobj(dev, "%s <- child done", dev->name));
|
|
|
|
}
|
2018-10-17 06:19:50 -05:00
|
|
|
|
|
|
|
/* Let's be careful with number of open files */
|
|
|
|
ul_path_close_dirfd(dev->sysfs);
|
2018-10-15 06:55:08 -05:00
|
|
|
}
|
|
|
|
|
2018-10-17 03:54:59 -05:00
|
|
|
/*
|
|
|
|
* Walks on tree and adds one line for each device to the smartcols table
|
|
|
|
*/
|
2018-10-15 06:55:08 -05:00
|
|
|
static void devtree_to_scols(struct lsblk_devtree *tr, struct libscols_table *tab)
|
|
|
|
{
|
|
|
|
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)
|
2018-10-18 05:39:41 -05:00
|
|
|
device_to_scols(dev, NULL, tab, NULL);
|
2010-11-24 10:06:39 -06:00
|
|
|
}
|
|
|
|
|
2020-09-09 05:18:07 -05:00
|
|
|
static int ignore_empty(struct lsblk_device *dev)
|
|
|
|
{
|
2020-09-09 07:50:02 -05:00
|
|
|
/* show all non-empty devices */
|
|
|
|
if (dev->size)
|
2020-09-09 05:18:07 -05:00
|
|
|
return 0;
|
2020-09-09 07:50:02 -05:00
|
|
|
|
|
|
|
/* ignore empty loop devices without backing file */
|
|
|
|
if (dev->maj == LOOPDEV_MAJOR &&
|
|
|
|
!loopdev_has_backing_file(dev->filename))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
2020-09-09 05:18:07 -05:00
|
|
|
}
|
|
|
|
|
2018-10-17 03:54:59 -05:00
|
|
|
/*
|
|
|
|
* Reads very basic information about the device from sysfs into the device struct
|
|
|
|
*/
|
2018-10-17 04:14:40 -05:00
|
|
|
static int initialize_device(struct lsblk_device *dev,
|
2018-10-09 05:46:28 -05:00
|
|
|
struct lsblk_device *wholedisk,
|
2012-02-14 02:53:16 -06:00
|
|
|
const char *name)
|
2010-11-24 10:06:39 -06:00
|
|
|
{
|
2011-05-16 08:24:26 -05:00
|
|
|
dev_t devno;
|
2010-11-24 10:06:39 -06:00
|
|
|
|
2018-10-17 04:14:40 -05:00
|
|
|
DBG(DEV, ul_debugobj(dev, "initialize %s [wholedisk=%p %s]",
|
|
|
|
name, wholedisk, wholedisk ? wholedisk->name : ""));
|
2015-04-01 05:06:15 -05:00
|
|
|
|
2020-05-13 17:06:10 -05:00
|
|
|
if (sysfs_devname_is_hidden(lsblk->sysroot, name)) {
|
|
|
|
DBG(DEV, ul_debugobj(dev, "%s: hidden, ignore", name));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-10-09 05:46:28 -05:00
|
|
|
dev->name = xstrdup(name);
|
2018-10-18 06:57:46 -05:00
|
|
|
|
|
|
|
if (wholedisk) {
|
|
|
|
dev->wholedisk = wholedisk;
|
|
|
|
lsblk_ref_device(wholedisk);
|
|
|
|
}
|
2010-11-24 10:06:39 -06:00
|
|
|
|
2018-10-09 05:46:28 -05:00
|
|
|
dev->filename = get_device_path(dev);
|
|
|
|
if (!dev->filename) {
|
2018-10-12 05:51:03 -05:00
|
|
|
DBG(DEV, ul_debugobj(dev, "%s: failed to get device path", dev->name));
|
2011-07-21 15:56:20 -05:00
|
|
|
return -1;
|
|
|
|
}
|
2018-10-12 05:51:03 -05:00
|
|
|
DBG(DEV, ul_debugobj(dev, "%s: filename=%s", dev->name, dev->filename));
|
2010-11-24 10:06:39 -06:00
|
|
|
|
2018-10-09 05:46:28 -05:00
|
|
|
devno = __sysfs_devname_to_devno(lsblk->sysroot, dev->name, wholedisk ? wholedisk->name : NULL);
|
2011-07-21 15:56:20 -05:00
|
|
|
if (!devno) {
|
2018-10-12 05:51:03 -05:00
|
|
|
DBG(DEV, ul_debugobj(dev, "%s: unknown device name", dev->name));
|
2011-07-21 15:56:20 -05:00
|
|
|
return -1;
|
|
|
|
}
|
2010-11-24 10:06:39 -06:00
|
|
|
|
2018-10-16 07:29:00 -05:00
|
|
|
dev->sysfs = ul_new_sysfs_path(devno, wholedisk ? wholedisk->sysfs : NULL, lsblk->sysroot);
|
|
|
|
if (!dev->sysfs) {
|
|
|
|
DBG(DEV, ul_debugobj(dev, "%s: failed to initialize sysfs handler", dev->name));
|
|
|
|
return -1;
|
2011-07-21 15:56:20 -05:00
|
|
|
}
|
2011-05-16 08:24:26 -05:00
|
|
|
|
2018-10-09 05:46:28 -05:00
|
|
|
dev->maj = major(devno);
|
|
|
|
dev->min = minor(devno);
|
|
|
|
dev->size = 0;
|
2010-11-24 10:06:39 -06:00
|
|
|
|
2018-10-09 05:46:28 -05:00
|
|
|
if (ul_path_read_u64(dev->sysfs, &dev->size, "size") == 0) /* in sectors */
|
|
|
|
dev->size <<= 9; /* in bytes */
|
2011-05-18 04:57:17 -05:00
|
|
|
|
2010-11-24 10:06:39 -06:00
|
|
|
/* Ignore devices of zero size */
|
2020-09-09 05:18:07 -05:00
|
|
|
if (!lsblk->all_devices && ignore_empty(dev)) {
|
2018-10-12 05:51:03 -05:00
|
|
|
DBG(DEV, ul_debugobj(dev, "zero size device -- ignore"));
|
2010-11-24 10:06:39 -06:00
|
|
|
return -1;
|
2015-04-01 05:06:15 -05:00
|
|
|
}
|
2018-10-09 05:46:28 -05:00
|
|
|
if (is_dm(dev->name)) {
|
|
|
|
ul_path_read_string(dev->sysfs, &dev->dm_name, "dm/name");
|
|
|
|
if (!dev->dm_name) {
|
2018-10-12 05:51:03 -05:00
|
|
|
DBG(DEV, ul_debugobj(dev, "%s: failed to get dm name", dev->name));
|
2011-07-21 15:56:20 -05:00
|
|
|
return -1;
|
|
|
|
}
|
2011-05-16 08:24:26 -05:00
|
|
|
}
|
2010-11-24 10:06:39 -06:00
|
|
|
|
2018-10-09 05:46:28 -05:00
|
|
|
dev->npartitions = sysfs_blkdev_count_partitions(dev->sysfs, dev->name);
|
|
|
|
dev->nholders = ul_path_count_dirents(dev->sysfs, "holders");
|
|
|
|
dev->nslaves = ul_path_count_dirents(dev->sysfs, "slaves");
|
2010-11-24 10:06:39 -06:00
|
|
|
|
2018-10-12 05:51:03 -05:00
|
|
|
DBG(DEV, ul_debugobj(dev, "%s: npartitions=%d, nholders=%d, nslaves=%d",
|
2018-10-09 05:46:28 -05:00
|
|
|
dev->name, dev->npartitions, dev->nholders, dev->nslaves));
|
2015-04-01 05:06:15 -05:00
|
|
|
|
2012-11-02 12:56:31 -05:00
|
|
|
/* ignore non-SCSI devices */
|
2018-10-09 05:46:28 -05:00
|
|
|
if (lsblk->scsi && sysfs_blkdev_scsi_get_hctl(dev->sysfs, NULL, NULL, NULL, NULL)) {
|
2018-10-12 05:51:03 -05:00
|
|
|
DBG(DEV, ul_debugobj(dev, "non-scsi device -- ignore"));
|
2012-11-02 12:56:31 -05:00
|
|
|
return -1;
|
2015-04-01 05:06:15 -05:00
|
|
|
}
|
2012-11-02 12:56:31 -05:00
|
|
|
|
2018-10-12 05:51:03 -05:00
|
|
|
DBG(DEV, ul_debugobj(dev, "%s: context successfully initialized", dev->name));
|
2010-11-24 10:06:39 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-16 06:39:25 -05:00
|
|
|
static struct lsblk_device *devtree_get_device_or_new(struct lsblk_devtree *tr,
|
2018-10-15 08:53:33 -05:00
|
|
|
struct lsblk_device *disk,
|
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
struct lsblk_device *dev = lsblk_devtree_get_device(tr, name);
|
|
|
|
|
|
|
|
if (!dev) {
|
2018-10-18 06:57:46 -05:00
|
|
|
dev = lsblk_new_device();
|
2018-10-15 08:53:33 -05:00
|
|
|
if (!dev)
|
|
|
|
err(EXIT_FAILURE, _("failed to allocate device"));
|
|
|
|
|
2018-10-17 04:14:40 -05:00
|
|
|
if (initialize_device(dev, disk, name) != 0) {
|
2018-10-15 08:53:33 -05:00
|
|
|
lsblk_unref_device(dev);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
lsblk_devtree_add_device(tr, dev);
|
|
|
|
lsblk_unref_device(dev); /* keep it referenced by devtree only */
|
|
|
|
} else
|
|
|
|
DBG(DEV, ul_debugobj(dev, "%s: already processed", name));
|
|
|
|
|
|
|
|
return dev;
|
|
|
|
}
|
2018-10-12 05:48:42 -05:00
|
|
|
|
2020-11-23 08:16:39 -06:00
|
|
|
static struct lsblk_device *devtree_pktcdvd_get_dep(
|
|
|
|
struct lsblk_devtree *tr,
|
|
|
|
struct lsblk_device *dev,
|
|
|
|
int want_slave)
|
|
|
|
{
|
|
|
|
char buf[PATH_MAX], *name;
|
|
|
|
dev_t devno;
|
|
|
|
|
|
|
|
devno = lsblk_devtree_pktcdvd_get_mate(tr,
|
|
|
|
makedev(dev->maj, dev->min), !want_slave);
|
|
|
|
if (!devno)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
name = sysfs_devno_to_devname(devno, buf, sizeof(buf));
|
|
|
|
if (!name)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return devtree_get_device_or_new(tr, NULL, name);
|
|
|
|
}
|
|
|
|
|
2018-10-16 06:39:25 -05:00
|
|
|
static int process_dependencies(
|
|
|
|
struct lsblk_devtree *tr,
|
|
|
|
struct lsblk_device *dev,
|
|
|
|
int do_partitions);
|
2012-02-14 02:53:16 -06:00
|
|
|
|
2010-11-24 10:06:39 -06:00
|
|
|
/*
|
2018-10-16 06:39:25 -05:00
|
|
|
* Read devices from whole-disk device into tree
|
2010-11-24 10:06:39 -06:00
|
|
|
*/
|
2018-10-16 07:35:36 -05:00
|
|
|
static int process_partitions(struct lsblk_devtree *tr, struct lsblk_device *disk)
|
2010-11-24 10:06:39 -06:00
|
|
|
{
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *d;
|
|
|
|
|
2018-10-16 07:35:36 -05:00
|
|
|
assert(disk);
|
2010-12-16 05:47:30 -06:00
|
|
|
|
2012-02-14 02:53:16 -06:00
|
|
|
/*
|
|
|
|
* Do not process further if there are no partitions for
|
|
|
|
* this device or the device itself is a partition.
|
|
|
|
*/
|
2018-10-18 06:57:46 -05:00
|
|
|
if (!disk->npartitions || device_is_partition(disk))
|
2018-10-16 07:35:36 -05:00
|
|
|
return -EINVAL;
|
2010-11-24 10:06:39 -06:00
|
|
|
|
2018-10-16 07:35:36 -05:00
|
|
|
DBG(DEV, ul_debugobj(disk, "%s: probe whole-disk for partitions", disk->name));
|
2015-04-01 05:06:15 -05:00
|
|
|
|
2018-10-16 07:35:36 -05:00
|
|
|
dir = ul_path_opendir(disk->sysfs, NULL);
|
2010-11-24 10:06:39 -06:00
|
|
|
if (!dir)
|
|
|
|
err(EXIT_FAILURE, _("failed to open device directory in sysfs"));
|
|
|
|
|
|
|
|
while ((d = xreaddir(dir))) {
|
2018-10-16 06:39:25 -05:00
|
|
|
struct lsblk_device *part;
|
2010-11-24 10:06:39 -06:00
|
|
|
|
2018-10-16 07:35:36 -05:00
|
|
|
if (!(sysfs_blkdev_is_partition_dirent(dir, d, disk->name)))
|
2011-07-21 15:56:20 -05:00
|
|
|
continue;
|
2012-02-14 02:53:16 -06:00
|
|
|
|
2018-10-16 07:35:36 -05:00
|
|
|
DBG(DEV, ul_debugobj(disk, " checking %s", d->d_name));
|
2015-04-01 05:06:15 -05:00
|
|
|
|
2018-10-16 07:35:36 -05:00
|
|
|
part = devtree_get_device_or_new(tr, disk, d->d_name);
|
2018-10-16 06:39:25 -05:00
|
|
|
if (!part)
|
|
|
|
continue;
|
|
|
|
|
2018-10-16 07:35:36 -05:00
|
|
|
if (lsblk_device_new_dependence(disk, part) == 0)
|
2018-10-16 06:39:25 -05:00
|
|
|
process_dependencies(tr, part, 0);
|
2018-10-17 06:19:50 -05:00
|
|
|
|
|
|
|
ul_path_close_dirfd(part->sysfs);
|
2010-11-24 10:06:39 -06:00
|
|
|
}
|
2012-02-14 02:53:16 -06:00
|
|
|
|
2018-10-17 06:19:50 -05:00
|
|
|
/* 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);
|
|
|
|
|
2018-10-16 07:35:36 -05:00
|
|
|
DBG(DEV, ul_debugobj(disk, "probe whole-disk for partitions -- done"));
|
2010-11-24 10:06:39 -06:00
|
|
|
closedir(dir);
|
2018-10-16 06:39:25 -05:00
|
|
|
return 0;
|
2012-02-14 02:53:16 -06:00
|
|
|
}
|
|
|
|
|
2018-10-16 06:39:25 -05:00
|
|
|
static char *get_wholedisk_from_partition_dirent(DIR *dir, struct dirent *d, char *buf, size_t bufsz)
|
2012-02-14 02:53:16 -06:00
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
int len;
|
|
|
|
|
2018-10-16 06:39:25 -05:00
|
|
|
if ((len = readlinkat(dirfd(dir), d->d_name, buf, bufsz - 1)) < 0)
|
2012-02-14 02:53:16 -06:00
|
|
|
return 0;
|
2012-03-07 07:28:52 -06:00
|
|
|
|
2018-10-16 06:39:25 -05:00
|
|
|
buf[len] = '\0';
|
2012-02-14 02:53:16 -06:00
|
|
|
|
|
|
|
/* The path ends with ".../<device>/<partition>" */
|
2018-10-16 06:39:25 -05:00
|
|
|
p = strrchr(buf, '/');
|
2012-03-07 07:28:52 -06:00
|
|
|
if (!p)
|
2018-10-16 06:39:25 -05:00
|
|
|
return NULL;
|
2012-03-07 07:28:52 -06:00
|
|
|
*p = '\0';
|
|
|
|
|
2018-10-16 06:39:25 -05:00
|
|
|
p = strrchr(buf, '/');
|
2012-03-07 07:28:52 -06:00
|
|
|
if (!p)
|
2018-10-16 06:39:25 -05:00
|
|
|
return NULL;
|
2012-03-07 07:28:52 -06:00
|
|
|
p++;
|
2010-11-24 10:06:39 -06:00
|
|
|
|
2018-10-16 06:39:25 -05:00
|
|
|
return p;
|
2012-02-14 02:53:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2018-10-17 03:54:59 -05:00
|
|
|
* Reads slaves/holders and partitions for specified device into device tree
|
2012-02-14 02:53:16 -06:00
|
|
|
*/
|
2018-10-16 06:39:25 -05:00
|
|
|
static int process_dependencies(
|
|
|
|
struct lsblk_devtree *tr,
|
|
|
|
struct lsblk_device *dev,
|
|
|
|
int do_partitions)
|
2012-02-14 02:53:16 -06:00
|
|
|
{
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *d;
|
2012-03-07 07:28:52 -06:00
|
|
|
const char *depname;
|
2020-11-23 08:16:39 -06:00
|
|
|
struct lsblk_device *dep = NULL;
|
2012-02-14 02:53:16 -06:00
|
|
|
|
2018-10-09 05:46:28 -05:00
|
|
|
assert(dev);
|
2012-02-14 02:53:16 -06:00
|
|
|
|
2018-10-17 07:13:31 -05:00
|
|
|
if (lsblk->nodeps)
|
|
|
|
return 0;
|
|
|
|
|
2018-10-16 06:39:25 -05:00
|
|
|
/* read all or specified partition */
|
|
|
|
if (do_partitions && dev->npartitions)
|
|
|
|
process_partitions(tr, dev);
|
|
|
|
|
|
|
|
DBG(DEV, ul_debugobj(dev, "%s: reading dependencies", dev->name));
|
2015-04-01 05:06:15 -05:00
|
|
|
|
2018-10-16 06:39:25 -05:00
|
|
|
if (!(lsblk->inverse ? dev->nslaves : dev->nholders)) {
|
|
|
|
DBG(DEV, ul_debugobj(dev, " ignore (no slaves/holders)"));
|
2020-11-23 08:16:39 -06:00
|
|
|
goto done;
|
2018-10-16 06:39:25 -05:00
|
|
|
}
|
2012-02-14 02:53:16 -06:00
|
|
|
|
2012-03-07 07:28:52 -06:00
|
|
|
depname = lsblk->inverse ? "slaves" : "holders";
|
2018-10-09 05:46:28 -05:00
|
|
|
dir = ul_path_opendir(dev->sysfs, depname);
|
2018-10-16 06:39:25 -05:00
|
|
|
if (!dir) {
|
|
|
|
DBG(DEV, ul_debugobj(dev, " ignore (no slaves/holders directory)"));
|
2020-11-23 08:16:39 -06:00
|
|
|
goto done;
|
2018-10-16 06:39:25 -05:00
|
|
|
}
|
2018-10-17 06:19:50 -05:00
|
|
|
ul_path_close_dirfd(dev->sysfs);
|
2010-11-24 10:06:39 -06:00
|
|
|
|
2018-10-16 06:39:25 -05:00
|
|
|
DBG(DEV, ul_debugobj(dev, " %s: checking for '%s' dependence", dev->name, depname));
|
2015-04-01 05:06:15 -05:00
|
|
|
|
2010-11-24 10:06:39 -06:00
|
|
|
while ((d = xreaddir(dir))) {
|
2018-10-17 06:19:50 -05:00
|
|
|
struct lsblk_device *disk = NULL;
|
2018-10-16 06:39:25 -05:00
|
|
|
|
2012-02-14 02:53:16 -06:00
|
|
|
/* Is the dependency a partition? */
|
2018-06-15 04:59:29 -05:00
|
|
|
if (sysfs_blkdev_is_partition_dirent(dir, d, NULL)) {
|
2018-10-16 06:39:25 -05:00
|
|
|
|
|
|
|
char buf[PATH_MAX];
|
|
|
|
char *diskname;
|
|
|
|
|
|
|
|
DBG(DEV, ul_debugobj(dev, " %s: dependence is partition", d->d_name));
|
|
|
|
|
|
|
|
diskname = get_wholedisk_from_partition_dirent(dir, d, buf, sizeof(buf));
|
|
|
|
if (diskname)
|
2018-10-16 07:29:00 -05:00
|
|
|
disk = devtree_get_device_or_new(tr, NULL, diskname);
|
2018-10-16 06:39:25 -05:00
|
|
|
if (!disk) {
|
|
|
|
DBG(DEV, ul_debugobj(dev, " ignore no wholedisk ???"));
|
2018-10-17 06:19:50 -05:00
|
|
|
goto next;
|
2015-04-01 05:06:15 -05:00
|
|
|
}
|
2018-10-16 06:39:25 -05:00
|
|
|
|
2018-10-16 07:29:00 -05:00
|
|
|
dep = devtree_get_device_or_new(tr, disk, d->d_name);
|
2018-10-16 06:39:25 -05:00
|
|
|
if (!dep)
|
2018-10-17 06:19:50 -05:00
|
|
|
goto next;
|
2018-10-16 06:39:25 -05:00
|
|
|
|
|
|
|
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);
|
2011-07-21 15:56:20 -05:00
|
|
|
}
|
2012-02-14 02:53:16 -06:00
|
|
|
/* The dependency is a whole device. */
|
2018-10-16 06:39:25 -05:00
|
|
|
else {
|
|
|
|
DBG(DEV, ul_debugobj(dev, " %s: %s: dependence is whole-disk",
|
2018-10-09 05:46:28 -05:00
|
|
|
dev->name, d->d_name));
|
2018-10-16 06:39:25 -05:00
|
|
|
|
2018-10-16 07:29:00 -05:00
|
|
|
dep = devtree_get_device_or_new(tr, NULL, d->d_name);
|
2018-10-16 06:39:25 -05:00
|
|
|
if (!dep)
|
2018-10-17 06:19:50 -05:00
|
|
|
goto next;
|
2018-10-16 06:39:25 -05:00
|
|
|
|
|
|
|
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);
|
2015-04-01 05:06:15 -05:00
|
|
|
}
|
2018-10-17 06:19:50 -05:00
|
|
|
next:
|
|
|
|
if (dep && dep->sysfs)
|
|
|
|
ul_path_close_dirfd(dep->sysfs);
|
|
|
|
if (disk && disk->sysfs)
|
|
|
|
ul_path_close_dirfd(disk->sysfs);
|
2010-11-24 10:06:39 -06:00
|
|
|
}
|
|
|
|
closedir(dir);
|
2020-11-23 08:16:39 -06:00
|
|
|
done:
|
|
|
|
dep = devtree_pktcdvd_get_dep(tr, dev, lsblk->inverse);
|
|
|
|
|
|
|
|
if (dep && lsblk_device_new_dependence(dev, dep) == 0) {
|
|
|
|
lsblk_devtree_remove_root(tr, dep);
|
|
|
|
process_dependencies(tr, dep, lsblk->inverse ? 0 : 1);
|
|
|
|
}
|
2010-11-24 10:06:39 -06:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-17 03:54:59 -05:00
|
|
|
/*
|
|
|
|
* Defines the device as root node in the device tree and walks on all dependencies of the device.
|
|
|
|
*/
|
2018-10-16 08:53:22 -05:00
|
|
|
static int __process_one_device(struct lsblk_devtree *tr, char *devname, dev_t devno)
|
2010-11-24 10:06:39 -06:00
|
|
|
{
|
2018-10-15 09:12:33 -05:00
|
|
|
struct lsblk_device *dev = NULL;
|
2018-10-17 06:19:50 -05:00
|
|
|
struct lsblk_device *disk = NULL;
|
2015-01-29 05:35:26 -06:00
|
|
|
char buf[PATH_MAX + 1], *name = NULL, *diskname = NULL;
|
|
|
|
int real_part = 0, rc = -EINVAL;
|
2010-11-24 10:06:39 -06:00
|
|
|
|
2019-05-23 05:36:53 -05:00
|
|
|
if (devno == 0 && devname) {
|
2018-10-16 08:53:22 -05:00
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
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;
|
2019-05-23 05:36:53 -05:00
|
|
|
} else if (devno) {
|
2018-10-16 08:53:22 -05:00
|
|
|
DBG(DEV, ul_debug("%d:%d: reading alone device", major(devno), minor(devno)));
|
2019-05-23 05:36:53 -05:00
|
|
|
} else {
|
|
|
|
assert(devno || devname);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2012-09-20 06:17:13 -05:00
|
|
|
|
2018-06-15 04:59:29 -05:00
|
|
|
/* TODO: sysfs_devno_to_devname() internally initializes path_cxt, it
|
|
|
|
* would be better to use ul_new_sysfs_path() + sysfs_blkdev_get_name()
|
2018-10-17 04:14:40 -05:00
|
|
|
* and reuse path_cxt for initialize_device()
|
2018-06-15 04:59:29 -05:00
|
|
|
*/
|
2018-10-16 08:53:22 -05:00
|
|
|
name = sysfs_devno_to_devname(devno, buf, sizeof(buf));
|
2018-06-15 04:59:29 -05:00
|
|
|
if (!name) {
|
2018-10-16 08:53:22 -05:00
|
|
|
if (devname)
|
|
|
|
warn(_("%s: failed to get sysfs name"), devname);
|
2015-01-29 05:35:26 -06:00
|
|
|
goto leave;
|
2010-11-24 10:06:39 -06:00
|
|
|
}
|
2018-06-19 09:10:17 -05:00
|
|
|
name = xstrdup(name);
|
2012-09-20 06:17:13 -05:00
|
|
|
|
|
|
|
if (!strncmp(name, "dm-", 3)) {
|
|
|
|
/* dm mapping is never a real partition! */
|
|
|
|
real_part = 0;
|
|
|
|
} else {
|
2018-10-17 06:19:50 -05:00
|
|
|
dev_t diskno = 0;
|
|
|
|
|
|
|
|
if (blkid_devno_to_wholedisk(devno, buf, sizeof(buf), &diskno)) {
|
2018-10-16 08:53:22 -05:00
|
|
|
warn(_("%s: failed to get whole-disk device number"), name);
|
2015-01-29 05:35:26 -06:00
|
|
|
goto leave;
|
2012-09-20 06:17:13 -05:00
|
|
|
}
|
|
|
|
diskname = buf;
|
2018-10-17 06:19:50 -05:00
|
|
|
real_part = devno != diskno;
|
2012-09-20 06:17:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!real_part) {
|
2010-11-24 10:06:39 -06:00
|
|
|
/*
|
2012-02-14 02:53:16 -06:00
|
|
|
* Device is not a partition.
|
2010-11-24 10:06:39 -06:00
|
|
|
*/
|
2018-10-16 06:39:25 -05:00
|
|
|
DBG(DEV, ul_debug(" non-partition"));
|
|
|
|
|
2018-10-16 07:29:00 -05:00
|
|
|
dev = devtree_get_device_or_new(tr, NULL, name);
|
2018-10-15 09:12:33 -05:00
|
|
|
if (!dev)
|
2011-07-21 15:56:20 -05:00
|
|
|
goto leave;
|
2018-10-15 09:12:33 -05:00
|
|
|
|
2018-10-16 06:39:25 -05:00
|
|
|
lsblk_devtree_add_root(tr, dev);
|
|
|
|
process_dependencies(tr, dev, !lsblk->inverse);
|
2011-07-21 15:56:20 -05:00
|
|
|
} else {
|
2010-11-24 10:06:39 -06:00
|
|
|
/*
|
2018-10-15 09:12:33 -05:00
|
|
|
* Partition, read sysfs name of the disk device
|
2010-11-24 10:06:39 -06:00
|
|
|
*/
|
2018-10-16 06:39:25 -05:00
|
|
|
DBG(DEV, ul_debug(" partition"));
|
|
|
|
|
2018-10-16 07:29:00 -05:00
|
|
|
disk = devtree_get_device_or_new(tr, NULL, diskname);
|
|
|
|
if (!disk)
|
2011-07-21 15:56:20 -05:00
|
|
|
goto leave;
|
2018-10-15 09:12:33 -05:00
|
|
|
|
2018-10-16 07:29:00 -05:00
|
|
|
dev = devtree_get_device_or_new(tr, disk, name);
|
2018-10-15 09:12:33 -05:00
|
|
|
if (!dev)
|
2011-07-21 15:56:20 -05:00
|
|
|
goto leave;
|
2012-02-14 02:53:16 -06:00
|
|
|
|
2018-10-16 06:39:25 -05:00
|
|
|
lsblk_devtree_add_root(tr, dev);
|
|
|
|
process_dependencies(tr, dev, 1);
|
2018-10-15 09:12:33 -05:00
|
|
|
|
2018-10-16 06:39:25 -05:00
|
|
|
if (lsblk->inverse
|
2018-10-16 07:29:00 -05:00
|
|
|
&& lsblk_device_new_dependence(dev, disk) == 0)
|
|
|
|
process_dependencies(tr, disk, 0);
|
2018-10-17 06:19:50 -05:00
|
|
|
else
|
|
|
|
ul_path_close_dirfd(disk->sysfs);
|
2010-11-24 10:06:39 -06:00
|
|
|
}
|
|
|
|
|
2015-01-29 05:35:26 -06:00
|
|
|
rc = 0;
|
2011-07-21 15:56:20 -05:00
|
|
|
leave:
|
2018-10-17 06:19:50 -05:00
|
|
|
if (dev && dev->sysfs)
|
|
|
|
ul_path_close_dirfd(dev->sysfs);
|
|
|
|
if (disk && disk->sysfs)
|
|
|
|
ul_path_close_dirfd(disk->sysfs);
|
2012-09-20 06:17:13 -05:00
|
|
|
free(name);
|
2015-01-29 05:35:26 -06:00
|
|
|
return rc;
|
2010-11-24 10:06:39 -06:00
|
|
|
}
|
|
|
|
|
2018-10-16 08:53:22 -05:00
|
|
|
static int process_one_device(struct lsblk_devtree *tr, char *devname)
|
|
|
|
{
|
2019-05-23 05:36:53 -05:00
|
|
|
assert(devname);
|
2018-10-16 08:53:22 -05:00
|
|
|
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)
|
2018-10-16 07:37:28 -05:00
|
|
|
{
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *d;
|
2018-10-16 08:53:22 -05:00
|
|
|
struct path_cxt *pc = ul_new_path(_PATH_SYS_DEVBLOCK);
|
|
|
|
|
|
|
|
assert(lsblk->inverse);
|
2018-10-16 07:37:28 -05:00
|
|
|
|
|
|
|
if (!pc)
|
|
|
|
err(EXIT_FAILURE, _("failed to allocate /sys handler"));
|
|
|
|
|
|
|
|
ul_path_set_prefix(pc, lsblk->sysroot);
|
2018-10-16 08:53:22 -05:00
|
|
|
dir = ul_path_opendir(pc, NULL);
|
|
|
|
if (!dir)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
DBG(DEV, ul_debug("iterate on " _PATH_SYS_DEVBLOCK));
|
|
|
|
|
|
|
|
while ((d = xreaddir(dir))) {
|
|
|
|
dev_t devno;
|
|
|
|
int maj, min;
|
2018-10-16 07:37:28 -05:00
|
|
|
|
2018-10-16 08:53:22 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-10-17 03:54:59 -05:00
|
|
|
/*
|
|
|
|
* Reads root nodes (devices) from /sys/block into devices tree
|
|
|
|
*/
|
2018-10-16 08:53:22 -05:00
|
|
|
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);
|
2018-10-16 07:37:28 -05:00
|
|
|
dir = ul_path_opendir(pc, NULL);
|
|
|
|
if (!dir)
|
|
|
|
goto done;
|
|
|
|
|
2018-10-16 08:53:22 -05:00
|
|
|
DBG(DEV, ul_debug("iterate on " _PATH_SYS_BLOCK));
|
2018-10-16 07:37:28 -05:00
|
|
|
|
|
|
|
while ((d = xreaddir(dir))) {
|
2018-10-17 06:19:50 -05:00
|
|
|
struct lsblk_device *dev = NULL;
|
2018-10-16 07:37:28 -05:00
|
|
|
|
|
|
|
DBG(DEV, ul_debug(" %s dentry", d->d_name));
|
|
|
|
dev = devtree_get_device_or_new(tr, NULL, d->d_name);
|
|
|
|
if (!dev)
|
2018-10-17 06:19:50 -05:00
|
|
|
goto next;
|
2018-10-16 07:37:28 -05:00
|
|
|
|
|
|
|
/* 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);
|
2019-05-06 05:39:07 -05:00
|
|
|
dev = NULL;
|
2018-10-17 06:19:50 -05:00
|
|
|
goto next;
|
2018-10-16 07:37:28 -05:00
|
|
|
}
|
|
|
|
|
2018-10-16 08:53:22 -05:00
|
|
|
if (dev->nslaves) {
|
|
|
|
DBG(DEV, ul_debug(" %s: ignore (in-middle)", d->d_name));
|
2018-10-17 06:19:50 -05:00
|
|
|
goto next;
|
2018-10-16 07:37:28 -05:00
|
|
|
}
|
2018-12-07 05:29:50 -06:00
|
|
|
|
2018-10-16 08:53:22 -05:00
|
|
|
lsblk_devtree_add_root(tr, dev);
|
|
|
|
process_dependencies(tr, dev, 1);
|
2018-10-17 06:19:50 -05:00
|
|
|
next:
|
|
|
|
/* Let's be careful with number of open files */
|
|
|
|
if (dev && dev->sysfs)
|
|
|
|
ul_path_close_dirfd(dev->sysfs);
|
2018-10-16 07:37:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dir);
|
|
|
|
done:
|
|
|
|
ul_unref_path(pc);
|
|
|
|
DBG(DEV, ul_debug("iterate on " _PATH_SYS_BLOCK " -- done"));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-17 03:54:59 -05:00
|
|
|
/*
|
|
|
|
* Parses major numbers as specified on lsblk command line
|
|
|
|
*/
|
2012-06-19 10:41:35 -05:00
|
|
|
static void parse_excludes(const char *str0)
|
2010-11-24 10:06:39 -06:00
|
|
|
{
|
2012-06-19 10:41:35 -05:00
|
|
|
const char *str = str0;
|
|
|
|
|
2010-11-24 10:06:39 -06:00
|
|
|
while (str && *str) {
|
|
|
|
char *end = NULL;
|
2011-03-11 06:36:00 -06:00
|
|
|
unsigned long n;
|
2010-11-24 10:06:39 -06:00
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
n = strtoul(str, &end, 10);
|
|
|
|
|
2012-06-19 10:41:35 -05:00
|
|
|
if (end == str || (end && *end && *end != ','))
|
|
|
|
errx(EXIT_FAILURE, _("failed to parse list '%s'"), str0);
|
|
|
|
if (errno != 0 && (n == ULONG_MAX || n == 0))
|
|
|
|
err(EXIT_FAILURE, _("failed to parse list '%s'"), str0);
|
2010-11-24 10:06:39 -06:00
|
|
|
excludes[nexcludes++] = n;
|
|
|
|
|
|
|
|
if (nexcludes == ARRAY_SIZE(excludes))
|
2011-07-30 14:28:07 -05:00
|
|
|
/* TRANSLATORS: The standard value for %d is 256. */
|
2010-11-24 10:06:39 -06:00
|
|
|
errx(EXIT_FAILURE, _("the list of excluded devices is "
|
|
|
|
"too large (limit is %d devices)"),
|
|
|
|
(int)ARRAY_SIZE(excludes));
|
2012-06-19 10:41:35 -05:00
|
|
|
|
2010-11-24 10:06:39 -06:00
|
|
|
str = end && *end ? end + 1 : NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-17 03:54:59 -05:00
|
|
|
/*
|
|
|
|
* Parses major numbers as specified on lsblk command line
|
|
|
|
* (TODO: what about refactor and merge parse_excludes() and parse_includes().)
|
|
|
|
*/
|
2012-06-19 10:41:35 -05:00
|
|
|
static void parse_includes(const char *str0)
|
2012-06-19 09:06:22 -05:00
|
|
|
{
|
2012-06-19 10:41:35 -05:00
|
|
|
const char *str = str0;
|
|
|
|
|
2012-06-19 09:06:22 -05:00
|
|
|
while (str && *str) {
|
|
|
|
char *end = NULL;
|
|
|
|
unsigned long n;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
n = strtoul(str, &end, 10);
|
|
|
|
|
2012-06-19 10:41:35 -05:00
|
|
|
if (end == str || (end && *end && *end != ','))
|
|
|
|
errx(EXIT_FAILURE, _("failed to parse list '%s'"), str0);
|
|
|
|
if (errno != 0 && (n == ULONG_MAX || n == 0))
|
|
|
|
err(EXIT_FAILURE, _("failed to parse list '%s'"), str0);
|
2012-06-19 09:06:22 -05:00
|
|
|
includes[nincludes++] = n;
|
|
|
|
|
|
|
|
if (nincludes == ARRAY_SIZE(includes))
|
|
|
|
/* TRANSLATORS: The standard value for %d is 256. */
|
|
|
|
errx(EXIT_FAILURE, _("the list of included devices is "
|
|
|
|
"too large (limit is %d devices)"),
|
|
|
|
(int)ARRAY_SIZE(includes));
|
|
|
|
str = end && *end ? end + 1 : NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-20 08:08:08 -05:00
|
|
|
/*
|
|
|
|
* see set_sortdata_u64() and columns initialization in main()
|
|
|
|
*/
|
|
|
|
static int cmp_u64_cells(struct libscols_cell *a,
|
|
|
|
struct libscols_cell *b,
|
|
|
|
__attribute__((__unused__)) void *data)
|
|
|
|
{
|
|
|
|
uint64_t *adata = (uint64_t *) scols_cell_get_userdata(a),
|
|
|
|
*bdata = (uint64_t *) scols_cell_get_userdata(b);
|
|
|
|
|
|
|
|
if (adata == NULL && bdata == NULL)
|
|
|
|
return 0;
|
|
|
|
if (adata == NULL)
|
|
|
|
return -1;
|
|
|
|
if (bdata == NULL)
|
|
|
|
return 1;
|
|
|
|
return *adata == *bdata ? 0 : *adata >= *bdata ? 1 : -1;
|
|
|
|
}
|
|
|
|
|
2018-10-18 08:46:07 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-06-20 13:20:29 -05:00
|
|
|
static void __attribute__((__noreturn__)) usage(void)
|
2010-11-24 10:06:39 -06:00
|
|
|
{
|
2017-06-20 13:20:29 -05:00
|
|
|
FILE *out = stdout;
|
2011-10-26 08:58:18 -05:00
|
|
|
size_t i;
|
2010-11-24 10:06:39 -06:00
|
|
|
|
2013-01-22 17:27:05 -06:00
|
|
|
fputs(USAGE_HEADER, out);
|
|
|
|
fprintf(out, _(" %s [options] [<device> ...]\n"), program_invocation_short_name);
|
2014-12-22 15:57:17 -06:00
|
|
|
|
|
|
|
fputs(USAGE_SEPARATOR, out);
|
|
|
|
fputs(_("List information about block devices.\n"), out);
|
|
|
|
|
2013-01-22 17:27:05 -06:00
|
|
|
fputs(USAGE_OPTIONS, out);
|
2018-12-07 05:29:50 -06:00
|
|
|
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);
|
2019-02-20 05:49:24 -06:00
|
|
|
fputs(_(" -T, --tree[=<column>] use tree format output\n"), out);
|
2013-01-22 17:27:05 -06:00
|
|
|
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(_(" -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(_(" -l, --list use list format output\n"), out);
|
2018-12-07 05:29:50 -06:00
|
|
|
fputs(_(" -M, --merge group parents of sub-trees (usable for RAIDs, Multi-path)\n"), out);
|
2013-10-12 04:17:19 -05:00
|
|
|
fputs(_(" -m, --perms output info about permissions\n"), out);
|
2013-01-22 17:27:05 -06:00
|
|
|
fputs(_(" -n, --noheadings don't print headings\n"), out);
|
|
|
|
fputs(_(" -o, --output <list> output columns\n"), out);
|
2013-04-11 12:51:39 -05:00
|
|
|
fputs(_(" -p, --paths print complete device path\n"), out);
|
2013-01-22 17:27:05 -06:00
|
|
|
fputs(_(" -r, --raw use raw output format\n"), out);
|
|
|
|
fputs(_(" -s, --inverse inverse dependencies\n"), out);
|
2013-10-12 04:17:19 -05:00
|
|
|
fputs(_(" -t, --topology output info about topology\n"), out);
|
2020-10-12 05:10:33 -05:00
|
|
|
fputs(_(" -w, --width <num> specifies output width as number of characters\n"), out);
|
2014-07-03 01:33:15 -05:00
|
|
|
fputs(_(" -x, --sort <column> sort output by <column>\n"), out);
|
2020-10-12 05:10:33 -05:00
|
|
|
fputs(_(" -z, --zoned print zone model\n"), out);
|
2018-06-19 09:30:29 -05:00
|
|
|
fputs(_(" --sysroot <dir> use specified directory as system root\n"), out);
|
2013-01-22 17:27:05 -06:00
|
|
|
fputs(USAGE_SEPARATOR, out);
|
2017-06-29 08:52:16 -05:00
|
|
|
printf(USAGE_HELP_OPTIONS(22));
|
2010-11-24 10:06:39 -06:00
|
|
|
|
2017-06-26 07:09:53 -05:00
|
|
|
fprintf(out, USAGE_COLUMNS);
|
2010-11-24 10:06:39 -06:00
|
|
|
|
2014-07-17 08:35:52 -05:00
|
|
|
for (i = 0; i < ARRAY_SIZE(infos); i++)
|
2020-10-12 06:19:35 -05:00
|
|
|
fprintf(out, " %12s %s\n", infos[i].name, _(infos[i].help));
|
2010-11-24 10:06:39 -06:00
|
|
|
|
2017-06-29 08:52:16 -05:00
|
|
|
printf(USAGE_MAN_TAIL("lsblk(8)"));
|
2010-11-24 10:06:39 -06:00
|
|
|
|
2017-06-20 13:20:29 -05:00
|
|
|
exit(EXIT_SUCCESS);
|
2010-11-24 10:06:39 -06:00
|
|
|
}
|
|
|
|
|
2011-10-05 08:37:48 -05:00
|
|
|
static void check_sysdevblock(void)
|
|
|
|
{
|
|
|
|
if (access(_PATH_SYS_DEVBLOCK, R_OK) != 0)
|
|
|
|
err(EXIT_FAILURE, _("failed to access sysfs directory: %s"),
|
|
|
|
_PATH_SYS_DEVBLOCK);
|
|
|
|
}
|
|
|
|
|
2010-11-24 10:06:39 -06:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2018-10-18 08:46:07 -05:00
|
|
|
struct lsblk _ls = {
|
|
|
|
.sort_id = -1,
|
|
|
|
.dedup_id = -1,
|
2019-02-20 05:49:24 -06:00
|
|
|
.flags = LSBLK_TREE,
|
|
|
|
.tree_id = COL_NAME
|
2018-10-18 08:46:07 -05:00
|
|
|
};
|
2018-10-15 07:14:33 -05:00
|
|
|
struct lsblk_devtree *tr = NULL;
|
2015-04-22 04:08:05 -05:00
|
|
|
int c, status = EXIT_FAILURE;
|
2012-07-26 08:52:05 -05:00
|
|
|
char *outarg = NULL;
|
2015-04-22 04:08:05 -05:00
|
|
|
size_t i;
|
2020-10-12 05:10:33 -05:00
|
|
|
unsigned int width = 0;
|
2019-02-20 05:49:24 -06:00
|
|
|
int force_tree = 0, has_tree_col = 0;
|
2010-11-24 10:06:39 -06:00
|
|
|
|
2018-06-18 06:01:27 -05:00
|
|
|
enum {
|
|
|
|
OPT_SYSROOT = CHAR_MAX + 1
|
|
|
|
};
|
|
|
|
|
2011-03-03 08:00:30 -06:00
|
|
|
static const struct option longopts[] = {
|
2017-02-11 14:23:26 -06:00
|
|
|
{ "all", no_argument, NULL, 'a' },
|
|
|
|
{ "bytes", no_argument, NULL, 'b' },
|
|
|
|
{ "nodeps", no_argument, NULL, 'd' },
|
|
|
|
{ "discard", no_argument, NULL, 'D' },
|
2018-12-07 05:29:50 -06:00
|
|
|
{ "dedup", required_argument, NULL, 'E' },
|
2017-04-18 19:25:58 -05:00
|
|
|
{ "zoned", no_argument, NULL, 'z' },
|
2017-02-11 14:23:26 -06:00
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "json", no_argument, NULL, 'J' },
|
|
|
|
{ "output", required_argument, NULL, 'o' },
|
|
|
|
{ "output-all", no_argument, NULL, 'O' },
|
2018-12-07 05:29:50 -06:00
|
|
|
{ "merge", no_argument, NULL, 'M' },
|
2017-02-11 14:23:26 -06:00
|
|
|
{ "perms", no_argument, NULL, 'm' },
|
|
|
|
{ "noheadings", no_argument, NULL, 'n' },
|
|
|
|
{ "list", no_argument, NULL, 'l' },
|
|
|
|
{ "ascii", no_argument, NULL, 'i' },
|
|
|
|
{ "raw", no_argument, NULL, 'r' },
|
|
|
|
{ "inverse", no_argument, NULL, 's' },
|
|
|
|
{ "fs", no_argument, NULL, 'f' },
|
|
|
|
{ "exclude", required_argument, NULL, 'e' },
|
|
|
|
{ "include", required_argument, NULL, 'I' },
|
|
|
|
{ "topology", no_argument, NULL, 't' },
|
|
|
|
{ "paths", no_argument, NULL, 'p' },
|
|
|
|
{ "pairs", no_argument, NULL, 'P' },
|
|
|
|
{ "scsi", no_argument, NULL, 'S' },
|
|
|
|
{ "sort", required_argument, NULL, 'x' },
|
2018-06-18 06:01:27 -05:00
|
|
|
{ "sysroot", required_argument, NULL, OPT_SYSROOT },
|
2019-02-20 05:49:24 -06:00
|
|
|
{ "tree", optional_argument, NULL, 'T' },
|
2017-02-11 14:23:26 -06:00
|
|
|
{ "version", no_argument, NULL, 'V' },
|
2020-10-12 05:10:33 -05:00
|
|
|
{ "width", required_argument, NULL, 'w' },
|
2017-02-11 14:23:26 -06:00
|
|
|
{ NULL, 0, NULL, 0 },
|
2010-11-24 10:06:39 -06:00
|
|
|
};
|
|
|
|
|
2017-02-10 05:05:04 -06:00
|
|
|
static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
|
2014-04-27 00:18:41 -05:00
|
|
|
{ 'D','O' },
|
2012-07-26 02:24:36 -05:00
|
|
|
{ 'I','e' },
|
2015-06-04 08:49:52 -05:00
|
|
|
{ 'J', 'P', 'r' },
|
2014-04-27 00:18:41 -05:00
|
|
|
{ 'O','S' },
|
|
|
|
{ 'O','f' },
|
|
|
|
{ 'O','m' },
|
2019-03-19 10:56:28 -05:00
|
|
|
{ 'O','o' },
|
2014-04-27 00:18:41 -05:00
|
|
|
{ 'O','t' },
|
2017-06-09 04:33:18 -05:00
|
|
|
{ 'P','T', 'l','r' },
|
2012-07-26 02:24:36 -05:00
|
|
|
{ 0 }
|
|
|
|
};
|
|
|
|
int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
|
|
|
|
|
2010-11-24 10:06:39 -06:00
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
bindtextdomain(PACKAGE, LOCALEDIR);
|
|
|
|
textdomain(PACKAGE);
|
2019-04-16 08:14:13 -05:00
|
|
|
close_stdout_atexit();
|
2010-11-24 10:06:39 -06:00
|
|
|
|
|
|
|
lsblk = &_ls;
|
|
|
|
|
2015-04-01 05:06:15 -05:00
|
|
|
lsblk_init_debug();
|
|
|
|
|
2012-07-26 02:24:36 -05:00
|
|
|
while((c = getopt_long(argc, argv,
|
2020-10-12 05:10:33 -05:00
|
|
|
"abdDzE:e:fhJlnMmo:OpPiI:rstVST::w:x:", longopts, NULL)) != -1) {
|
2012-07-26 02:24:36 -05:00
|
|
|
|
|
|
|
err_exclusive_options(c, longopts, excl, excl_st);
|
|
|
|
|
2010-11-24 10:06:39 -06:00
|
|
|
switch(c) {
|
|
|
|
case 'a':
|
|
|
|
lsblk->all_devices = 1;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
lsblk->bytes = 1;
|
|
|
|
break;
|
2010-12-16 05:47:30 -06:00
|
|
|
case 'd':
|
|
|
|
lsblk->nodeps = 1;
|
|
|
|
break;
|
2011-05-04 10:16:05 -05:00
|
|
|
case 'D':
|
2017-04-19 07:28:16 -05:00
|
|
|
add_uniq_column(COL_NAME);
|
|
|
|
add_uniq_column(COL_DALIGN);
|
|
|
|
add_uniq_column(COL_DGRAN);
|
|
|
|
add_uniq_column(COL_DMAX);
|
|
|
|
add_uniq_column(COL_DZERO);
|
2011-05-04 10:16:05 -05:00
|
|
|
break;
|
2017-04-18 19:25:58 -05:00
|
|
|
case 'z':
|
2017-04-19 07:28:16 -05:00
|
|
|
add_uniq_column(COL_NAME);
|
|
|
|
add_uniq_column(COL_ZONED);
|
2017-04-18 19:25:58 -05:00
|
|
|
break;
|
2010-11-24 10:06:39 -06:00
|
|
|
case 'e':
|
|
|
|
parse_excludes(optarg);
|
|
|
|
break;
|
2015-06-04 08:49:52 -05:00
|
|
|
case 'J':
|
2017-04-13 03:17:36 -05:00
|
|
|
lsblk->flags |= LSBLK_JSON;
|
2015-06-04 08:49:52 -05:00
|
|
|
break;
|
2010-11-24 10:06:39 -06:00
|
|
|
case 'l':
|
2017-04-13 03:17:36 -05:00
|
|
|
lsblk->flags &= ~LSBLK_TREE; /* disable the default */
|
2010-11-24 10:06:39 -06:00
|
|
|
break;
|
2018-12-07 05:29:50 -06:00
|
|
|
case 'M':
|
|
|
|
lsblk->merge = 1;
|
|
|
|
break;
|
2010-11-24 10:06:39 -06:00
|
|
|
case 'n':
|
2017-04-13 03:17:36 -05:00
|
|
|
lsblk->flags |= LSBLK_NOHEADINGS;
|
2010-11-24 10:06:39 -06:00
|
|
|
break;
|
|
|
|
case 'o':
|
2012-07-26 08:52:05 -05:00
|
|
|
outarg = optarg;
|
2010-11-24 10:06:39 -06:00
|
|
|
break;
|
2014-04-27 00:18:41 -05:00
|
|
|
case 'O':
|
2015-04-22 04:08:05 -05:00
|
|
|
for (ncolumns = 0 ; ncolumns < ARRAY_SIZE(infos); ncolumns++)
|
2014-04-27 00:18:41 -05:00
|
|
|
columns[ncolumns] = ncolumns;
|
|
|
|
break;
|
2013-03-29 11:16:02 -05:00
|
|
|
case 'p':
|
|
|
|
lsblk->paths = 1;
|
|
|
|
break;
|
2011-05-10 03:38:29 -05:00
|
|
|
case 'P':
|
2017-04-13 03:17:36 -05:00
|
|
|
lsblk->flags |= LSBLK_EXPORT;
|
|
|
|
lsblk->flags &= ~LSBLK_TREE; /* disable the default */
|
2011-05-10 03:38:29 -05:00
|
|
|
break;
|
2010-11-24 10:06:39 -06:00
|
|
|
case 'i':
|
2017-04-13 03:17:36 -05:00
|
|
|
lsblk->flags |= LSBLK_ASCII;
|
2010-11-24 10:06:39 -06:00
|
|
|
break;
|
2012-06-19 09:06:22 -05:00
|
|
|
case 'I':
|
|
|
|
parse_includes(optarg);
|
|
|
|
break;
|
2010-11-24 10:06:39 -06:00
|
|
|
case 'r':
|
2017-04-13 03:17:36 -05:00
|
|
|
lsblk->flags &= ~LSBLK_TREE; /* disable the default */
|
|
|
|
lsblk->flags |= LSBLK_RAW; /* enable raw */
|
2010-11-24 10:06:39 -06:00
|
|
|
break;
|
2012-02-14 02:53:16 -06:00
|
|
|
case 's':
|
|
|
|
lsblk->inverse = 1;
|
|
|
|
break;
|
2010-11-24 10:06:39 -06:00
|
|
|
case 'f':
|
2017-04-19 07:28:16 -05:00
|
|
|
add_uniq_column(COL_NAME);
|
|
|
|
add_uniq_column(COL_FSTYPE);
|
2019-11-15 04:23:44 -06:00
|
|
|
add_uniq_column(COL_FSVERSION);
|
2017-04-19 07:28:16 -05:00
|
|
|
add_uniq_column(COL_LABEL);
|
|
|
|
add_uniq_column(COL_UUID);
|
2018-09-13 05:11:06 -05:00
|
|
|
add_uniq_column(COL_FSAVAIL);
|
|
|
|
add_uniq_column(COL_FSUSEPERC);
|
2017-04-19 07:28:16 -05:00
|
|
|
add_uniq_column(COL_TARGET);
|
2010-11-24 10:06:39 -06:00
|
|
|
break;
|
|
|
|
case 'm':
|
2017-04-19 07:28:16 -05:00
|
|
|
add_uniq_column(COL_NAME);
|
|
|
|
add_uniq_column(COL_SIZE);
|
|
|
|
add_uniq_column(COL_OWNER);
|
|
|
|
add_uniq_column(COL_GROUP);
|
|
|
|
add_uniq_column(COL_MODE);
|
2010-11-24 10:06:39 -06:00
|
|
|
break;
|
|
|
|
case 't':
|
2017-04-19 07:28:16 -05:00
|
|
|
add_uniq_column(COL_NAME);
|
|
|
|
add_uniq_column(COL_ALIOFF);
|
|
|
|
add_uniq_column(COL_MINIO);
|
|
|
|
add_uniq_column(COL_OPTIO);
|
|
|
|
add_uniq_column(COL_PHYSEC);
|
|
|
|
add_uniq_column(COL_LOGSEC);
|
|
|
|
add_uniq_column(COL_ROTA);
|
|
|
|
add_uniq_column(COL_SCHED);
|
|
|
|
add_uniq_column(COL_RQ_SIZE);
|
|
|
|
add_uniq_column(COL_RA);
|
|
|
|
add_uniq_column(COL_WSAME);
|
2010-11-24 10:06:39 -06:00
|
|
|
break;
|
2012-11-02 12:56:31 -05:00
|
|
|
case 'S':
|
|
|
|
lsblk->nodeps = 1;
|
|
|
|
lsblk->scsi = 1;
|
2017-04-19 07:28:16 -05:00
|
|
|
add_uniq_column(COL_NAME);
|
|
|
|
add_uniq_column(COL_HCTL);
|
|
|
|
add_uniq_column(COL_TYPE);
|
|
|
|
add_uniq_column(COL_VENDOR);
|
|
|
|
add_uniq_column(COL_MODEL);
|
|
|
|
add_uniq_column(COL_REV);
|
2020-07-12 05:40:48 -05:00
|
|
|
add_uniq_column(COL_SERIAL);
|
2017-04-19 07:28:16 -05:00
|
|
|
add_uniq_column(COL_TRANSPORT);
|
2012-11-02 12:56:31 -05:00
|
|
|
break;
|
2017-06-09 04:33:18 -05:00
|
|
|
case 'T':
|
|
|
|
force_tree = 1;
|
2020-09-02 04:10:18 -05:00
|
|
|
if (optarg) {
|
|
|
|
if (*optarg == '=')
|
|
|
|
optarg++;
|
2019-02-20 05:49:24 -06:00
|
|
|
lsblk->tree_id = column_name_to_id(optarg, strlen(optarg));
|
2020-09-02 04:10:18 -05:00
|
|
|
}
|
2017-06-09 04:33:18 -05:00
|
|
|
break;
|
2018-06-18 06:01:27 -05:00
|
|
|
case OPT_SYSROOT:
|
|
|
|
lsblk->sysroot = optarg;
|
|
|
|
break;
|
2018-12-07 05:29:50 -06:00
|
|
|
case 'E':
|
2018-10-18 08:46:07 -05:00
|
|
|
lsblk->dedup_id = column_name_to_id(optarg, strlen(optarg));
|
|
|
|
if (lsblk->dedup_id >= 0)
|
|
|
|
break;
|
|
|
|
errtryhelp(EXIT_FAILURE);
|
|
|
|
break;
|
2020-10-12 05:10:33 -05:00
|
|
|
case 'w':
|
|
|
|
width = strtou32_or_err(optarg, _("invalid output width number argument"));
|
|
|
|
break;
|
2014-05-20 08:08:08 -05:00
|
|
|
case 'x':
|
2017-04-13 03:17:36 -05:00
|
|
|
lsblk->flags &= ~LSBLK_TREE; /* disable the default */
|
2014-05-20 08:08:08 -05:00
|
|
|
lsblk->sort_id = column_name_to_id(optarg, strlen(optarg));
|
|
|
|
if (lsblk->sort_id >= 0)
|
|
|
|
break;
|
2019-04-16 08:14:13 -05:00
|
|
|
errtryhelp(EXIT_FAILURE);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'h':
|
|
|
|
usage();
|
|
|
|
case 'V':
|
|
|
|
print_version(EXIT_SUCCESS);
|
2010-11-24 10:06:39 -06:00
|
|
|
default:
|
2016-12-19 06:30:56 -06:00
|
|
|
errtryhelp(EXIT_FAILURE);
|
2010-11-24 10:06:39 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-09 04:33:18 -05:00
|
|
|
if (force_tree)
|
|
|
|
lsblk->flags |= LSBLK_TREE;
|
|
|
|
|
2011-10-05 08:37:48 -05:00
|
|
|
check_sysdevblock();
|
|
|
|
|
2010-11-24 10:06:39 -06:00
|
|
|
if (!ncolumns) {
|
2017-04-19 07:28:16 -05:00
|
|
|
add_column(COL_NAME);
|
|
|
|
add_column(COL_MAJMIN);
|
|
|
|
add_column(COL_RM);
|
|
|
|
add_column(COL_SIZE);
|
|
|
|
add_column(COL_RO);
|
|
|
|
add_column(COL_TYPE);
|
|
|
|
add_column(COL_TARGET);
|
2010-11-24 10:06:39 -06:00
|
|
|
}
|
|
|
|
|
2012-07-26 08:52:05 -05:00
|
|
|
if (outarg && string_add_to_idarray(outarg, columns, ARRAY_SIZE(columns),
|
|
|
|
&ncolumns, column_name_to_id) < 0)
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
2018-05-15 03:29:22 -05:00
|
|
|
if (lsblk->all_devices == 0 && nexcludes == 0 && nincludes == 0)
|
2010-11-24 10:06:39 -06:00
|
|
|
excludes[nexcludes++] = 1; /* default: ignore RAM disks */
|
2012-04-03 06:43:50 -05:00
|
|
|
|
2016-11-02 08:40:31 -05:00
|
|
|
if (lsblk->sort_id < 0)
|
|
|
|
/* Since Linux 4.8 we have sort devices by default, because
|
|
|
|
* /sys is no more sorted */
|
|
|
|
lsblk->sort_id = COL_MAJMIN;
|
|
|
|
|
2020-02-25 08:31:23 -06:00
|
|
|
/* For --{inverse,raw,pairs} --list we still follow parent->child relation */
|
|
|
|
if (!(lsblk->flags & LSBLK_TREE)
|
|
|
|
&& (lsblk->inverse || lsblk->flags & LSBLK_EXPORT || lsblk->flags & LSBLK_RAW))
|
2017-04-13 04:39:22 -05:00
|
|
|
lsblk->force_tree_order = 1;
|
|
|
|
|
2015-12-10 06:03:46 -06:00
|
|
|
if (lsblk->sort_id >= 0 && column_id_to_number(lsblk->sort_id) < 0) {
|
|
|
|
/* the sort column is not between output columns -- add as hidden */
|
2017-04-19 07:28:16 -05:00
|
|
|
add_column(lsblk->sort_id);
|
2015-12-10 06:03:46 -06:00
|
|
|
lsblk->sort_hidden = 1;
|
|
|
|
}
|
2014-05-20 08:08:08 -05:00
|
|
|
|
2018-10-18 08:46:07 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-09-14 08:45:53 -05:00
|
|
|
lsblk_mnt_init();
|
2014-06-06 06:47:06 -05:00
|
|
|
scols_init_debug(0);
|
2018-06-19 05:02:52 -05:00
|
|
|
ul_path_init_debug();
|
2012-04-03 06:43:50 -05:00
|
|
|
|
2010-11-24 10:06:39 -06:00
|
|
|
/*
|
|
|
|
* initialize output columns
|
|
|
|
*/
|
2014-04-03 09:09:57 -05:00
|
|
|
if (!(lsblk->table = scols_new_table()))
|
2017-05-18 04:39:34 -05:00
|
|
|
errx(EXIT_FAILURE, _("failed to allocate output table"));
|
2017-04-13 03:17:36 -05:00
|
|
|
scols_table_enable_raw(lsblk->table, !!(lsblk->flags & LSBLK_RAW));
|
|
|
|
scols_table_enable_export(lsblk->table, !!(lsblk->flags & LSBLK_EXPORT));
|
|
|
|
scols_table_enable_ascii(lsblk->table, !!(lsblk->flags & LSBLK_ASCII));
|
|
|
|
scols_table_enable_json(lsblk->table, !!(lsblk->flags & LSBLK_JSON));
|
|
|
|
scols_table_enable_noheadings(lsblk->table, !!(lsblk->flags & LSBLK_NOHEADINGS));
|
2010-11-24 10:06:39 -06:00
|
|
|
|
2017-04-13 03:17:36 -05:00
|
|
|
if (lsblk->flags & LSBLK_JSON)
|
2015-06-04 08:49:52 -05:00
|
|
|
scols_table_set_name(lsblk->table, "blockdevices");
|
2020-10-12 05:10:33 -05:00
|
|
|
if (width) {
|
|
|
|
scols_table_set_termwidth(lsblk->table, width);
|
|
|
|
scols_table_set_termforce(lsblk->table, SCOLS_TERMFORCE_ALWAYS);
|
|
|
|
}
|
2015-06-04 08:49:52 -05:00
|
|
|
|
2010-11-24 10:06:39 -06:00
|
|
|
for (i = 0; i < ncolumns; i++) {
|
|
|
|
struct colinfo *ci = get_column_info(i);
|
2014-05-20 08:08:08 -05:00
|
|
|
struct libscols_column *cl;
|
|
|
|
int id = get_column_id(i), fl = ci->flags;
|
2010-11-24 10:06:39 -06:00
|
|
|
|
2019-02-20 05:49:24 -06:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-12-10 06:03:46 -06:00
|
|
|
if (lsblk->sort_hidden && lsblk->sort_id == id)
|
|
|
|
fl |= SCOLS_FL_HIDDEN;
|
2018-10-18 08:46:07 -05:00
|
|
|
if (lsblk->dedup_hidden && lsblk->dedup_id == id)
|
|
|
|
fl |= SCOLS_FL_HIDDEN;
|
2010-11-24 10:06:39 -06:00
|
|
|
|
2019-03-18 10:58:41 -05:00
|
|
|
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;
|
|
|
|
|
2014-05-20 08:08:08 -05:00
|
|
|
cl = scols_table_new_column(lsblk->table, ci->name, ci->whint, fl);
|
|
|
|
if (!cl) {
|
2017-05-18 04:39:34 -05:00
|
|
|
warn(_("failed to allocate output column"));
|
2010-11-24 10:06:39 -06:00
|
|
|
goto leave;
|
|
|
|
}
|
2014-05-20 08:08:08 -05:00
|
|
|
if (!lsblk->sort_col && lsblk->sort_id == id) {
|
|
|
|
lsblk->sort_col = cl;
|
|
|
|
scols_column_set_cmpfunc(cl,
|
2018-04-03 08:10:21 -05:00
|
|
|
ci->type == COLTYPE_NUM ? cmp_u64_cells :
|
|
|
|
ci->type == COLTYPE_SIZE ? cmp_u64_cells :
|
|
|
|
ci->type == COLTYPE_SORTNUM ? cmp_u64_cells : scols_cmpstr_cells,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
if (lsblk->flags & LSBLK_JSON) {
|
|
|
|
switch (ci->type) {
|
|
|
|
case COLTYPE_SIZE:
|
|
|
|
if (!lsblk->bytes)
|
|
|
|
break;
|
|
|
|
/* fallthrough */
|
|
|
|
case COLTYPE_NUM:
|
|
|
|
scols_column_set_json_type(cl, SCOLS_JSON_NUMBER);
|
|
|
|
break;
|
|
|
|
case COLTYPE_BOOL:
|
|
|
|
scols_column_set_json_type(cl, SCOLS_JSON_BOOLEAN);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
scols_column_set_json_type(cl, SCOLS_JSON_STRING);
|
|
|
|
break;
|
|
|
|
}
|
2014-05-20 08:08:08 -05:00
|
|
|
}
|
2010-11-24 10:06:39 -06:00
|
|
|
}
|
|
|
|
|
2018-10-15 06:55:08 -05:00
|
|
|
tr = lsblk_new_devtree();
|
|
|
|
if (!tr)
|
|
|
|
err(EXIT_FAILURE, _("failed to allocate device tree"));
|
|
|
|
|
2018-10-16 08:53:22 -05:00
|
|
|
if (optind == argc) {
|
|
|
|
int rc = lsblk->inverse ?
|
|
|
|
process_all_devices_inverse(tr) :
|
|
|
|
process_all_devices(tr);
|
|
|
|
|
|
|
|
status = rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
|
|
|
|
} else {
|
2015-01-29 05:35:26 -06:00
|
|
|
int cnt = 0, cnt_err = 0;
|
|
|
|
|
|
|
|
while (optind < argc) {
|
2018-10-15 09:12:33 -05:00
|
|
|
if (process_one_device(tr, argv[optind++]) != 0)
|
2015-01-29 05:35:26 -06:00
|
|
|
cnt_err++;
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
status = cnt == 0 ? EXIT_FAILURE : /* nothing */
|
|
|
|
cnt == cnt_err ? LSBLK_EXIT_ALLFAILED :/* all failed */
|
|
|
|
cnt_err ? LSBLK_EXIT_SOMEOK : /* some ok */
|
|
|
|
EXIT_SUCCESS; /* all success */
|
|
|
|
}
|
2010-11-24 10:06:39 -06:00
|
|
|
|
2018-10-18 08:46:07 -05:00
|
|
|
if (lsblk->dedup_id > -1) {
|
|
|
|
devtree_set_dedupkeys(tr, lsblk->dedup_id);
|
|
|
|
lsblk_devtree_deduplicate_devices(tr);
|
|
|
|
}
|
|
|
|
|
2018-10-15 06:55:08 -05:00
|
|
|
devtree_to_scols(tr, lsblk->table);
|
|
|
|
|
2014-05-20 08:08:08 -05:00
|
|
|
if (lsblk->sort_col)
|
|
|
|
scols_sort_table(lsblk->table, lsblk->sort_col);
|
2017-04-13 04:39:22 -05:00
|
|
|
if (lsblk->force_tree_order)
|
|
|
|
scols_sort_table_by_tree(lsblk->table);
|
2014-05-20 08:08:08 -05:00
|
|
|
|
2014-03-27 10:29:13 -05:00
|
|
|
scols_print_table(lsblk->table);
|
2010-11-24 10:06:39 -06:00
|
|
|
|
|
|
|
leave:
|
2014-05-20 08:08:08 -05:00
|
|
|
if (lsblk->sort_col)
|
|
|
|
unref_sortdata(lsblk->table);
|
|
|
|
|
2014-03-27 10:29:13 -05:00
|
|
|
scols_unref_table(lsblk->table);
|
2013-08-21 09:07:51 -05:00
|
|
|
|
2018-09-14 08:45:53 -05:00
|
|
|
lsblk_mnt_deinit();
|
2018-09-19 04:25:08 -05:00
|
|
|
lsblk_properties_deinit();
|
2018-10-15 07:14:33 -05:00
|
|
|
lsblk_unref_devtree(tr);
|
2018-09-19 04:25:08 -05:00
|
|
|
|
2010-11-24 10:06:39 -06:00
|
|
|
return status;
|
|
|
|
}
|