2018-09-14 08:36:59 -05:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010-2018 Red Hat, Inc. All rights reserved.
|
|
|
|
* Written by Milan Broz <mbroz@redhat.com>
|
|
|
|
* Karel Zak <kzak@redhat.com>
|
|
|
|
*/
|
|
|
|
#ifndef UTIL_LINUX_LSBLK_H
|
|
|
|
#define UTIL_LINUX_LSBLK_H
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <inttypes.h>
|
2018-09-26 01:23:08 -05:00
|
|
|
#include <sys/stat.h>
|
2018-09-14 08:36:59 -05:00
|
|
|
#include <sys/statvfs.h>
|
|
|
|
|
|
|
|
#include <libsmartcols.h>
|
2021-01-08 06:20:50 -06:00
|
|
|
#include <libmount.h>
|
2018-09-14 08:36:59 -05:00
|
|
|
|
|
|
|
#include "c.h"
|
2018-10-12 05:50:03 -05:00
|
|
|
#include "list.h"
|
2018-09-14 08:36:59 -05:00
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
#define LSBLK_DEBUG_INIT (1 << 1)
|
|
|
|
#define LSBLK_DEBUG_FILTER (1 << 2)
|
|
|
|
#define LSBLK_DEBUG_DEV (1 << 3)
|
2018-10-12 05:51:03 -05:00
|
|
|
#define LSBLK_DEBUG_TREE (1 << 4)
|
2018-10-23 07:04:55 -05:00
|
|
|
#define LSBLK_DEBUG_DEP (1 << 5)
|
2018-09-14 08:36:59 -05:00
|
|
|
#define LSBLK_DEBUG_ALL 0xFFFF
|
|
|
|
|
|
|
|
UL_DEBUG_DECLARE_MASK(lsblk);
|
|
|
|
#define DBG(m, x) __UL_DBG(lsblk, LSBLK_DEBUG_, m, x)
|
|
|
|
#define ON_DBG(m, x) __UL_DBG_CALL(lsblk, LSBLK_DEBUG_, m, x)
|
|
|
|
|
|
|
|
#define UL_DEBUG_CURRENT_MASK UL_DEBUG_MASK(lsblk)
|
|
|
|
#include "debugobj.h"
|
|
|
|
|
|
|
|
struct lsblk {
|
|
|
|
struct libscols_table *table; /* output table */
|
|
|
|
struct libscols_column *sort_col;/* sort output by this column */
|
2019-02-20 05:49:24 -06:00
|
|
|
|
|
|
|
int sort_id; /* id of the sort column */
|
|
|
|
int tree_id; /* od of column used for tree */
|
2018-09-14 08:36:59 -05:00
|
|
|
|
2018-10-18 08:46:07 -05:00
|
|
|
int dedup_id;
|
|
|
|
|
2020-11-23 08:16:39 -06:00
|
|
|
|
2018-09-14 08:36:59 -05:00
|
|
|
const char *sysroot;
|
|
|
|
int flags; /* LSBLK_* */
|
|
|
|
|
|
|
|
unsigned int all_devices:1; /* print all devices, including empty */
|
|
|
|
unsigned int bytes:1; /* print SIZE in bytes */
|
|
|
|
unsigned int inverse:1; /* print inverse dependencies */
|
2018-12-07 05:29:50 -06:00
|
|
|
unsigned int merge:1; /* merge sub-trees */
|
2018-09-14 08:36:59 -05:00
|
|
|
unsigned int nodeps:1; /* don't print slaves/holders */
|
|
|
|
unsigned int scsi:1; /* print only device with HCTL (SCSI) */
|
|
|
|
unsigned int paths:1; /* print devnames with "/dev" prefix */
|
|
|
|
unsigned int sort_hidden:1; /* sort column not between output columns */
|
2018-10-18 08:46:07 -05:00
|
|
|
unsigned int dedup_hidden :1; /* deduplication column not between output columns */
|
2018-09-14 08:36:59 -05:00
|
|
|
unsigned int force_tree_order:1;/* sort lines by parent->tree relation */
|
|
|
|
};
|
|
|
|
|
|
|
|
extern struct lsblk *lsblk; /* global handler */
|
|
|
|
|
2018-09-19 04:04:35 -05:00
|
|
|
struct lsblk_devprop {
|
2019-10-15 06:08:09 -05:00
|
|
|
/* udev / blkid based */
|
2018-09-19 04:04:35 -05:00
|
|
|
char *fstype; /* detected fs, NULL or "?" if cannot detect */
|
2019-11-12 06:55:55 -06:00
|
|
|
char *fsversion; /* filesystem version */
|
2018-09-19 04:04:35 -05:00
|
|
|
char *uuid; /* filesystem UUID (or stack uuid) */
|
|
|
|
char *ptuuid; /* partition table UUID */
|
|
|
|
char *pttype; /* partition table type */
|
|
|
|
char *label; /* filesystem label */
|
|
|
|
char *parttype; /* partition type UUID */
|
|
|
|
char *partuuid; /* partition UUID */
|
|
|
|
char *partlabel; /* partition label */
|
|
|
|
char *partflags; /* partition flags */
|
|
|
|
char *wwn; /* storage WWN */
|
|
|
|
char *serial; /* disk serial number */
|
|
|
|
char *model; /* disk model */
|
2019-10-15 06:08:09 -05:00
|
|
|
|
|
|
|
/* lsblk specific (for --sysroot only) */
|
|
|
|
char *owner; /* user name */
|
|
|
|
char *group; /* group name */
|
|
|
|
char *mode; /* access mode in ls(1)-like notation */
|
2018-09-19 04:04:35 -05:00
|
|
|
};
|
|
|
|
|
2018-10-12 05:50:03 -05:00
|
|
|
/* Device dependence
|
|
|
|
*
|
|
|
|
* Note that the same device may be slave/holder for more another devices. It
|
|
|
|
* means we need to allocate list member rather than use @child directly.
|
|
|
|
*/
|
|
|
|
struct lsblk_devdep {
|
2018-10-23 07:04:55 -05:00
|
|
|
struct list_head ls_childs; /* item in parent->childs */
|
|
|
|
struct list_head ls_parents; /* item in child->parents */
|
|
|
|
|
2018-10-12 05:50:03 -05:00
|
|
|
struct lsblk_device *child;
|
2018-10-23 07:04:55 -05:00
|
|
|
struct lsblk_device *parent;
|
2018-10-12 05:50:03 -05:00
|
|
|
};
|
|
|
|
|
2018-10-09 05:46:28 -05:00
|
|
|
struct lsblk_device {
|
2018-10-12 05:50:03 -05:00
|
|
|
int refcount;
|
|
|
|
|
2018-10-23 07:04:55 -05:00
|
|
|
struct list_head childs; /* list with lsblk_devdep */
|
|
|
|
struct list_head parents;
|
2018-10-12 05:50:03 -05:00
|
|
|
struct list_head ls_roots; /* item in devtree->roots list */
|
|
|
|
struct list_head ls_devices; /* item in devtree->devices list */
|
2018-09-14 08:36:59 -05:00
|
|
|
|
2018-10-18 06:57:46 -05:00
|
|
|
struct lsblk_device *wholedisk; /* for partitions */
|
2018-10-12 05:50:03 -05:00
|
|
|
|
2018-12-07 05:29:50 -06:00
|
|
|
struct libscols_line *scols_line;
|
|
|
|
|
2018-10-12 05:50:03 -05:00
|
|
|
struct lsblk_devprop *properties;
|
2018-09-14 08:36:59 -05:00
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
char *name; /* kernel name in /sys/block */
|
|
|
|
char *dm_name; /* DM name (dm/block) */
|
|
|
|
|
|
|
|
char *filename; /* path to device node */
|
2018-10-18 08:46:07 -05:00
|
|
|
char *dedupkey; /* de-duplication key */
|
2018-09-14 08:36:59 -05:00
|
|
|
|
|
|
|
struct path_cxt *sysfs;
|
|
|
|
|
2021-01-08 06:20:50 -06:00
|
|
|
struct libmnt_fs **fss; /* filesystems attached to the device */
|
|
|
|
size_t nfss; /* number of items in fss[] */
|
|
|
|
|
2018-09-14 08:36:59 -05:00
|
|
|
struct statvfs fsstat; /* statvfs() result */
|
|
|
|
|
|
|
|
int npartitions; /* # of partitions this device has */
|
|
|
|
int nholders; /* # of devices mapped directly to this device
|
|
|
|
* /sys/block/.../holders */
|
|
|
|
int nslaves; /* # of devices this device maps to */
|
|
|
|
int maj, min; /* devno */
|
2018-10-17 07:10:27 -05:00
|
|
|
|
|
|
|
uint64_t discard_granularity; /* sunknown:-1, yes:1, not:0 */
|
2018-09-14 08:36:59 -05:00
|
|
|
|
|
|
|
uint64_t size; /* device size */
|
2018-10-17 06:48:43 -05:00
|
|
|
int removable; /* unknown:-1, yes:1, not:0 */
|
2018-09-14 08:36:59 -05:00
|
|
|
|
2018-09-14 08:57:45 -05:00
|
|
|
unsigned int is_mounted : 1,
|
2018-09-19 04:04:35 -05:00
|
|
|
is_swap : 1,
|
2018-12-07 05:29:50 -06:00
|
|
|
is_printed : 1,
|
2018-09-19 04:04:35 -05:00
|
|
|
udev_requested : 1,
|
2019-10-15 05:36:16 -05:00
|
|
|
blkid_requested : 1,
|
|
|
|
file_requested : 1;
|
2018-09-14 08:36:59 -05:00
|
|
|
};
|
|
|
|
|
2018-10-18 06:57:46 -05:00
|
|
|
#define device_is_partition(_x) ((_x)->wholedisk != NULL)
|
2018-10-12 05:50:03 -05:00
|
|
|
|
2020-11-23 08:16:39 -06:00
|
|
|
/* Unfortunately, pktcdvd dependence on block device is not defined by
|
|
|
|
* slave/holder symlinks. The struct lsblk_devnomap represents one line in
|
|
|
|
* /sys/class/pktcdvd/device_map
|
|
|
|
*/
|
|
|
|
struct lsblk_devnomap {
|
|
|
|
dev_t slave; /* packet device devno */
|
|
|
|
dev_t holder; /* block device devno */
|
|
|
|
|
|
|
|
struct list_head ls_devnomap;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-10-12 05:50:03 -05:00
|
|
|
/*
|
2019-01-14 16:40:34 -06:00
|
|
|
* Note that lsblk tree uses bottom devices (devices without slaves) as root
|
2018-10-12 05:50:03 -05:00
|
|
|
* of the tree, and partitions are interpreted as a dependence too; it means:
|
|
|
|
* sda -> sda1 -> md0
|
|
|
|
*
|
|
|
|
* The flag 'is_inverted' turns the tree over (root is device without holders):
|
|
|
|
* md0 -> sda1 -> sda
|
|
|
|
*/
|
|
|
|
struct lsblk_devtree {
|
|
|
|
int refcount;
|
|
|
|
|
|
|
|
struct list_head roots; /* tree root devices */
|
|
|
|
struct list_head devices; /* all devices */
|
2020-11-23 08:16:39 -06:00
|
|
|
struct list_head pktcdvd_map; /* devnomap->ls_devnomap */
|
2018-10-12 05:50:03 -05:00
|
|
|
|
2020-11-23 08:16:39 -06:00
|
|
|
unsigned int is_inverse : 1, /* inverse tree */
|
|
|
|
pktcdvd_read : 1;
|
2018-10-12 05:50:03 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Generic iterator
|
|
|
|
*/
|
|
|
|
struct lsblk_iter {
|
|
|
|
struct list_head *p; /* current position */
|
|
|
|
struct list_head *head; /* start position */
|
|
|
|
int direction; /* LSBLK_ITER_{FOR,BACK}WARD */
|
|
|
|
};
|
|
|
|
|
|
|
|
#define LSBLK_ITER_FORWARD 0
|
|
|
|
#define LSBLK_ITER_BACKWARD 1
|
|
|
|
|
|
|
|
#define IS_ITER_FORWARD(_i) ((_i)->direction == LSBLK_ITER_FORWARD)
|
|
|
|
#define IS_ITER_BACKWARD(_i) ((_i)->direction == LSBLK_ITER_BACKWARD)
|
|
|
|
|
|
|
|
#define LSBLK_ITER_INIT(itr, list) \
|
|
|
|
do { \
|
|
|
|
(itr)->p = IS_ITER_FORWARD(itr) ? \
|
|
|
|
(list)->next : (list)->prev; \
|
|
|
|
(itr)->head = (list); \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
#define LSBLK_ITER_ITERATE(itr, res, restype, member) \
|
|
|
|
do { \
|
|
|
|
res = list_entry((itr)->p, restype, member); \
|
|
|
|
(itr)->p = IS_ITER_FORWARD(itr) ? \
|
|
|
|
(itr)->p->next : (itr)->p->prev; \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
|
2018-09-14 08:49:00 -05:00
|
|
|
/* lsblk-mnt.c */
|
2018-09-14 08:45:53 -05:00
|
|
|
extern void lsblk_mnt_init(void);
|
|
|
|
extern void lsblk_mnt_deinit(void);
|
|
|
|
|
2021-01-08 06:20:50 -06:00
|
|
|
extern void lsblk_device_free_filesystems(struct lsblk_device *dev);
|
|
|
|
extern const char *lsblk_device_get_mountpoint(struct lsblk_device *dev);
|
|
|
|
extern struct libmnt_fs **lsblk_device_get_filesystems(struct lsblk_device *dev, size_t *n);
|
2018-09-14 08:41:39 -05:00
|
|
|
|
2018-09-19 04:25:08 -05:00
|
|
|
/* lsblk-properties.c */
|
|
|
|
extern void lsblk_device_free_properties(struct lsblk_devprop *p);
|
2018-10-09 05:46:28 -05:00
|
|
|
extern struct lsblk_devprop *lsblk_device_get_properties(struct lsblk_device *dev);
|
2018-09-19 04:25:08 -05:00
|
|
|
extern void lsblk_properties_deinit(void);
|
|
|
|
|
2019-12-04 05:38:57 -06:00
|
|
|
extern const char *lsblk_parttype_code_to_string(const char *code, const char *pttype);
|
|
|
|
|
2018-10-12 05:50:03 -05:00
|
|
|
/* lsblk-devtree.c */
|
|
|
|
void lsblk_reset_iter(struct lsblk_iter *itr, int direction);
|
2018-10-18 06:57:46 -05:00
|
|
|
struct lsblk_device *lsblk_new_device(void);
|
2018-10-12 05:50:03 -05:00
|
|
|
void lsblk_ref_device(struct lsblk_device *dev);
|
|
|
|
void lsblk_unref_device(struct lsblk_device *dev);
|
2018-10-16 06:37:36 -05:00
|
|
|
int lsblk_device_new_dependence(struct lsblk_device *parent, struct lsblk_device *child);
|
2018-10-23 07:04:55 -05:00
|
|
|
int lsblk_device_has_child(struct lsblk_device *dev, struct lsblk_device *child);
|
2018-10-12 05:50:03 -05:00
|
|
|
int lsblk_device_next_child(struct lsblk_device *dev,
|
|
|
|
struct lsblk_iter *itr,
|
|
|
|
struct lsblk_device **child);
|
|
|
|
|
2020-11-23 08:16:39 -06:00
|
|
|
dev_t lsblk_devtree_pktcdvd_get_mate(struct lsblk_devtree *tr, dev_t devno, int is_slave);
|
|
|
|
|
2018-12-07 05:29:50 -06:00
|
|
|
int lsblk_device_is_last_parent(struct lsblk_device *dev, struct lsblk_device *parent);
|
|
|
|
int lsblk_device_next_parent(
|
|
|
|
struct lsblk_device *dev,
|
|
|
|
struct lsblk_iter *itr,
|
|
|
|
struct lsblk_device **parent);
|
|
|
|
|
2018-10-12 05:50:03 -05:00
|
|
|
struct lsblk_devtree *lsblk_new_devtree(void);
|
|
|
|
void lsblk_ref_devtree(struct lsblk_devtree *tr);
|
|
|
|
void lsblk_unref_devtree(struct lsblk_devtree *tr);
|
|
|
|
int lsblk_devtree_add_root(struct lsblk_devtree *tr, struct lsblk_device *dev);
|
2020-11-23 08:16:39 -06:00
|
|
|
int lsblk_devtree_remove_root(struct lsblk_devtree *tr, struct lsblk_device *dev);
|
2018-10-12 05:50:03 -05:00
|
|
|
int lsblk_devtree_next_root(struct lsblk_devtree *tr,
|
|
|
|
struct lsblk_iter *itr,
|
|
|
|
struct lsblk_device **dev);
|
|
|
|
int lsblk_devtree_add_device(struct lsblk_devtree *tr, struct lsblk_device *dev);
|
|
|
|
int lsblk_devtree_next_device(struct lsblk_devtree *tr,
|
|
|
|
struct lsblk_iter *itr,
|
|
|
|
struct lsblk_device **dev);
|
2018-10-15 06:53:19 -05:00
|
|
|
int lsblk_devtree_has_device(struct lsblk_devtree *tr, struct lsblk_device *dev);
|
2018-10-12 05:50:03 -05:00
|
|
|
struct lsblk_device *lsblk_devtree_get_device(struct lsblk_devtree *tr, const char *name);
|
2018-10-15 06:53:19 -05:00
|
|
|
int lsblk_devtree_remove_device(struct lsblk_devtree *tr, struct lsblk_device *dev);
|
2018-10-18 08:46:07 -05:00
|
|
|
int lsblk_devtree_deduplicate_devices(struct lsblk_devtree *tr);
|
2018-10-12 05:50:03 -05:00
|
|
|
|
2018-09-14 08:36:59 -05:00
|
|
|
#endif /* UTIL_LINUX_LSBLK_H */
|