Added support for showing the disk's model name under Linux.

This commit is contained in:
Rod Smith
2017-07-26 19:45:51 -04:00
parent fc0e014bea
commit 7dfc896734
7 changed files with 32 additions and 6 deletions

11
NEWS
View File

@@ -1,11 +1,18 @@
1.0.2 (?/??/2017): 1.0.2 (7/26/2017):
------------------ ------------------
- On Linux, the p/-p/--print command now shows the disk's model name, as
reported in /sys/block/sda/device/model (or equivalent filenames for other
disks). This feature does not yet work on other platforms, on which the
model name line is omitted from the output. This line is also not shown
when accessing disk image files, even on Linux.
- GPT fdisk can now report both the physical and logical sector sizes of - GPT fdisk can now report both the physical and logical sector sizes of
disks, but only on 2.6.32 and later Linux kernels. The verify feature now disks, but only on 2.6.32 and later Linux kernels. The verify feature now
uses the larger of the set alignment and physical/logical block sizes for uses the larger of the set alignment and physical/logical block sizes for
testing alignment, and setting alignment to something other than an exact testing alignment, and setting alignment to something other than an exact
multiple of the physical/logical block size results in a warning. multiple of the ratio of the physical to logical block size results in a
warning.
- Addition of new verification checks, mostly (but not exclusively) related - Addition of new verification checks, mostly (but not exclusively) related
to the new j/-j/--move-main-table option. to the new j/-j/--move-main-table option.

View File

@@ -30,6 +30,8 @@
#endif #endif
#include <iostream> #include <iostream>
#include <fstream>
#include <sstream>
#include "diskio.h" #include "diskio.h"
@@ -66,6 +68,7 @@ int DiskIO::OpenForRead(void) {
cerr << "The specified file does not exist!\n"; cerr << "The specified file does not exist!\n";
realFilename = ""; realFilename = "";
userFilename = ""; userFilename = "";
modelName = "";
isOpen = 0; isOpen = 0;
openForWrite = 0; openForWrite = 0;
} else { } else {
@@ -86,6 +89,16 @@ int DiskIO::OpenForRead(void) {
else else
isOpen = 1; isOpen = 1;
} // if (fstat64()...) } // if (fstat64()...)
#if defined(__linux__) && !defined(EFI)
if (isOpen && realFilename.substr(0,4) == "/dev") {
ostringstream modelNameFilename;
modelNameFilename << "/sys/block" << realFilename.substr(4,512) << "/device/model";
ifstream modelNameFile(modelNameFilename.str().c_str());
if (modelNameFile.is_open()) {
getline(modelNameFile, modelName);
} // if
} // if
#endif
} // if/else } // if/else
} // if } // if

View File

@@ -147,7 +147,7 @@ int DiskIO::GetBlockSize(void) {
// In theory, returns the physical block size. In practice, this is only // In theory, returns the physical block size. In practice, this is only
// supported in Linux, as of yet. // supported in Linux, as of yet.
// TODO: Get this working in Windows. // TODO: Get this working in Windows.
int DiskIO:GetPhysBlockSize(void) { int DiskIO::GetPhysBlockSize(void) {
return 0; return 0;
} // DiskIO::GetPhysBlockSize() } // DiskIO::GetPhysBlockSize()

View File

@@ -41,6 +41,7 @@ using namespace std;
DiskIO::DiskIO(void) { DiskIO::DiskIO(void) {
userFilename = ""; userFilename = "";
realFilename = ""; realFilename = "";
modelName = "";
isOpen = 0; isOpen = 0;
openForWrite = 0; openForWrite = 0;
} // constructor } // constructor

View File

@@ -49,6 +49,7 @@ class DiskIO {
protected: protected:
string userFilename; string userFilename;
string realFilename; string realFilename;
string modelName;
int isOpen; int isOpen;
int openForWrite; int openForWrite;
#ifdef _WIN32 #ifdef _WIN32
@@ -72,6 +73,7 @@ class DiskIO {
int DiskSync(void); // resync disk caches to use new partitions int DiskSync(void); // resync disk caches to use new partitions
int GetBlockSize(void); int GetBlockSize(void);
int GetPhysBlockSize(void); int GetPhysBlockSize(void);
string GetModel(void) {return modelName;}
uint32_t GetNumHeads(void); uint32_t GetNumHeads(void);
uint32_t GetNumSecsPerTrack(void); uint32_t GetNumSecsPerTrack(void);
int IsOpen(void) {return isOpen;} int IsOpen(void) {return isOpen;}

7
gpt.cc
View File

@@ -1486,9 +1486,12 @@ void GPTData::DisplayGPTData(void) {
cout << "Disk " << device << ": " << diskSize << " sectors, " cout << "Disk " << device << ": " << diskSize << " sectors, "
<< BytesToIeee(diskSize, blockSize) << "\n"; << BytesToIeee(diskSize, blockSize) << "\n";
cout << "Logical sector size: " << blockSize << " bytes\n"; if (myDisk.GetModel() != "")
cout << "Model: " << myDisk.GetModel() << "\n";
if (physBlockSize > 0) if (physBlockSize > 0)
cout << "Physical sector size: " << physBlockSize << " bytes\n"; cout << "Sector size (logical/physical): " << blockSize << "/" << physBlockSize << " bytes\n";
else
cout << "Sector size (logical): " << blockSize << " bytes\n";
cout << "Disk identifier (GUID): " << mainHeader.diskGUID << "\n"; cout << "Disk identifier (GUID): " << mainHeader.diskGUID << "\n";
cout << "Partition table holds up to " << numParts << " entries\n"; cout << "Partition table holds up to " << numParts << " entries\n";
cout << "Main partition table begins at sector " << mainHeader.partitionEntriesLBA cout << "Main partition table begins at sector " << mainHeader.partitionEntriesLBA

View File

@@ -8,7 +8,7 @@
#ifndef __GPTSUPPORT #ifndef __GPTSUPPORT
#define __GPTSUPPORT #define __GPTSUPPORT
#define GPTFDISK_VERSION "1.0.1.1" #define GPTFDISK_VERSION "1.0.2"
#if defined (__FreeBSD__) || defined (__FreeBSD_kernel__) || defined (__APPLE__) #if defined (__FreeBSD__) || defined (__FreeBSD_kernel__) || defined (__APPLE__)
// Darwin (Mac OS) & FreeBSD: disk IOCTLs are different, and there is no lseek64 // Darwin (Mac OS) & FreeBSD: disk IOCTLs are different, and there is no lseek64