]>
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 <mbroz@redhat.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 */
46 int flags
; /* LSBLK_* */
48 unsigned int all_devices
:1; /* print all devices, including empty */
49 unsigned int bytes
:1; /* print SIZE in bytes */
50 unsigned int inverse
:1; /* print inverse dependencies */
51 unsigned int merge
:1; /* merge sub-trees */
52 unsigned int nodeps
:1; /* don't print slaves/holders */
53 unsigned int scsi
:1; /* print only device with HCTL (SCSI) */
54 unsigned int paths
:1; /* print devnames with "/dev" prefix */
55 unsigned int sort_hidden
:1; /* sort column not between output columns */
56 unsigned int dedup_hidden
:1; /* deduplication column not between output columns */
57 unsigned int force_tree_order
:1;/* sort lines by parent->tree relation */
58 unsigned int noempty
:1; /* hide empty devices */
61 extern struct lsblk
*lsblk
; /* global handler */
63 struct lsblk_devprop
{
64 /* udev / blkid based */
65 char *fstype
; /* detected fs, NULL or "?" if cannot detect */
66 char *fsversion
; /* filesystem version */
67 char *uuid
; /* filesystem UUID (or stack uuid) */
68 char *ptuuid
; /* partition table UUID */
69 char *pttype
; /* partition table type */
70 char *label
; /* filesystem label */
71 char *parttype
; /* partition type UUID */
72 char *partuuid
; /* partition UUID */
73 char *partlabel
; /* partition label */
74 char *partflags
; /* partition flags */
75 char *wwn
; /* storage WWN */
76 char *serial
; /* disk serial number */
77 char *model
; /* disk model */
79 /* lsblk specific (for --sysroot only) */
80 char *owner
; /* user name */
81 char *group
; /* group name */
82 char *mode
; /* access mode in ls(1)-like notation */
87 * Note that the same device may be slave/holder for more another devices. It
88 * means we need to allocate list member rather than use @child directly.
91 struct list_head ls_childs
; /* item in parent->childs */
92 struct list_head ls_parents
; /* item in child->parents */
94 struct lsblk_device
*child
;
95 struct lsblk_device
*parent
;
101 struct list_head childs
; /* list with lsblk_devdep */
102 struct list_head parents
;
103 struct list_head ls_roots
; /* item in devtree->roots list */
104 struct list_head ls_devices
; /* item in devtree->devices list */
106 struct lsblk_device
*wholedisk
; /* for partitions */
108 struct libscols_line
*scols_line
;
110 struct lsblk_devprop
*properties
;
113 char *name
; /* kernel name in /sys/block */
114 char *dm_name
; /* DM name (dm/block) */
116 char *filename
; /* path to device node */
117 char *dedupkey
; /* de-duplication key */
119 struct path_cxt
*sysfs
;
121 struct libmnt_fs
**fss
; /* filesystems attached to the device */
122 size_t nfss
; /* number of items in fss[] */
124 struct statvfs fsstat
; /* statvfs() result */
126 int npartitions
; /* # of partitions this device has */
127 int nholders
; /* # of devices mapped directly to this device
128 * /sys/block/.../holders */
129 int nslaves
; /* # of devices this device maps to */
130 int maj
, min
; /* devno */
132 uint64_t discard_granularity
; /* sunknown:-1, yes:1, not:0 */
134 uint64_t size
; /* device size */
135 int removable
; /* unknown:-1, yes:1, not:0 */
137 unsigned int is_mounted
: 1,
145 #define device_is_partition(_x) ((_x)->wholedisk != NULL)
147 /* Unfortunately, pktcdvd dependence on block device is not defined by
148 * slave/holder symlinks. The struct lsblk_devnomap represents one line in
149 * /sys/class/pktcdvd/device_map
151 struct lsblk_devnomap
{
152 dev_t slave
; /* packet device devno */
153 dev_t holder
; /* block device devno */
155 struct list_head ls_devnomap
;
160 * Note that lsblk tree uses bottom devices (devices without slaves) as root
161 * of the tree, and partitions are interpreted as a dependence too; it means:
164 * The flag 'is_inverted' turns the tree over (root is device without holders):
167 struct lsblk_devtree
{
170 struct list_head roots
; /* tree root devices */
171 struct list_head devices
; /* all devices */
172 struct list_head pktcdvd_map
; /* devnomap->ls_devnomap */
174 unsigned int is_inverse
: 1, /* inverse tree */
183 struct list_head
*p
; /* current position */
184 struct list_head
*head
; /* start position */
185 int direction
; /* LSBLK_ITER_{FOR,BACK}WARD */
188 #define LSBLK_ITER_FORWARD 0
189 #define LSBLK_ITER_BACKWARD 1
191 #define IS_ITER_FORWARD(_i) ((_i)->direction == LSBLK_ITER_FORWARD)
192 #define IS_ITER_BACKWARD(_i) ((_i)->direction == LSBLK_ITER_BACKWARD)
194 #define LSBLK_ITER_INIT(itr, list) \
196 (itr)->p = IS_ITER_FORWARD(itr) ? \
197 (list)->next : (list)->prev; \
198 (itr)->head = (list); \
201 #define LSBLK_ITER_ITERATE(itr, res, restype, member) \
203 res = list_entry((itr)->p, restype, member); \
204 (itr)->p = IS_ITER_FORWARD(itr) ? \
205 (itr)->p->next : (itr)->p->prev; \
210 extern void lsblk_mnt_init(void);
211 extern void lsblk_mnt_deinit(void);
213 extern void lsblk_device_free_filesystems(struct lsblk_device
*dev
);
214 extern const char *lsblk_device_get_mountpoint(struct lsblk_device
*dev
);
215 extern struct libmnt_fs
**lsblk_device_get_filesystems(struct lsblk_device
*dev
, size_t *n
);
217 /* lsblk-properties.c */
218 extern void lsblk_device_free_properties(struct lsblk_devprop
*p
);
219 extern struct lsblk_devprop
*lsblk_device_get_properties(struct lsblk_device
*dev
);
220 extern void lsblk_properties_deinit(void);
222 extern const char *lsblk_parttype_code_to_string(const char *code
, const char *pttype
);
224 /* lsblk-devtree.c */
225 void lsblk_reset_iter(struct lsblk_iter
*itr
, int direction
);
226 struct lsblk_device
*lsblk_new_device(void);
227 void lsblk_ref_device(struct lsblk_device
*dev
);
228 void lsblk_unref_device(struct lsblk_device
*dev
);
229 int lsblk_device_new_dependence(struct lsblk_device
*parent
, struct lsblk_device
*child
);
230 int lsblk_device_has_child(struct lsblk_device
*dev
, struct lsblk_device
*child
);
231 int lsblk_device_next_child(struct lsblk_device
*dev
,
232 struct lsblk_iter
*itr
,
233 struct lsblk_device
**child
);
235 dev_t
lsblk_devtree_pktcdvd_get_mate(struct lsblk_devtree
*tr
, dev_t devno
, int is_slave
);
237 int lsblk_device_is_last_parent(struct lsblk_device
*dev
, struct lsblk_device
*parent
);
238 int lsblk_device_next_parent(
239 struct lsblk_device
*dev
,
240 struct lsblk_iter
*itr
,
241 struct lsblk_device
**parent
);
243 struct lsblk_devtree
*lsblk_new_devtree(void);
244 void lsblk_ref_devtree(struct lsblk_devtree
*tr
);
245 void lsblk_unref_devtree(struct lsblk_devtree
*tr
);
246 int lsblk_devtree_add_root(struct lsblk_devtree
*tr
, struct lsblk_device
*dev
);
247 int lsblk_devtree_remove_root(struct lsblk_devtree
*tr
, struct lsblk_device
*dev
);
248 int lsblk_devtree_next_root(struct lsblk_devtree
*tr
,
249 struct lsblk_iter
*itr
,
250 struct lsblk_device
**dev
);
251 int lsblk_devtree_add_device(struct lsblk_devtree
*tr
, struct lsblk_device
*dev
);
252 int lsblk_devtree_next_device(struct lsblk_devtree
*tr
,
253 struct lsblk_iter
*itr
,
254 struct lsblk_device
**dev
);
255 int lsblk_devtree_has_device(struct lsblk_devtree
*tr
, struct lsblk_device
*dev
);
256 struct lsblk_device
*lsblk_devtree_get_device(struct lsblk_devtree
*tr
, const char *name
);
257 int lsblk_devtree_remove_device(struct lsblk_devtree
*tr
, struct lsblk_device
*dev
);
258 int lsblk_devtree_deduplicate_devices(struct lsblk_devtree
*tr
);
260 #endif /* UTIL_LINUX_LSBLK_H */