Git-Url: 
http://git.frugalware.org/gitweb/gitweb.cgi?p=fwsetup-ng.git;a=commitdiff;h=bbec93a3668c4d6631c189906c5cda0f038e47af

commit bbec93a3668c4d6631c189906c5cda0f038e47af
Author: James Buren <[email protected]>
Date:   Sat Aug 18 16:58:03 2012 -0500

device: add physical disk detection

diff --git a/Device.cc b/Device.cc
index 24ae029..43574ef 100644
--- a/Device.cc
+++ b/Device.cc
@@ -3,8 +3,63 @@
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
+#include <string.h>
+#include <linux/major.h>
#include "Device.hh"

+#define VIRTBLK_MAJOR 253
+
+static inline bool isIdeDisk(const struct stat &st)
+{
+  switch(major(st.st_rdev))
+  {
+    case IDE0_MAJOR:
+    case IDE1_MAJOR:
+    case IDE2_MAJOR:
+    case IDE3_MAJOR:
+    case IDE4_MAJOR:
+    case IDE5_MAJOR:
+    case IDE6_MAJOR:
+    case IDE7_MAJOR:
+    case IDE8_MAJOR:
+    case IDE9_MAJOR:
+      return true;
+    default:
+      return false;
+  }
+}
+
+static inline bool isScsiDisk(const struct stat &st)
+{
+  switch(major(st.st_rdev))
+  {
+    case SCSI_DISK0_MAJOR:
+    case SCSI_DISK1_MAJOR:
+    case SCSI_DISK2_MAJOR:
+    case SCSI_DISK3_MAJOR:
+    case SCSI_DISK4_MAJOR:
+    case SCSI_DISK5_MAJOR:
+    case SCSI_DISK6_MAJOR:
+    case SCSI_DISK7_MAJOR:
+    case SCSI_DISK8_MAJOR:
+    case SCSI_DISK9_MAJOR:
+    case SCSI_DISK10_MAJOR:
+    case SCSI_DISK11_MAJOR:
+    case SCSI_DISK12_MAJOR:
+    case SCSI_DISK13_MAJOR:
+    case SCSI_DISK14_MAJOR:
+    case SCSI_DISK15_MAJOR:
+      return true;
+    default:
+      return false;
+  }
+}
+
+static inline bool isVirtioDisk(const struct stat &st)
+{
+  return (major(st.st_rdev) == VIRTBLK_MAJOR);
+}
+
Device::Device()
{
_path = "";
@@ -16,6 +71,8 @@ Device::Device()
_alignratio = 0;

_sectors = 0;
+
+  _disk = false;
}

Device::~Device()
@@ -32,8 +89,12 @@ bool Device::read(const string &path)
unsigned long long alignratio = 0;
blkid_loff_t size = 0;
unsigned long long sectors = 0;
+  struct stat st;
+  bool disk = false;
bool rv = false;

+  memset(&st,0,sizeof(struct stat));
+
// Open file descriptor as read-only.
if((fd = open(path.c_str(),O_RDONLY)) == -1)
goto bail;
@@ -60,10 +121,20 @@ bool Device::read(const string &path)

sectors = size / lsectorsize;

-  // Now, some sanity checks on the topology data.
+  // Now, perform some sanity checks on the topology data.
if(lsectorsize == 0 || psectorsize == 0 || (psectorsize % lsectorsize) != 0 || 
size <= 0 || (size % lsectorsize) != 0)
goto bail;

+  // Read the device's stats.
+  if(fstat(fd,&st) == -1)
+    goto bail;
+
+  // Is it a physical disk?
+  if(isIdeDisk(st) || isScsiDisk(st) || isVirtioDisk(st))
+    disk = true;
+  else
+    goto bail;
+
// Now, assign the details we've assembled.

_path = path;
@@ -73,9 +144,11 @@ bool Device::read(const string &path)
_psectorsize = psectorsize;

_alignratio = alignratio;
-
+
_sectors = sectors;

+  _disk = disk;
+
bail:

if(fd != -1)
diff --git a/Device.hh b/Device.hh
index 7ab737b..790f2cc 100644
--- a/Device.hh
+++ b/Device.hh
@@ -18,6 +18,7 @@ private:
unsigned long long _psectorsize;
unsigned long long _alignratio;
unsigned long long _sectors;
+  bool _disk;

};
_______________________________________________
Frugalware-git mailing list
[email protected]
http://frugalware.org/mailman/listinfo/frugalware-git

Reply via email to