]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - src/install+setup/install/main.c
installer: Make function to determine the amount of system memory.
[people/pmueller/ipfire-2.x.git] / src / install+setup / install / main.c
CommitLineData
2bb7b134 1
d6aaa55d
MT
2/* SmoothWall install program.
3 *
4 * This program is distributed under the terms of the GNU General Public
5 * Licence. See the file COPYING for details.
6 *
7 * (c) Lawrence Manning, 2001
f9cc0d70 8 * Contains main entry point, and misc functions.6
d6aaa55d 9 *
d6aaa55d 10 */
10bc6f06 11
72d80898 12#define _GNU_SOURCE
f0fa1795
MT
13
14#include <assert.h>
15#include <errno.h>
16#include <stdlib.h>
17#include <string.h>
18#include <sys/mount.h>
19
20#include "hw.h"
21#include "install.h"
72d80898 22
68561214 23#define INST_FILECOUNT 21000
33634aa8 24#define UNATTENDED_CONF "/cdrom/boot/unattended.conf"
918546e2 25#define LICENSE_FILE "/cdrom/COPYING"
d6aaa55d 26
ddd1589d 27#define EXT2 0
ddd1589d 28#define EXT3 1
c8d680dc
AF
29#define EXT4 2
30#define REISERFS 3
cd8dd8dd 31
d6aaa55d
MT
32FILE *flog = NULL;
33char *mylog;
10bc6f06 34
d6aaa55d
MT
35char **ctr;
36
d6aaa55d
MT
37extern char url[STRING_SIZE];
38
f9cc0d70
HS
39struct nic nics[20] = { { "" , "" , "" } }; // only defined for compile
40struct knic knics[20] = { { "" , "" , "" , "" } }; // only defined for compile
5057b611 41
10bc6f06 42extern char *en_tr[];
cf4dd3c0 43extern char *es_tr[];
10bc6f06 44extern char *de_tr[];
462515e4 45extern char *fr_tr[];
1d76272b 46extern char *nl_tr[];
b6c9668f 47extern char *pl_tr[];
2bb7b134 48extern char *ru_tr[];
910193da 49extern char *tr_tr[];
d6aaa55d 50
d7dd283b
MT
51static int newtChecklist(const char* title, const char* message,
52 unsigned int width, unsigned int height, unsigned int num_entries,
53 const char** entries, int* states) {
54 int ret;
55 const int list_height = 4;
56
57 char cbstates[num_entries];
58
59 for (unsigned int i = 0; i < num_entries; i++) {
60 cbstates[i] = states[i] ? '*' : ' ';
61 }
62
63 newtCenteredWindow(width, height, title);
64
65 newtComponent textbox = newtTextbox(1, 1, width - 2, height - 6 - list_height,
66 NEWT_FLAG_WRAP);
67 newtTextboxSetText(textbox, message);
68
69 int top = newtTextboxGetNumLines(textbox) + 2;
70
71 newtComponent form = newtForm(NULL, NULL, 0);
72
73 newtComponent sb = NULL;
74 if (list_height < num_entries) {
75 sb = newtVerticalScrollbar(
76 width - 4, top + 1, list_height,
77 NEWT_COLORSET_CHECKBOX, NEWT_COLORSET_ACTCHECKBOX);
78
79 newtFormAddComponent(form, sb);
80 }
81
82 newtComponent subform = newtForm(sb, NULL, 0);
83 newtFormSetBackground(subform, NEWT_COLORSET_CHECKBOX);
84
85 newtFormSetHeight(subform, list_height);
86 newtFormSetWidth(subform, width - 10);
87
88 for (unsigned int i = 0; i < num_entries; i++) {
89 newtComponent cb = newtCheckbox(4, top + i, entries[i], cbstates[i],
90 NULL, &cbstates[i]);
91
92 newtFormAddComponent(subform, cb);
93 }
94
95 newtFormAddComponents(form, textbox, subform, NULL);
96
97 newtComponent btn_okay = newtButton((width - 18) / 3, height - 4, ctr[TR_OK]);
98 newtComponent btn_cancel = newtButton((width - 18) / 3 * 2 + 9, height - 4, ctr[TR_CANCEL]);
99 newtFormAddComponents(form, btn_okay, btn_cancel, NULL);
100
101 newtComponent answer = newtRunForm(form);
102
103 if ((answer == NULL) || (answer == btn_cancel)) {
104 ret = -1;
105 } else {
106 ret = 0;
107
108 for (unsigned int i = 0; i < num_entries; i++) {
109 states[i] = (cbstates[i] != ' ');
110
111 if (states[i])
112 ret++;
113 }
114 }
115
116 newtFormDestroy(form);
117 newtPopWindow();
118
119 return ret;
120}
121
122static int newtWinOkCancel(const char* title, const char* message, int width, int height,
123 const char* btn_txt_ok, const char* btn_txt_cancel) {
124 int ret = 1;
125
126 newtCenteredWindow(width, height, title);
127
128 newtComponent form = newtForm(NULL, NULL, 0);
129
130 newtComponent textbox = newtTextbox(1, 1, width - 2, height - 6, NEWT_FLAG_WRAP);
131 newtTextboxSetText(textbox, message);
132 newtFormAddComponent(form, textbox);
133
134 newtComponent btn_ok = newtButton((width - 16) / 3, height - 4, btn_txt_ok);
135 newtComponent btn_cancel = newtButton((width - 16) / 3 * 2 + 9, height - 4,
136 btn_txt_cancel);
137
138 newtFormAddComponents(form, btn_ok, btn_cancel, NULL);
139
140 newtComponent answer = newtRunForm(form);
141
142 if (answer == btn_ok) {
143 ret = 0;
144 }
145
146 newtFormDestroy(form);
147 newtPopWindow();
148
149 return ret;
150}
151
f0fa1795
MT
152int main(int argc, char *argv[]) {
153 struct hw* hw = hw_init();
e0bbaf87
AF
154
155 char discl_msg[40000] = "Disclaimer\n";
156
910193da
EY
157 char *langnames[] = { "Deutsch", "English", "Français", "Español", "Nederlands", "Polski", "Русский", "Türkçe", NULL };
158 char *shortlangnames[] = { "de", "en", "fr", "es", "nl", "pl", "ru", "tr", NULL };
159 char **langtrs[] = { de_tr, en_tr, fr_tr, es_tr, nl_tr, pl_tr, ru_tr, tr_tr, NULL };
f0fa1795 160 char* sourcedrive = NULL;
72d80898 161 int rc = 0;
d6aaa55d 162 char commandstring[STRING_SIZE];
cd8dd8dd 163 char mkfscommand[STRING_SIZE];
c8d680dc 164 char *fstypes[] = { "ext2", "ext3", "ext4", "ReiserFS", NULL };
eea897b3 165 int fstype = EXT4;
d6aaa55d
MT
166 int choice;
167 char shortlangname[10];
d7dd283b 168 char message[STRING_SIZE];
d6aaa55d
MT
169 char title[STRING_SIZE];
170 int allok = 0;
10bc6f06 171 int allok_fastexit=0;
d6aaa55d 172 struct keyvalue *ethernetkv = initkeyvalues();
e0bbaf87 173 FILE *handle, *cmdfile, *copying;
d6aaa55d
MT
174 char line[STRING_SIZE];
175 char string[STRING_SIZE];
f0fa1795 176 long memory = 0, disk = 0;
8e2e5cf3 177 long system_partition, boot_partition, root_partition, swap_file;
5057b611 178
72d80898 179 int unattended = 0;
5faa66cf 180 int serialconsole = 0;
72d80898 181 struct keyvalue *unattendedkv = initkeyvalues();
66335974 182 char restore_file[STRING_SIZE] = "";
d6aaa55d
MT
183
184 setlocale (LC_ALL, "");
10bc6f06 185 sethostname( SNAME , 10);
72d80898 186
d6aaa55d
MT
187 /* Log file/terminal stuff. */
188 if (argc >= 2)
189 {
190 if (!(flog = fopen(argv[1], "w+")))
191 return 0;
192 }
193 else
194 return 0;
195
196 mylog = argv[1];
197
198 fprintf(flog, "Install program started.\n");
199
200 newtInit();
201 newtCls();
202
7ea444c8
AF
203 newtDrawRootText(14, 0, NAME " " VERSION " - " SLOGAN );
204 sprintf (title, "%s %s - %s", NAME, VERSION, SLOGAN);
205
d6aaa55d
MT
206 if (! (cmdfile = fopen("/proc/cmdline", "r")))
207 {
208 fprintf(flog, "Couldn't open commandline: /proc/cmdline\n");
209 } else {
210 fgets(line, STRING_SIZE, cmdfile);
61e3d218 211
72d80898 212 // check if we have to make an unattended install
ee78a5ef 213 if (strstr (line, "unattended") != NULL) {
72d80898 214 unattended = 1;
bba7212c
MT
215 runcommandwithstatus("/bin/sleep 10", "WARNING: Unattended installation will start in 10 seconds...");
216 }
5faa66cf
AF
217 // check if we have to patch for serial console
218 if (strstr (line, "console=ttyS0") != NULL) {
219 serialconsole = 1;
220 }
d6aaa55d 221 }
0db33b56 222
7ea444c8 223 // Load common modules
bba7212c 224 mysystem("/sbin/modprobe vfat"); // USB key
d6aaa55d 225
72d80898 226 /* German is the default */
d6aaa55d
MT
227 for (choice = 0; langnames[choice]; choice++)
228 {
215bd18d 229 if (strcmp(langnames[choice], "English") == 0)
d6aaa55d
MT
230 break;
231 }
232 if (!langnames[choice])
233 goto EXIT;
234
72d80898 235 if (!unattended) {
51f3b7f5 236 rc = newtWinMenu("Language selection", "Select the language you wish to use for the " NAME ".", 50, 5, 5, 8,
72d80898
MT
237 langnames, &choice, "Ok", NULL);
238 }
239
d6aaa55d
MT
240 ctr = langtrs[choice];
241 strcpy(shortlangname, shortlangnames[choice]);
3d6e1202 242
d6aaa55d 243 newtPushHelpLine(ctr[TR_HELPLINE]);
0db33b56 244
e0bbaf87 245 if (!unattended) {
c5685c24
MT
246 sprintf(message, ctr[TR_WELCOME], NAME);
247 newtWinMessage(title, ctr[TR_OK], message);
e0bbaf87
AF
248 }
249
f0fa1795
MT
250 /* Search for a source drive that holds the right
251 * version of the image we are going to install. */
252 sourcedrive = hw_find_source_medium(hw);
9607771a 253
f0fa1795
MT
254 fprintf(flog, "Source drive: %s\n", sourcedrive);
255 if (!sourcedrive) {
0f680bcc 256 newtWinMessage(title, ctr[TR_OK], ctr[TR_NO_LOCAL_SOURCE]);
f0fa1795 257 runcommandwithstatus("/bin/downloadsource.sh", ctr[TR_DOWNLOADING_ISO]);
0f680bcc
AF
258 if ((handle = fopen("/tmp/source_device", "r")) == NULL) {
259 errorbox(ctr[TR_DOWNLOAD_ERROR]);
260 goto EXIT;
261 }
f0fa1795
MT
262
263 fgets(sourcedrive, 5, handle);
264 fclose(handle);
72d80898 265 }
0f680bcc 266
f0fa1795
MT
267 assert(sourcedrive);
268
269 int r = hw_mount(sourcedrive, SOURCE_MOUNT_PATH, MS_RDONLY);
270 if (r) {
271 fprintf(flog, "Could not mount %s to %s\n", sourcedrive, SOURCE_MOUNT_PATH);
272 fprintf(flog, strerror(errno));
273 exit(1);
274 }
918546e2 275
d7dd283b
MT
276 /* load unattended configuration */
277 if (unattended) {
278 fprintf(flog, "unattended: Reading unattended.conf\n");
279
280 (void) readkeyvalues(unattendedkv, UNATTENDED_CONF);
281 findkey(unattendedkv, "RESTORE_FILE", restore_file);
282 }
283
918546e2
MT
284 if (!unattended) {
285 // Read the license file.
286 if (!(copying = fopen(LICENSE_FILE, "r"))) {
287 sprintf(discl_msg, "Could not open license file: %s\n", LICENSE_FILE);
288 fprintf(flog, discl_msg);
289 } else {
290 fread(discl_msg, 1, 40000, copying);
291 fclose(copying);
292
293 if (disclaimerbox(discl_msg)==0) {
294 errorbox(ctr[TR_LICENSE_NOT_ACCEPTED]);
295 goto EXIT;
296 }
297 }
298 }
299
d7dd283b 300 int part_type = HW_PART_TYPE_NORMAL;
ee78a5ef 301
d7dd283b
MT
302 // Scan for disks to install on.
303 struct hw_disk** disks = hw_find_disks(hw);
304
305 struct hw_disk** selected_disks = NULL;
306 unsigned int num_selected_disks = 0;
307
308 // Check how many disks have been found and what
309 // we can do with them.
310 unsigned int num_disks = hw_count_disks(disks);
311
312 while (1) {
313 // no harddisks found
314 if (num_disks == 0) {
315 errorbox(ctr[TR_NO_HARDDISK]);
316 goto EXIT;
317
318 // exactly one disk has been found
319 } else if (num_disks == 1) {
320 selected_disks = hw_select_disks(disks, NULL);
321
322 // more than one usable disk has been found and
323 // the user needs to choose what to do with them
324 } else {
325 const char* disk_names[num_disks];
326 int disk_selection[num_disks];
327
328 for (unsigned int i = 0; i < num_disks; i++) {
329 disk_names[i] = &disks[i]->description;
330 disk_selection[i] = 0;
331 }
332
333 while (!selected_disks) {
334 rc = newtChecklist(ctr[TR_DISK_SELECTION], ctr[TR_DISK_SELECTION_MSG],
335 50, 20, num_disks, disk_names, disk_selection);
336
337 // Error
338 if (rc < 0) {
339 goto EXIT;
340
341 // Nothing has been selected
342 } else if (rc == 0) {
343 errorbox(ctr[TR_NO_DISK_SELECTED]);
344
345 } else {
346 selected_disks = hw_select_disks(disks, disk_selection);
347 }
348 }
349 }
350
351 num_selected_disks = hw_count_disks(selected_disks);
352
353 if (num_selected_disks == 1) {
354 snprintf(message, sizeof(message), ctr[TR_DISK_SETUP_DESC], (*selected_disks)->description);
355 rc = newtWinOkCancel(ctr[TR_DISK_SETUP], message, 50, 10,
356 ctr[TR_DELETE_ALL_DATA], ctr[TR_CANCEL]);
357
358 if (rc == 0)
56b548f1 359 break;
d7dd283b
MT
360
361 } else if (num_selected_disks == 2) {
362 snprintf(message, sizeof(message), ctr[TR_RAID_SETUP_DESC],
363 (*selected_disks)->description, (*selected_disks + 1)->description);
364 rc = newtWinOkCancel(ctr[TR_RAID_SETUP], message, 50, 10,
365 ctr[TR_DELETE_ALL_DATA], ctr[TR_CANCEL]);
366
367 if (rc == 0) {
368 part_type = HW_PART_TYPE_RAID1;
369
ee78a5ef 370 break;
d7dd283b
MT
371 }
372
373 // Currently not supported
374 } else {
375 errorbox(ctr[TR_DISK_CONFIGURATION_NOT_SUPPORTED]);
aee3027d 376 }
ee78a5ef 377
d7dd283b
MT
378 if (selected_disks) {
379 hw_free_disks(selected_disks);
380 selected_disks = NULL;
381 }
212cab4f 382 }
212cab4f 383
d7dd283b 384 hw_free_disks(disks);
72d80898 385
d7dd283b
MT
386 struct hw_destination* destination = hw_make_destination(part_type, selected_disks);
387 assert(destination);
5057b611 388
d7dd283b
MT
389 fprintf(flog, "Destination drive: %s\n", destination->path);
390 fprintf(flog, " boot: %s\n", destination->part_boot);
391 fprintf(flog, " swap: %s\n", destination->part_swap);
392 fprintf(flog, " root: %s\n", destination->part_root);
393 fprintf(flog, " data: %s\n", destination->part_data);
72d80898 394
c8d680dc
AF
395 fstypes[0]=ctr[TR_EXT2FS_DESCR];
396 fstypes[1]=ctr[TR_EXT3FS_DESCR];
397 fstypes[2]=ctr[TR_EXT4FS_DESCR];
398 fstypes[3]=ctr[TR_REISERFS_DESCR];
399 fstypes[4]=NULL;
400
cd8dd8dd 401 if (!unattended) {
1700769c
CS
402 sprintf(message, ctr[TR_CHOOSE_FILESYSTEM]);
403 rc = newtWinMenu( ctr[TR_CHOOSE_FILESYSTEM], message,
cd8dd8dd
MT
404 50, 5, 5, 6, fstypes, &fstype, ctr[TR_OK],
405 ctr[TR_CANCEL], NULL);
406 } else {
407 rc = 1;
2e8b71ca 408 fstype = EXT4;
cd8dd8dd
MT
409 }
410 if (rc == 2)
411 goto EXIT;
412
c4e96674 413 memory = hw_memory() / 1024 / 1024;
72d80898
MT
414
415 /* Partition, mkswp, mkfs.
416 * before partitioning, first determine the sizes of each
417 * partition. In order to do that we need to know the size of
418 * the disk.
419 */
d7dd283b 420 disk = destination->size / 1024 / 1024;
72d80898 421
ffd4d196 422 fprintf(flog, "Disksize = %ld, memory = %ld", disk, memory);
72d80898 423
ffd4d196 424 /* Calculating Swap-Size dependend of Ram Size */
841323bc 425 if (memory <= 256)
6380cbd1 426 swap_file = 128;
841323bc 427 else if (memory <= 1024 && memory > 256)
ddd1589d 428 swap_file = 256;
66294b69 429 else
ddd1589d 430 swap_file = memory / 4;
ffd4d196
CS
431
432 /* Calculating Root-Size dependend of Max Disk Space */
b5d2be5d
AF
433 if ( disk < 2048 )
434 root_partition = 1024;
435 else if ( disk >= 2048 && disk <= 3072 )
436 root_partition = 1536;
66294b69
MT
437 else
438 root_partition = 2048;
439
ffd4d196
CS
440
441 /* Calculating the amount of free space */
558a0052 442 boot_partition = 64; /* in MB */
ffd4d196 443 system_partition = disk - ( root_partition + swap_file + boot_partition );
d3fb18db 444
66294b69
MT
445 fprintf(flog, ", boot = %ld, swap = %ld, mylog = %ld, root = %ld\n",
446 boot_partition, swap_file, system_partition, root_partition);
9cd0c7fd 447 rc = 0;
66294b69 448
9cd0c7fd 449 if ( (!unattended) && (((disk - (root_partition + swap_file + boot_partition)) < 256 ) && ((disk - (root_partition + boot_partition )) > 256)) ) {
a5997a4c 450 rc = newtWinChoice(title, ctr[TR_OK], ctr[TR_CANCEL], ctr[TR_CONTINUE_NO_SWAP]);
9cd0c7fd
MT
451 if (rc == 1){
452 swap_file = 0;
a5997a4c 453 system_partition = disk - ( root_partition + swap_file + boot_partition );
9cd0c7fd 454 fprintf(flog, "Changing Swap Size to 0 MB.\n");
a5997a4c 455 }
9cd0c7fd 456 else if (rc == 2){
a5997a4c
MT
457 fprintf(flog, "Disk is too small.\n");
458 errorbox(ctr[TR_DISK_TOO_SMALL]);goto EXIT;
459 }
66294b69 460 }
58c7871a 461 else if (disk - (root_partition + swap_file + boot_partition) >= 256) {
66294b69
MT
462
463 }
464 else {
465 fprintf(flog, "Disk is too small.\n");
466 errorbox(ctr[TR_DISK_TOO_SMALL]);goto EXIT;
467 }
a5997a4c 468
72d80898
MT
469 handle = fopen("/tmp/partitiontable", "w");
470
72d80898 471 /* Make swapfile */
9cd0c7fd
MT
472 if (swap_file) {
473 fprintf(handle, ",%ld,L,*\n,%ld,S,\n,%ld,L,\n,,L,\n",
474 boot_partition, swap_file, root_partition);
475 } else {
476 fprintf(handle, ",%ld,L,*\n,0,0,\n,%ld,L,\n,,L,\n",
477 boot_partition, root_partition);
478 }
72d80898
MT
479
480 fclose(handle);
481
4f26ff7f
AF
482 if (disk < 2097150) {
483 // <2TB use sfdisk and normal mbr
d7dd283b 484 snprintf(commandstring, STRING_SIZE, "/sbin/sfdisk -L -uM %s < /tmp/partitiontable", destination->path);
4f26ff7f
AF
485 } else {
486 // >2TB use parted with gpt
d7dd283b 487 snprintf(commandstring, STRING_SIZE, "/usr/sbin/parted -s %s mklabel gpt mkpart boot ext2 1M 64M mkpart swap linux-swap 64M 1000M mkpart root ext4 1000M 5000M mkpart var ext4 5000M 100%% disk_set pmbr_boot on", destination->path);
4f26ff7f
AF
488 }
489
72d80898
MT
490 if (runcommandwithstatus(commandstring, ctr[TR_PARTITIONING_DISK]))
491 {
492 errorbox(ctr[TR_UNABLE_TO_PARTITION]);
493 goto EXIT;
494 }
9a3d3d95 495
ddd1589d 496 if (fstype == EXT2) {
337726bf 497// mysystem("/sbin/modprobe ext2");
6cf9e770 498 sprintf(mkfscommand, "/sbin/mke2fs -T ext2");
cd8dd8dd
MT
499 } else if (fstype == REISERFS) {
500 mysystem("/sbin/modprobe reiserfs");
501 sprintf(mkfscommand, "/sbin/mkreiserfs -f");
502 } else if (fstype == EXT3) {
337726bf 503// mysystem("/sbin/modprobe ext3");
6cf9e770 504 sprintf(mkfscommand, "/sbin/mke2fs -T ext3");
c8d680dc 505 } else if (fstype == EXT4) {
337726bf 506// mysystem("/sbin/modprobe ext4");
c8d680dc 507 sprintf(mkfscommand, "/sbin/mke2fs -T ext4");
cd8dd8dd 508 }
72d80898 509
d7dd283b 510 snprintf(commandstring, STRING_SIZE, "/sbin/mke2fs -T ext2 -I 128 %s", destination->part_boot);
72d80898
MT
511 if (runcommandwithstatus(commandstring, ctr[TR_MAKING_BOOT_FILESYSTEM]))
512 {
513 errorbox(ctr[TR_UNABLE_TO_MAKE_BOOT_FILESYSTEM]);
514 goto EXIT;
515 }
516
517 if (swap_file) {
d7dd283b 518 snprintf(commandstring, STRING_SIZE, "/sbin/mkswap %s", destination->part_swap);
72d80898
MT
519 if (runcommandwithstatus(commandstring, ctr[TR_MAKING_SWAPSPACE]))
520 {
521 errorbox(ctr[TR_UNABLE_TO_MAKE_SWAPSPACE]);
522 goto EXIT;
523 }
524 }
525
d7dd283b 526 snprintf(commandstring, STRING_SIZE, "%s %s", mkfscommand, destination->part_root);
72d80898
MT
527 if (runcommandwithstatus(commandstring, ctr[TR_MAKING_ROOT_FILESYSTEM]))
528 {
529 errorbox(ctr[TR_UNABLE_TO_MAKE_ROOT_FILESYSTEM]);
530 goto EXIT;
531 }
532
d7dd283b 533 snprintf(commandstring, STRING_SIZE, "%s %s", mkfscommand, destination->part_data);
72d80898
MT
534 if (runcommandwithstatus(commandstring, ctr[TR_MAKING_LOG_FILESYSTEM]))
535 {
cd8dd8dd 536 errorbox(ctr[TR_UNABLE_TO_MAKE_LOG_FILESYSTEM]);
72d80898 537 goto EXIT;
9607771a 538 }
72d80898 539
d7dd283b 540 snprintf(commandstring, STRING_SIZE, "/bin/mount %s /harddisk", destination->part_root);
72d80898
MT
541 if (runcommandwithstatus(commandstring, ctr[TR_MOUNTING_ROOT_FILESYSTEM]))
542 {
543 errorbox(ctr[TR_UNABLE_TO_MOUNT_ROOT_FILESYSTEM]);
544 goto EXIT;
545 }
546
547 mkdir("/harddisk/boot", S_IRWXU|S_IRWXG|S_IRWXO);
548 mkdir("/harddisk/var", S_IRWXU|S_IRWXG|S_IRWXO);
549 mkdir("/harddisk/var/log", S_IRWXU|S_IRWXG|S_IRWXO);
b8e2d108 550
d7dd283b 551 snprintf(commandstring, STRING_SIZE, "/bin/mount %s /harddisk/boot", destination->part_boot);
72d80898
MT
552 if (runcommandwithstatus(commandstring, ctr[TR_MOUNTING_BOOT_FILESYSTEM]))
553 {
554 errorbox(ctr[TR_UNABLE_TO_MOUNT_BOOT_FILESYSTEM]);
555 goto EXIT;
556 }
557 if (swap_file) {
d7dd283b 558 snprintf(commandstring, STRING_SIZE, "/sbin/swapon %s", destination->part_swap);
72d80898
MT
559 if (runcommandwithstatus(commandstring, ctr[TR_MOUNTING_SWAP_PARTITION]))
560 {
561 errorbox(ctr[TR_UNABLE_TO_MOUNT_SWAP_PARTITION]);
562 goto EXIT;
563 }
564 }
d7dd283b 565 snprintf(commandstring, STRING_SIZE, "/bin/mount %s /harddisk/var", destination->part_data);
72d80898
MT
566 if (runcommandwithstatus(commandstring, ctr[TR_MOUNTING_LOG_FILESYSTEM]))
567 {
568 errorbox(ctr[TR_UNABLE_TO_MOUNT_LOG_FILESYSTEM]);
569 goto EXIT;
9607771a 570 }
c78a77eb 571
03d956be 572 snprintf(commandstring, STRING_SIZE,
23ed79cb 573 "/bin/tar -C /harddisk -xvf /cdrom/" SNAME "-" VERSION ".tlz --lzma 2>/dev/null");
d6aaa55d 574
edd536b6
MT
575 if (runcommandwithprogress(60, 4, title, commandstring, INST_FILECOUNT,
576 ctr[TR_INSTALLING_FILES]))
d6aaa55d
MT
577 {
578 errorbox(ctr[TR_UNABLE_TO_INSTALL_FILES]);
579 goto EXIT;
580 }
406f019f
MT
581
582 /* Save language und local settings */
583 write_lang_configs(shortlangname);
d6aaa55d 584
9607771a
MT
585 /* mount proc filesystem */
586 mysystem("mkdir /harddisk/proc");
ee78a5ef
MT
587 mysystem("/bin/mount --bind /proc /harddisk/proc");
588 mysystem("/bin/mount --bind /dev /harddisk/dev");
589 mysystem("/bin/mount --bind /sys /harddisk/sys");
9607771a 590
330345c2 591 /* Build cache lang file */
6cf9e770 592 snprintf(commandstring, STRING_SIZE, "/usr/sbin/chroot /harddisk /usr/bin/perl -e \"require '" CONFIG_ROOT "/lang.pl'; &Lang::BuildCacheLang\"");
330345c2
MT
593 if (runcommandwithstatus(commandstring, ctr[TR_INSTALLING_LANG_CACHE]))
594 {
595 errorbox(ctr[TR_UNABLE_TO_INSTALL_LANG_CACHE]);
596 goto EXIT;
597 }
598
599 /* Update /etc/fstab */
d7dd283b 600 snprintf(commandstring, STRING_SIZE, "/bin/sed -i -e \"s#DEVICE1#UUID=$(/sbin/blkid %s -sUUID | /usr/bin/cut -d'\"' -f2)#g\" /harddisk/etc/fstab", destination->part_boot);
a9a26c5a 601 system(commandstring);
d7dd283b 602 snprintf(commandstring, STRING_SIZE, "/bin/sed -i -e \"s#DEVICE2#UUID=$(/sbin/blkid %s -sUUID | /usr/bin/cut -d'\"' -f2)#g\" /harddisk/etc/fstab", destination->part_swap);
a9a26c5a 603 system(commandstring);
d7dd283b 604 snprintf(commandstring, STRING_SIZE, "/bin/sed -i -e \"s#DEVICE3#UUID=$(/sbin/blkid %s -sUUID | /usr/bin/cut -d'\"' -f2)#g\" /harddisk/etc/fstab", destination->part_root);
a9a26c5a 605 system(commandstring);
d7dd283b 606 snprintf(commandstring, STRING_SIZE, "/bin/sed -i -e \"s#DEVICE4#UUID=$(/sbin/blkid %s -sUUID | /usr/bin/cut -d'\"' -f2)#g\" /harddisk/etc/fstab", destination->part_data);
a9a26c5a
AF
607 system(commandstring);
608
ddd1589d
CS
609 if (fstype == EXT2) {
610 replace("/harddisk/etc/fstab", "FSTYPE", "ext2");
611 replace("/harddisk/boot/grub/grub.conf", "MOUNT", "ro");
cd8dd8dd
MT
612 } else if (fstype == REISERFS) {
613 replace("/harddisk/etc/fstab", "FSTYPE", "reiserfs");
d3fb18db 614 replace("/harddisk/boot/grub/grub.conf", "MOUNT", "ro");
cd8dd8dd
MT
615 } else if (fstype == EXT3) {
616 replace("/harddisk/etc/fstab", "FSTYPE", "ext3");
c8d680dc
AF
617 replace("/harddisk/boot/grub/grub.conf", "MOUNT", "ro");
618 } else if (fstype == EXT4) {
619 replace("/harddisk/etc/fstab", "FSTYPE", "ext4");
d3fb18db 620 replace("/harddisk/boot/grub/grub.conf", "MOUNT", "ro");
cd8dd8dd 621 }
73d9a908 622
a869b064
AF
623 replace("/harddisk/boot/grub/grub.conf", "KVER", KERNEL_VERSION);
624
d7dd283b 625 snprintf(commandstring, STRING_SIZE, "/bin/sed -i -e \"s#root=ROOT#root=UUID=$(/sbin/blkid %s -sUUID | /usr/bin/cut -d'\"' -f2)#g\" /harddisk/boot/grub/grub.conf", destination->part_root);
a9a26c5a
AF
626 system(commandstring);
627
f4e27420 628 mysystem("ln -s grub.conf /harddisk/boot/grub/menu.lst");
fd0763dc 629
a9a26c5a 630 system("/bin/sed -e 's#/harddisk#/#g' -e 's#//#/#g' < /proc/mounts > /harddisk/etc/mtab");
edd536b6 631
423400cf
MT
632 /*
633 * Generate device.map to help grub finding the device to install itself on.
634 */
635 FILE *f = NULL;
636 if (f = fopen("/harddisk/boot/grub/device.map", "w")) {
d7dd283b 637 fprintf(f, "(hd0) %s\n", destination->path);
423400cf
MT
638 fclose(f);
639 }
640
edd536b6 641 snprintf(commandstring, STRING_SIZE,
d7dd283b 642 "/usr/sbin/chroot /harddisk /usr/sbin/grub-install --no-floppy %s", destination->path);
edd536b6
MT
643 if (runcommandwithstatus(commandstring, ctr[TR_INSTALLING_GRUB])) {
644 errorbox(ctr[TR_UNABLE_TO_INSTALL_GRUB]);
645 goto EXIT;
646 }
5faa66cf
AF
647
648 /* Serial console ? */
649 if (serialconsole) {
650 /* grub */
651 replace("/harddisk/boot/grub/grub.conf", "splashimage", "#splashimage");
652 replace("/harddisk/boot/grub/grub.conf", "#serial", "serial");
653 replace("/harddisk/boot/grub/grub.conf", "#terminal", "terminal");
1445c9e0 654 replace("/harddisk/boot/grub/grub.conf", " panic=10 ", " console=ttyS0,115200n8 panic=10 ");
5faa66cf
AF
655
656 /*inittab*/
657 replace("/harddisk/etc/inittab", "1:2345:respawn:", "#1:2345:respawn:");
658 replace("/harddisk/etc/inittab", "2:2345:respawn:", "#2:2345:respawn:");
659 replace("/harddisk/etc/inittab", "3:2345:respawn:", "#3:2345:respawn:");
660 replace("/harddisk/etc/inittab", "4:2345:respawn:", "#4:2345:respawn:");
661 replace("/harddisk/etc/inittab", "5:2345:respawn:", "#5:2345:respawn:");
662 replace("/harddisk/etc/inittab", "6:2345:respawn:", "#6:2345:respawn:");
663 replace("/harddisk/etc/inittab", "#7:2345:respawn:", "7:2345:respawn:");
664 }
665
dfa59dbd
AF
666 /* Set marker that the user has already accepted the gpl */
667 mysystem("/usr/bin/touch /harddisk/var/ipfire/main/gpl_accepted");
668
c25a0343 669 /* Copy restore file from cdrom */
7062cecd 670 if (unattended && (strlen(restore_file) > 0)) {
c25a0343 671 fprintf(flog, "unattended: Copy restore file\n");
dca095e1 672 snprintf(commandstring, STRING_SIZE,
c25a0343 673 "cp /cdrom/%s /harddisk/var/ipfire/backup", restore_file);
dca095e1 674 mysystem(commandstring);
c25a0343
DG
675 }
676
8de160ff 677 mysystem("umount /cdrom");
23ed79cb 678 snprintf(commandstring, STRING_SIZE, "/usr/bin/eject /dev/%s", sourcedrive);
3ef6c343 679 mysystem(commandstring);
22b9e405 680
73d9a908
MT
681 if (!unattended) {
682 sprintf(message, ctr[TR_CONGRATULATIONS_LONG],
3ad8835c 683 NAME, SNAME, NAME);
fac85ccd 684 newtWinMessage(ctr[TR_CONGRATULATIONS], ctr[TR_PRESS_OK_TO_REBOOT], message);
73d9a908 685 }
cd8dd8dd 686
22b9e405 687 allok = 1;
edd536b6 688
d6aaa55d 689EXIT:
10bc6f06 690 fprintf(flog, "Install program ended.\n");
d6aaa55d 691
10bc6f06 692 if (!(allok))
d6aaa55d 693 newtWinMessage(title, ctr[TR_OK], ctr[TR_PRESS_OK_TO_REBOOT]);
10bc6f06 694
d6aaa55d 695 freekeyvalues(ethernetkv);
d6aaa55d 696
10bc6f06 697 if (allok && !allok_fastexit)
d6aaa55d 698 {
ee78a5ef
MT
699 if (unattended) {
700 fprintf(flog, "Entering unattended setup\n");
701 if (unattended_setup(unattendedkv)) {
c78a77eb
MT
702 snprintf(commandstring, STRING_SIZE, "/bin/sleep 10");
703 runcommandwithstatus(commandstring, "Unattended installation finished, system will reboot");
ee78a5ef 704 } else {
c78a77eb
MT
705 errorbox("Unattended setup failed.");
706 goto EXIT;
c78a77eb 707 }
ee78a5ef 708 }
c78a77eb 709
ee78a5ef
MT
710 fflush(flog);
711 fclose(flog);
712 newtFinished();
713
ee78a5ef
MT
714 if (system("/bin/umount /harddisk/proc"))
715 printf("Unable to umount /harddisk/proc.\n");
5057b611
HS
716 } else {
717 fflush(flog);
718 fclose(flog);
719 newtFinished();
d6aaa55d 720 }
5057b611 721
d6aaa55d
MT
722 fcloseall();
723
3a1019f6 724 if (swap_file) {
d7dd283b 725 snprintf(commandstring, STRING_SIZE, "/bin/swapoff %s", destination->part_swap);
3a1019f6
MT
726 }
727
728 newtFinished();
729
942d7058
MT
730 system("/bin/umount /harddisk/proc >/dev/null 2>&1");
731 system("/bin/umount /harddisk/dev >/dev/null 2>&1");
732 system("/bin/umount /harddisk/sys >/dev/null 2>&1");
5057b611 733
942d7058
MT
734 system("/bin/umount /harddisk/var >/dev/null 2>&1");
735 system("/bin/umount /harddisk/boot >/dev/null 2>&1");
736 system("/bin/umount /harddisk >/dev/null 2>&1");
6cf9e770
AF
737
738 if (!(allok))
739 system("/etc/halt");
d6aaa55d 740
f0fa1795
MT
741 // Free resources
742 free(sourcedrive);
d7dd283b
MT
743 free(destination);
744
745 if (selected_disks)
746 hw_free_disks(selected_disks);
747
748 hw_free(hw);
f0fa1795 749
d6aaa55d
MT
750 return 0;
751}