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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
|
/* $Id: UnattendedInstaller.cpp $ */
/** @file
* UnattendedInstaller class and it's descendants implementation
*/
/*
* Copyright (C) 2006-2022 Oracle and/or its affiliates.
*
* This file is part of VirtualBox base platform packages, as
* available from https://www.virtualbox.org.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, in version 3 of the
* License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses>.
*
* SPDX-License-Identifier: GPL-3.0-only
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#define LOG_GROUP LOG_GROUP_MAIN_UNATTENDED
#include "LoggingNew.h"
#include "VirtualBoxBase.h"
#include "VirtualBoxErrorInfoImpl.h"
#include "AutoCaller.h"
#include <VBox/com/ErrorInfo.h>
#include "UnattendedImpl.h"
#include "UnattendedInstaller.h"
#include "UnattendedScript.h"
#include <VBox/err.h>
#include <iprt/ctype.h>
#include <iprt/fsisomaker.h>
#include <iprt/fsvfs.h>
#include <iprt/getopt.h>
#include <iprt/file.h>
#include <iprt/path.h>
#include <iprt/stream.h>
#include <iprt/vfs.h>
#ifdef RT_OS_SOLARIS
# undef ES /* Workaround for someone dragging the namespace pollutor sys/regset.h. Sigh. */
#endif
#include <iprt/formats/iso9660.h>
#include <iprt/cpp/path.h>
using namespace std;
/* static */ UnattendedInstaller *
UnattendedInstaller::createInstance(VBOXOSTYPE enmDetectedOSType, const Utf8Str &strDetectedOSType,
const Utf8Str &strDetectedOSVersion, const Utf8Str &strDetectedOSFlavor,
const Utf8Str &strDetectedOSHints, Unattended *pParent)
{
UnattendedInstaller *pUinstaller = NULL;
if (strDetectedOSType.find("Windows") != RTCString::npos)
{
if (enmDetectedOSType >= VBOXOSTYPE_WinVista)
pUinstaller = new UnattendedWindowsXmlInstaller(pParent);
else
pUinstaller = new UnattendedWindowsSifInstaller(pParent);
}
else if (enmDetectedOSType >= VBOXOSTYPE_OS2 && enmDetectedOSType < VBOXOSTYPE_Linux)
pUinstaller = new UnattendedOs2Installer(pParent, strDetectedOSHints);
else
{
if (enmDetectedOSType >= VBOXOSTYPE_Debian && enmDetectedOSType <= VBOXOSTYPE_Debian_latest_x64)
pUinstaller = new UnattendedDebianInstaller(pParent);
else if (enmDetectedOSType >= VBOXOSTYPE_Ubuntu && enmDetectedOSType <= VBOXOSTYPE_Ubuntu_latest_x64)
pUinstaller = new UnattendedUbuntuInstaller(pParent);
else if (enmDetectedOSType >= VBOXOSTYPE_RedHat && enmDetectedOSType <= VBOXOSTYPE_RedHat_latest_x64)
{
if (RTStrVersionCompare(strDetectedOSVersion.c_str(), "8") >= 0)
pUinstaller = new UnattendedRhel8Installer(pParent);
else if (RTStrVersionCompare(strDetectedOSVersion.c_str(), "7") >= 0)
pUinstaller = new UnattendedRhel7Installer(pParent);
else if (RTStrVersionCompare(strDetectedOSVersion.c_str(), "6") >= 0)
pUinstaller = new UnattendedRhel6Installer(pParent);
else if (RTStrVersionCompare(strDetectedOSVersion.c_str(), "5") >= 0)
pUinstaller = new UnattendedRhel5Installer(pParent);
else if (RTStrVersionCompare(strDetectedOSVersion.c_str(), "4") >= 0)
pUinstaller = new UnattendedRhel4Installer(pParent);
else if (RTStrVersionCompare(strDetectedOSVersion.c_str(), "3") >= 0)
pUinstaller = new UnattendedRhel3Installer(pParent);
else
pUinstaller = new UnattendedRhel6Installer(pParent);
}
else if (enmDetectedOSType >= VBOXOSTYPE_FedoraCore && enmDetectedOSType <= VBOXOSTYPE_FedoraCore_x64)
pUinstaller = new UnattendedFedoraInstaller(pParent);
else if (enmDetectedOSType >= VBOXOSTYPE_Oracle && enmDetectedOSType <= VBOXOSTYPE_Oracle_latest_x64)
{
if (RTStrVersionCompare(strDetectedOSVersion.c_str(), "8") >= 0)
pUinstaller = new UnattendedOracleLinux8Installer(pParent);
else if (RTStrVersionCompare(strDetectedOSVersion.c_str(), "7") >= 0)
pUinstaller = new UnattendedOracleLinux7Installer(pParent);
else if (RTStrVersionCompare(strDetectedOSVersion.c_str(), "6") >= 0)
pUinstaller = new UnattendedOracleLinux6Installer(pParent);
else
pUinstaller = new UnattendedOracleLinux6Installer(pParent);
}
else if (enmDetectedOSType >= VBOXOSTYPE_FreeBSD && enmDetectedOSType <= VBOXOSTYPE_FreeBSD_x64)
pUinstaller = new UnattendedFreeBsdInstaller(pParent);
#if 0 /* doesn't work, so convert later. */
else if (enmDetectedOSType == VBOXOSTYPE_OpenSUSE || enmDetectedOSType == VBOXOSTYPE_OpenSUSE_x64)
pUinstaller = new UnattendedSuseInstaller(new UnattendedSUSEXMLScript(pParent), pParent);
#endif
}
RT_NOREF_PV(strDetectedOSFlavor);
RT_NOREF_PV(strDetectedOSHints);
return pUinstaller;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
/*
*
*
* Implementation Unattended functions
*
*/
//////////////////////////////////////////////////////////////////////////////////////////////////////
/*
*
* UnattendedInstaller public methods
*
*/
UnattendedInstaller::UnattendedInstaller(Unattended *pParent,
const char *pszMainScriptTemplateName, const char *pszPostScriptTemplateName,
const char *pszMainScriptFilename, const char *pszPostScriptFilename,
DeviceType_T enmBootDevice /*= DeviceType_DVD */)
: mMainScript(pParent, pszMainScriptTemplateName, pszMainScriptFilename)
, mPostScript(pParent, pszPostScriptTemplateName, pszPostScriptFilename)
, mpParent(pParent)
, meBootDevice(enmBootDevice)
{
AssertPtr(pParent);
Assert(*pszMainScriptTemplateName);
Assert(*pszMainScriptFilename);
Assert(*pszPostScriptTemplateName);
Assert(*pszPostScriptFilename);
Assert(enmBootDevice == DeviceType_DVD || enmBootDevice == DeviceType_Floppy);
}
UnattendedInstaller::~UnattendedInstaller()
{
mpParent = NULL;
}
HRESULT UnattendedInstaller::initInstaller()
{
/*
* Calculate the full main script template location.
*/
if (mpParent->i_getScriptTemplatePath().isNotEmpty())
mStrMainScriptTemplate = mpParent->i_getScriptTemplatePath();
else
{
int vrc = RTPathAppPrivateNoArchCxx(mStrMainScriptTemplate);
if (RT_SUCCESS(vrc))
vrc = RTPathAppendCxx(mStrMainScriptTemplate, "UnattendedTemplates");
if (RT_SUCCESS(vrc))
vrc = RTPathAppendCxx(mStrMainScriptTemplate, mMainScript.getDefaultTemplateFilename());
if (RT_FAILURE(vrc))
return mpParent->setErrorBoth(E_FAIL, vrc,
tr("Failed to construct path to the unattended installer script templates (%Rrc)"),
vrc);
}
/*
* Calculate the full post script template location.
*/
if (mpParent->i_getPostInstallScriptTemplatePath().isNotEmpty())
mStrPostScriptTemplate = mpParent->i_getPostInstallScriptTemplatePath();
else
{
int vrc = RTPathAppPrivateNoArchCxx(mStrPostScriptTemplate);
if (RT_SUCCESS(vrc))
vrc = RTPathAppendCxx(mStrPostScriptTemplate, "UnattendedTemplates");
if (RT_SUCCESS(vrc))
vrc = RTPathAppendCxx(mStrPostScriptTemplate, mPostScript.getDefaultTemplateFilename());
if (RT_FAILURE(vrc))
return mpParent->setErrorBoth(E_FAIL, vrc,
tr("Failed to construct path to the unattended installer script templates (%Rrc)"),
vrc);
}
/*
* Construct paths we need.
*/
if (isAuxiliaryFloppyNeeded())
{
mStrAuxiliaryFloppyFilePath = mpParent->i_getAuxiliaryBasePath();
mStrAuxiliaryFloppyFilePath.append("aux-floppy.img");
}
if (isAuxiliaryIsoNeeded())
{
mStrAuxiliaryIsoFilePath = mpParent->i_getAuxiliaryBasePath();
if (!isAuxiliaryIsoIsVISO())
mStrAuxiliaryIsoFilePath.append("aux-iso.iso");
else
mStrAuxiliaryIsoFilePath.append("aux-iso.viso");
}
/*
* Check that we've got the minimum of data available.
*/
if (mpParent->i_getIsoPath().isEmpty())
return mpParent->setError(E_INVALIDARG, tr("Cannot proceed with an empty installation ISO path"));
if (mpParent->i_getUser().isEmpty())
return mpParent->setError(E_INVALIDARG, tr("Empty user name is not allowed"));
if (mpParent->i_getPassword().isEmpty())
return mpParent->setError(E_INVALIDARG, tr("Empty password is not allowed"));
LogRelFunc(("UnattendedInstaller::savePassedData(): \n"));
return S_OK;
}
#if 0 /* Always in AUX ISO */
bool UnattendedInstaller::isAdditionsIsoNeeded() const
{
/* In the VISO case, we'll add the additions to the VISO in a subdir. */
return !isAuxiliaryIsoIsVISO() && mpParent->i_getInstallGuestAdditions();
}
bool UnattendedInstaller::isValidationKitIsoNeeded() const
{
/* In the VISO case, we'll add the validation kit to the VISO in a subdir. */
return !isAuxiliaryIsoIsVISO() && mpParent->i_getInstallTestExecService();
}
#endif
bool UnattendedInstaller::isAuxiliaryIsoNeeded() const
{
/* In the VISO case we use the AUX ISO for GAs and TXS. */
return isAuxiliaryIsoIsVISO()
&& ( mpParent->i_getInstallGuestAdditions()
|| mpParent->i_getInstallTestExecService());
}
HRESULT UnattendedInstaller::prepareUnattendedScripts()
{
LogFlow(("UnattendedInstaller::prepareUnattendedScripts()\n"));
/*
* The script template editor calls setError, so status codes just needs to
* be passed on to the caller. Do the same for both scripts.
*/
HRESULT hrc = mMainScript.read(getTemplateFilePath());
if (SUCCEEDED(hrc))
{
hrc = mMainScript.parse();
if (SUCCEEDED(hrc))
{
/* Ditto for the post script. */
hrc = mPostScript.read(getPostTemplateFilePath());
if (SUCCEEDED(hrc))
{
hrc = mPostScript.parse();
if (SUCCEEDED(hrc))
{
LogFlow(("UnattendedInstaller::prepareUnattendedScripts: returns S_OK\n"));
return S_OK;
}
LogFlow(("UnattendedInstaller::prepareUnattendedScripts: parse failed on post script (%Rhrc)\n", hrc));
}
else
LogFlow(("UnattendedInstaller::prepareUnattendedScripts: error reading post install script template file (%Rhrc)\n", hrc));
}
else
LogFlow(("UnattendedInstaller::prepareUnattendedScripts: parse failed (%Rhrc)\n", hrc));
}
else
LogFlow(("UnattendedInstaller::prepareUnattendedScripts: error reading installation script template file (%Rhrc)\n", hrc));
return hrc;
}
HRESULT UnattendedInstaller::prepareMedia(bool fOverwrite /*=true*/)
{
LogRelFlow(("UnattendedInstaller::prepareMedia:\n"));
HRESULT hrc = S_OK;
if (isAuxiliaryFloppyNeeded())
hrc = prepareAuxFloppyImage(fOverwrite);
if (SUCCEEDED(hrc))
{
if (isAuxiliaryIsoNeeded())
{
hrc = prepareAuxIsoImage(fOverwrite);
if (FAILED(hrc))
{
LogRelFlow(("UnattendedInstaller::prepareMedia: prepareAuxIsoImage failed\n"));
/* Delete the floppy image if we created one. */
if (isAuxiliaryFloppyNeeded())
RTFileDelete(getAuxiliaryFloppyFilePath().c_str());
}
}
}
LogRelFlow(("UnattendedInstaller::prepareMedia: returns %Rrc\n", hrc));
return hrc;
}
/*
*
* UnattendedInstaller protected methods
*
*/
HRESULT UnattendedInstaller::prepareAuxFloppyImage(bool fOverwrite)
{
Assert(isAuxiliaryFloppyNeeded());
/*
* Create the image.
*/
RTVFSFILE hVfsFile;
HRESULT hrc = newAuxFloppyImage(getAuxiliaryFloppyFilePath().c_str(), fOverwrite, &hVfsFile);
if (SUCCEEDED(hrc))
{
/*
* Open the FAT file system so we can copy files onto the floppy.
*/
RTERRINFOSTATIC ErrInfo;
RTVFS hVfs;
int vrc = RTFsFatVolOpen(hVfsFile, false /*fReadOnly*/, 0 /*offBootSector*/, &hVfs, RTErrInfoInitStatic(&ErrInfo));
RTVfsFileRelease(hVfsFile);
if (RT_SUCCESS(vrc))
{
/*
* Call overridable method to copies the files onto it.
*/
hrc = copyFilesToAuxFloppyImage(hVfs);
/*
* Release the VFS. On failure, delete the floppy image so the operation can
* be repeated in non-overwrite mode and so that we don't leave any mess behind.
*/
RTVfsRelease(hVfs);
}
else if (RTErrInfoIsSet(&ErrInfo.Core))
hrc = mpParent->setErrorBoth(E_FAIL, vrc,
tr("Failed to open FAT file system on newly created floppy image '%s': %Rrc: %s"),
getAuxiliaryFloppyFilePath().c_str(), vrc, ErrInfo.Core.pszMsg);
else
hrc = mpParent->setErrorBoth(E_FAIL, vrc,
tr("Failed to open FAT file system onnewly created floppy image '%s': %Rrc"),
getAuxiliaryFloppyFilePath().c_str(), vrc);
if (FAILED(hrc))
RTFileDelete(getAuxiliaryFloppyFilePath().c_str());
}
return hrc;
}
HRESULT UnattendedInstaller::newAuxFloppyImage(const char *pszFilename, bool fOverwrite, PRTVFSFILE phVfsFile)
{
/*
* Open the image file.
*/
HRESULT hrc;
RTVFSFILE hVfsFile;
uint64_t fOpen = RTFILE_O_READWRITE | RTFILE_O_DENY_ALL | (0660 << RTFILE_O_CREATE_MODE_SHIFT);
if (fOverwrite)
fOpen |= RTFILE_O_CREATE_REPLACE;
else
fOpen |= RTFILE_O_OPEN;
int vrc = RTVfsFileOpenNormal(pszFilename, fOpen, &hVfsFile);
if (RT_SUCCESS(vrc))
{
/*
* Format it.
*/
vrc = RTFsFatVolFormat144(hVfsFile, false /*fQuick*/);
if (RT_SUCCESS(vrc))
{
*phVfsFile = hVfsFile;
LogRelFlow(("UnattendedInstaller::newAuxFloppyImage: created and formatted '%s'\n", pszFilename));
return S_OK;
}
hrc = mpParent->setErrorBoth(E_FAIL, vrc, tr("Failed to format floppy image '%s': %Rrc"), pszFilename, vrc);
RTVfsFileRelease(hVfsFile);
RTFileDelete(pszFilename);
}
else
hrc = mpParent->setErrorBoth(E_FAIL, vrc, tr("Failed to create floppy image '%s': %Rrc"), pszFilename, vrc);
return hrc;
}
HRESULT UnattendedInstaller::copyFilesToAuxFloppyImage(RTVFS hVfs)
{
HRESULT hrc = addScriptToFloppyImage(&mMainScript, hVfs);
if (SUCCEEDED(hrc))
hrc = addScriptToFloppyImage(&mPostScript, hVfs);
return hrc;
}
HRESULT UnattendedInstaller::addScriptToFloppyImage(BaseTextScript *pEditor, RTVFS hVfs)
{
/*
* Open the destination file.
*/
HRESULT hrc;
RTVFSFILE hVfsFileDst;
int vrc = RTVfsFileOpen(hVfs, pEditor->getDefaultFilename(),
RTFILE_O_WRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_ALL
| (0660 << RTFILE_O_CREATE_MODE_SHIFT),
&hVfsFileDst);
if (RT_SUCCESS(vrc))
{
/*
* Save the content to a string.
*/
Utf8Str strScript;
hrc = pEditor->saveToString(strScript);
if (SUCCEEDED(hrc))
{
/*
* Write the string.
*/
vrc = RTVfsFileWrite(hVfsFileDst, strScript.c_str(), strScript.length(), NULL);
if (RT_SUCCESS(vrc))
hrc = S_OK; /* done */
else
hrc = mpParent->setErrorBoth(E_FAIL, vrc,
tr("Error writing %zu bytes to '%s' in floppy image '%s': %Rrc",
"", strScript.length()),
strScript.length(), pEditor->getDefaultFilename(),
getAuxiliaryFloppyFilePath().c_str());
}
RTVfsFileRelease(hVfsFileDst);
}
else
hrc = mpParent->setErrorBoth(E_FAIL, vrc,
tr("Error creating '%s' in floppy image '%s': %Rrc"),
pEditor->getDefaultFilename(), getAuxiliaryFloppyFilePath().c_str());
return hrc;
}
HRESULT UnattendedInstaller::addFileToFloppyImage(RTVFS hVfs, const char *pszSrc, const char *pszDst)
{
HRESULT hrc;
/*
* Open the source file.
*/
RTVFSIOSTREAM hVfsIosSrc;
int vrc = RTVfsIoStrmOpenNormal(pszSrc, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE, &hVfsIosSrc);
if (RT_SUCCESS(vrc))
{
/*
* Open the destination file.
*/
RTVFSFILE hVfsFileDst;
vrc = RTVfsFileOpen(hVfs, pszDst,
RTFILE_O_WRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_ALL | (0660 << RTFILE_O_CREATE_MODE_SHIFT),
&hVfsFileDst);
if (RT_SUCCESS(vrc))
{
/*
* Do the copying.
*/
RTVFSIOSTREAM hVfsIosDst = RTVfsFileToIoStream(hVfsFileDst);
vrc = RTVfsUtilPumpIoStreams(hVfsIosSrc, hVfsIosDst, 0);
if (RT_SUCCESS(vrc))
hrc = S_OK;
else
hrc = mpParent->setErrorBoth(VBOX_E_FILE_ERROR, vrc, tr("Error writing copying '%s' to floppy image '%s': %Rrc"),
pszSrc, getAuxiliaryFloppyFilePath().c_str(), vrc);
RTVfsIoStrmRelease(hVfsIosDst);
RTVfsFileRelease(hVfsFileDst);
}
else
hrc = mpParent->setErrorBoth(VBOX_E_FILE_ERROR, vrc, tr("Error opening '%s' on floppy image '%s' for writing: %Rrc"),
pszDst, getAuxiliaryFloppyFilePath().c_str(), vrc);
RTVfsIoStrmRelease(hVfsIosSrc);
}
else
hrc = mpParent->setErrorBoth(VBOX_E_FILE_ERROR, vrc, tr("Error opening '%s' for copying onto floppy image '%s': %Rrc"),
pszSrc, getAuxiliaryFloppyFilePath().c_str(), vrc);
return hrc;
}
HRESULT UnattendedInstaller::prepareAuxIsoImage(bool fOverwrite)
{
/*
* Open the original installation ISO.
*/
RTVFS hVfsOrgIso;
HRESULT hrc = openInstallIsoImage(&hVfsOrgIso);
if (SUCCEEDED(hrc))
{
/*
* The next steps depends on the kind of image we're making.
*/
if (!isAuxiliaryIsoIsVISO())
{
RTFSISOMAKER hIsoMaker;
hrc = newAuxIsoImageMaker(&hIsoMaker);
if (SUCCEEDED(hrc))
{
hrc = addFilesToAuxIsoImageMaker(hIsoMaker, hVfsOrgIso);
if (SUCCEEDED(hrc))
hrc = finalizeAuxIsoImage(hIsoMaker, getAuxiliaryIsoFilePath().c_str(), fOverwrite);
RTFsIsoMakerRelease(hIsoMaker);
}
}
else
{
RTCList<RTCString> vecFiles(0);
RTCList<RTCString> vecArgs(0);
try
{
vecArgs.append() = "--iprt-iso-maker-file-marker-bourne-sh";
RTUUID Uuid;
int vrc = RTUuidCreate(&Uuid); AssertRC(vrc);
char szTmp[RTUUID_STR_LENGTH + 1];
vrc = RTUuidToStr(&Uuid, szTmp, sizeof(szTmp)); AssertRC(vrc);
vecArgs.append() = szTmp;
vecArgs.append() = "--file-mode=0444";
vecArgs.append() = "--dir-mode=0555";
}
catch (std::bad_alloc &)
{
hrc = E_OUTOFMEMORY;
}
if (SUCCEEDED(hrc))
{
hrc = addFilesToAuxVisoVectors(vecArgs, vecFiles, hVfsOrgIso, fOverwrite);
if (SUCCEEDED(hrc))
hrc = finalizeAuxVisoFile(vecArgs, getAuxiliaryIsoFilePath().c_str(), fOverwrite);
if (FAILED(hrc))
for (size_t i = 0; i < vecFiles.size(); i++)
RTFileDelete(vecFiles[i].c_str());
}
}
RTVfsRelease(hVfsOrgIso);
}
return hrc;
}
HRESULT UnattendedInstaller::openInstallIsoImage(PRTVFS phVfsIso, uint32_t fFlags /*= 0*/)
{
/* Open the file. */
const char *pszIsoPath = mpParent->i_getIsoPath().c_str();
RTVFSFILE hOrgIsoFile;
int vrc = RTVfsFileOpenNormal(pszIsoPath, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE, &hOrgIsoFile);
if (RT_FAILURE(vrc))
return mpParent->setErrorBoth(E_FAIL, vrc, tr("Failed to open ISO image '%s' (%Rrc)"), pszIsoPath, vrc);
/* Pass the file to the ISO file system interpreter. */
RTERRINFOSTATIC ErrInfo;
vrc = RTFsIso9660VolOpen(hOrgIsoFile, fFlags, phVfsIso, RTErrInfoInitStatic(&ErrInfo));
RTVfsFileRelease(hOrgIsoFile);
if (RT_SUCCESS(vrc))
return S_OK;
if (RTErrInfoIsSet(&ErrInfo.Core))
return mpParent->setErrorBoth(E_FAIL, vrc, tr("ISO reader fail to open '%s' (%Rrc): %s"),
pszIsoPath, vrc, ErrInfo.Core.pszMsg);
return mpParent->setErrorBoth(E_FAIL, vrc, tr("ISO reader fail to open '%s' (%Rrc)"), pszIsoPath, vrc);
}
HRESULT UnattendedInstaller::newAuxIsoImageMaker(PRTFSISOMAKER phIsoMaker)
{
int vrc = RTFsIsoMakerCreate(phIsoMaker);
if (RT_SUCCESS(vrc))
return S_OK;
return mpParent->setErrorBoth(E_FAIL, vrc, tr("RTFsIsoMakerCreate failed (%Rrc)"), vrc);
}
HRESULT UnattendedInstaller::addFilesToAuxIsoImageMaker(RTFSISOMAKER hIsoMaker, RTVFS hVfsOrgIso)
{
RT_NOREF(hVfsOrgIso);
/*
* Add the two scripts to the image with default names.
*/
HRESULT hrc = addScriptToIsoMaker(&mMainScript, hIsoMaker);
if (SUCCEEDED(hrc))
hrc = addScriptToIsoMaker(&mPostScript, hIsoMaker);
return hrc;
}
HRESULT UnattendedInstaller::addScriptToIsoMaker(BaseTextScript *pEditor, RTFSISOMAKER hIsoMaker,
const char *pszDstFilename /*= NULL*/)
{
/*
* Calc default destination filename if desired.
*/
RTCString strDstNameBuf;
if (!pszDstFilename)
{
try
{
strDstNameBuf = RTPATH_SLASH_STR;
strDstNameBuf.append(pEditor->getDefaultTemplateFilename());
pszDstFilename = strDstNameBuf.c_str();
}
catch (std::bad_alloc &)
{
return E_OUTOFMEMORY;
}
}
/*
* Create a memory file for the script.
*/
Utf8Str strScript;
HRESULT hrc = pEditor->saveToString(strScript);
if (SUCCEEDED(hrc))
{
RTVFSFILE hVfsScriptFile;
size_t cchScript = strScript.length();
int vrc = RTVfsFileFromBuffer(RTFILE_O_READ, strScript.c_str(), strScript.length(), &hVfsScriptFile);
strScript.setNull();
if (RT_SUCCESS(vrc))
{
/*
* Add it to the ISO.
*/
vrc = RTFsIsoMakerAddFileWithVfsFile(hIsoMaker, pszDstFilename, hVfsScriptFile, NULL);
RTVfsFileRelease(hVfsScriptFile);
if (RT_SUCCESS(vrc))
hrc = S_OK;
else
hrc = mpParent->setErrorBoth(E_FAIL, vrc,
tr("RTFsIsoMakerAddFileWithVfsFile failed on the script '%s' (%Rrc)"),
pszDstFilename, vrc);
}
else
hrc = mpParent->setErrorBoth(E_FAIL, vrc,
tr("RTVfsFileFromBuffer failed on the %zu byte script '%s' (%Rrc)", "", cchScript),
cchScript, pszDstFilename, vrc);
}
return hrc;
}
HRESULT UnattendedInstaller::finalizeAuxIsoImage(RTFSISOMAKER hIsoMaker, const char *pszFilename, bool fOverwrite)
{
/*
* Finalize the image.
*/
int vrc = RTFsIsoMakerFinalize(hIsoMaker);
if (RT_FAILURE(vrc))
return mpParent->setErrorBoth(E_FAIL, vrc, tr("RTFsIsoMakerFinalize failed (%Rrc)"), vrc);
/*
* Open the destination file.
*/
uint64_t fOpen = RTFILE_O_WRITE | RTFILE_O_DENY_ALL;
if (fOverwrite)
fOpen |= RTFILE_O_CREATE_REPLACE;
else
fOpen |= RTFILE_O_CREATE;
RTVFSFILE hVfsDstFile;
vrc = RTVfsFileOpenNormal(pszFilename, fOpen, &hVfsDstFile);
if (RT_FAILURE(vrc))
{
if (vrc == VERR_ALREADY_EXISTS)
return mpParent->setErrorBoth(E_FAIL, vrc, tr("The auxiliary ISO image file '%s' already exists"),
pszFilename);
return mpParent->setErrorBoth(E_FAIL, vrc, tr("Failed to open the auxiliary ISO image file '%s' for writing (%Rrc)"),
pszFilename, vrc);
}
/*
* Get the source file from the image maker.
*/
HRESULT hrc;
RTVFSFILE hVfsSrcFile;
vrc = RTFsIsoMakerCreateVfsOutputFile(hIsoMaker, &hVfsSrcFile);
if (RT_SUCCESS(vrc))
{
RTVFSIOSTREAM hVfsSrcIso = RTVfsFileToIoStream(hVfsSrcFile);
RTVFSIOSTREAM hVfsDstIso = RTVfsFileToIoStream(hVfsDstFile);
if ( hVfsSrcIso != NIL_RTVFSIOSTREAM
&& hVfsDstIso != NIL_RTVFSIOSTREAM)
{
vrc = RTVfsUtilPumpIoStreams(hVfsSrcIso, hVfsDstIso, 0 /*cbBufHint*/);
if (RT_SUCCESS(vrc))
hrc = S_OK;
else
hrc = mpParent->setErrorBoth(E_FAIL, vrc, tr("Error writing auxiliary ISO image '%s' (%Rrc)"),
pszFilename, vrc);
}
else
hrc = mpParent->setErrorBoth(E_FAIL, VERR_INTERNAL_ERROR_2,
tr("Internal Error: Failed to case VFS file to VFS I/O stream"));
RTVfsIoStrmRelease(hVfsSrcIso);
RTVfsIoStrmRelease(hVfsDstIso);
}
else
hrc = mpParent->setErrorBoth(E_FAIL, vrc, tr("RTFsIsoMakerCreateVfsOutputFile failed (%Rrc)"), vrc);
RTVfsFileRelease(hVfsSrcFile);
RTVfsFileRelease(hVfsDstFile);
if (FAILED(hrc))
RTFileDelete(pszFilename);
return hrc;
}
HRESULT UnattendedInstaller::addFilesToAuxVisoVectors(RTCList<RTCString> &rVecArgs, RTCList<RTCString> &rVecFiles,
RTVFS hVfsOrgIso, bool fOverwrite)
{
RT_NOREF(hVfsOrgIso);
/*
* Save and add the scripts.
*/
HRESULT hrc = addScriptToVisoVectors(&mMainScript, rVecArgs, rVecFiles, fOverwrite);
if (SUCCEEDED(hrc))
hrc = addScriptToVisoVectors(&mPostScript, rVecArgs, rVecFiles, fOverwrite);
if (SUCCEEDED(hrc))
{
try
{
/*
* If we've got a Guest Additions ISO, add its content to a /vboxadditions dir.
*/
if (mpParent->i_getInstallGuestAdditions())
{
rVecArgs.append().append("--push-iso=").append(mpParent->i_getAdditionsIsoPath());
rVecArgs.append() = "/vboxadditions=/";
rVecArgs.append() = "--pop";
}
/*
* If we've got a Validation Kit ISO, add its content to a /vboxvalidationkit dir.
*/
if (mpParent->i_getInstallTestExecService())
{
rVecArgs.append().append("--push-iso=").append(mpParent->i_getValidationKitIsoPath());
rVecArgs.append() = "/vboxvalidationkit=/";
rVecArgs.append() = "--pop";
}
}
catch (std::bad_alloc &)
{
hrc = E_OUTOFMEMORY;
}
}
return hrc;
}
HRESULT UnattendedInstaller::addScriptToVisoVectors(BaseTextScript *pEditor, RTCList<RTCString> &rVecArgs,
RTCList<RTCString> &rVecFiles, bool fOverwrite)
{
/*
* Calc the aux script file name.
*/
RTCString strScriptName;
try
{
strScriptName = mpParent->i_getAuxiliaryBasePath();
strScriptName.append(pEditor->getDefaultFilename());
}
catch (std::bad_alloc &)
{
return E_OUTOFMEMORY;
}
/*
* Save it.
*/
HRESULT hrc = pEditor->save(strScriptName.c_str(), fOverwrite);
if (SUCCEEDED(hrc))
{
/*
* Add it to the vectors.
*/
try
{
rVecArgs.append().append('/').append(pEditor->getDefaultFilename()).append('=').append(strScriptName);
rVecFiles.append(strScriptName);
}
catch (std::bad_alloc &)
{
RTFileDelete(strScriptName.c_str());
hrc = E_OUTOFMEMORY;
}
}
return hrc;
}
HRESULT UnattendedInstaller::finalizeAuxVisoFile(RTCList<RTCString> const &rVecArgs, const char *pszFilename, bool fOverwrite)
{
/*
* Create a C-style argument vector and turn that into a command line string.
*/
size_t const cArgs = rVecArgs.size();
const char **papszArgs = (const char **)RTMemTmpAlloc((cArgs + 1) * sizeof(const char *));
if (!papszArgs)
return E_OUTOFMEMORY;
for (size_t i = 0; i < cArgs; i++)
papszArgs[i] = rVecArgs[i].c_str();
papszArgs[cArgs] = NULL;
char *pszCmdLine;
int vrc = RTGetOptArgvToString(&pszCmdLine, papszArgs, RTGETOPTARGV_CNV_QUOTE_BOURNE_SH);
RTMemTmpFree(papszArgs);
if (RT_FAILURE(vrc))
return mpParent->setErrorBoth(E_FAIL, vrc, tr("RTGetOptArgvToString failed (%Rrc)"), vrc);
/*
* Open the file.
*/
HRESULT hrc;
uint64_t fOpen = RTFILE_O_WRITE | RTFILE_O_DENY_WRITE | RTFILE_O_DENY_READ;
if (fOverwrite)
fOpen |= RTFILE_O_CREATE_REPLACE;
else
fOpen |= RTFILE_O_CREATE;
RTFILE hFile;
vrc = RTFileOpen(&hFile, pszFilename, fOpen);
if (RT_SUCCESS(vrc))
{
vrc = RTFileWrite(hFile, pszCmdLine, strlen(pszCmdLine), NULL);
if (RT_SUCCESS(vrc))
vrc = RTFileClose(hFile);
else
RTFileClose(hFile);
if (RT_SUCCESS(vrc))
hrc = S_OK;
else
hrc = mpParent->setErrorBoth(VBOX_E_FILE_ERROR, vrc, tr("Error writing '%s' (%Rrc)"), pszFilename, vrc);
}
else
hrc = mpParent->setErrorBoth(VBOX_E_FILE_ERROR, vrc, tr("Failed to create '%s' (%Rrc)"), pszFilename, vrc);
RTStrFree(pszCmdLine);
return hrc;
}
HRESULT UnattendedInstaller::loadAndParseFileFromIso(RTVFS hVfsOrgIso, const char *pszFilename, AbstractScript *pEditor)
{
HRESULT hrc;
RTVFSFILE hVfsFile;
int vrc = RTVfsFileOpen(hVfsOrgIso, pszFilename, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE, &hVfsFile);
if (RT_SUCCESS(vrc))
{
hrc = pEditor->readFromHandle(hVfsFile, pszFilename);
RTVfsFileRelease(hVfsFile);
if (SUCCEEDED(hrc))
hrc = pEditor->parse();
}
else
hrc = mpParent->setErrorBoth(VBOX_E_FILE_ERROR, vrc, tr("Failed to open '%s' on the ISO '%s' (%Rrc)"),
pszFilename, mpParent->i_getIsoPath().c_str(), vrc);
return hrc;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
/*
*
*
* Implementation UnattendedLinuxInstaller functions
*
*/
//////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT UnattendedLinuxInstaller::editIsoLinuxCfg(GeneralTextScript *pEditor)
{
try
{
/* Comment out 'display <filename>' directives that's used for displaying files at boot time. */
std::vector<size_t> vecLineNumbers = pEditor->findTemplate("display", RTCString::CaseInsensitive);
for (size_t i = 0; i < vecLineNumbers.size(); ++i)
if (pEditor->getContentOfLine(vecLineNumbers[i]).startsWithWord("display", RTCString::CaseInsensitive))
{
HRESULT hrc = pEditor->prependToLine(vecLineNumbers.at(i), "#");
if (FAILED(hrc))
return hrc;
}
}
catch (std::bad_alloc &)
{
return E_OUTOFMEMORY;
}
return editIsoLinuxCommon(pEditor);
}
HRESULT UnattendedLinuxInstaller::editIsoLinuxCommon(GeneralTextScript *pEditor)
{
try
{
/* Set timeouts to 4 seconds. */
std::vector<size_t> vecLineNumbers = pEditor->findTemplate("timeout", RTCString::CaseInsensitive);
for (size_t i = 0; i < vecLineNumbers.size(); ++i)
if (pEditor->getContentOfLine(vecLineNumbers[i]).startsWithWord("timeout", RTCString::CaseInsensitive))
{
HRESULT hrc = pEditor->setContentOfLine(vecLineNumbers.at(i), "timeout 4");
if (FAILED(hrc))
return hrc;
}
/* Modify kernel parameters. */
vecLineNumbers = pEditor->findTemplate("append", RTCString::CaseInsensitive);
if (vecLineNumbers.size() > 0)
{
Utf8Str const &rStrAppend = mpParent->i_getExtraInstallKernelParameters().isNotEmpty()
? mpParent->i_getExtraInstallKernelParameters()
: mStrDefaultExtraInstallKernelParameters;
for (size_t i = 0; i < vecLineNumbers.size(); ++i)
if (pEditor->getContentOfLine(vecLineNumbers[i]).startsWithWord("append", RTCString::CaseInsensitive))
{
Utf8Str strLine = pEditor->getContentOfLine(vecLineNumbers[i]);
/* Do removals. */
if (mArrStrRemoveInstallKernelParameters.size() > 0)
{
size_t offStart = strLine.find("append") + 5;
while (offStart < strLine.length() && !RT_C_IS_SPACE(strLine[offStart]))
offStart++;
while (offStart < strLine.length() && RT_C_IS_SPACE(strLine[offStart]))
offStart++;
if (offStart < strLine.length())
{
for (size_t iRemove = 0; iRemove < mArrStrRemoveInstallKernelParameters.size(); iRemove++)
{
RTCString const &rStrRemove = mArrStrRemoveInstallKernelParameters[iRemove];
for (size_t off = offStart; off < strLine.length(); )
{
Assert(!RT_C_IS_SPACE(strLine[off]));
/* Find the end of word. */
size_t offEnd = off + 1;
while (offEnd < strLine.length() && !RT_C_IS_SPACE(strLine[offEnd]))
offEnd++;
/* Check if it matches. */
if (RTStrSimplePatternNMatch(rStrRemove.c_str(), rStrRemove.length(),
strLine.c_str() + off, offEnd - off))
{
while (off > 0 && RT_C_IS_SPACE(strLine[off - 1]))
off--;
strLine.erase(off, offEnd - off);
}
/* Advance to the next word. */
off = offEnd;
while (off < strLine.length() && RT_C_IS_SPACE(strLine[off]))
off++;
}
}
}
}
/* Do the appending. */
if (rStrAppend.isNotEmpty())
{
if (!rStrAppend.startsWith(" ") && !strLine.endsWith(" "))
strLine.append(' ');
strLine.append(rStrAppend);
}
/* Update line. */
HRESULT hrc = pEditor->setContentOfLine(vecLineNumbers.at(i), strLine);
if (FAILED(hrc))
return hrc;
}
}
}
catch (std::bad_alloc &)
{
return E_OUTOFMEMORY;
}
return S_OK;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
/*
*
*
* Implementation UnattendedDebianInstaller functions
*
*/
//////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Helper for checking if a file exists.
* @todo promote to IPRT?
*/
static bool hlpVfsFileExists(RTVFS hVfs, const char *pszPath)
{
RTFSOBJINFO ObjInfo;
int vrc = RTVfsQueryPathInfo(hVfs, pszPath, &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_FOLLOW_LINK);
return RT_SUCCESS(vrc) && RTFS_IS_FILE(ObjInfo.Attr.fMode);
}
HRESULT UnattendedDebianInstaller::addFilesToAuxVisoVectors(RTCList<RTCString> &rVecArgs, RTCList<RTCString> &rVecFiles,
RTVFS hVfsOrgIso, bool fOverwrite)
{
/*
* Figure out the name of the menu config file that we have to edit.
*/
bool fMenuConfigIsGrub = false;
const char *pszMenuConfigFilename = "/isolinux/txt.cfg";
if (!hlpVfsFileExists(hVfsOrgIso, pszMenuConfigFilename))
{
/* On Debian Live ISOs (at least from 9 to 11) the there is only menu.cfg. */
if (hlpVfsFileExists(hVfsOrgIso, "/isolinux/menu.cfg"))
pszMenuConfigFilename = "/isolinux/menu.cfg";
/* On Linux Mint 20.3, 21, and 19 (at least) there is only isolinux.cfg. */
else if (hlpVfsFileExists(hVfsOrgIso, "/isolinux/isolinux.cfg"))
pszMenuConfigFilename = "/isolinux/isolinux.cfg";
/* Ubuntus 21.10+ are UEFI only. No isolinux directory. We modify grub.cfg. */
else if (hlpVfsFileExists(hVfsOrgIso, "/boot/grub/grub.cfg"))
{
pszMenuConfigFilename = "/boot/grub/grub.cfg";
fMenuConfigIsGrub = true;
}
}
/* Check for existence of isolinux.cfg since UEFI-only ISOs do not have this file. */
bool const fIsoLinuxCfgExists = hlpVfsFileExists(hVfsOrgIso, "isolinux/isolinux.cfg");
Assert(!fIsoLinuxCfgExists || !fMenuConfigIsGrub); /** @todo r=bird: Perhaps prefix the hlpVfsFileExists call with 'fIsoLinuxCfgExists &&' above ? */
/*
* VISO bits and filenames.
*/
RTCString strIsoLinuxCfg;
RTCString strTxtCfg;
try
{
/* Remaster ISO. */
rVecArgs.append() = "--no-file-mode";
rVecArgs.append() = "--no-dir-mode";
rVecArgs.append() = "--import-iso";
rVecArgs.append(mpParent->i_getIsoPath());
rVecArgs.append() = "--file-mode=0444";
rVecArgs.append() = "--dir-mode=0555";
/* Replace the isolinux.cfg configuration file. */
if (fIsoLinuxCfgExists)
{
/* First remove. */
rVecArgs.append() = "isolinux/isolinux.cfg=:must-remove:";
/* Then add the modified file. */
strIsoLinuxCfg = mpParent->i_getAuxiliaryBasePath();
strIsoLinuxCfg.append("isolinux-isolinux.cfg");
rVecArgs.append().append("isolinux/isolinux.cfg=").append(strIsoLinuxCfg);
}
/*
* Replace menu configuration file as well.
* Some distros (Linux Mint) has only isolinux.cfg. No menu.cfg or txt.cfg.
*/
if (RTStrICmp(pszMenuConfigFilename, "/isolinux/isolinux.cfg") != 0)
{
/* Replace menu configuration file as well. */
rVecArgs.append().assign(pszMenuConfigFilename).append("=:must-remove:");
strTxtCfg = mpParent->i_getAuxiliaryBasePath();
if (fMenuConfigIsGrub)
strTxtCfg.append("grub.cfg");
else
strTxtCfg.append("isolinux-txt.cfg");
rVecArgs.append().assign(pszMenuConfigFilename).append("=").append(strTxtCfg);
}
}
catch (std::bad_alloc &)
{
return E_OUTOFMEMORY;
}
/*
* Edit the isolinux.cfg file if it is there.
*/
if (fIsoLinuxCfgExists)
{
GeneralTextScript Editor(mpParent);
HRESULT hrc = loadAndParseFileFromIso(hVfsOrgIso, "/isolinux/isolinux.cfg", &Editor);
if (SUCCEEDED(hrc))
hrc = editIsoLinuxCfg(&Editor, RTPathFilename(pszMenuConfigFilename));
if (SUCCEEDED(hrc))
{
hrc = Editor.save(strIsoLinuxCfg, fOverwrite);
if (SUCCEEDED(hrc))
{
try
{
rVecFiles.append(strIsoLinuxCfg);
}
catch (std::bad_alloc &)
{
RTFileDelete(strIsoLinuxCfg.c_str());
hrc = E_OUTOFMEMORY;
}
}
}
if (FAILED(hrc))
return hrc;
}
/*
* Edit the menu config file.
* Some distros (Linux Mint) has only isolinux.cfg. No menu.cfg or txt.cfg.
*/
if (RTStrICmp(pszMenuConfigFilename, "/isolinux/isolinux.cfg") != 0)
{
GeneralTextScript Editor(mpParent);
HRESULT hrc = loadAndParseFileFromIso(hVfsOrgIso, pszMenuConfigFilename, &Editor);
if (SUCCEEDED(hrc))
{
if (fMenuConfigIsGrub)
hrc = editDebianGrubCfg(&Editor);
else
hrc = editDebianMenuCfg(&Editor);
if (SUCCEEDED(hrc))
{
hrc = Editor.save(strTxtCfg, fOverwrite);
if (SUCCEEDED(hrc))
{
try
{
rVecFiles.append(strTxtCfg);
}
catch (std::bad_alloc &)
{
RTFileDelete(strTxtCfg.c_str());
hrc = E_OUTOFMEMORY;
}
}
}
}
if (FAILED(hrc))
return hrc;
}
/*
* Call parent to add the preseed file from mAlg.
*/
return UnattendedLinuxInstaller::addFilesToAuxVisoVectors(rVecArgs, rVecFiles, hVfsOrgIso, fOverwrite);
}
HRESULT UnattendedDebianInstaller::editIsoLinuxCfg(GeneralTextScript *pEditor, const char *pszMenuConfigFileName)
{
try
{
/* Include menu config file. Since it can be txt.cfg, menu.cfg or something else we need to parametrize this. */
if (pszMenuConfigFileName && pszMenuConfigFileName[0] != '\0')
{
std::vector<size_t> vecLineNumbers = pEditor->findTemplate("include", RTCString::CaseInsensitive);
for (size_t i = 0; i < vecLineNumbers.size(); ++i)
{
if (pEditor->getContentOfLine(vecLineNumbers[i]).startsWithWord("include", RTCString::CaseInsensitive))
{
Utf8Str strIncludeLine("include ");
strIncludeLine.append(pszMenuConfigFileName);
HRESULT hrc = pEditor->setContentOfLine(vecLineNumbers.at(i), strIncludeLine);
if (FAILED(hrc))
return hrc;
}
}
}
/* Comment out default directives since in Debian case default is handled in menu config file. */
std::vector<size_t> vecLineNumbers = pEditor->findTemplate("default", RTCString::CaseInsensitive);
for (size_t i = 0; i < vecLineNumbers.size(); ++i)
if (pEditor->getContentOfLine(vecLineNumbers[i]).startsWithWord("default", RTCString::CaseInsensitive)
&& !pEditor->getContentOfLine(vecLineNumbers[i]).contains("default vesa", RTCString::CaseInsensitive))
{
HRESULT hrc = pEditor->prependToLine(vecLineNumbers.at(i), "#");
if (FAILED(hrc))
return hrc;
}
/* Comment out "ui gfxboot bootlogo" line as it somehow messes things up on Kubuntu 20.04 (possibly others as well). */
vecLineNumbers = pEditor->findTemplate("ui gfxboot", RTCString::CaseInsensitive);
for (size_t i = 0; i < vecLineNumbers.size(); ++i)
if (pEditor->getContentOfLine(vecLineNumbers[i]).startsWithWord("ui gfxboot", RTCString::CaseInsensitive))
{
HRESULT hrc = pEditor->prependToLine(vecLineNumbers.at(i), "#");
if (FAILED(hrc))
return hrc;
}
}
catch (std::bad_alloc &)
{
return E_OUTOFMEMORY;
}
return UnattendedLinuxInstaller::editIsoLinuxCfg(pEditor);
}
HRESULT UnattendedDebianInstaller::editDebianMenuCfg(GeneralTextScript *pEditor)
{
/*
* Unlike Redhats, Debian variants define boot menu not in isolinux.cfg but some other
* menu configuration files. They are mostly called txt.cfg and/or menu.cfg (and possibly some other names)
* In this functions we attempt to set menu's default label (default menu item) to the one containing the word 'install',
* failing to find such a label (on Kubuntu 20.04 for example) we pick the first label with name 'live'.
*/
try
{
HRESULT hrc = S_OK;
std::vector<size_t> vecLineNumbers = pEditor->findTemplate("label", RTCString::CaseInsensitive);
const char *pszNewLabelName = "VBoxUnatendedInstall";
bool fLabelFound = modifyLabelLine(pEditor, vecLineNumbers, "install", pszNewLabelName);
if (!fLabelFound)
fLabelFound = modifyLabelLine(pEditor, vecLineNumbers, "live", pszNewLabelName);
if (!fLabelFound)
hrc = E_FAIL;;
if (SUCCEEDED(hrc))
{
/* Modify the content of default lines so that they point to label we have chosen above. */
Utf8Str strNewContent("default ");
strNewContent.append(pszNewLabelName);
std::vector<size_t> vecDefaultLineNumbers = pEditor->findTemplate("default", RTCString::CaseInsensitive);
if (!vecDefaultLineNumbers.empty())
{
for (size_t j = 0; j < vecDefaultLineNumbers.size(); ++j)
{
hrc = pEditor->setContentOfLine(vecDefaultLineNumbers[j], strNewContent);
if (FAILED(hrc))
break;
}
}
/* Add a defaul label line. */
else
hrc = pEditor->appendLine(strNewContent);
}
if (FAILED(hrc))
return hrc;
}
catch (std::bad_alloc &)
{
return E_OUTOFMEMORY;
}
return UnattendedLinuxInstaller::editIsoLinuxCommon(pEditor);
}
bool UnattendedDebianInstaller::modifyLabelLine(GeneralTextScript *pEditor, const std::vector<size_t> &vecLineNumbers,
const char *pszKeyWord, const char *pszNewLabelName)
{
if (!pEditor)
return false;
Utf8Str strNewLabel("label ");
strNewLabel.append(pszNewLabelName);
HRESULT hrc = S_OK;
for (size_t i = 0; i < vecLineNumbers.size(); ++i)
{
RTCString const &rContent = pEditor->getContentOfLine(vecLineNumbers[i]);
/* Skip this line if it does not start with the word 'label'. */
if (!RTStrIStartsWith(rContent.c_str(), "label"))
continue;
/* Use the first menu item starting with word label and includes pszKeyWord.*/
if (RTStrIStr(rContent.c_str(), pszKeyWord) != NULL)
{
/* Set the content of the line. It looks like multiple word labels (like label Debian Installer)
* does not work very well in some cases. */
hrc = pEditor->setContentOfLine(vecLineNumbers[i], strNewLabel);
if (SUCCEEDED(hrc))
return true;
}
}
return false;
}
HRESULT UnattendedDebianInstaller::editDebianGrubCfg(GeneralTextScript *pEditor)
{
/* Default menu entry of grub.cfg is set in /etc/deafult/grub file. */
try
{
/* Set timeouts to 4 seconds. */
std::vector<size_t> vecLineNumbers = pEditor->findTemplate("set timeout", RTCString::CaseInsensitive);
for (size_t i = 0; i < vecLineNumbers.size(); ++i)
if (pEditor->getContentOfLine(vecLineNumbers[i]).startsWithWord("set timeout", RTCString::CaseInsensitive))
{
HRESULT hrc = pEditor->setContentOfLine(vecLineNumbers.at(i), "set timeout=4");
if (FAILED(hrc))
return hrc;
}
/* Modify kernel lines assuming that they starts with 'linux' keyword and 2nd word is the kernel command.*
* we remove whatever comes after command and add our own command line options. */
vecLineNumbers = pEditor->findTemplate("linux", RTCString::CaseInsensitive);
if (vecLineNumbers.size() > 0)
{
Utf8Str const &rStrAppend = mpParent->i_getExtraInstallKernelParameters().isNotEmpty()
? mpParent->i_getExtraInstallKernelParameters()
: mStrDefaultExtraInstallKernelParameters;
for (size_t i = 0; i < vecLineNumbers.size(); ++i)
{
HRESULT hrc = S_OK;
if (pEditor->getContentOfLine(vecLineNumbers[i]).startsWithWord("linux", RTCString::CaseInsensitive))
{
Utf8Str strLine = pEditor->getContentOfLine(vecLineNumbers[i]);
size_t cbPos = strLine.find("linux") + strlen("linux");
bool fSecondWord = false;
/* Find the end of 2nd word assuming that it is kernel command. */
while (cbPos < strLine.length())
{
if (!fSecondWord)
{
if (strLine[cbPos] != '\t' && strLine[cbPos] != ' ')
fSecondWord = true;
}
else
{
if (strLine[cbPos] == '\t' || strLine[cbPos] == ' ')
break;
}
++cbPos;
}
if (!fSecondWord)
hrc = E_FAIL;
if (SUCCEEDED(hrc))
{
strLine.erase(cbPos, strLine.length() - cbPos);
/* Do the appending. */
if (rStrAppend.isNotEmpty())
{
if (!rStrAppend.startsWith(" ") && !strLine.endsWith(" "))
strLine.append(' ');
strLine.append(rStrAppend);
}
/* Update line. */
hrc = pEditor->setContentOfLine(vecLineNumbers.at(i), strLine);
}
if (FAILED(hrc))
return hrc;
}
}
}
}
catch (std::bad_alloc &)
{
return E_OUTOFMEMORY;
}
return S_OK;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
/*
*
*
* Implementation UnattendedRhel6Installer functions
*
*/
//////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT UnattendedRhel6Installer::addFilesToAuxVisoVectors(RTCList<RTCString> &rVecArgs, RTCList<RTCString> &rVecFiles,
RTVFS hVfsOrgIso, bool fOverwrite)
{
Utf8Str strIsoLinuxCfg;
try
{
#if 1
/* Remaster ISO. */
rVecArgs.append() = "--no-file-mode";
rVecArgs.append() = "--no-dir-mode";
rVecArgs.append() = "--import-iso";
rVecArgs.append(mpParent->i_getIsoPath());
rVecArgs.append() = "--file-mode=0444";
rVecArgs.append() = "--dir-mode=0555";
/* We replace isolinux.cfg with our edited version (see further down). */
rVecArgs.append() = "isolinux/isolinux.cfg=:must-remove:";
strIsoLinuxCfg = mpParent->i_getAuxiliaryBasePath();
strIsoLinuxCfg.append("isolinux-isolinux.cfg");
rVecArgs.append().append("isolinux/isolinux.cfg=").append(strIsoLinuxCfg);
#else
/** @todo Maybe we should just remaster the ISO for redhat derivatives too?
* One less CDROM to mount. */
/* Name the ISO. */
rVecArgs.append() = "--volume-id=VBox Unattended Boot";
/* Copy the isolinux directory from the original install ISO. */
rVecArgs.append().append("--push-iso=").append(mpParent->i_getIsoPath());
rVecArgs.append() = "/isolinux=/isolinux";
rVecArgs.append() = "--pop";
/* We replace isolinux.cfg with our edited version (see further down). */
rVecArgs.append() = "/isolinux/isolinux.cfg=:must-remove:";
strIsoLinuxCfg = mpParent->i_getAuxiliaryBasePath();
strIsoLinuxCfg.append("isolinux-isolinux.cfg");
rVecArgs.append().append("/isolinux/isolinux.cfg=").append(strIsoLinuxCfg);
/* Configure booting /isolinux/isolinux.bin. */
rVecArgs.append() = "--eltorito-boot";
rVecArgs.append() = "/isolinux/isolinux.bin";
rVecArgs.append() = "--no-emulation-boot";
rVecArgs.append() = "--boot-info-table";
rVecArgs.append() = "--boot-load-seg=0x07c0";
rVecArgs.append() = "--boot-load-size=4";
/* Make the boot catalog visible in the file system. */
rVecArgs.append() = "--boot-catalog=/isolinux/vboxboot.cat";
#endif
}
catch (std::bad_alloc &)
{
return E_OUTOFMEMORY;
}
/*
* Edit isolinux.cfg and save it.
*/
{
GeneralTextScript Editor(mpParent);
HRESULT hrc = loadAndParseFileFromIso(hVfsOrgIso, "/isolinux/isolinux.cfg", &Editor);
if (SUCCEEDED(hrc))
hrc = editIsoLinuxCfg(&Editor);
if (SUCCEEDED(hrc))
{
hrc = Editor.save(strIsoLinuxCfg, fOverwrite);
if (SUCCEEDED(hrc))
{
try
{
rVecFiles.append(strIsoLinuxCfg);
}
catch (std::bad_alloc &)
{
RTFileDelete(strIsoLinuxCfg.c_str());
hrc = E_OUTOFMEMORY;
}
}
}
if (FAILED(hrc))
return hrc;
}
/*
* Call parent to add the ks.cfg file from mAlg.
*/
return UnattendedLinuxInstaller::addFilesToAuxVisoVectors(rVecArgs, rVecFiles, hVfsOrgIso, fOverwrite);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
/*
*
*
* Implementation UnattendedSuseInstaller functions
*
*/
//////////////////////////////////////////////////////////////////////////////////////////////////////
#if 0 /* doesn't work, so convert later */
/*
*
* UnattendedSuseInstaller protected methods
*
*/
HRESULT UnattendedSuseInstaller::setUserData()
{
HRESULT rc = S_OK;
//here base class function must be called first
//because user home directory is set after user name
rc = UnattendedInstaller::setUserData();
rc = mAlg->setField(USERHOMEDIR_ID, "");
if (FAILED(rc))
return rc;
return rc;
}
/*
*
* UnattendedSuseInstaller private methods
*
*/
HRESULT UnattendedSuseInstaller::iv_initialPhase()
{
Assert(isAuxiliaryIsoNeeded());
if (mParent->i_isGuestOs64Bit())
mFilesAndDirsToExtractFromIso.append("boot/x86_64/loader/ ");
else
mFilesAndDirsToExtractFromIso.append("boot/i386/loader/ ");
return extractOriginalIso(mFilesAndDirsToExtractFromIso);
}
HRESULT UnattendedSuseInstaller::setupScriptOnAuxiliaryCD(const Utf8Str &path)
{
HRESULT rc = S_OK;
GeneralTextScript isoSuseCfgScript(mpParent);
rc = isoSuseCfgScript.read(path);
rc = isoSuseCfgScript.parse();
//fix linux core bootable parameters: add path to the preseed script
std::vector<size_t> listOfLines = isoSuseCfgScript.findTemplate("append");
for(unsigned int i=0; i<listOfLines.size(); ++i)
{
isoSuseCfgScript.appendToLine(listOfLines.at(i),
" auto=true priority=critical autoyast=default instmode=cd quiet splash noprompt noshell --");
}
//find all lines with "label" inside
listOfLines = isoSuseCfgScript.findTemplate("label");
for(unsigned int i=0; i<listOfLines.size(); ++i)
{
Utf8Str content = isoSuseCfgScript.getContentOfLine(listOfLines.at(i));
//suppose general string looks like "label linux", two words separated by " ".
RTCList<RTCString> partsOfcontent = content.split(" ");
if (partsOfcontent.at(1).contains("linux"))
{
std::vector<size_t> listOfDefault = isoSuseCfgScript.findTemplate("default");
//handle the lines more intelligently
for(unsigned int j=0; j<listOfDefault.size(); ++j)
{
Utf8Str newContent("default ");
newContent.append(partsOfcontent.at(1));
isoSuseCfgScript.setContentOfLine(listOfDefault.at(j), newContent);
}
}
}
rc = isoSuseCfgScript.save(path, true);
LogRelFunc(("UnattendedSuseInstaller::setupScriptsOnAuxiliaryCD(): The file %s has been changed\n", path.c_str()));
return rc;
}
#endif
//////////////////////////////////////////////////////////////////////////////////////////////////////
/*
*
*
* Implementation UnattendedFreeBsdInstaller functions
*
*/
//////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT UnattendedFreeBsdInstaller::addFilesToAuxVisoVectors(RTCList<RTCString> &rVecArgs, RTCList<RTCString> &rVecFiles,
RTVFS hVfsOrgIso, bool fOverwrite)
{
try
{
RTCString strScriptName;
strScriptName = mpParent->i_getAuxiliaryBasePath();
strScriptName.append(mMainScript.getDefaultFilename());
/* Need to retain the original file permissions for executables. */
rVecArgs.append() = "--no-file-mode";
rVecArgs.append() = "--no-dir-mode";
rVecArgs.append() = "--import-iso";
rVecArgs.append(mpParent->i_getIsoPath());
rVecArgs.append() = "--file-mode=0444";
rVecArgs.append() = "--dir-mode=0555";
/* Remaster ISO, the installer config has to go into /etc. */
rVecArgs.append().append("/etc/installerconfig=").append(strScriptName);
}
catch (std::bad_alloc &)
{
return E_OUTOFMEMORY;
}
/*
* Call parent to add the remaining files
*/
return UnattendedInstaller::addFilesToAuxVisoVectors(rVecArgs, rVecFiles, hVfsOrgIso, fOverwrite);
}
|