-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
362 lines (344 loc) · 10.2 KB
/
main.c
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int isInternetAvaliable();
int isUEFI();
void clear();
int verifyBootName(char *bootname);
char* read_file(const char *filename);
void clearString(char str[], int size);
char* read_file(const char* filename) {
FILE* fp = fopen(filename, "r");
if (fp == NULL) {
fprintf(stderr, "Error: could not open file '%s'\n", filename);
return NULL;
}
// Get file size
fseek(fp, 0L, SEEK_END);
long file_size = ftell(fp);
rewind(fp);
// Allocate memory for buffer
char* buffer = (char*) malloc(file_size + 1);
if (buffer == NULL) {
fprintf(stderr, "Error: could not allocate memory for file '%s'\n", filename);
fclose(fp);
return NULL;
}
// Read file into buffer
size_t bytes_read = fread(buffer, sizeof(char), file_size, fp);
if (bytes_read < file_size) {
fprintf(stderr, "Error: could not read file '%s'\n", filename);
fclose(fp);
free(buffer);
return NULL;
}
buffer[bytes_read] = '\0';
// Close file and return buffer
fclose(fp);
return buffer;
}
void clear() {
system("clear");
}
int isInternetAvaliable() {
if (system("ping google.com -c 1 &> /dev/null") == 0) {
return 1;
}
else {
return 0;
}
}
int isUEFI() {
if (system("ls /sys/firmware/efi/efivars &> /dev/null") == 0) {
return 1;
}
else {
return 0;
}
}
int verifyBootName(char *bootname) {
system("lsblk --nodeps -o NAME > disks.txt");
char *drivelist = read_file("disks.txt");
if (drivelist == NULL) {
printf("Error: could not read disk list from file\n");
return 0;
}
if (strstr(drivelist, bootname) != NULL) {
free(drivelist);
return 1;
}
else {
free(drivelist);
return 0;
}
}
struct UserInfo {
char *name;
char *hostname;
char **kernel;
char *disk;
char isSecureBoot;
};
void clearString(char str[], int size) {
for (int i = 0;i<size;i++) {
str[i] = '\0';
}
}
int main(int argc, char *argv) {
// Initialize struct
// I used struct to seperate variable from main
// All the variables declared in main are temporary and not to be used afterwards
struct UserInfo user;
char cmd[300];
char cmd2[300];
char cmd3[300];
int kchoice;
char op1;
// Clear screen
clear();
// Test for internet connection
// If connection not found then wait for it.
printf("\rChecking if Internet Connection persists...");
while (1) {
if (isInternetAvaliable()) {
printf("\rInternet Connection Verified ! \n");
break;
}
else {
printf("\rWaiting for internet connection... ");
}
}
sleep(1);
// Then check if the system is UEFI or BIOS
if (isUEFI()) {
printf("Starting Installation for UEFI...\n");
}
else {
printf("BIOS is yet not supported for installing. Come Later. :(\n");
}
sleep(1);
// First display disks
printf("CHOOSE A DISK FOR BELOW\n");
system("lsblk | grep sd");
system("lsblk | grep nvme");
system("lsblk | grep mmcblk");
while (1) {
printf("Type disk name (sda):");
scanf("%s", &(user.disk));
if (verifyBootName(user.disk)) {
break;
}
else {
printf("Wrong Boot Drive. Try Again !\n");
}
}
printf("Partitioning %s\n", *(user.disk));
sleep(1);
// Partition Code
clearString(cmd, sizeof(cmd));
sprintf(cmd, "parted /dev/%s --script mktable gpt \
mkpart primary fat32 1MiB 551MiB \
set 1 esp on \
mkpart primary linux-swap 551MiB 2551MiB \
mkpart primary ext4 2551MiB 100%%", *(user.disk));
// Apply Partition
system(cmd);
// Then format drive
clearString(cmd, sizeof(cmd));
sprintf(cmd, "mkfs.fat -F32 /dev/%s1", *(user.disk));
system(cmd);
clearString(cmd, sizeof(cmd));
sprintf(cmd, "mkswap /dev/%s2", *(user.disk));
system(cmd);
clearString(cmd, sizeof(cmd));
sprintf(cmd, "swapon /dev/%s2", *(user.disk));
system(cmd);
clearString(cmd, sizeof(cmd));
sprintf(cmd, "mkfs.ext4 /dev/%s3", *(user.disk));
system(cmd);
clearString(cmd, sizeof(cmd));
// After formatting Mount drive
printf("Mounting %s", *(user.disk));
sprintf(cmd2, "mount /dev/%s3 /mnt", *(user.disk));
system(cmd2);
clearString(cmd, sizeof(cmd));
printf("Mounted %s", *(user.disk));
sleep(1);
clear();
printf("Select a Linux Kernel\n1. Linux\n2. Linux LTS\n3.Linux Zen\n(1)>>");
scanf("%d", &kchoice);
if (kchoice == 1) {
*(user.kernel) = "linux";
}
else if (kchoice == 2) {
*(user.kernel) = "linux-lts";
}
else if (kchoice == 3) {
*(user.kernel) = "linux-zen";
}
clear();
printf("INSTALLING BASE SYSTEM\n");
clearString(cmd, sizeof(cmd));
sprintf(cmd3, "pacstrap /mnt base %s linux-firmware base-devel", user.kernel);
sleep(1);
clearString(cmd, sizeof(cmd));
sprintf(cmd3, "pacstrap -K /mnt base base-devel linux-firmware %s", user.kernel);
system(cmd3);
printf("Creating fstab...\n");
system("genfstab -U /mnt >> /mnt/etc/fstab");
clear();
printf("Installed Base System");
sleep(1);
clear();
printf("TIMEZONE\n");
system("timedatectl list-timezones > timelist.txt");
while (1) {
printf("Type s to search for timezone and e to enter timezone:");
scanf(" %c", &op1);
if (op1 == 's') {
// Search
printf("Type term to search:");
clearString(cmd2, sizeof(cmd2));
scanf("%s", cmd2);
clearString(cmd, sizeof(cmd));
sprintf(cmd, "cat timelist.txt | grep %s", cmd2);
system(cmd);
}
else if (op1 == 'e') {
// Directly Edit
clearString(cmd, sizeof(cmd));
printf("Type timezone Continent/Region :");
clearString(cmd2, sizeof(cmd2));
scanf("%s", cmd2);
sprintf(cmd, "arch-chroot /mnt ln -sf /usr/share/zoneinfo/%s /etc/localtime", cmd2);
if (system(cmd) == 0) {
break;
}
else {
printf("The Timezone you entered is incorrect. Try Again !\n");
}
}
}
printf("Timezone set to %s sucessfully\n", cmd);
sleep(1);
clear();
printf("Setting Hardware Clock to current time...\n");
system("arch-chroot /mnt hwclock --systohc");
printf("Hardware Clock set sucessfully\n");
sleep(1);
printf("Setting locale to 'en_IN UTF-8'\n");
system("arch-chroot /mnt echo 'en_IN UTF-8' >> /etc/locale.gen");
system("arch-chroot /mnt locale-gen");
printf("Locale Set\n");
printf("Type the name of the machine you want to keep:");
scanf("%s", &user.hostname);
clearString(cmd2, sizeof(cmd2));
sprintf(cmd2, "arch-chroot /mnt echo '%s' > /etc/hostname", user.hostname);
system(cmd2);
printf("Type password for root user:");
system("arch-chroot /mnt passwd");
clear();
printf("Now setting up network services...\n");
system("arch-chroot /mnt echo '127.0.0.1 localhost' >> /etc/hosts");
system("arch-chroot /mnt echo '::1 localhost' >> /etc/hosts");
clearString(cmd, sizeof(cmd));
sprintf(cmd, "arch-chroot /mnt echo '127.0.1.1 %s.localdomain %s' > /etc/hosts", user.hostname, user.hostname);
system(cmd);
printf("Type your username:");
scanf("%s", &user.name);
clearString(cmd, sizeof(cmd));
sprintf(cmd, "arch-chroot /mnt useradd -m %s", *(user.name));
system(cmd);
printf("Now type password for %s:", *(user.name));
clearString(cmd, sizeof(cmd));
sprintf(cmd, "arch-chroot /mnt passwd %s", *(user.name));
system(cmd);
printf("Installing sudo...\n");
system("arch-chroot /mnt pacman -S sudo --noconfirm");
clear();
printf("Adding %s to wheel group...\n", *(user.name));
clearString(cmd, sizeof(cmd));
sprintf(cmd, "arch-chroot /mnt usermod -aG wheel,video,audio,optical,storage %s", *(user.name));
system(cmd);
printf("Giving %s user privelleges...\n", *(user.name));
system("arch-chroot /mnt echo 'root ALL=(ALL:ALL) ALL' > /etc/sudoers");
system("arch-chroot /mnt echo 'wheel ALL=(ALL:ALL) ALL' >> /etc/sudoers");
system("arch-chroot /mnt echo '@includedir /etc/sudoers.d' >> /etc/sudoers");
printf("Setting UEFI Partition...\n");
clearString(cmd, sizeof(cmd));
sprintf(cmd, "arch-chroot /mnt mount --mkdir /dev/%s1 /boot/EFI", *(user.disk));
system(cmd);
printf("Installing GRUB...\n");
system("arch-chroot /mnt pacman -S grub efibootmgr dosfstools mtools os-prober --noconfirm");
printf("Does your system support Secure Boot (y/N):");
scanf(" %c", &user.isSecureBoot);
if (user.isSecureBoot == 'y') {
if (system("arch-chroot /mnt grub-install --target=x86_64-efi --bootloader-id=grub_uefi --modules='tpm' --disable-shim-lock --recheck") == 0) {
printf("Generating Grub Configuration file...");
}
else {
system("arch-chroot /mnt grub-install --target=x86_64-efi --bootloader-id=grub_uefi --recheck");
}
}
else {
system("arch-chroot /mnt grub-install --target=x86_64-efi --bootloader-id=grub_uefi --recheck");
printf("Generating Grub Configuration file...");
}
// Now generating grub configuration file
system("arch-chroot /mnt grub-mkconfig -o /boot/grub/grub.cfg");
printf("\nBootloader Installed Sucessfully\n");
// Now install network manager
printf("Installing Network Manager...\n");
system("arch-chroot /mnt pacman -S networkmanager --noconfirm");
system("arch-chroot /mnt sudo systemctl enable NetworkManager");
// Now ask user for choosing a desktop enviroment or any window manager
// Then after taking input install the desktop enviroment in the user pc
clear();
printf("CHOOSE A DESKTOP ENVIROMENT TO INSTALL\n");
printf("1. GNOME\n2. KDE Plasma\n3. XFCE\n4. OpenBox\n5. None\n");
char ch;
while (1) {
printf(">>");
scanf(" %c", &ch);
if (ch == '1') {
// GNOME
system("arch-chroot /mnt pacman -S xorg xorg-server gnome gdm --noconfirm");
system("arch-chroot /mnt systemctl enable gdm");
break;
}
else if (ch == '2') {
// KDE Plasma
system("arch-chroot /mnt pacman -S xorg plasma plasma-meta doplhin konsole sddm --noconfirm");
system("arch-chroot /mnt systemctl enable sddm");
break;
}
else if (ch == '3') {
// XFCE
system("arch-chroot /mnt pacman -S install xfce4 xfce4-goodies lightdm xorg --noconfirm");
system("arch-chroot /mnt systemctl enable lightdm");
break;
}
else if (ch == '4') {
// Openbox
system("arch-chroot /mnt pacman -S openbox lightdm xorg --noconfirm");
system("arch-chroot /mnt systemctl enable lightdm");
break;
}
else if (kchoice == '5') {
// None
break;
}
else {
printf("Wrong Choice, choice doesnt exist :(\n");
}
}
clear();
printf("Installation Completed Sucessfully\n");
printf("Happy Arch :)\n");
printf("Rebooting in 5 seconds...");
system("umount -R /mnt");
sleep(5);
system("systemctl reboot");
return 0;
}