/
commandtool.cpp
375 lines (342 loc) · 9.11 KB
/
commandtool.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#include "commandtool.h"
#include "util.h"
#include <QMap>
#include <QProcess>
#include <QFile>
#include <QDir>
#include <QDebug>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <mntent.h>
#define COMMAND_PARTED "parted"
#define COMMAND_MKFSEXT4 "mkfs.ext4"
#define COMMAND_UMOUNT "umount"
#define COMMAND_MOUNT "mount"
#define FSTAB_FILE "/etc/fstab"
commandtool::commandtool()
{
}
int commandtool::create_part(const blockdevice *block, qulonglong start, qulonglong end)
{
QString result;
QStringList args;
args.clear();
args << block->device << "-s" << "mklabel" << "gpt" << "mkpart" << "primary" << "ext4";
if (start == 0 && end == 0)
{
args << "0%" << "100%"; //
}
else
{
args << QString("%1").arg(start) << QString("%1").arg(end);
}
//args << "/sbin/parted" << dev << "unit" << "B" << "mkpart" << partName << "ext4" << QString("%1").arg(offset) << QString("%1").arg(end);
return exec(COMMAND_PARTED, args, result);
}
int commandtool::remove_part(const blockdevice *block)
{
QString result;
QStringList args;
args.clear();
args << block->drive << "-s" << "rm" << QString("%1").arg(block->partnum);
return exec(COMMAND_PARTED, args, result);
}
int commandtool::format_part(const blockdevice *block)
{
int ret = umount_part(block);
if (ret == -1)
{
util::log(QString("umount error when format device %1. code %2").arg(block->device).arg(ret));
return -1;
}
QString result;
QStringList args;
args << block->device;
int format_timeout = 1800000;
int r, trys;
trys = 0;
while (true)
{
r = exec(COMMAND_MKFSEXT4, args, result, format_timeout);
if (r != 0)
{
sleep(5);
if (++trys >= 3)
break;
//try again
}
else
{
break;
}
}
return (r == 0) ? 0 : -1;
}
int commandtool::mount_part(const blockdevice *block, QString mount_point, QString uid, QString gid)
{
int r, trys;
QString result;
QStringList args;
args << block->device << mount_point;
trys = 0;
int usr_id = uid.toInt();
int grp_id = getgid();
if (!gid.isEmpty())
{
grp_id = gid.toInt();
}
QDir mnt(mount_point);
if (!mnt.exists())
{
mnt.mkdir(mount_point);
}
while (true)
{
r = exec(COMMAND_MOUNT, args, result);
if (r == 32) //mount failure
{
sleep(2);
if (++trys > 8)
{
break;
}
}
else{
break;
}
}
r = chown(mount_point.toAscii().data(), usr_id, grp_id);
if(r < 0)
{
util::log(QString("chown %1 to uid=%2 gid=%3 error %3").arg(mount_point).arg(usr_id).arg(grp_id).arg(errno));
}
return (r == 0) ? 0 : -1;
}
int commandtool::umount_part(const blockdevice *block)
{
QMap<QString, dfentry> entries;
df(entries);
QMap<QString,dfentry>::const_iterator i = entries.constFind(block->device);
if (i == entries.constEnd())
{
util::log(QString("device %1 already umounted").arg(block->device));
return 0;
}
QString result;
QStringList args;
args << block->device;
int r, trys;
trys = 0;
while (true)
{
r = exec(COMMAND_UMOUNT, args, result);
if (r != 0)
{
sleep(1);
if (++trys >= 3)
break;
//try again
}
else
{
break;
}
}
return (r == 0) ? 0 : -1;
}
int commandtool::deautomnt_part(const blockdevice *block)
{
const char *fstab_tmp = "/etc/fstab~";
struct mntent *m1 = NULL;
FILE *f1= NULL,*f2 = NULL;
f1 = setmntent(FSTAB_FILE, "r");
f2 = setmntent(fstab_tmp, "w+");
if (f1 == NULL || f2 == NULL)
{
util::log(QString("deautomount %1 error %2").arg(block->device).arg(errno));
return -1;
}
while ((m1 = getmntent(f1)))
{
QString fsname = QString(m1->mnt_fsname);
bool has = false;
foreach(const QString& symlink, block->symlinks)
{
if (fsname == symlink)
{
has = true;
qDebug() <<symlink;
break;
}
}
if (!has)
{
addmntent(f2,m1);
}
}
endmntent(f1);
endmntent(f2);
rename(fstab_tmp, FSTAB_FILE);
return 0;
}
int commandtool::automnt_part(const blockdevice *block, QString mount_point)
{
struct mntent *mnt;
FILE *fp;
fp = setmntent(FSTAB_FILE, "r");
if (fp == NULL)
{
util::log(QString("automount %1 error %2").arg(block->device).arg(errno));
return -1;
}
while ((mnt = getmntent(fp)))
{
QString fsname = QString(mnt->mnt_fsname);
foreach(const QString& symlink, block->symlinks)
{
if (fsname == symlink)
{
endmntent(fp);
return 0; //already exist;
}
}
}
endmntent(fp);
//add mmntent
QString device_path = QString("%1%2").arg(DEVICE_PATH_PREFIX).arg(block->id);
bool found = false;
foreach(const QString& symlink, block->symlinks)
{
if (symlink == device_path)
{
found = true;
break;
}
}
if (!found){
util::log(QString("automount %1 at can not found in %2 symbol links").arg(device_path).arg(block->device));
}
struct mntent *m = (mntent*)calloc(1,sizeof(mntent));
m->mnt_fsname = strdup(device_path.toAscii().data());
m->mnt_dir = strdup(mount_point.toAscii().data());
m->mnt_type = strdup("ext4");
m->mnt_opts = strdup("defaults,nofail,user");
m->mnt_passno = 2;
m->mnt_freq = 0;
util::log(QString("mnt=%1 fsname=%2").arg(mount_point).arg(device_path));
fp = setmntent(FSTAB_FILE,"a+"); //open file for describing the mounted filesystems
addmntent(fp,m);
endmntent(fp);
free(m->mnt_dir);
free(m->mnt_fsname);
free(m->mnt_type);
free(m->mnt_opts);
free(m);
return 0;
}
int commandtool::df(QMap<QString, dfentry> &entries)
{
entries.clear();
QString result;
QStringList args;
int r = exec("df", args, result);
if (r != 0)
{
util::log(QString("execute df error %d").arg(r));
return r;
}
QStringList ps = result.split('\n',QString::SkipEmptyParts);
foreach (const QString& string, ps)
{
QStringList fields = string.split(' ',QString::SkipEmptyParts);
if (fields.size() < 6)
continue;
dfentry entry;
entry.dev = fields.at(0);
entry.used = fields.at(2).toULongLong()*1024;
entry.avail = fields.at(3).toULongLong()*1024;
entry.mountpoint =fields.at(5);
entries[entry.dev]= entry;
}
return r;
}
int commandtool::dport(QMap<QString, int> &ports)
{
ports.clear();
QString result;
QStringList args;
int r = exec("./grdport", args, result);
if (r != 0)
{
return r;
}
QStringList portlist = result.split("\n",QString::SkipEmptyParts);
foreach(QString portline, portlist)
{
QStringList portpair = portline.split(" ");
if (portpair.size() == 2)
{
QString dev = portpair.last();
if (!dev.startsWith("/dev/"))
{
dev = "/dev/" + dev;
}
ports[dev] = portpair.first().toInt();
}
}
return r;
}
bool commandtool::lookup_command(QString command, QString &path)
{
QStringList search_path;
search_path << "/sbin/" <<"/bin/" << "/usr/bin/" << "/usr/sbin/";
QString full_path;
foreach(QString p, search_path)
{
full_path = p + command;
QFile file(full_path);
if(file.exists())
{
path = full_path;
return true;
}
}
return false;
}
int commandtool::exec(QString command, QStringList args, QString &result, int timeout)
{
QProcess *proc = new QProcess();
QString full_path_cmd;
if (!command.startsWith("./") && !lookup_command(command, full_path_cmd))
{
util::log(QString("Can not found %1").arg(command));
full_path_cmd = command;
}
proc->start(full_path_cmd, args);
if (!proc->waitForStarted())
{
util::log(QString("start %1 error args=%2").arg(command).arg(args.join("|")));
proc->deleteLater();
return -1;
}
if (!proc->waitForFinished(timeout))
{
util::log(QString("stop %1 error args=%2").arg(full_path_cmd).arg(args.join("|")));
proc->deleteLater();
return -1;
}
int exitCode = proc->exitCode();
result = QString(proc->readAll());
proc->close();
proc->deleteLater();
if (exitCode != 0)
{
util::log(QString("exec %1 error args=%2 ret=%3 result=%4").arg(full_path_cmd).arg(args.join("|")).arg(exitCode).arg(result));
}
if (g_option.verbose)
{
util::log(QString("exec %1 args=%2 ret=%3 result=%4").arg(full_path_cmd).arg(args.join("|")).arg(exitCode).arg(result));
}
return exitCode;
}