From: Michael Tremer Date: Sat, 4 Dec 2010 14:26:45 +0000 (+0100) Subject: Add detection for harddisk serial number. X-Git-Tag: v0.6~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3f70e7fdeaf9475dedf8de16b57a9082ea5b9fa4;p=oddments%2Ffireinfo.git Add detection for harddisk serial number. --- diff --git a/fireinfo/system.py b/fireinfo/system.py index bf2fb43..f8202e0 100644 --- a/fireinfo/system.py +++ b/fireinfo/system.py @@ -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), diff --git a/src/fireinfo.c b/src/fireinfo.c index d98844f..d295e65 100644 --- a/src/fireinfo.c +++ b/src/fireinfo.c @@ -19,7 +19,10 @@ #include +#include +#include #include +#include /* 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 } };