-
Notifications
You must be signed in to change notification settings - Fork 0
/
nvme_sn_tool.cpp
116 lines (97 loc) · 2.51 KB
/
nvme_sn_tool.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "nvme_util.h"
#include <iostream>
#include <unistd.h>
#include <vector>
using namespace std;
int getNVMEIDInfo(std::string deviceStr);
const char * format_char_array(char * str, int strsize, const char * chr, int chrsize);
template<size_t STRSIZE, size_t CHRSIZE>
inline const char * format_char_array(char (& str)[STRSIZE], const char (& chr)[CHRSIZE])
{ return format_char_array(str, (int)STRSIZE, chr, (int)CHRSIZE); }
const char *format_char_array(char *str, int strsize, const char *chr, int chrsize)
{
int b = 0;
while (b < chrsize && chr[b] == ' ')
b++;
int n = 0;
while (b + n < chrsize && chr[b + n])
n++;
while (n > 0 && chr[b + n - 1] == ' ')
n--;
if (n >= strsize)
n = strsize - 1;
for (int i = 0; i < n; i++)
{
char c = chr[b + i];
str[i] = (' ' <= c && c <= '~' ? c : '?');
}
str[n] = 0;
return str;
}
std::string exec(const char* cmd) {
char buffer[128];
std::string result = "";
FILE* pipe = popen(cmd, "r");
if (!pipe) throw std::runtime_error("popen() failed!");
try {
while (fgets(buffer, sizeof buffer, pipe) != NULL) {
result += buffer;
}
} catch (...) {
pclose(pipe);
throw;
}
pclose(pipe);
return result;
}
int getNVMEIDInfo(string deviceStr)
{
nvme_Device * nvmeDev;
nvmeDev = new nvme_Device(deviceStr.c_str(), "", 0);
int fd = nvmeDev->nvmeOpen();
if (fd < 0)
return -1;
char buf[64];
nvme_Device::nvme_id_ctrl id_ctrl;
const char *model_str_byte;
const char *serialno_str_byte;
const char *fwrev_str_byte;
nvmeDev->nvme_read_id_ctrl(id_ctrl);
cout << "---------- Disk Information ----------" << endl;
model_str_byte = format_char_array(buf, id_ctrl.mn);
cout << "Model\t\t\t: " + std::string(model_str_byte) << endl;
fwrev_str_byte = format_char_array(buf, id_ctrl.fr);
cout << "FW Version\t\t: " + std::string(fwrev_str_byte) << endl;
serialno_str_byte = format_char_array(buf, id_ctrl.sn);
cout << "Serial No\t\t: " + std::string(serialno_str_byte) << endl;
::close(fd);
return 0;
}
void showGuide()
{
cout << "Usage:" << endl;
cout << " " << "nvme_sn_tool <nvme_device>" << endl;
}
int main(int argc, char *argv[])
{
std::vector<std::string > args;
for (int i = 0; i < argc; i++)
{
args.push_back(argv[i]);
}
if (argc == 2){
if (args[1].find("nvme") != std::string::npos)
{
int resultGetDevice = getNVMEIDInfo(args[1]);
if(resultGetDevice == -1){
cout << args[1] << " Device not found. " << endl;
}
}
else{
showGuide();
}
}
else{
showGuide();
}
}