/
installworker.cpp
353 lines (277 loc) · 13.3 KB
/
installworker.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
#include "installworker.h"
installWorker::installWorker(MainWindow *parent) {
parentWindow = parent;
}
// --- DECONSTRUCTOR ---
installWorker::~installWorker() {
}
void installWorker::process() {
p = new QProcess(this);
//p->setProcessChannelMode(QProcess::MergedChannels);
connect(p, SIGNAL(finished(int)), this, SLOT(lastProcessFinished(int)));
connect(p, SIGNAL(readyReadStandardOutput()), this, SLOT(outputAvaliable()));
connect(p, SIGNAL(readyReadStandardError()), this, SLOT(errorAvaliable()));
standardOutput.append("[theos_installer] Executing command umount /mnt\n");
emit output(standardOutput);
p->start("umount", QStringList() << "/mnt");
p->waitForFinished(-1);
if (parentWindow->formatPartition) {
lastProcessDone = false;
emit message("Formatting " + parentWindow->partition + "...");
standardOutput.append("[theos_installer] Executing command mkfs -t ext4 -F -F " + parentWindow->partition);
p->start("mkfs -t ext4 -F -F " + parentWindow->partition);
if (!p->waitForStarted()) {
standardOutput.append("[theos_installer] Error occurred executing command!\n");
}
p->waitForFinished(-1);
}
emit message("Mounting " + parentWindow->partition + "...");
standardOutput.append("[theos_installer] Executing command mount " + parentWindow->partition + " /mnt\n");
emit output(standardOutput);
p->start("mount " + parentWindow->partition + " /mnt");
p->waitForFinished(-1);
if (QDir("/sys/firmware/efi").exists()) {
standardOutput.append("This system is EFI, attempting to mount ESP onto /boot\n");
emit output(standardOutput);
QProcess* lsblk = new QProcess();
lsblk->start("lsblk -r --output NAME,PARTTYPE");
lsblk->waitForStarted(-1);
lsblk->waitForFinished(-1);
QString lsblkOutput(lsblk->readAllStandardOutput());
for (QString partition : lsblkOutput.split("\n")) {
if (partition.split(" ").count() != 1) {
if (partition.split(" ").at(1).contains("C12A7328-F81F-11D2-BA4B-00A0C93EC93B", Qt::CaseInsensitive)) {
QDir("/mnt").mkdir("boot");
emit message("Mounting " + partition.split(" ").at(0) + "...");
standardOutput.append("[theos_installer] Executing command mount " + parentWindow->partition + " /mnt/boot\n");
emit output(standardOutput);
p->start("mount /dev/" + partition.split(" ").at(0) + " /mnt/boot");
p->waitForFinished(-1);
break;
}
}
}
}
emit message("Downloading and copying new files...");
standardOutput.append("[theos_installer] Executing command pacstrap /mnt base base-devel linux-headers\n");
emit output(standardOutput);
p->start("pacstrap /mnt base base-devel linux-headers");
p->waitForFinished(-1);
if (p->exitCode() != 0) {
emit message("An error occurred. Inspect the output to see what happened.");
emit failed();
return;
}
emit message("Configuring system...");
standardOutput.append("[theos_installer] Generating fstab...\n");
emit output(standardOutput);
QProcess *fstab = new QProcess(this);
fstab->start("genfstab -p /mnt");
fstab->waitForFinished();
QFile fstabFile("/mnt/etc/fstab");
fstabFile.open(QFile::WriteOnly);
fstabFile.write(fstab->readAllStandardOutput());
fstabFile.close();
standardOutput.append("[theos_installer] Setting hostname...\n");
while (parentWindow->hostname == "") {} //Stall until hostname is ready
QFile hostnameFile("/mnt/etc/hostname");
hostnameFile.open(QFile::WriteOnly);
hostnameFile.write(parentWindow->hostname.toUtf8());
hostnameFile.close();
standardOutput.append("[theos_installer] Generating locales...\n");
QFile::copy("/etc/locale.gen", "/mnt/etc/locale.gen");
standardOutput.append("[theos_installer] Executing command arch-chroot /mnt locale-gen \n");
emit output(standardOutput);
p->start("arch-chroot /mnt locale-gen");
p->waitForFinished(-1);
standardOutput.append("[theos_installer] Executing command arch-chroot /mnt mkinitcpio -p linux\n");
emit output(standardOutput);
p->start("arch-chroot /mnt mkinitcpio -p linux");
p->waitForFinished(-1);
emit message("Downloading and installing bootloader...");
standardOutput.append("[theos_installer] Executing command pacstrap /mnt os-prober grub\n");
emit output(standardOutput);
p->start("pacstrap /mnt os-prober efibootmgr grub");
p->waitForFinished(-1);
QString disk = parentWindow->partition;
disk.chop(1);
if (QDir("/sys/firmware/efi").exists()) {
standardOutput.append("[theos_installer] Executing command arch-chroot /mnt grub-install --target=x86_64-efi --efi-directory=/boot/ --bootloader-id=grub\n");
p->start("arch-chroot /mnt grub-install --target=x86_64-efi --efi-directory=/boot/ --bootloader-id=grub");
p->waitForFinished(-1);
} else {
standardOutput.append("[theos_installer] Executing command arch-chroot /mnt grub-install --target=i386-pc " + disk + "\n");
emit output(standardOutput);
p->start("arch-chroot /mnt grub-install --target=i386-pc " + disk);
p->waitForFinished(-1);
}
QFile grubDefault("/mnt/etc/default/grub");
grubDefault.open(QFile::ReadOnly);
QString grubDefaults(grubDefault.readAll());
grubDefault.close();
QStringList grubDefaultsArray = grubDefaults.split("\n");
for (QString line : grubDefaultsArray) {
if (line.startsWith("GRUB_CMDLINE_LINUX_DEFAULT")) {
int index = grubDefaultsArray.indexOf(line);
grubDefaultsArray.removeAt(index);
grubDefaultsArray.insert(index, "GRUB_CMDLINE_LINUX_DEFAULT=\"quiet splash\"");
}
}
grubDefaults = "";
for (QString line : grubDefaultsArray) {
grubDefaults.append(line + "\n");
}
p->waitForFinished(-1);
grubDefault.open(QFile::WriteOnly);
grubDefault.write(grubDefaults.toUtf8());
grubDefault.close();
standardOutput.append("[theos_installer] Executing command arch-chroot /mnt grub-mkconfig -o /boot/grub/grub.cfg\n");
emit output(standardOutput);
p->start("arch-chroot /mnt grub-mkconfig -o /boot/grub/grub.cfg");
p->waitForFinished(-1);
QFile grubConfig("/mnt/boot/grub/grub.cfg");
grubConfig.open(QFile::ReadWrite);
QString grubConfiguration(grubConfig.readAll());
grubConfig.close();
grubConfiguration = grubConfiguration.replace("Arch Linux", "theOS");
grubConfig.open(QFile::ReadWrite);
grubConfig.write(grubConfiguration.toUtf8());
grubConfig.close();
emit message("Downloading and installing new files...");
standardOutput.append("[theos_installer] Installing additional packages...\n");
emit output(standardOutput);
p->start(QString("pacstrap /mnt xf86-video-vesa xf86-video-intel xf86-video-nouveau xf86-video-vmware kde-cli-tools kdesu")
.append(" virtualbox-guest-utils xorg-server xorg-xinit xf86-input-synaptics lightdm breeze breeze-gtk breeze-icons")
.append(" breeze-kde4 networkmanager gtk3 breeze-gtk chromium kinfocenter partitionmanager ntfs-3g")
.append(" hfsprogs kate bluez bluedevil libreoffice-fresh hunspell hunspell-en kdegraphics-okular")
.append(" ksuperkey kscreen user-manager kdeconnect gstreamer0.10 gstreamer0.10-bad gstreamer0.10-plugins")
.append(" gstreamer0.10-base gstreamer0.10-base-plugins gstreamer0.10-ffmpeg gstreamer0.10-good")
.append(" gstreamer0.10-good-plugins gstreamer0.10-ugly gstreamer0.10-ugly-plugins gst-plugins-good")
.append(" gst-plugins-ugly kmail korganizer cups ark kcalc gwenview alsa-utils pulseaudio pulseaudio-alsa festival festival-english"));
p->waitForFinished(-1);
QDir localPackagesDir("/root/.packages/");
QDirIterator *packageIterator = new QDirIterator(localPackagesDir);
while (packageIterator->hasNext()) {
QString packageName = packageIterator->next();
QString packageToInstall = packageName;
QString installLocation = "/mnt/var/cache/pacman/pkg/" + packageName.remove("/root/.packages/");
if (!QFile::copy(packageToInstall, installLocation)) {
standardOutput.append("[theos_installer] Error copying " + packageToInstall + " to " + installLocation);
emit output(standardOutput);
}
p->start("arch-chroot /mnt pacman -U --noconfirm " + installLocation.remove(0, 4));
p->waitForFinished(-1);
}
emit message("Configuring System...");
standardOutput.append("[theos_installer] Configuring users...\n");
QFile chfnDefault("/mnt/etc/login.defs");
chfnDefault.open(QFile::ReadOnly);
QString chfnDefaults(chfnDefault.readAll());
chfnDefault.close();
QStringList chfnDefaultsArray = chfnDefaults.split("\n");
for (QString line : chfnDefaultsArray) {
if (line.startsWith("CHFN_RESTRICT")) {
int index = chfnDefaultsArray.indexOf(line);
chfnDefaultsArray.removeAt(index);
chfnDefaultsArray.insert(index, "CHFN_RESTRICT frwh");
}
}
chfnDefaults = "";
for (QString line : chfnDefaultsArray) {
chfnDefaults.append(line + "\n");
}
chfnDefault.open(QFile::WriteOnly);
chfnDefault.write(chfnDefaults.toUtf8());
chfnDefault.close();
p->start("useradd -R /mnt -g wheel -M " + parentWindow->loginname);
p->waitForFinished(-1);
p->start("arch-chroot /mnt chfn -f \"" + parentWindow->fullname + "\" " + parentWindow->loginname);
p->waitForFinished(-1);
p->start("chpasswd -R /mnt");
p->write(QString("root:" + parentWindow->password + "\n").toUtf8());
p->write(QString(parentWindow->loginname + ":" + parentWindow->password + "\n").toUtf8());
p->closeWriteChannel();
p->waitForFinished(-1);
QFile sudoersConfig("/mnt/etc/sudoers");
sudoersConfig.open(QFile::ReadWrite);
QString sudoersConfiguration(sudoersConfig.readAll());
sudoersConfig.close();
sudoersConfiguration = sudoersConfiguration.replace("# %wheel ALL=(ALL) ALL", "%wheel ALL=(ALL) ALL");
sudoersConfig.open(QFile::ReadWrite);
sudoersConfig.write(sudoersConfiguration.toUtf8());
sudoersConfig.close();
standardOutput.append("[theos_installer] Configuring services...\n");
p->start("arch-chroot /mnt systemctl enable NetworkManager");
p->waitForFinished(-1);
p->start("arch-chroot /mnt systemctl enable bluetooth");
p->waitForFinished(-1);
p->start("arch-chroot /mnt systemctl enable lightdm");
p->waitForFinished(-1);
QFile lightdmConf("/mnt/etc/lightdm/lightdm.conf");
lightdmConf.open(QFile::ReadOnly);
QString lightdmDefaults(lightdmConf.readAll());
lightdmConf.close();
QStringList lightdmDefaultsArray = lightdmDefaults.split("\n");
for (QString line : lightdmDefaultsArray) {
if (line.startsWith("#greeter-session=")) {
int index = lightdmDefaultsArray.indexOf(line);
lightdmDefaultsArray.removeAt(index);
lightdmDefaultsArray.insert(index, "greeter-session=lightdm-webkit2-greeter");
}
}
lightdmDefaults = "";
for (QString line : lightdmDefaultsArray) {
lightdmDefaults.append(line + "\n");
}
lightdmConf.open(QFile::WriteOnly);
lightdmConf.write(lightdmDefaults.toUtf8());
lightdmConf.close();
QFile lightdmWebkitConf("/mnt/etc/lightdm/lightdm-webkit2-greeter.conf");
lightdmWebkitConf.open(QFile::WriteOnly);
lightdmWebkitConf.write(QString("[greeter]\nwebkit-theme=contemporary\n").toUtf8());
lightdmWebkitConf.close();
QFile initConf("/mnt/etc/mkinitcpio.conf");
initConf.open(QFile::ReadOnly);
QString init(initConf.readAll());
initConf.close();
QStringList initArray = init.split("\n");
for (QString line : initArray) {
if (line.startsWith("HOOKS")) {
int index = initArray.indexOf(line);
initArray.removeAt(index);
initArray.insert(index, "HOOKS=\"base udev plymouth autodetect modconf block filesystems keyboard fsck\"");
} else if (line.startsWith("MODULES")) {
int index = initArray.indexOf(line);
initArray.removeAt(index);
initArray.insert(index, "MODULES=\"i915\"");
}
}
init = "";
for (QString line : initArray) {
init.append(line + "\n");
}
initConf.open(QFile::WriteOnly);
initConf.write(init.toUtf8());
initConf.close();
QFile("/etc/os-release").copy("/mnt/etc/os-release");
p->start("arch-chroot /mnt plymouth-set-default-theme theos --rebuild-initrd" );
p->waitForFinished(-1);
p->start("cp -r /root /mnt/home/" + parentWindow->loginname);
p->waitForFinished(-1);
p->start("arch-chroot /mnt chown -R " + parentWindow->loginname + " /home/" + parentWindow->loginname + "/ " );
p->waitForFinished(-1);
emit finished();
}
void installWorker::lastProcessFinished(int ret) {
qDebug() << "The last process returned" << ret;
}
void installWorker::outputAvaliable() {
QString out(p->readAllStandardOutput());
//qDebug() << out;
standardOutput.append(out);
emit output(standardOutput);
}
void installWorker::errorAvaliable() {
standardOutput.append(QString(p->readAllStandardError()) + "\n");
emit output(standardOutput);
}