Good time of day, list.
I try to fix "Fixme" in svg.c:
/* FIXME: this works only in the simple case */

By default function try to get root=/dev/*
I write small function to determine block device name by specified LABEL, UUID, PARTUUID.

Please check code, its working, but i think it can look more pretty.
May be i missed(reimplemented) some internal functional of systemd?

I also attach patch to email. Thanks.

From 896ef1c040a6395375e3adcd230bf2b766db633c Mon Sep 17 00:00:00 2001
From: Timofey Titovets <nefelim...@gmail.com>
Date: Tue, 28 Oct 2014 03:17:40 +0300
Subject: [PATCH] Bootchart: allow parse LABEL, UUID, PARTUUID for svg info

---
src/bootchart/svg.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 54 insertions(+), 4 deletions(-)

diff --git a/src/bootchart/svg.c b/src/bootchart/svg.c
index faf377e..a074b71 100644
--- a/src/bootchart/svg.c
+++ b/src/bootchart/svg.c
@@ -32,6 +32,7 @@
 #include <sys/utsname.h>
 #include <sys/stat.h>
 #include <fcntl.h>
+#include <dirent.h>

 #include "util.h"
 #include "macro.h"
@@ -151,6 +152,57 @@ static void svg_header(void) {
         svg("    ]]>\n   </style>\n</defs>\n\n");
 }

+static void get_root_disk(const char *cmdline, char *rootbdev) {
+        char *ptr = strstr(cmdline, "root=");
+        char path[PATH_MAX]="";
+        char dev_path[PATH_MAX]="";
+        DIR *d;
+        struct dirent *dir;
+
+        if (!ptr)
+                goto out;
+
+        ptr = &ptr[5];
+        if (ptr[0] == '/') {
+                ptr = &ptr[5];
+                strncpy(rootbdev, ptr, 3);
+                rootbdev[3] = '\0';
+                return;
+        }
+
+        d = opendir("/dev/disk/");
+        if (!d)
+                goto out;
+
+        if (!strncmp("LABEL", ptr, 5))
+                strncpy(path, "/dev/disk/by-label/", 30);
+        else if (!strncmp("UUID", ptr, 4))
+                strncpy(path, "/dev/disk/by-uuid/", 30);
+        else if (!strncmp("PARTUUID", ptr, 8))
+                strncpy(path, "/dev/disk/by-partuuid/", 30);
+
+        ptr = strstr(ptr, "=");
+        ptr++;
+        ptr = strtok(ptr," ");
+
+        d = opendir(path);
+
+        while ((dir = readdir(d)) != NULL) {
+                if (!strncmp(dir->d_name, ptr, 32)) {
+                        break;
+                }
+        }
+
+        strcat(path, ptr);
+        readlink (path, dev_path, sizeof(dev_path));
+        ptr = &dev_path[6];
+        strncpy(rootbdev, ptr, 3);
+        rootbdev[3] = '\0';
+
+out:
+        closedir(d);
+}
+
 static void svg_title(const char *build) {
         char cmdline[256] = "";
         char filename[PATH_MAX];
@@ -175,11 +227,9 @@ static void svg_title(const char *build) {
         }

         /* extract root fs so we can find disk model name in sysfs */
-        /* FIXME: this works only in the simple case */
-        c = strstr(cmdline, "root=/dev/");
+        c = strstr(cmdline, "root=");
         if (c) {
-                strncpy(rootbdev, &c[10], 3);
-                rootbdev[3] = '\0';
+                get_root_disk(cmdline, rootbdev);
                 sprintf(filename, "block/%s/device/model", rootbdev);
                 fd = openat(sysfd, filename, O_RDONLY);
                 f = fdopen(fd, "r");
--
2.1.2

>From 896ef1c040a6395375e3adcd230bf2b766db633c Mon Sep 17 00:00:00 2001
From: Timofey Titovets <nefelim...@gmail.com>
Date: Tue, 28 Oct 2014 03:17:40 +0300
Subject: [PATCH] Bootchart: allow parse LABEL, UUID, PARTUUID for svg info

---
 src/bootchart/svg.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 54 insertions(+), 4 deletions(-)

diff --git a/src/bootchart/svg.c b/src/bootchart/svg.c
index faf377e..a074b71 100644
--- a/src/bootchart/svg.c
+++ b/src/bootchart/svg.c
@@ -32,6 +32,7 @@
 #include <sys/utsname.h>
 #include <sys/stat.h>
 #include <fcntl.h>
+#include <dirent.h>
 
 #include "util.h"
 #include "macro.h"
@@ -151,6 +152,57 @@ static void svg_header(void) {
         svg("    ]]>\n   </style>\n</defs>\n\n");
 }
 
+static void get_root_disk(const char *cmdline, char *rootbdev) {
+        char *ptr = strstr(cmdline, "root=");
+        char path[PATH_MAX]="";
+        char dev_path[PATH_MAX]="";
+        DIR *d;
+        struct dirent *dir;
+
+        if (!ptr)
+                goto out;
+
+        ptr = &ptr[5];
+        if (ptr[0] == '/') {
+                ptr = &ptr[5];
+                strncpy(rootbdev, ptr, 3);
+                rootbdev[3] = '\0';
+                return;
+        }
+
+        d = opendir("/dev/disk/");
+        if (!d)
+                goto out;
+
+        if (!strncmp("LABEL", ptr, 5))
+                strncpy(path, "/dev/disk/by-label/", 30);
+        else if (!strncmp("UUID", ptr, 4))
+                strncpy(path, "/dev/disk/by-uuid/", 30);
+        else if (!strncmp("PARTUUID", ptr, 8))
+                strncpy(path, "/dev/disk/by-partuuid/", 30);
+
+        ptr = strstr(ptr, "=");
+        ptr++;
+        ptr = strtok(ptr," ");
+
+        d = opendir(path);
+
+        while ((dir = readdir(d)) != NULL) {
+                if (!strncmp(dir->d_name, ptr, 32)) {
+                        break;
+                }
+        }
+
+        strcat(path, ptr);
+        readlink (path, dev_path, sizeof(dev_path));
+        ptr = &dev_path[6];
+        strncpy(rootbdev, ptr, 3);
+        rootbdev[3] = '\0';
+
+out:
+        closedir(d);
+}
+
 static void svg_title(const char *build) {
         char cmdline[256] = "";
         char filename[PATH_MAX];
@@ -175,11 +227,9 @@ static void svg_title(const char *build) {
         }
 
         /* extract root fs so we can find disk model name in sysfs */
-        /* FIXME: this works only in the simple case */
-        c = strstr(cmdline, "root=/dev/");
+        c = strstr(cmdline, "root=");
         if (c) {
-                strncpy(rootbdev, &c[10], 3);
-                rootbdev[3] = '\0';
+                get_root_disk(cmdline, rootbdev);
                 sprintf(filename, "block/%s/device/model", rootbdev);
                 fd = openat(sysfd, filename, O_RDONLY);
                 f = fdopen(fd, "r");
-- 
2.1.2

_______________________________________________
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/systemd-devel

Reply via email to