This repository has been archived by the owner on Feb 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 434
/
multiimagewritethread.cpp
1046 lines (903 loc) · 32.6 KB
/
multiimagewritethread.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "multiimagewritethread.h"
#include "config.h"
#include "json.h"
#include "util.h"
#include "mbr.h"
#include "osinfo.h"
#include "partitioninfo.h"
#include <QDir>
#include <QFile>
#include <QDebug>
#include <QProcess>
#include <QProcessEnvironment>
#include <QSettings>
#include <QTime>
#include <unistd.h>
#include <linux/fs.h>
#include <linux/magic.h>
#include <sys/statfs.h>
#include <sys/ioctl.h>
#include <QtEndian>
MultiImageWriteThread::MultiImageWriteThread(const QString &bootdrive, const QString &rootdrive, QObject *parent) :
QThread(parent), _drive(rootdrive), _bootdrive(bootdrive), _extraSpacePerPartition(0), _part(5)
{
QDir dir;
_multiDrives = (bootdrive != rootdrive);
if (!dir.exists("/mnt2"))
dir.mkdir("/mnt2");
}
void MultiImageWriteThread::addImage(const QString &folder, const QString &flavour)
{
_images.append(new OsInfo(folder, flavour, this));
//_images.insert(folder, flavour);
}
void MultiImageWriteThread::run()
{
/* Calculate space requirements, and check special requirements */
uint totalnominalsize = 0, totaluncompressedsize = 0, numparts = 0, numexpandparts = 0;
uint startSector = getFileContents(sysclassblock(_drive, 5)+"/start").trimmed().toUInt()
+ getFileContents(sysclassblock(_drive, 5)+"/size").trimmed().toUInt();
uint totalSectors = getFileContents(sysclassblock(_drive)+"/size").trimmed().toUInt();
uint availableMB = (totalSectors-startSector)/2048;
/* key: partition number, value: partition information */
QMap<int, PartitionInfo *> partitionMap, bootPartitionMap;
foreach (OsInfo *image, _images)
{
QList<PartitionInfo *> *partitions = image->partitions();
if (partitions->isEmpty())
{
emit error(tr("partitions.json invalid"));
return;
}
if (nameMatchesRiscOS( image->folder() ))
{
/* Check the riscos_offset in os.json matches what we're expecting.
In theory we shouldn't hit either of these errors because the invalid RISC_OS
should have been filtered out already (not added to OS-list) in mainwindow.cpp */
if (image->riscosOffset())
{
if (image->riscosOffset() != RISCOS_OFFSET)
{
emit error(tr("RISCOS cannot be installed. RISCOS offset value mismatch."));
return;
}
}
else
{
emit error(tr("RISCOS cannot be installed. RISCOS offset value missing."));
return;
}
if (startSector > RISCOS_SECTOR_OFFSET-2048)
{
emit error(tr("RISCOS cannot be installed. Size of recovery partition too large."));
return;
}
totalnominalsize += (RISCOS_SECTOR_OFFSET - startSector)/2048;
partitions->first()->setRequiresPartitionNumber(6);
partitions->first()->setOffset(RISCOS_SECTOR_OFFSET);
partitions->last()->setRequiresPartitionNumber(7);
}
foreach (PartitionInfo *partition, *partitions)
{
numparts++;
if ( partition->wantMaximised() )
numexpandparts++;
totalnominalsize += partition->partitionSizeNominal();
totaluncompressedsize += partition->uncompressedTarballSize();
if (partition->fsType() == "ext4")
{
totaluncompressedsize += /*0.035*/ 0.01 * totalnominalsize; /* overhead for file system meta data */
}
int reqPart = partition->requiresPartitionNumber();
if (reqPart)
{
if (partitionMap.contains(reqPart))
{
emit error(tr("More than one operating system requires partition number %1").arg(reqPart));
return;
}
if (reqPart == 1 || reqPart == 5)
{
emit error(tr("Operating system cannot require a system partition (1,5)"));
return;
}
if ((reqPart == 2 && partitionMap.contains(4)) || (reqPart == 4 && partitionMap.contains(2)))
{
emit error(tr("Operating system cannot claim both primary partitions 2 and 4"));
return;
}
partition->setPartitionDevice(partdev(_drive, reqPart));
partitionMap.insert(reqPart, partition);
}
/* Maximum overhead per partition for alignment */
#ifdef SHRINK_PARTITIONS_TO_MINIMIZE_GAPS
if (partition->wantMaximised() || (partition->partitionSizeNominal()*2048) % PARTITION_ALIGNMENT != 0)
totalnominalsize += PARTITION_ALIGNMENT/2048;
#else
totalnominalsize += PARTITION_ALIGNMENT/2048;
#endif
}
}
if (numexpandparts)
{
/* Extra spare space available for partitions that want to be expanded */
_extraSpacePerPartition = (availableMB-totalnominalsize)/numexpandparts;
}
emit parsedImagesize(qint64(totaluncompressedsize)*1024*1024);
if (totalnominalsize > availableMB)
{
emit error(tr("Not enough disk space. Need %1 MB, got %2 MB").arg(QString::number(totalnominalsize), QString::number(availableMB)));
return;
}
/* Assign logical partition numbers to partitions that did not reserve a special number */
int pnr, bootpnr;
uint offset = 0;
if (partitionMap.isEmpty())
pnr = 6;
else
pnr = qMax(partitionMap.keys().last(), 5)+1;
if (_multiDrives)
{
bootpnr = 6;
offset = getFileContents(sysclassblock(_bootdrive, 5)+"/start").trimmed().toUInt()
+ getFileContents(sysclassblock(_bootdrive, 5)+"/size").trimmed().toUInt();
}
foreach (OsInfo *image, _images)
{
foreach (PartitionInfo *partition, *(image->partitions()))
{
if (!partition->requiresPartitionNumber())
{
if (_multiDrives && partition->bootable() && !partition->wantMaximised() )
{
bootPartitionMap.insert(bootpnr, partition);
partition->setPartitionDevice(partdev(_bootdrive, bootpnr));
bootpnr++;
offset += PARTITION_GAP;
/* Align at 4 MiB offset */
if (offset % PARTITION_ALIGNMENT != 0)
{
offset += PARTITION_ALIGNMENT-(offset % PARTITION_ALIGNMENT);
}
partition->setOffset(offset);
uint partsizeSectors = partition->partitionSizeNominal() * 2048;
partition->setPartitionSizeSectors(partsizeSectors);
offset += partsizeSectors;
}
else
{
partitionMap.insert(pnr, partition);
partition->setPartitionDevice(partdev(_drive, pnr));
pnr++;
}
}
}
}
/* Set partition starting sectors and sizes.
* First allocate space to all logical partitions, then to primary partitions */
QList<PartitionInfo *> log_before_prim = partitionMap.values();
if (!log_before_prim.isEmpty() && log_before_prim.first()->requiresPartitionNumber() == 2)
log_before_prim.push_back(log_before_prim.takeFirst());
if (!log_before_prim.isEmpty() && log_before_prim.first()->requiresPartitionNumber() == 3)
log_before_prim.push_back(log_before_prim.takeFirst());
if (!log_before_prim.isEmpty() && log_before_prim.first()->requiresPartitionNumber() == 4)
log_before_prim.push_back(log_before_prim.takeFirst());
offset = startSector;
foreach (PartitionInfo *p, log_before_prim)
{
if (p->offset()) /* OS wants its partition at a fixed offset */
{
if (p->offset() <= offset)
{
emit error(tr("Fixed partition offset too low"));
return;
}
offset = p->offset();
}
else
{
offset += PARTITION_GAP;
/* Align at 4 MiB offset */
if (offset % PARTITION_ALIGNMENT != 0)
{
offset += PARTITION_ALIGNMENT-(offset % PARTITION_ALIGNMENT);
}
p->setOffset(offset);
}
uint partsizeMB = p->partitionSizeNominal();
if ( p->wantMaximised() )
partsizeMB += _extraSpacePerPartition;
uint partsizeSectors = partsizeMB * 2048;
if (p == log_before_prim.last())
{
/* Let last partition have any remaining space that we couldn't divide evenly */
uint spaceleft = totalSectors - offset - partsizeSectors;
if (spaceleft > 0 && p->wantMaximised())
{
partsizeSectors += spaceleft;
}
}
else
{
#ifdef SHRINK_PARTITIONS_TO_MINIMIZE_GAPS
if (partsizeSectors % PARTITION_ALIGNMENT == 0 && p->fsType() != "raw")
{
/* Partition size is dividable by 4 MiB
Take off a couple sectors of the end of our partition to make room
for the EBR of the next partition, so the next partition can
align nicely without having a 4 MiB gap */
partsizeSectors -= PARTITION_GAP;
}
#endif
if (p->wantMaximised() && (partsizeSectors+PARTITION_GAP) % PARTITION_ALIGNMENT != 0)
{
/* Enlarge partition to close gap to next partition */
partsizeSectors += PARTITION_ALIGNMENT-((partsizeSectors+PARTITION_GAP) % PARTITION_ALIGNMENT);
}
}
p->setPartitionSizeSectors(partsizeSectors);
offset += partsizeSectors;
}
/* Delete information about previously installed operating systems */
QFile f("/settings/installed_os.json");
if (f.exists())
f.remove();
emit statusUpdate(tr("Writing partition table"));
if (!writePartitionTable(_drive, partitionMap))
return;
/* Zero out first sector of partitions, to make sure to get rid of previous file system (label) */
emit statusUpdate(tr("Zero'ing start of each partition"));
foreach (PartitionInfo *p, partitionMap.values())
{
if (p->partitionSizeSectors())
QProcess::execute("/bin/dd count=3 bs=512 if=/dev/zero of="+p->partitionDevice());
}
/* Write partition table to boot drive (if using multiple drives) */
if (_multiDrives)
{
emit statusUpdate(tr("Writing boot partition table"));
if (!writePartitionTable(_bootdrive, bootPartitionMap))
return;
/* Zero out first sector of partitions, to make sure to get rid of previous file system (label) */
emit statusUpdate(tr("Zero'ing start of each partition"));
foreach (PartitionInfo *p, bootPartitionMap.values())
{
if (p->partitionSizeSectors())
QProcess::execute("/bin/dd count=3 bs=512 if=/dev/zero of="+p->partitionDevice());
}
if (QProcess::execute("mount -t ext4 "+partdev(_bootdrive, SETTINGS_PARTNR)+" /mnt2") == 0)
{
QFile f("/mnt2/installed_os.json");
if (f.exists())
f.remove();
QProcess::execute("umount /mnt2");
}
}
/* Install each operating system */
foreach (OsInfo *image, _images)
{
if (!processImage(image))
return;
}
emit statusUpdate(tr("Finish writing (sync)"));
sync();
emit completed();
}
bool MultiImageWriteThread::writePartitionTable(const QString &drive, const QMap<int, PartitionInfo *> &pmap)
{
/* Write partition table using sfdisk */
/* Fixed NOOBS partition */
uint startP1 = getFileContents(sysclassblock(drive, 1)+"/start").trimmed().toUInt();
uint sizeP1 = getFileContents(sysclassblock(drive, 1)+"/size").trimmed().toUInt();
/* Fixed start of extended partition. End is not fixed, as it depends on primary partition 3 & 4 */
int startExtended = startP1+sizeP1;
/* Fixed settings partition */
uint startP5 = getFileContents(sysclassblock(drive, SETTINGS_PARTNR)+"/start").trimmed().toUInt();
uint sizeP5 = getFileContents(sysclassblock(drive, SETTINGS_PARTNR)+"/size").trimmed().toUInt();
if (!startP1 || !sizeP1 || !startP5 || !sizeP5)
{
emit error(tr("Error reading existing partition table"));
return false;
}
/* Clone partition map, and add our system partitions to it */
QMap<int, PartitionInfo *> partitionMap(pmap);
partitionMap.insert(1, new PartitionInfo(1, startP1, sizeP1, "0E", this)); /* FAT boot partition */
partitionMap.insert(5, new PartitionInfo(5, startP5, sizeP5, "L", this)); /* Ext4 settings partition */
uint sizeExtended = partitionMap.values().last()->endSector() - startExtended;
if (!partitionMap.contains(2))
{
partitionMap.insert(2, new PartitionInfo(2, startExtended, sizeExtended, "E", this));
}
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", this));
}
/* Add partitions */
qDebug() << "partition map:" << partitionMap;
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";
}
}
qDebug() << "New partition table:";
qDebug() << partitionTable;
/* Unmount everything before modifying partition table */
QString driveP1 = partdev(drive, 1).replace("/dev/", "");
if (drive == _bootdrive)
{
QProcess::execute("umount /mnt");
QProcess::execute("umount /settings");
}
if (QFile::exists("/tmp/media/"+driveP1))
{
QProcess::execute("umount /tmp/media/"+driveP1);
}
/* Let sfdisk write a proper partition table */
QProcess proc;
proc.setProcessChannelMode(proc.MergedChannels);
proc.start("/sbin/sfdisk -uS --force "+drive);
proc.write(partitionTable);
proc.closeWriteChannel();
proc.waitForFinished(-1);
qDebug() << "sfdisk done, output:" << proc.readAll();
::sync();
QThread::msleep(500);
QProcess::execute("/usr/sbin/partprobe");
QThread::msleep(500);
/* Remount */
if (drive == _bootdrive)
{
QProcess::execute("mount -o ro -t vfat "+partdev(drive, 1)+" /mnt");
QProcess::execute("mount -t ext4 "+partdev(drive, SETTINGS_PARTNR)+" /settings");
}
if (QFile::exists("/tmp/media/"+driveP1))
{
QProcess::execute("mount -o ro -t vfat /dev/"+driveP1+" /tmp/media/"+driveP1);
}
if (proc.exitCode() != 0)
{
emit error(tr("Error creating partition table")+"\n"+proc.readAll());
return false;
}
return true;
}
bool MultiImageWriteThread::processImage(OsInfo *image)
{
QString os_name = image->name();
qDebug() << "Processing OS:" << os_name;
QList<PartitionInfo *> *partitions = image->partitions();
foreach (PartitionInfo *p, *partitions)
{
QByteArray fstype = p->fsType();
QByteArray mkfsopt = p->mkfsOptions();
QByteArray label = p->label();
QString tarball = p->tarball();
bool emptyfs = p->emptyFS();
if (!emptyfs && tarball.isEmpty())
{
/* If no tarball URL is specified, we expect the tarball to reside in the folder and be named <label.tar.xz> */
if (fstype == "raw" || fstype.startsWith("partclone"))
tarball = image->folder()+"/"+label+".xz";
else
tarball = image->folder()+"/"+label+".tar.xz";
if (!QFile::exists(tarball))
{
emit error(tr("File '%1' does not exist").arg(tarball));
return false;
}
}
QByteArray partdevice = p->partitionDevice();
emit newDrive(partdevice);
if (fstype == "raw")
{
emit statusUpdate(tr("%1: Writing OS image").arg(os_name));
if (!dd(tarball, partdevice))
return false;
}
else if (fstype.startsWith("partclone"))
{
emit statusUpdate(tr("%1: Writing OS image").arg(os_name));
if (!partclone_restore(tarball, partdevice))
return false;
}
else if (fstype != "unformatted")
{
emit runningMKFS();
emit statusUpdate(tr("%1: Creating filesystem (%2)").arg(os_name, QString(fstype)));
if (!mkfs(partdevice, fstype, label, mkfsopt))
return false;
emit finishedMKFS();
if (!emptyfs)
{
emit statusUpdate(tr("%1: Mounting file system").arg(os_name));
QString mountcmd;
if (fstype == "ntfs")
mountcmd = "/sbin/mount.ntfs-3g ";
else
mountcmd = "mount ";
if (QProcess::execute(mountcmd+partdevice+" /mnt2") != 0)
{
emit error(tr("%1: Error mounting file system").arg(os_name));
return false;
}
if (tarball.startsWith("http"))
emit statusUpdate(tr("%1: Downloading and extracting filesystem").arg(os_name));
else
emit statusUpdate(tr("%1: Extracting filesystem").arg(os_name));
bool result = untar(tarball);
QProcess::execute("umount /mnt2");
if (!result)
return false;
}
}
_part++;
}
emit statusUpdate(tr("%1: Mounting FAT partition").arg(os_name));
if (QProcess::execute("mount "+partitions->first()->partitionDevice()+" /mnt2") != 0)
{
emit error(tr("%1: Error mounting file system").arg(os_name));
return false;
}
if (QFile::exists("/mnt/firmware.override"))
{
if (::system("cp /mnt/firmware.override/* /mnt2") != 0) { }
}
emit statusUpdate(tr("%1: Creating os_config.json").arg(os_name));
QString description = getDescription(image->folder(), image->flavour());
QVariantList vpartitions;
foreach (PartitionInfo *p, *partitions)
{
QString part = p->partitionDevice();
if (part.left(13) != "/dev/mmcblk0p")
{
part = getPartUUID(part);
}
vpartitions.append(part);
}
QSettings settings("/settings/noobs.conf", QSettings::IniFormat);
int videomode = settings.value("display_mode", 0).toInt();
QString language = settings.value("language", "en").toString();
QString keyboard = settings.value("keyboard_layout", "gb").toString();
QVariantMap qm;
qm.insert("flavour", image->flavour());
qm.insert("release_date", image->releaseDate());
qm.insert("imagefolder", image->folder());
qm.insert("description", description);
qm.insert("videomode", videomode);
qm.insert("partitions", vpartitions);
qm.insert("language", language);
qm.insert("keyboard", keyboard);
Json::saveToFile("/mnt2/os_config.json", qm);
emit statusUpdate(tr("%1: Saving display mode to config.txt").arg(os_name));
patchConfigTxt();
/* Partition setup script can either reside in the image folder
* or inside the boot partition tarball */
QString postInstallScript = image->folder()+"/partition_setup.sh";
if (!QFile::exists(postInstallScript))
postInstallScript = "/mnt2/partition_setup.sh";
if (QFile::exists(postInstallScript))
{
emit statusUpdate(tr("%1: Running partition setup script").arg(os_name));
QProcess proc;
QProcessEnvironment env;
QStringList args(postInstallScript);
env.insert("PATH", "/bin:/usr/bin:/sbin:/usr/sbin");
/* - Parameters to the partition-setup script are supplied both as
* command line parameters and set as environement variables
* - Boot partition is mounted, working directory is set to its mnt folder
*
* partition_setup.sh part1=/dev/mmcblk0p3 id1=LABEL=BOOT part2=/dev/mmcblk0p4
* id2=UUID=550e8400-e29b-41d4-a716-446655440000
*/
int pnr = 1;
foreach (PartitionInfo *p, *partitions)
{
QString part = p->partitionDevice();
QString nr = QString::number(pnr);
QString uuid = getUUID(part);
QString label = getLabel(part);
QString partuuid = getPartUUID(part);
QString id;
if (!label.isEmpty())
id = "LABEL="+label;
else
id = "UUID="+uuid;
if (_drive != "/dev/mmcblk0")
part = partuuid;
qDebug() << "part" << part << uuid << label;
args << "part"+nr+"="+part << "id"+nr+"="+id;
env.insert("part"+nr, part);
env.insert("id"+nr, id);
env.insert("partuuid"+nr, partuuid);
pnr++;
}
qDebug() << "Executing: sh" << args;
qDebug() << "Env:" << env.toStringList();
proc.setProcessChannelMode(proc.MergedChannels);
proc.setProcessEnvironment(env);
proc.setWorkingDirectory("/mnt2");
proc.start("/bin/sh", args);
proc.waitForFinished(-1);
qDebug() << proc.exitStatus();
if (proc.exitCode() != 0)
{
emit error(tr("%1: Error executing partition setup script").arg(os_name)+"\n"+proc.readAll());
return false;
}
}
emit statusUpdate(tr("%1: Unmounting FAT partition").arg(os_name));
if (QProcess::execute("umount /mnt2") != 0)
{
emit error(tr("%1: Error unmounting").arg(os_name));
}
/* Save information about installed operating systems in installed_os.json */
QVariantMap ventry;
ventry["name"] = image->flavour();
ventry["description"] = description;
ventry["folder"] = image->folder();
ventry["release_date"]= image->releaseDate();
ventry["partitions"] = vpartitions;
ventry["bootable"] = image->bootable();
if (!image->supportedModels().isEmpty())
ventry["supported_models"] = image->supportedModels();
QString iconfilename = image->folder()+"/"+image->flavour()+".png";
iconfilename.replace(" ", "_");
if (QFile::exists(iconfilename))
{
if (iconfilename.startsWith("/tmp/media/"))
{
/* Copy icon to settings folder, as USB storage may take longer to get ready on boot */
QDir dir;
QString dirname = "/settings/os/"+image->flavour().replace(" ", "_");
dir.mkpath(dirname);
QFile::copy(iconfilename, dirname+"/icon.png");
iconfilename = dirname+"/icon.png";
}
ventry["icon"] = iconfilename;
}
else if (QFile::exists(image->folder()+"/icon.png"))
ventry["icon"] = image->folder()+"/icon.png";
installed_os.append(ventry);
Json::saveToFile("/settings/installed_os.json", installed_os);
return true;
}
QString MultiImageWriteThread::shorten(QString example, int maxLabelLen)
{
QString test;
if (example.size()<=maxLabelLen)
{
return(example);
}
example.replace("_","#");
example.replace("-","#");
example.replace(" ","#");
QStringList parts = example.split("#", QString::SkipEmptyParts);
int numParts = qMin(3, parts.count());
int r;
int len;
int l1,l2;
int rem;
switch (numParts)
{
case 3:
len=parts.last().size();
r=qMin((maxLabelLen-4),len);
rem = maxLabelLen -r-2;
l2 = rem/2;
l1 = rem-l2;
test= parts.first().left(l1)+"_"+parts.at(1).right(l2)+"_"+parts.last().left(r);
break;
case 2:
len=parts.last().size();
r=qMin(maxLabelLen-2, len);
test = parts.first().left(maxLabelLen-r-1) + "_" + parts.last().left(r);
break;
default:
test = parts.first();
test=test.left(maxLabelLen);
break;
}
return(test);
}
QByteArray MultiImageWriteThread::makeLabelUnique(QByteArray label, int maxLabelLen)
{
if (label.size() > maxLabelLen)
{ //restrict to maximum size
label = shorten(label, maxLabelLen).toAscii();
}
if (!isLabelAvailable(label))
{
if (label.size() == maxLabelLen)
{ //Make room for extra digit
label = label.left(maxLabelLen-1);
}
for (int i=0; i<10; i++)
{
if (isLabelAvailable(label+QByteArray::number(i)))
{
label = label+QByteArray::number(i);
return(label);
}
}
//Let's add some more now that we can have 56 OSes on a USB installed!
for (char c='A'; c<='Z'; c++)
{
if (isLabelAvailable(label+c))
{
label = label+c;
return(label);
}
}
//No hope if we get to here
label="";
}
return (label);
}
bool MultiImageWriteThread::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 "+makeLabelUnique(label, 11)+" ";
}
}
else if (fstype == "ext4")
{
cmd = "/usr/sbin/mkfs.ext4 ";
if (!label.isEmpty())
{
cmd += "-L "+makeLabelUnique(label, 16)+" ";
}
}
else if (fstype == "ntfs")
{
cmd = "/sbin/mkfs.ntfs --fast ";
if (!label.isEmpty())
{
cmd += "-L "+makeLabelUnique(label, 32)+" ";
}
}
if (!mkfsopt.isEmpty())
cmd += mkfsopt+" ";
cmd += device;
qDebug() << "Executing:" << cmd;
QProcess p;
p.setProcessChannelMode(p.MergedChannels);
p.start(cmd);
p.closeWriteChannel();
p.waitForFinished(-1);
if (p.exitCode() != 0)
{
emit error(tr("Error creating file system")+"\n"+p.readAll());
return false;
}
return true;
}
bool MultiImageWriteThread::isLabelAvailable(const QByteArray &label)
{
return (QProcess::execute("/sbin/findfs LABEL="+label) != 0);
}
bool MultiImageWriteThread::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
{
emit error(tr("Unknown compression format file extension. Expecting .lzo, .gz, .xz, .bz2 or .zip"));
return false;
}
if (!isURL(tarball))
{
cmd += " "+tarball;
}
cmd += " | bsdtar -xf - -C /mnt2 ";
struct statfs st;
if (statfs("/mnt2", &st) == 0 && st.f_type == MSDOS_SUPER_MAGIC)
{
/* File system does not support uid/gid, tell bsdtar not to set those or it will error out */
cmd += " --no-same-owner ";
}
cmd += "\"";
QTime t1;
t1.start();
qDebug() << "Executing:" << cmd;
QProcess p;
p.setProcessChannelMode(p.MergedChannels);
p.start(cmd);
p.closeWriteChannel();
p.waitForFinished(-1);
if (p.exitCode() != 0)
{
QByteArray msg = p.readAll();
qDebug() << msg;
emit error(tr("Error downloading or extracting tarball")+"\n"+msg);
return false;
}
qDebug() << "finished writing filesystem in" << (t1.elapsed()/1000.0) << "seconds";
return true;
}
bool MultiImageWriteThread::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
{
emit error(tr("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();
qDebug() << "Executing:" << cmd;
QProcess p;
p.setProcessChannelMode(p.MergedChannels);
p.start(cmd);
p.closeWriteChannel();
p.waitForFinished(-1);
if (p.exitCode() != 0)
{
emit error(tr("Error downloading or writing OS to SD card")+"\n"+p.readAll());
return false;
}
qDebug() << "finished writing filesystem in" << (t1.elapsed()/1000.0) << "seconds";
return true;
}
bool MultiImageWriteThread::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
{
emit error(tr("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();
qDebug() << "Executing:" << cmd;
QProcess p;
p.setProcessChannelMode(p.MergedChannels);
p.start(cmd);
p.closeWriteChannel();
p.waitForFinished(-1);
if (p.exitCode() != 0)
{
emit error(tr("Error downloading or writing OS to SD card")+"\n"+p.readAll());
return false;
}
qDebug() << "finished writing filesystem in" << (t1.elapsed()/1000.0) << "seconds";
return true;
}
void MultiImageWriteThread::patchConfigTxt()
{
QSettings settings("/settings/noobs.conf", QSettings::IniFormat);
int videomode = settings.value("display_mode", 0).toInt();
QByteArray dispOptions;
switch (videomode)
{
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 */