summaryrefslogtreecommitdiffstats
path: root/libfdisk
diff options
context:
space:
mode:
authorKarel Zak2018-08-30 14:01:46 +0200
committerKarel Zak2018-08-30 14:01:46 +0200
commit745801e44ab01eef759b99372e5792d0eadf19ab (patch)
treef97bf31e5daedbc0bd55784352eab78a9c5a1591 /libfdisk
parentdocs: add note about lsblk usage/free cols (diff)
downloadkernel-qcow2-util-linux-745801e44ab01eef759b99372e5792d0eadf19ab.tar.gz
kernel-qcow2-util-linux-745801e44ab01eef759b99372e5792d0eadf19ab.tar.xz
kernel-qcow2-util-linux-745801e44ab01eef759b99372e5792d0eadf19ab.zip
libfdisk: add fdisk_get_devmodel() and fdisk_get_devno()
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'libfdisk')
-rw-r--r--libfdisk/docs/libfdisk-sections.txt2
-rw-r--r--libfdisk/src/context.c50
-rw-r--r--libfdisk/src/fdiskP.h2
-rw-r--r--libfdisk/src/libfdisk.h.in5
-rw-r--r--libfdisk/src/libfdisk.sym2
5 files changed, 61 insertions, 0 deletions
diff --git a/libfdisk/docs/libfdisk-sections.txt b/libfdisk/docs/libfdisk-sections.txt
index ecc37caf4..4b9d92e18 100644
--- a/libfdisk/docs/libfdisk-sections.txt
+++ b/libfdisk/docs/libfdisk-sections.txt
@@ -300,7 +300,9 @@ fdisk_disable_dialogs
fdisk_get_alignment_offset
fdisk_get_collision
fdisk_get_devfd
+fdisk_get_devmodel
fdisk_get_devname
+fdisk_get_devno
fdisk_get_disklabel_item
fdisk_get_first_lba
fdisk_get_geom_cylinders
diff --git a/libfdisk/src/context.c b/libfdisk/src/context.c
index 779a9a889..ad5f99de4 100644
--- a/libfdisk/src/context.c
+++ b/libfdisk/src/context.c
@@ -112,6 +112,10 @@ static int init_nested_from_parent(struct fdisk_context *cxt, int isnew)
cxt->protect_bootbits = parent->protect_bootbits;
}
+ free(cxt->dev_model);
+ cxt->dev_model = NULL;
+ cxt->dev_model_probed = 0;
+
free(cxt->dev_path);
cxt->dev_path = NULL;
@@ -541,6 +545,10 @@ static void reset_context(struct fdisk_context *cxt)
free(cxt->dev_path);
cxt->dev_path = NULL;
+ free(cxt->dev_model);
+ cxt->dev_model = NULL;
+ cxt->dev_model_probed = 0;
+
free(cxt->collision);
cxt->collision = NULL;
@@ -1326,6 +1334,48 @@ const char *fdisk_get_devname(struct fdisk_context *cxt)
}
/**
+ * fdisk_get_devno:
+ * @cxt: context
+ *
+ * Returns: device number or zero for non-block devices
+ */
+dev_t fdisk_get_devno(struct fdisk_context *cxt)
+{
+ assert(cxt);
+ return S_ISBLK(cxt->dev_st.st_mode) ? cxt->dev_st.st_rdev : 0;
+}
+
+/**
+ * fdisk_get_devmodel:
+ * @cxt: context
+ *
+ * Returns: device model string or NULL.
+ */
+const char *fdisk_get_devmodel(struct fdisk_context *cxt)
+{
+#ifdef __linux__
+ assert(cxt);
+
+ if (cxt->dev_model_probed)
+ return cxt->dev_model;
+
+ if (fdisk_get_devno(cxt)) {
+ struct path_cxt *pc = ul_new_sysfs_path(fdisk_get_devno(cxt), NULL, NULL);
+
+ if (pc) {
+ ul_path_read_string(pc, &cxt->dev_model, "device/model");
+ ul_unref_path(pc);
+ }
+ }
+ cxt->dev_model_probed = 1;
+ return cxt->dev_model;
+#else
+ return NULL;
+#endif
+
+}
+
+/**
* fdisk_get_devfd:
* @cxt: context
*
diff --git a/libfdisk/src/fdiskP.h b/libfdisk/src/fdiskP.h
index bf2f60e08..19c1c7953 100644
--- a/libfdisk/src/fdiskP.h
+++ b/libfdisk/src/fdiskP.h
@@ -365,6 +365,7 @@ struct fdisk_ask {
struct fdisk_context {
int dev_fd; /* device descriptor */
char *dev_path; /* device path */
+ char *dev_model; /* on linux /sys/block/<name>/device/model or NULL */
struct stat dev_st; /* stat(2) result */
int refcount;
@@ -387,6 +388,7 @@ struct fdisk_context {
protect_bootbits : 1, /* don't zeroize first sector */
pt_collision : 1, /* another PT detected by libblkid */
no_disalogs : 1, /* disable dialog-driven partititoning */
+ dev_model_probed : 1, /* tried to read from sys */
listonly : 1; /* list partition, nothing else */
char *collision; /* name of already existing FS/PT */
diff --git a/libfdisk/src/libfdisk.h.in b/libfdisk/src/libfdisk.h.in
index 0f8ecd96f..6f5188d3f 100644
--- a/libfdisk/src/libfdisk.h.in
+++ b/libfdisk/src/libfdisk.h.in
@@ -28,6 +28,7 @@ extern "C" {
#include <stdio.h>
#include <stdarg.h>
#include <stdint.h>
+#include <sys/types.h>
/**
* LIBFDISK_VERSION:
@@ -231,8 +232,12 @@ fdisk_sector_t fdisk_set_first_lba(struct fdisk_context *cxt, fdisk_sector_t lba
fdisk_sector_t fdisk_get_last_lba(struct fdisk_context *cxt);
fdisk_sector_t fdisk_set_last_lba(struct fdisk_context *cxt, fdisk_sector_t lba);
fdisk_sector_t fdisk_get_nsectors(struct fdisk_context *cxt);
+
const char *fdisk_get_devname(struct fdisk_context *cxt);
int fdisk_get_devfd(struct fdisk_context *cxt);
+dev_t fdisk_get_devno(struct fdisk_context *cxt);
+const char *fdisk_get_devmodel(struct fdisk_context *cxt);
+
unsigned int fdisk_get_geom_heads(struct fdisk_context *cxt);
fdisk_sector_t fdisk_get_geom_sectors(struct fdisk_context *cxt);
diff --git a/libfdisk/src/libfdisk.sym b/libfdisk/src/libfdisk.sym
index c9f35bebb..8f80d7964 100644
--- a/libfdisk/src/libfdisk.sym
+++ b/libfdisk/src/libfdisk.sym
@@ -300,4 +300,6 @@ FDISK_2.32 {
FDISK_2.33 {
fdisk_ask_number_is_wrap_negative;
+ fdisk_get_devmodel;
+ fdisk_get_devno;
} FDISK_2.32;