1 /* SmoothWall install program.
3 * This program is distributed under the terms of the GNU General Public
4 * Licence. See the file COPYING for details.
6 * (c) Lawrence Manning, 2001
7 * Contains main entry point, and misc functions.6
14 #define INST_FILECOUNT 6200
15 #define UNATTENDED_CONF "/cdrom/boot/unattended.conf"
26 extern char url
[STRING_SIZE
];
28 struct nic nics
[20] = { { "" , "" , "" } }; // only defined for compile
29 struct knic knics
[20] = { { "" , "" , "" , "" } }; // only defined for compile
34 int main(int argc
, char *argv
[])
36 char *langnames
[] = { "Deutsch", "English", NULL
};
37 char *shortlangnames
[] = { "de", "en", NULL
};
38 char **langtrs
[] = { de_tr
, en_tr
, NULL
};
40 char harddrive
[30], sourcedrive
[5]; /* Device holder. */
41 struct devparams hdparams
, cdromparams
; /* Params for CDROM and HD */
43 char commandstring
[STRING_SIZE
];
44 char mkfscommand
[STRING_SIZE
];
45 char *fstypes
[] = { "Reiser4", "ReiserFS", "ext3", NULL
};
46 int fstype
= REISERFS
;
51 char shortlangname
[10];
53 char title
[STRING_SIZE
];
57 struct keyvalue
*ethernetkv
= initkeyvalues();
58 FILE *handle
, *cmdfile
;
59 char line
[STRING_SIZE
];
60 char string
[STRING_SIZE
];
61 long memory
= 0, disk
= 0, free
;
62 long system_partition
, boot_partition
, root_partition
, swap_file
;
64 char *yesnoharddisk
[3]; // char *yesnoharddisk = { "NO", "YES", NULL };
67 struct keyvalue
*unattendedkv
= initkeyvalues();
69 char restore_file
[STRING_SIZE
] = "";
71 setlocale (LC_ALL
, "");
72 sethostname( SNAME
, 10);
74 memset(&hdparams
, 0, sizeof(struct devparams
));
75 memset(&cdromparams
, 0, sizeof(struct devparams
));
77 /* Log file/terminal stuff. */
80 if (!(flog
= fopen(argv
[1], "w+")))
88 fprintf(flog
, "Install program started.\n");
93 newtDrawRootText(14, 0, NAME
" " VERSION
" - " SLOGAN
);
94 sprintf (title
, "%s %s - %s", NAME
, VERSION
, SLOGAN
);
96 if (! (cmdfile
= fopen("/proc/cmdline", "r")))
98 fprintf(flog
, "Couldn't open commandline: /proc/cmdline\n");
100 fgets(line
, STRING_SIZE
, cmdfile
);
102 // check if we have to make an unattended install
103 if (strstr (line
, "unattended") != NULL
) {
105 runcommandwithstatus("/bin/sleep 10", "WARNING: Unattended installation will start in 10 seconds...");
109 // Load ata-piix prior kudzu because kudzu use ata-generic for ich7
110 mysystem("/sbin/modprobe ata_piix");
112 // Starting hardware detection
113 runcommandwithstatus("/bin/probehw.sh", "Probing Hardware ...");
115 // Load common modules
116 mysystem("/sbin/modprobe ide-generic");
117 mysystem("/sbin/modprobe ide-cd");
118 mysystem("/sbin/modprobe ide-disk");
119 mysystem("/sbin/modprobe uhci-hcd");
120 mysystem("/sbin/modprobe ohci-hcd");
121 mysystem("/sbin/modprobe ehci-hcd");
122 mysystem("/sbin/modprobe ohci1394");
123 mysystem("/sbin/modprobe sd_mod");
124 mysystem("/sbin/modprobe sr_mod");
125 mysystem("/sbin/modprobe usb-storage");
126 mysystem("/sbin/modprobe usbhid");
128 mysystem("/sbin/modprobe iso9660"); // CDROM
129 mysystem("/sbin/modprobe ext2"); // Boot patition
130 mysystem("/sbin/modprobe vfat"); // USB key
132 runcommandwithstatus("/bin/sleep 10", "Waiting for USB Hardware ...");
134 /* German is the default */
135 for (choice
= 0; langnames
[choice
]; choice
++)
137 if (strcmp(langnames
[choice
], "Deutsch") == 0)
140 if (!langnames
[choice
])
144 rc
= newtWinMenu("Language selection", "Select the language you wish to use for the " NAME
".", 50, 5, 5, 8,
145 langnames
, &choice
, "Ok", NULL
);
148 ctr
= langtrs
[choice
];
149 strcpy(shortlangname
, shortlangnames
[choice
]);
151 newtPushHelpLine(ctr
[TR_HELPLINE
]);
153 sprintf(message
, ctr
[TR_WELCOME
], NAME
);
154 newtWinMessage(title
, ctr
[TR_OK
], message
);
156 switch (mysystem("/bin/mountsource.sh")) {
160 errorbox(ctr
[TR_NO_CDROM
]);
164 /* read source drive letter */
165 if ((handle
= fopen("/tmp/source_device", "r")) == NULL
) {
166 errorbox(ctr
[TR_ERROR_PROBING_CDROM
]);
169 fgets(sourcedrive
, 5, handle
);
170 fprintf(flog
, "Source drive: %s\n", sourcedrive
);
176 fprintf(flog
, "Harddisk scan pass %i\n", i
);
178 switch (mysystem("/bin/mountdest.sh") % 255) {
179 case 0: // Found IDE disk
184 case 1: // Found SCSI disk
189 case 2: // Found RAID disk
194 case 10: // No harddisk found
196 errorbox(ctr
[TR_NO_HARDDISK
]);
199 // Do this if the kudzu-scan fails...
200 runcommandwithstatus("/bin/probehw.sh deep-scan", ctr
[TR_PROBING_HARDWARE
]);
205 if ((handle
= fopen("/tmp/dest_device", "r")) == NULL
) {
206 errorbox(ctr
[TR_NO_HARDDISK
]);
209 fgets(harddrive
, 30, handle
);
212 /* load unattended configuration */
214 fprintf(flog
, "unattended: Reading unattended.conf\n");
216 (void) readkeyvalues(unattendedkv
, UNATTENDED_CONF
);
217 findkey(unattendedkv
, "RESTORE_FILE", restore_file
);
220 /* Make the hdparms struct and print the contents.
221 With USB-KEY install and SCSI disk, while installing, the disk
222 is named 'sdb,sdc,...' (following keys)
223 On reboot, it will become 'sda'
224 To avoid many test, all names are built in the struct.
226 sprintf(hdparams
.devnode_disk
, "/dev/%s", harddrive
);
227 /* Address the partition or raid partition (eg dev/sda or /dev/sdap1 */
228 sprintf(hdparams
.devnode_part
, "/dev/%s%s", harddrive
,raid_disk
? "p" : "");
229 /* Now the names after the machine is booted. Only scsi is affected
230 and we only install on the first scsi disk. */
232 strcpy(tmp
, scsi_disk
? "sda" : harddrive
);
233 sprintf(hdparams
.devnode_disk_run
, "/dev/%s", tmp
);
234 sprintf(hdparams
.devnode_part_run
, "/dev/%s%s", tmp
, raid_disk
? "p" : "");
237 fprintf(flog
, "Destination drive: %s\n", hdparams
.devnode_disk
);
239 sprintf(message
, ctr
[TR_PREPARE_HARDDISK
], hdparams
.devnode_disk
);
243 yesnoharddisk
[0] = ctr
[TR_NO
];
244 yesnoharddisk
[1] = ctr
[TR_YES
];
245 yesnoharddisk
[2] = NULL
;
249 rc
= newtWinMenu(title
, message
,
250 50, 5, 5, 6, yesnoharddisk
,
252 ctr
[TR_CANCEL
], NULL
);
260 sprintf(message
, ctr
[TR_CHOOSE_FILESYSTEM
]);
261 rc
= newtWinMenu( ctr
[TR_CHOOSE_FILESYSTEM
], message
,
262 50, 5, 5, 6, fstypes
, &fstype
, ctr
[TR_OK
],
263 ctr
[TR_CANCEL
], NULL
);
271 /* Calculate amount of memory in machine */
272 if ((handle
= fopen("/proc/meminfo", "r")))
274 while (fgets(line
, STRING_SIZE
-1, handle
)) {
275 if (sscanf (line
, "MemTotal: %s kB", string
)) {
276 memory
= atoi(string
) / 1024 ;
282 /* Partition, mkswp, mkfs.
283 * before partitioning, first determine the sizes of each
284 * partition. In order to do that we need to know the size of
287 /* Don't use mysystem here so we can redirect output */
288 sprintf(commandstring
, "/bin/sfdisk -s /dev/%s > /tmp/disksize 2> /dev/null", harddrive
);
289 system(commandstring
);
291 /* Calculate amount of disk space */
292 if ((handle
= fopen("/tmp/disksize", "r"))) {
293 fgets(line
, STRING_SIZE
-1, handle
);
294 if (sscanf (line
, "%s", string
)) {
295 disk
= atoi(string
) / 1024;
300 fprintf(flog
, "Disksize = %ld, memory = %ld", disk
, memory
);
302 /* Calculating Swap-Size dependend of Ram Size */
305 else if (memory
>= 1024)
310 /* Calculating Root-Size dependend of Max Disk Space */
312 root_partition
= 200;
313 else if ( disk
>= 756 && disk
<= 3072 )
314 root_partition
= 512;
316 root_partition
= 2048;
319 /* Calculating the amount of free space */
320 boot_partition
= 20; /* in MB */
321 system_partition
= disk
- ( root_partition
+ swap_file
+ boot_partition
);
323 fprintf(flog
, ", boot = %ld, swap = %ld, mylog = %ld, root = %ld\n",
324 boot_partition
, swap_file
, system_partition
, root_partition
);
327 if ( (!unattended
) && (((disk
- (root_partition
+ swap_file
+ boot_partition
)) < 256 ) && ((disk
- (root_partition
+ boot_partition
)) > 256)) ) {
328 rc
= newtWinChoice(title
, ctr
[TR_OK
], ctr
[TR_CANCEL
], ctr
[TR_CONTINUE_NO_SWAP
]);
331 system_partition
= disk
- ( root_partition
+ swap_file
+ boot_partition
);
332 fprintf(flog
, "Changing Swap Size to 0 MB.\n");
335 fprintf(flog
, "Disk is too small.\n");
336 errorbox(ctr
[TR_DISK_TOO_SMALL
]);goto EXIT
;
339 else if (disk
- (root_partition
+ swap_file
+ boot_partition
) >= 256) {
343 fprintf(flog
, "Disk is too small.\n");
344 errorbox(ctr
[TR_DISK_TOO_SMALL
]);goto EXIT
;
347 handle
= fopen("/tmp/partitiontable", "w");
351 fprintf(handle
, ",%ld,L,*\n,%ld,S,\n,%ld,L,\n,,L,\n",
352 boot_partition
, swap_file
, root_partition
);
354 fprintf(handle
, ",%ld,L,*\n,0,0,\n,%ld,L,\n,,L,\n",
355 boot_partition
, root_partition
);
360 snprintf(commandstring
, STRING_SIZE
, "/bin/sfdisk -L -uM %s < /tmp/partitiontable", hdparams
.devnode_disk
);
361 if (runcommandwithstatus(commandstring
, ctr
[TR_PARTITIONING_DISK
]))
363 errorbox(ctr
[TR_UNABLE_TO_PARTITION
]);
367 if (fstype
== REISER4
) {
368 mysystem("/sbin/modprobe reiser4");
369 sprintf(mkfscommand
, "/sbin/mkfs.reiser4 -y");
370 } else if (fstype
== REISERFS
) {
371 mysystem("/sbin/modprobe reiserfs");
372 sprintf(mkfscommand
, "/sbin/mkreiserfs -f");
373 } else if (fstype
== EXT3
) {
374 mysystem("/sbin/modprobe ext3");
375 sprintf(mkfscommand
, "/sbin/mke2fs -T ext3 -c");
378 snprintf(commandstring
, STRING_SIZE
, "/sbin/mke2fs -T ext2 -c %s1", hdparams
.devnode_part
);
379 if (runcommandwithstatus(commandstring
, ctr
[TR_MAKING_BOOT_FILESYSTEM
]))
381 errorbox(ctr
[TR_UNABLE_TO_MAKE_BOOT_FILESYSTEM
]);
386 snprintf(commandstring
, STRING_SIZE
, "/sbin/mkswap %s2", hdparams
.devnode_part
);
387 if (runcommandwithstatus(commandstring
, ctr
[TR_MAKING_SWAPSPACE
]))
389 errorbox(ctr
[TR_UNABLE_TO_MAKE_SWAPSPACE
]);
394 snprintf(commandstring
, STRING_SIZE
, "%s %s3", mkfscommand
, hdparams
.devnode_part
);
395 if (runcommandwithstatus(commandstring
, ctr
[TR_MAKING_ROOT_FILESYSTEM
]))
397 errorbox(ctr
[TR_UNABLE_TO_MAKE_ROOT_FILESYSTEM
]);
401 snprintf(commandstring
, STRING_SIZE
, "%s %s4", mkfscommand
, hdparams
.devnode_part
);
402 if (runcommandwithstatus(commandstring
, ctr
[TR_MAKING_LOG_FILESYSTEM
]))
404 errorbox(ctr
[TR_UNABLE_TO_MAKE_LOG_FILESYSTEM
]);
408 snprintf(commandstring
, STRING_SIZE
, "/bin/mount %s3 /harddisk", hdparams
.devnode_part
);
409 if (runcommandwithstatus(commandstring
, ctr
[TR_MOUNTING_ROOT_FILESYSTEM
]))
411 errorbox(ctr
[TR_UNABLE_TO_MOUNT_ROOT_FILESYSTEM
]);
415 mkdir("/harddisk/boot", S_IRWXU
|S_IRWXG
|S_IRWXO
);
416 mkdir("/harddisk/var", S_IRWXU
|S_IRWXG
|S_IRWXO
);
417 mkdir("/harddisk/var/log", S_IRWXU
|S_IRWXG
|S_IRWXO
);
419 snprintf(commandstring
, STRING_SIZE
, "/bin/mount %s1 /harddisk/boot", hdparams
.devnode_part
);
420 if (runcommandwithstatus(commandstring
, ctr
[TR_MOUNTING_BOOT_FILESYSTEM
]))
422 errorbox(ctr
[TR_UNABLE_TO_MOUNT_BOOT_FILESYSTEM
]);
426 snprintf(commandstring
, STRING_SIZE
, "/sbin/swapon %s2", hdparams
.devnode_part
);
427 if (runcommandwithstatus(commandstring
, ctr
[TR_MOUNTING_SWAP_PARTITION
]))
429 errorbox(ctr
[TR_UNABLE_TO_MOUNT_SWAP_PARTITION
]);
433 snprintf(commandstring
, STRING_SIZE
, "/bin/mount %s4 /harddisk/var", hdparams
.devnode_part
);
434 if (runcommandwithstatus(commandstring
, ctr
[TR_MOUNTING_LOG_FILESYSTEM
]))
436 errorbox(ctr
[TR_UNABLE_TO_MOUNT_LOG_FILESYSTEM
]);
440 snprintf(commandstring
, STRING_SIZE
,
441 "/bin/tar -C /harddisk -xvjf /cdrom/" SNAME
"-" VERSION
".tbz2");
443 if (runcommandwithprogress(60, 4, title
, commandstring
, INST_FILECOUNT
,
444 ctr
[TR_INSTALLING_FILES
]))
446 errorbox(ctr
[TR_UNABLE_TO_INSTALL_FILES
]);
450 /* Save language und local settings */
451 write_lang_configs(shortlangname
);
453 /* touch the modules.dep files */
454 snprintf(commandstring
, STRING_SIZE
,
455 "/bin/touch /harddisk/lib/modules/%s-ipfire/modules.dep",
457 mysystem(commandstring
);
458 /* snprintf(commandstring, STRING_SIZE,
459 "/bin/touch /harddisk/lib/modules/%s-ipfire-smp/modules.dep",
461 mysystem(commandstring);
465 rename ("/harddisk/bin/uname.bak", "/harddisk/bin/uname");
467 /* mount proc filesystem */
468 mysystem("mkdir /harddisk/proc");
469 mysystem("/bin/mount --bind /proc /harddisk/proc");
470 mysystem("/bin/mount --bind /dev /harddisk/dev");
471 mysystem("/bin/mount --bind /sys /harddisk/sys");
473 /* Build cache lang file */
474 snprintf(commandstring
, STRING_SIZE
, "/sbin/chroot /harddisk /usr/bin/perl -e \"require '" CONFIG_ROOT
"/lang.pl'; &Lang::BuildCacheLang\"");
475 if (runcommandwithstatus(commandstring
, ctr
[TR_INSTALLING_LANG_CACHE
]))
477 errorbox(ctr
[TR_UNABLE_TO_INSTALL_LANG_CACHE
]);
481 /* Update /etc/fstab */
482 replace("/harddisk/etc/fstab", "DEVICE", hdparams
.devnode_part_run
);
484 if (fstype
== REISER4
) {
485 replace("/harddisk/etc/fstab", "FSTYPE", "reiser4");
486 replace("/harddisk/boot/grub/grub.conf", "MOUNT", "rw");
487 } else if (fstype
== REISERFS
) {
488 replace("/harddisk/etc/fstab", "FSTYPE", "reiserfs");
489 replace("/harddisk/boot/grub/grub.conf", "MOUNT", "ro");
490 } else if (fstype
== EXT3
) {
491 snprintf(commandstring
, STRING_SIZE
, "tune2fs -j %s3", hdparams
.devnode_part
);
492 if (runcommandwithstatus(commandstring
, ctr
[TR_JOURNAL_EXT3
]))
494 errorbox(ctr
[TR_JOURNAL_ERROR
]);
495 replace("/harddisk/etc/fstab", "FSTYPE", "ext2");
498 snprintf(commandstring
, STRING_SIZE
, "tune2fs -j %s4", hdparams
.devnode_part
);
499 if (runcommandwithstatus(commandstring
, ctr
[TR_JOURNAL_EXT3
]))
501 errorbox(ctr
[TR_JOURNAL_ERROR
]);
502 replace("/harddisk/etc/fstab", "FSTYPE", "ext2");
505 replace("/harddisk/etc/fstab", "FSTYPE", "ext3");
507 replace("/harddisk/boot/grub/grub.conf", "MOUNT", "ro");
510 replace("/harddisk/boot/grub/grub.conf", "KVER", KERNEL_VERSION
);
512 /* Build the emergency ramdisk with all drivers */
513 mysystem("cp -f /harddisk/etc/mkinitcpio.conf /harddisk/etc/mkinitcpio.conf.org");
515 replace("/harddisk/etc/mkinitcpio.conf", " autodetect ", " ");
516 snprintf(commandstring
, STRING_SIZE
, "/sbin/chroot /harddisk /sbin/mkinitcpio -g /boot/ipfirerd-%s-emergency.img -k %s-ipfire", KERNEL_VERSION
, KERNEL_VERSION
);
517 runcommandwithstatus(commandstring
, ctr
[TR_BUILDING_INITRD
]);
519 mysystem("cp -f /harddisk/etc/mkinitcpio.conf.org /harddisk/etc/mkinitcpio.conf");
521 /* mkinitcpio has a problem if ide and pata are included */
522 if ( scsi_disk
==1 ) {
523 /* Remove the ide hook if we install sda */
524 replace("/harddisk/etc/mkinitcpio.conf", " ide ", " ");
526 /* Remove the pata & sata hook if we install hda */
527 replace("/harddisk/etc/mkinitcpio.conf", " pata ", " ");
528 replace("/harddisk/etc/mkinitcpio.conf", " sata ", " ");
530 /* Going to make our initrd... */
531 snprintf(commandstring
, STRING_SIZE
, "/sbin/chroot /harddisk /sbin/mkinitcpio -g /boot/ipfirerd-%s.img -k %s-ipfire", KERNEL_VERSION
, KERNEL_VERSION
);
532 runcommandwithstatus(commandstring
, ctr
[TR_BUILDING_INITRD
]);
533 /* snprintf(commandstring, STRING_SIZE, "/sbin/chroot /harddisk /sbin/mkinitcpio -g /boot/ipfirerd-%s-smp.img -k %s-ipfire-smp", KERNEL_VERSION, KERNEL_VERSION );
534 runcommandwithstatus(commandstring, ctr[TR_BUILDING_INITRD]);
538 sprintf(string
, "root=%s3", hdparams
.devnode_part_run
);
539 replace( "/harddisk/boot/grub/grub.conf", "root=ROOT", string
);
540 mysystem("ln -s grub.conf /harddisk/boot/grub/menu.lst");
542 system("sed -e 's#harddisk\\/##g' < /proc/mounts > /harddisk/etc/mtab");
544 snprintf(commandstring
, STRING_SIZE
,
545 "/sbin/chroot /harddisk /usr/sbin/grub-install --no-floppy %s", hdparams
.devnode_disk
);
546 if (runcommandwithstatus(commandstring
, ctr
[TR_INSTALLING_GRUB
])) {
547 errorbox(ctr
[TR_UNABLE_TO_INSTALL_GRUB
]);
551 /* Copy restore file from cdrom */
552 if (unattended
&& (strlen(restore_file
) > 0)) {
553 fprintf(flog
, "unattended: Copy restore file\n");
554 snprintf(commandstring
, STRING_SIZE
,
555 "cp /cdrom/%s /harddisk/var/ipfire/backup", restore_file
);
556 mysystem(commandstring
);
559 mysystem("umount /cdrom");
560 snprintf(commandstring
, STRING_SIZE
, "eject /dev/%s", sourcedrive
);
561 mysystem(commandstring
);
564 sprintf(message
, ctr
[TR_CONGRATULATIONS_LONG
],
566 newtWinMessage(ctr
[TR_CONGRATULATIONS
], ctr
[TR_OK
], message
);
572 fprintf(flog
, "Install program ended.\n");
575 newtWinMessage(title
, ctr
[TR_OK
], ctr
[TR_PRESS_OK_TO_REBOOT
]);
577 freekeyvalues(ethernetkv
);
579 if (allok
&& !allok_fastexit
)
582 fprintf(flog
, "Entering unattended setup\n");
583 if (unattended_setup(unattendedkv
)) {
584 snprintf(commandstring
, STRING_SIZE
, "/bin/sleep 10");
585 runcommandwithstatus(commandstring
, "Unattended installation finished, system will reboot");
587 errorbox("Unattended setup failed.");
597 if (system("/sbin/chroot /harddisk /usr/local/sbin/setup /dev/tty2 INSTALL"))
598 printf("Unable to run setup.\n");
601 if (system("/bin/umount /harddisk/proc"))
602 printf("Unable to umount /harddisk/proc.\n");
612 snprintf(commandstring
, STRING_SIZE
, "/bin/swapoff %s2", hdparams
.devnode_part
);
617 system("/bin/umount /harddisk/proc");
618 system("/bin/umount /harddisk/dev");
619 system("/bin/umount /harddisk/sys");
621 system("/bin/umount /harddisk/var");
622 system("/bin/umount /harddisk/boot");
623 system("/bin/umount /harddisk");