]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/lsblk.h
2 * Copyright (C) 2010-2018 Red Hat, Inc. All rights reserved.
3 * Written by Milan Broz <gmazyland@gmail.com>
4 * Karel Zak <kzak@redhat.com>
6 #ifndef UTIL_LINUX_LSBLK_H
7 #define UTIL_LINUX_LSBLK_H
12 #include <sys/statvfs.h>
14 #include <libsmartcols.h>
21 #define LSBLK_DEBUG_INIT (1 << 1)
22 #define LSBLK_DEBUG_FILTER (1 << 2)
23 #define LSBLK_DEBUG_DEV (1 << 3)
24 #define LSBLK_DEBUG_TREE (1 << 4)
25 #define LSBLK_DEBUG_DEP (1 << 5)
26 #define LSBLK_DEBUG_ALL 0xFFFF
28 UL_DEBUG_DECLARE_MASK(lsblk
);
29 #define DBG(m, x) __UL_DBG(lsblk, LSBLK_DEBUG_, m, x)
30 #define ON_DBG(m, x) __UL_DBG_CALL(lsblk, LSBLK_DEBUG_, m, x)
32 #define UL_DEBUG_CURRENT_MASK UL_DEBUG_MASK(lsblk)
36 struct libscols_table
*table
; /* output table */
37 struct libscols_column
*sort_col
;/* sort output by this column */
39 int sort_id
; /* id of the sort column */
40 int tree_id
; /* od of column used for tree */
44 struct libscols_filter
*filter
;
45 struct libscols_filter
*hlighter
;
46 const char *hlighter_seq
;
48 struct libscols_filter
**ct_filters
; /* array of counters' filters */
49 size_t ncts
; /* number of ct filters */
52 int flags
; /* LSBLK_* */
54 unsigned int all_devices
:1; /* print all devices, including empty */
55 unsigned int bytes
:1; /* print SIZE in bytes */
56 unsigned int inverse
:1; /* print inverse dependencies */
57 unsigned int merge
:1; /* merge sub-trees */
58 unsigned int nodeps
:1; /* don't print slaves/holders */
59 unsigned int scsi
:1; /* print only device with HCTL (SCSI) */
60 unsigned int nvme
:1; /* print NVMe device only */
61 unsigned int virtio
:1; /* print virtio device only */
62 unsigned int paths
:1; /* print devnames with "/dev" prefix */
63 unsigned int sort_hidden
:1; /* sort column not between output columns */
64 unsigned int dedup_hidden
:1; /* deduplication column not between output columns */
65 unsigned int force_tree_order
:1;/* sort lines by parent->tree relation */
66 unsigned int noempty
:1; /* hide empty devices */
69 extern struct lsblk
*lsblk
; /* global handler */
71 struct lsblk_devprop
{
72 /* udev / blkid based */
73 char *fstype
; /* detected fs, NULL or "?" if cannot detect */
74 char *fsversion
; /* filesystem version */
75 char *uuid
; /* filesystem UUID (or stack uuid) */
76 char *ptuuid
; /* partition table UUID */
77 char *pttype
; /* partition table type */
78 char *label
; /* filesystem label */
79 char *parttype
; /* partition type UUID */
80 char *partuuid
; /* partition UUID */
81 char *partlabel
; /* partition label */
82 char *partflags
; /* partition flags */
83 char *partn
; /* partition number */
84 char *wwn
; /* storage WWN */
85 char *serial
; /* disk serial number */
86 char *model
; /* disk model */
87 char *idlink
; /* /dev/disk/by-id/<name> */
88 char *revision
; /* firmware revision/version */
90 /* lsblk specific (for --sysroot only) */
91 char *owner
; /* user name */
92 char *group
; /* group name */
93 char *mode
; /* access mode in ls(1)-like notation */
98 * Note that the same device may be slave/holder for more another devices. It
99 * means we need to allocate list member rather than use @child directly.
101 struct lsblk_devdep
{
102 struct list_head ls_childs
; /* item in parent->childs */
103 struct list_head ls_parents
; /* item in child->parents */
105 struct lsblk_device
*child
;
106 struct lsblk_device
*parent
;
109 struct lsblk_device
{
112 struct list_head childs
; /* list with lsblk_devdep */
113 struct list_head parents
;
114 struct list_head ls_roots
; /* item in devtree->roots list */
115 struct list_head ls_devices
; /* item in devtree->devices list */
117 struct lsblk_device
*wholedisk
; /* for partitions */
119 struct libscols_line
*scols_line
;
121 struct lsblk_devprop
*properties
;
124 char *name
; /* kernel name in /sys/block */
125 char *dm_name
; /* DM name (dm/block) */
127 char *filename
; /* path to device node */
128 char *dedupkey
; /* de-duplication key */
130 struct path_cxt
*sysfs
;
132 struct libmnt_fs
**fss
; /* filesystems attached to the device */
133 size_t nfss
; /* number of items in fss[] */
135 struct statvfs fsstat
; /* statvfs() result */
137 int npartitions
; /* # of partitions this device has */
138 int nholders
; /* # of devices mapped directly to this device
139 * /sys/block/.../holders */
140 int nslaves
; /* # of devices this device maps to */
141 int maj
, min
; /* devno */
143 uint64_t discard_granularity
; /* sunknown:-1, yes:1, not:0 */
145 uint64_t size
; /* device size */
146 int removable
; /* unknown:-1, yes:1, not:0 */
148 unsigned int is_mounted
: 1,
156 #define device_is_partition(_x) ((_x)->wholedisk != NULL)
158 /* Unfortunately, pktcdvd dependence on block device is not defined by
159 * slave/holder symlinks. The struct lsblk_devnomap represents one line in
160 * /sys/class/pktcdvd/device_map
162 struct lsblk_devnomap
{
163 dev_t slave
; /* packet device devno */
164 dev_t holder
; /* block device devno */
166 struct list_head ls_devnomap
;
171 * Note that lsblk tree uses bottom devices (devices without slaves) as root
172 * of the tree, and partitions are interpreted as a dependence too; it means:
175 * The flag 'is_inverted' turns the tree over (root is device without holders):
178 struct lsblk_devtree
{
181 struct list_head roots
; /* tree root devices */
182 struct list_head devices
; /* all devices */
183 struct list_head pktcdvd_map
; /* devnomap->ls_devnomap */
185 unsigned int is_inverse
: 1, /* inverse tree */
194 struct list_head
*p
; /* current position */
195 struct list_head
*head
; /* start position */
196 int direction
; /* LSBLK_ITER_{FOR,BACK}WARD */
199 #define LSBLK_ITER_FORWARD 0
200 #define LSBLK_ITER_BACKWARD 1
202 #define IS_ITER_FORWARD(_i) ((_i)->direction == LSBLK_ITER_FORWARD)
203 #define IS_ITER_BACKWARD(_i) ((_i)->direction == LSBLK_ITER_BACKWARD)
205 #define LSBLK_ITER_INIT(itr, list) \
207 (itr)->p = IS_ITER_FORWARD(itr) ? \
208 (list)->next : (list)->prev; \
209 (itr)->head = (list); \
212 #define LSBLK_ITER_ITERATE(itr, res, restype, member) \
214 res = list_entry((itr)->p, restype, member); \
215 (itr)->p = IS_ITER_FORWARD(itr) ? \
216 (itr)->p->next : (itr)->p->prev; \
221 extern void lsblk_mnt_init(void);
222 extern void lsblk_mnt_deinit(void);
224 extern void lsblk_device_free_filesystems(struct lsblk_device
*dev
);
225 extern const char *lsblk_device_get_mountpoint(struct lsblk_device
*dev
);
226 extern struct libmnt_fs
**lsblk_device_get_filesystems(struct lsblk_device
*dev
, size_t *n
);
228 /* lsblk-properties.c */
229 extern void lsblk_device_free_properties(struct lsblk_devprop
*p
);
230 extern struct lsblk_devprop
*lsblk_device_get_properties(struct lsblk_device
*dev
);
231 extern void lsblk_properties_deinit(void);
233 extern const char *lsblk_parttype_code_to_string(const char *code
, const char *pttype
);
235 /* lsblk-devtree.c */
236 void lsblk_reset_iter(struct lsblk_iter
*itr
, int direction
);
237 struct lsblk_device
*lsblk_new_device(void);
238 void lsblk_ref_device(struct lsblk_device
*dev
);
239 void lsblk_unref_device(struct lsblk_device
*dev
);
240 int lsblk_device_new_dependence(struct lsblk_device
*parent
, struct lsblk_device
*child
);
241 int lsblk_device_has_child(struct lsblk_device
*dev
, struct lsblk_device
*child
);
242 int lsblk_device_next_child(struct lsblk_device
*dev
,
243 struct lsblk_iter
*itr
,
244 struct lsblk_device
**child
);
246 dev_t
lsblk_devtree_pktcdvd_get_mate(struct lsblk_devtree
*tr
, dev_t devno
, int is_slave
);
248 int lsblk_device_is_last_parent(struct lsblk_device
*dev
, struct lsblk_device
*parent
);
249 int lsblk_device_next_parent(
250 struct lsblk_device
*dev
,
251 struct lsblk_iter
*itr
,
252 struct lsblk_device
**parent
);
254 struct lsblk_devtree
*lsblk_new_devtree(void);
255 void lsblk_ref_devtree(struct lsblk_devtree
*tr
);
256 void lsblk_unref_devtree(struct lsblk_devtree
*tr
);
257 int lsblk_devtree_add_root(struct lsblk_devtree
*tr
, struct lsblk_device
*dev
);
258 int lsblk_devtree_remove_root(struct lsblk_devtree
*tr
, struct lsblk_device
*dev
);
259 int lsblk_devtree_next_root(struct lsblk_devtree
*tr
,
260 struct lsblk_iter
*itr
,
261 struct lsblk_device
**dev
);
262 int lsblk_devtree_add_device(struct lsblk_devtree
*tr
, struct lsblk_device
*dev
);
263 int lsblk_devtree_next_device(struct lsblk_devtree
*tr
,
264 struct lsblk_iter
*itr
,
265 struct lsblk_device
**dev
);
266 int lsblk_devtree_has_device(struct lsblk_devtree
*tr
, struct lsblk_device
*dev
);
267 struct lsblk_device
*lsblk_devtree_get_device(struct lsblk_devtree
*tr
, const char *name
);
268 int lsblk_devtree_remove_device(struct lsblk_devtree
*tr
, struct lsblk_device
*dev
);
269 int lsblk_devtree_deduplicate_devices(struct lsblk_devtree
*tr
);
271 #endif /* UTIL_LINUX_LSBLK_H */