This repository has been archived by the owner on Sep 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInstallManager_Utility.cpp
370 lines (308 loc) · 11.5 KB
/
InstallManager_Utility.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
//
// Created by Frank Steiler on 9/14/16 as part of NOOBS4IoT (https://github.com/steilerDev/NOOBS4IoT)
//
// InstallManager_Utility.cpp:
// This file contains the supporting functions for installing Operation System(s).
// For more information see https://github.com/steilerDev/NOOBS4IoT/wiki.
//
// This file is based on several files from the NOOBS project (c) 2013, Raspberry Pi All rights reserved.
// See https://github.com/raspberrypi/noobs for more information.
//
// This file is licensed under a GNU General Public License v3.0 (c) Frank Steiler.
// See https://raw.githubusercontent.com/steilerDev/NOOBS4IoT/master/LICENSE for more information.
//
#include <QProcess>
#include "InstallManager.h"
#include "Utility.h"
#include "libs/easylogging++.h"
#include "BootManager.h"
#include "Utility.h"
#include <QDir>
#include <QDebug>
#include <QProcess>
#include <QSettings>
#include <QTime>
bool InstallManager::writePartitionTable() {
/* Write partition table using sfdisk */
/* Fixed NOOBS partition */
int startP1 = Utility::Sys::getFileContents("/sys/class/block/mmcblk0p1/start").trimmed().toInt();
int sizeP1 = Utility::Sys::getFileContents("/sys/class/block/mmcblk0p1/size").trimmed().toInt();
/* Fixed start of extended partition. End is not fixed, as it depends on primary partition 3 & 4 */
int startExtended = startP1+sizeP1;
/* Fixed settings partition */
int startP5 = Utility::Sys::getFileContents("/sys/class/block/mmcblk0p5/start").trimmed().toInt();
int sizeP5 = Utility::Sys::getFileContents("/sys/class/block/mmcblk0p5/size").trimmed().toInt();
if (!startP1 || !sizeP1 || !startP5 || !sizeP5) {
LFATAL << "Error reading existing partition table";
return false;
}
/* Clone partition map, and add our system partitions to it */
QMap<int, PartitionInfo *> partitionMap(_partitionMap);
partitionMap.insert(1, new PartitionInfo(1, startP1, sizeP1, "0E")); /* FAT boot partition */
partitionMap.insert(5, new PartitionInfo(5, startP5, sizeP5, "L")); /* Ext4 settings partition */
int sizeExtended = partitionMap.values().last()->endSector() - startExtended;
if (!partitionMap.contains(2)) {
partitionMap.insert(2, new PartitionInfo(2, startExtended, sizeExtended, "E"));
} else {
/* If an OS already claimed primary partition 2, use out-of-order partitions, and store extended at partition 4 */
partitionMap.insert(4, new PartitionInfo(4, startExtended, sizeExtended, "E"));
}
/* Add partitions */
LDEBUG << "Printing partition map (size " << partitionMap.size() << ")";
foreach(PartitionInfo* partition, partitionMap.values()) {
LDEBUG << "Found partition: " << partition->label().constData();
LDEBUG << " Offset: " << partition->offset();
LDEBUG << " Size sector: " << partition->partitionSizeSectors();
LDEBUG << " Type: " << partition->partitionType().constData();
LDEBUG << " Number: " << partitionMap.key(partition);
}
QByteArray partitionTable;
for (int i=1; i <= partitionMap.keys().last(); i++) {
if (partitionMap.contains(i)) {
PartitionInfo *p = partitionMap.value(i);
partitionTable += QByteArray::number(p->offset())+","+QByteArray::number(p->partitionSizeSectors())+","+p->partitionType();
if (p->active()) {
partitionTable += " *";
}
partitionTable += "\n";
} else {
partitionTable += "0,0\n";
}
}
LDEBUG << "Partition map information gathered";
LDEBUG << "Unmounting all partitions";
/* Unmount everything before modifying partition table */
Utility::Sys::unmountPartition(SYSTEMS_DIR);
Utility::Sys::unmountPartition(SETTINGS_DIR);
LDEBUG << "Writing partition table using sfdisk";
/* Let sfdisk write a proper partition table */
QProcess proc;
proc.setProcessChannelMode(proc.MergedChannels);
proc.start("/sbin/sfdisk -uS /dev/mmcblk0");
proc.write(partitionTable);
proc.closeWriteChannel();
proc.waitForFinished(-1);
LDEBUG << "sfdisk done, output " << proc.readAll().constData();
::sync();
usleep(500000);
LDEBUG << "Doing partprobe";
QProcess::execute("/usr/sbin/partprobe");
usleep(500000);
/* Remount */
Utility::Sys::mountSystemsPartition();
Utility::Sys::mountSettingsPartition();
if (proc.exitCode() != 0) {
LFATAL << "Error creating partition table (exit code: " << proc.exitCode() << "): " << proc.readAll().constData();
return false;
} else {
return true;
}
}
bool InstallManager::mkfs(const QByteArray &device, const QByteArray &fstype, const QByteArray &label, const QByteArray &mkfsopt) {
QString cmd;
if (fstype == "fat" || fstype == "FAT") {
cmd = "/sbin/mkfs.fat ";
if (!label.isEmpty()) {
cmd += "-n "+label+" ";
}
} else if (fstype == "ext4") {
cmd = "/usr/sbin/mkfs.ext4 ";
if (!label.isEmpty()) {
cmd += "-L "+label+" ";
}
} else if (fstype == "ntfs") {
cmd = "/sbin/mkfs.ntfs --fast ";
if (!label.isEmpty()) {
cmd += "-L "+label+" ";
}
}
if (!mkfsopt.isEmpty()) {
cmd += mkfsopt+" ";
}
cmd += device;
LDEBUG << "Executing:" << cmd.toUtf8().constData();
QProcess p;
p.setProcessChannelMode(p.MergedChannels);
p.start(cmd);
p.closeWriteChannel();
p.waitForFinished(-1);
if (p.exitCode() != 0) {
LFATAL << "Error creating file system: " << p.readAll().constData();
return false;
} else {
return true;
}
}
bool InstallManager::isLabelAvailable(const QByteArray &label) {
return (QProcess::execute("/sbin/findfs LABEL="+label) != 0);
}
bool InstallManager::untar(const QString &tarball) {
QString cmd = "sh -o pipefail -c \"";
if (isURL(tarball)) {
cmd += "wget --no-verbose --tries=inf -O- "+tarball+" | ";
}
if (tarball.endsWith(".gz")) {
cmd += "gzip -dc";
} else if (tarball.endsWith(".xz")) {
cmd += "xz -dc";
} else if (tarball.endsWith(".bz2")) {
cmd += "bzip2 -dc";
} else if (tarball.endsWith(".lzo")) {
cmd += "lzop -dc";
} else if (tarball.endsWith(".zip")) {
/* Note: the image must be the only file inside the .zip */
cmd += "unzip -p";
} else {
LFATAL << "Unknown compression format file extension. Expecting .lzo, .gz, .xz, .bz2 or .zip";
return false;
}
if (!isURL(tarball)) {
cmd += " "+tarball;
}
cmd += " | tar x -C /mnt2 ";
cmd += "\"";
QTime t1;
t1.start();
LDEBUG << "Executing:" << cmd.toUtf8().constData();
QProcess p;
p.setProcessChannelMode(p.MergedChannels);
p.start(cmd);
p.closeWriteChannel();
p.waitForFinished(-1);
if (p.exitCode() != 0) {
LFATAL << "Error downloading or extracting tarball: " << p.readAll().constData();
return false;
} else {
LDEBUG << "Finished writing filesystem in " << (t1.elapsed()/1000.0) << " seconds";
return true;
}
}
bool InstallManager::dd(const QString &imagePath, const QString &device) {
QString cmd = "sh -o pipefail -c \"";
if (isURL(imagePath)) {
cmd += "wget --no-verbose --tries=inf -O- "+imagePath+" | ";
}
if (imagePath.endsWith(".gz")) {
cmd += "gzip -dc";
} else if (imagePath.endsWith(".xz")) {
cmd += "xz -dc";
} else if (imagePath.endsWith(".bz2")) {
cmd += "bzip2 -dc";
} else if (imagePath.endsWith(".lzo")) {
cmd += "lzop -dc";
} else if (imagePath.endsWith(".zip")) {
/* Note: the image must be the only file inside the .zip */
cmd += "unzip -p";
} else {
LFATAL << "Unknown compression format file extension. Expecting .lzo, .gz, .xz, .bz2 or .zip";
return false;
}
if (!isURL(imagePath)) {
cmd += " "+imagePath;
}
cmd += " | dd of="+device+" conv=fsync obs=4M\"";
QTime t1;
t1.start();
LDEBUG << "Executing:" << cmd.toUtf8().constData();
QProcess p;
p.setProcessChannelMode(p.MergedChannels);
p.start(cmd);
p.closeWriteChannel();
p.waitForFinished(-1);
if (p.exitCode() != 0) {
LFATAL << "Error downloading or writing OS to SD card: " << p.readAll().constData();
return false;
} else {
LDEBUG << "Finished writing filesystem in " << (t1.elapsed() / 1000.0) << " seconds";
return true;
}
}
bool InstallManager::partclone_restore(const QString &imagePath, const QString &device) {
QString cmd = "sh -o pipefail -c \"";
if (isURL(imagePath)) {
cmd += "wget --no-verbose --tries=inf -O- "+imagePath+" | ";
}
if (imagePath.endsWith(".gz")) {
cmd += "gzip -dc";
} else if (imagePath.endsWith(".xz")) {
cmd += "xz -dc";
} else if (imagePath.endsWith(".bz2")) {
cmd += "bzip2 -dc";
} else if (imagePath.endsWith(".lzo")) {
cmd += "lzop -dc";
} else if (imagePath.endsWith(".zip")) {
/* Note: the image must be the only file inside the .zip */
cmd += "unzip -p";
} else {
LFATAL << "Unknown compression format file extension. Expecting .lzo, .gz, .xz, .bz2 or .zip";
return false;
}
if (!isURL(imagePath)) {
cmd += " "+imagePath;
}
cmd += " | partclone.restore -q -s - -o "+device+" \"";
QTime t1;
t1.start();
LDEBUG << "Executing:" << cmd.toUtf8().constData();
QProcess p;
p.setProcessChannelMode(p.MergedChannels);
p.start(cmd);
p.closeWriteChannel();
p.waitForFinished(-1);
if (p.exitCode() != 0) {
LFATAL << "Error downloading or writing OS to SD card" << p.readAll().constData();
return false;
} else {
LDEBUG << "Finished writing filesystem in " << (t1.elapsed()/1000.0) << " seconds";
}
return true;
}
void InstallManager::patchConfigTxt() {
QSettings settings("/settings/noobs.conf", QSettings::IniFormat);
int videomode = settings.value("display_mode", 0).toInt();
QByteArray dispOptions;
switch (videomode) {
case 0: /* HDMI PREFERRED */
dispOptions = "hdmi_force_hotplug=1\r\n";
break;
case 1: /* HDMI VGA */
dispOptions = "hdmi_ignore_edid=0xa5000080\r\nhdmi_force_hotplug=1\r\nhdmi_group=2\r\nhdmi_mode=4\r\n";
break;
case 2: /* PAL */
dispOptions = "hdmi_ignore_hotplug=1\r\nsdtv_mode=2\r\n";
break;
case 3: /* NTSC */
dispOptions = "hdmi_ignore_hotplug=1\r\nsdtv_mode=0\r\n";
break;
default:
LWARNING << "Reached default case in patchConfigTxt, this should not happen";
break;
}
QFile f("/mnt2/config.txt");
f.open(f.Append);
f.write("\r\n# NOOBS Auto-generated Settings:\r\n"+dispOptions);
f.close();
}
QByteArray InstallManager::getLabel(const QString part) {
QByteArray result;
QProcess p;
p.start("/sbin/blkid -s LABEL -o value "+part);
p.waitForFinished();
if (p.exitCode() == 0) {
result = p.readAll().trimmed();
}
return result;
}
QByteArray InstallManager::getUUID(const QString part) {
QByteArray result;
QProcess p;
p.start("/sbin/blkid -s UUID -o value "+part);
p.waitForFinished();
if (p.exitCode() == 0) {
result = p.readAll().trimmed();
}
return result;
}
bool InstallManager::isURL(const QString &s) {
return s.startsWith("http:") || s.startsWith("https:");
}