]> git.ipfire.org Git - oddments/fireinfo.git/commitdiff
Add detection for harddisk serial number.
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 4 Dec 2010 14:26:45 +0000 (15:26 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 4 Dec 2010 14:26:45 +0000 (15:26 +0100)
fireinfo/system.py
src/fireinfo.c

index bf2fb433276711f75fda9801fae8cc2fdf8f4e27..f8202e068600378071dc77d9eb47a504de8ce957 100644 (file)
@@ -5,6 +5,8 @@ import json
 import os
 import string
 
+import _fireinfo
+
 import cpu
 import device
 import hypervisor
@@ -172,6 +174,11 @@ class System(object):
                        id = read_from_file(os.path.join(SYS_CLASS_DMI, file)) or ""
                        ids.append(id)
 
+               # Use serial number from root disk (if available)
+               root_disk_serial = self.root_disk_serial
+               if root_disk_serial:
+                       ids.append(root_disk_serial)
+
                # As last resort, we use the UUID from pakfire.
                if not ids:
                        id = read_from_file("/opt/pakfire/db/uuid") or ""
@@ -248,6 +255,10 @@ class System(object):
                        return
                with open(path, "r") as f:
                        return int(f.readline())*512/1024
+
+       @property
+       def root_disk_serial(self):
+               return _fireinfo.get_harddisk_serial("/dev/%s" % self.root_disk).rstrip()
                                        
        def scan(self):
                toscan = (("/sys/bus/pci/devices", device.PCIDevice),
index d98844f4d041e899a21b76fc7cf9d276592904b5..d295e65d7bc746925dbd09bbc78163bfb07c2ef6 100644 (file)
 
 #include <Python.h>
 
+#include <fcntl.h>
+#include <linux/hdreg.h>
 #include <stdbool.h>
+#include <sys/ioctl.h>
 
 /*
        Big parts of this were taken from
@@ -241,6 +244,20 @@ read_hypervisor(struct hypervisor_desc *desc)
        }
 }
 
+static void
+read_harddisk_serial(char *device, char *serial) {
+       static struct hd_driveid hd;
+       int fd;
+
+       if ((fd = open(device, O_RDONLY | O_NONBLOCK)) < 0) {
+           return;
+       }
+
+       if (!ioctl(fd, HDIO_GET_IDENTITY, &hd)) {
+           strncpy(serial, (const char *)hd.serial_no, sizeof(serial));
+       }
+}
+
 static bool
 is_virtualized() {
        unsigned int eax, ebx, ecx, edx;
@@ -301,9 +318,31 @@ do_is_virtualized() {
        return Py_False;
 }
 
+static PyObject *
+do_get_harddisk_serial(PyObject *o, PyObject *args) {
+       /*
+               Python wrapper around read_harddisk_serial.
+       */
+
+       char serial[21];
+       memset(serial, 0, sizeof(serial));
+
+       char *device;
+       if (!PyArg_ParseTuple(args, "s", &device))
+               return NULL;
+
+       read_harddisk_serial(device, serial);
+
+       if (serial[0])
+               return PyString_FromString(serial);
+
+       return Py_None;
+}
+
 static PyMethodDef fireinfoModuleMethods[] = {
        { "get_hypervisor", (PyCFunction) do_get_hypervisor, METH_NOARGS, NULL },
        { "is_virtualized", (PyCFunction) do_is_virtualized, METH_NOARGS, NULL },
+       { "get_harddisk_serial", (PyCFunction) do_get_harddisk_serial, METH_VARARGS, NULL },
        { NULL, NULL, 0, NULL }
 };