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
35 int main(int argc
, char *argv
[])
37 char *langnames
[] = { "Deutsch", "English", "Français", NULL
};
38 char *shortlangnames
[] = { "de", "en", "fr" ,NULL
};
39 char **langtrs
[] = { de_tr
, en_tr
, fr_tr
,NULL
};
41 char harddrive
[30], sourcedrive
[5]; /* Device holder. */
42 struct devparams hdparams
, cdromparams
; /* Params for CDROM and HD */
44 char commandstring
[STRING_SIZE
];
45 char mkfscommand
[STRING_SIZE
];
46 char *fstypes
[] = { "Reiser4", "ReiserFS", "ext3", NULL
};
47 int fstype
= REISERFS
;
52 char shortlangname
[10];
54 char title
[STRING_SIZE
];
58 struct keyvalue
*ethernetkv
= initkeyvalues();
59 FILE *handle
, *cmdfile
;
60 char line
[STRING_SIZE
];
61 char string
[STRING_SIZE
];
62 long memory
= 0, disk
= 0, free
;
63 long system_partition
, boot_partition
, root_partition
, swap_file
;
65 char *yesnoharddisk
[3]; // char *yesnoharddisk = { "NO", "YES", NULL };
68 struct keyvalue
*unattendedkv
= initkeyvalues();
70 char restore_file
[STRING_SIZE
] = "";
72 setlocale (LC_ALL
, "");
73 sethostname( SNAME
, 10);
75 memset(&hdparams
, 0, sizeof(struct devparams
));
76 memset(&cdromparams
, 0, sizeof(struct devparams
));
78 /* Log file/terminal stuff. */
81 if (!(flog
= fopen(argv
[1], "w+")))
89 fprintf(flog
, "Install program started.\n");
94 newtDrawRootText(14, 0, NAME
" " VERSION
" - " SLOGAN
);
95 sprintf (title
, "%s %s - %s", NAME
, VERSION
, SLOGAN
);
97 if (! (cmdfile
= fopen("/proc/cmdline", "r")))
99 fprintf(flog
, "Couldn't open commandline: /proc/cmdline\n");
101 fgets(line
, STRING_SIZE
, cmdfile
);
103 // check if we have to make an unattended install
104 if (strstr (line
, "unattended") != NULL
) {
106 runcommandwithstatus("/bin/sleep 10", "WARNING: Unattended installation will start in 10 seconds...");
110 // Load ata-piix prior kudzu because kudzu use ata-generic for ich7
111 mysystem("/sbin/modprobe ata_piix");
113 // Starting hardware detection
114 runcommandwithstatus("/bin/probehw.sh", "Probing Hardware ...");
116 // Load common modules
117 mysystem("/sbin/modprobe ide-generic");
118 mysystem("/sbin/modprobe ide-cd");
119 mysystem("/sbin/modprobe ide-disk");
120 mysystem("/sbin/modprobe ehci-hcd");
121 mysystem("/sbin/modprobe uhci-hcd");
122 mysystem("/sbin/modprobe ohci-hcd");
123 mysystem("/sbin/modprobe ohci1394");
124 mysystem("/sbin/modprobe sd_mod");
125 mysystem("/sbin/modprobe sr_mod");
126 mysystem("/sbin/modprobe usb-storage");
127 mysystem("/sbin/modprobe usbhid");
129 mysystem("/sbin/modprobe iso9660"); // CDROM
130 mysystem("/sbin/modprobe ext2"); // Boot patition
131 mysystem("/sbin/modprobe vfat"); // USB key
133 runcommandwithstatus("/bin/sleep 10", "Waiting for USB Hardware ...");
135 /* German is the default */
136 for (choice
= 0; langnames
[choice
]; choice
++)
138 if (strcmp(langnames
[choice
], "Deutsch") == 0)
141 if (!langnames
[choice
])
145 rc
= newtWinMenu("Language selection", "Select the language you wish to use for the " NAME
".", 50, 5, 5, 8,
146 langnames
, &choice
, "Ok", NULL
);
149 ctr
= langtrs
[choice
];
150 strcpy(shortlangname
, shortlangnames
[choice
]);
152 newtPushHelpLine(ctr
[TR_HELPLINE
]);
154 sprintf(message
, ctr
[TR_WELCOME
], NAME
);
155 newtWinMessage(title
, ctr
[TR_OK
], message
);
157 switch (mysystem("/bin/mountsource.sh")) {
161 errorbox(ctr
[TR_NO_CDROM
]);
165 /* read source drive letter */
166 if ((handle
= fopen("/tmp/source_device", "r")) == NULL
) {
167 errorbox(ctr
[TR_ERROR_PROBING_CDROM
]);
170 fgets(sourcedrive
, 5, handle
);
171 fprintf(flog
, "Source drive: %s\n", sourcedrive
);
177 fprintf(flog
, "Harddisk scan pass %i\n", i
);
179 switch (mysystem("/bin/mountdest.sh") % 255) {
180 case 0: // Found IDE disk
185 case 1: // Found SCSI disk
190 case 2: // Found RAID disk
195 case 10: // No harddisk found
197 errorbox(ctr
[TR_NO_HARDDISK
]);
200 // Do this if the kudzu-scan fails...
201 runcommandwithstatus("/bin/probehw.sh deep-scan", ctr
[TR_PROBING_HARDWARE
]);
206 if ((handle
= fopen("/tmp/dest_device", "r")) == NULL
) {
207 errorbox(ctr
[TR_NO_HARDDISK
]);
210 fgets(harddrive
, 30, handle
);
213 /* load unattended configuration */
215 fprintf(flog
, "unattended: Reading unattended.conf\n");
217 (void) readkeyvalues(unattendedkv
, UNATTENDED_CONF
);
218 findkey(unattendedkv
, "RESTORE_FILE", restore_file
);
221 /* Make the hdparms struct and print the contents.
222 With USB-KEY install and SCSI disk, while installing, the disk
223 is named 'sdb,sdc,...' (following keys)
224 On reboot, it will become 'sda'
225 To avoid many test, all names are built in the struct.
227 sprintf(hdparams
.devnode_disk
, "/dev/%s", harddrive
);
228 /* Address the partition or raid partition (eg dev/sda or /dev/sdap1 */
229 sprintf(hdparams
.devnode_part
, "/dev/%s%s", harddrive
,raid_disk
? "p" : "");
230 /* Now the names after the machine is booted. Only scsi is affected
231 and we only install on the first scsi disk. */
233 strcpy(tmp
, scsi_disk
? "sda" : harddrive
);
234 sprintf(hdparams
.devnode_disk_run
, "/dev/%s", tmp
);
235 sprintf(hdparams
.devnode_part_run
, "/dev/%s%s", tmp
, raid_disk
? "p" : "");
238 fprintf(flog
, "Destination drive: %s\n", hdparams
.devnode_disk
);
240 sprintf(message
, ctr
[TR_PREPARE_HARDDISK
], hdparams
.devnode_disk
);
244 yesnoharddisk
[0] = ctr
[TR_NO
];
245 yesnoharddisk
[1] = ctr
[TR_YES
];
246 yesnoharddisk
[2] = NULL
;
250 rc
= newtWinMenu(title
, message
,
251 50, 5, 5, 6, yesnoharddisk
,
253 ctr
[TR_CANCEL
], NULL
);
261 sprintf(message
, ctr
[TR_CHOOSE_FILESYSTEM
]);
262 rc
= newtWinMenu( ctr
[TR_CHOOSE_FILESYSTEM
], message
,
263 50, 5, 5, 6, fstypes
, &fstype
, ctr
[TR_OK
],
264 ctr
[TR_CANCEL
], NULL
);
272 /* Calculate amount of memory in machine */
273 if ((handle
= fopen("/proc/meminfo", "r")))
275 while (fgets(line
, STRING_SIZE
-1, handle
)) {
276 if (sscanf (line
, "MemTotal: %s kB", string
)) {
277 memory
= atoi(string
) / 1024 ;
283 /* Partition, mkswp, mkfs.
284 * before partitioning, first determine the sizes of each
285 * partition. In order to do that we need to know the size of
288 /* Don't use mysystem here so we can redirect output */
289 sprintf(commandstring
, "/bin/sfdisk -s /dev/%s > /tmp/disksize 2> /dev/null", harddrive
);
290 system(commandstring
);
292 /* Calculate amount of disk space */
293 if ((handle
= fopen("/tmp/disksize", "r"))) {
294 fgets(line
, STRING_SIZE
-1, handle
);
295 if (sscanf (line
, "%s", string
)) {
296 disk
= atoi(string
) / 1024;
301 fprintf(flog
, "Disksize = %ld, memory = %ld", disk
, memory
);
303 /* Calculating Swap-Size dependend of Ram Size */
306 else if (memory
>= 1024)
311 /* Calculating Root-Size dependend of Max Disk Space */
313 root_partition
= 200;
314 else if ( disk
>= 756 && disk
<= 3072 )
315 root_partition
= 512;
317 root_partition
= 2048;
320 /* Calculating the amount of free space */
321 boot_partition
= 20; /* in MB */
322 system_partition
= disk
- ( root_partition
+ swap_file
+ boot_partition
);
324 fprintf(flog
, ", boot = %ld, swap = %ld, mylog = %ld, root = %ld\n",
325 boot_partition
, swap_file
, system_partition
, root_partition
);
328 if ( (!unattended
) && (((disk
- (root_partition
+ swap_file
+ boot_partition
)) < 256 ) && ((disk
- (root_partition
+ boot_partition
)) > 256)) ) {
329 rc
= newtWinChoice(title
, ctr
[TR_OK
], ctr
[TR_CANCEL
], ctr
[TR_CONTINUE_NO_SWAP
]);
332 system_partition
= disk
- ( root_partition
+ swap_file
+ boot_partition
);
333 fprintf(flog
, "Changing Swap Size to 0 MB.\n");
336 fprintf(flog
, "Disk is too small.\n");
337 errorbox(ctr
[TR_DISK_TOO_SMALL
]);goto EXIT
;
340 else if (disk
- (root_partition
+ swap_file
+ boot_partition
) >= 256) {
344 fprintf(flog
, "Disk is too small.\n");
345 errorbox(ctr
[TR_DISK_TOO_SMALL
]);goto EXIT
;
348 handle
= fopen("/tmp/partitiontable", "w");
352 fprintf(handle
, ",%ld,L,*\n,%ld,S,\n,%ld,L,\n,,L,\n",
353 boot_partition
, swap_file
, root_partition
);
355 fprintf(handle
, ",%ld,L,*\n,0,0,\n,%ld,L,\n,,L,\n",
356 boot_partition
, root_partition
);
361 snprintf(commandstring
, STRING_SIZE
, "/bin/sfdisk -L -uM %s < /tmp/partitiontable", hdparams
.devnode_disk
);
362 if (runcommandwithstatus(commandstring
, ctr
[TR_PARTITIONING_DISK
]))
364 errorbox(ctr
[TR_UNABLE_TO_PARTITION
]);
368 if (fstype
== REISER4
) {
369 mysystem("/sbin/modprobe reiser4");
370 sprintf(mkfscommand
, "/sbin/mkfs.reiser4 -y");
371 } else if (fstype
== REISERFS
) {
372 mysystem("/sbin/modprobe reiserfs");
373 sprintf(mkfscommand
, "/sbin/mkreiserfs -f");
374 } else if (fstype
== EXT3
) {
375 mysystem("/sbin/modprobe ext3");
376 sprintf(mkfscommand
, "/sbin/mke2fs -T ext3 -c");
379 snprintf(commandstring
, STRING_SIZE
, "/sbin/mke2fs -T ext2 -c %s1", hdparams
.devnode_part
);
380 if (runcommandwithstatus(commandstring
, ctr
[TR_MAKING_BOOT_FILESYSTEM
]))
382 errorbox(ctr
[TR_UNABLE_TO_MAKE_BOOT_FILESYSTEM
]);
387 snprintf(commandstring
, STRING_SIZE
, "/sbin/mkswap %s2", hdparams
.devnode_part
);
388 if (runcommandwithstatus(commandstring
, ctr
[TR_MAKING_SWAPSPACE
]))
390 errorbox(ctr
[TR_UNABLE_TO_MAKE_SWAPSPACE
]);
395 snprintf(commandstring
, STRING_SIZE
, "%s %s3", mkfscommand
, hdparams
.devnode_part
);
396 if (runcommandwithstatus(commandstring
, ctr
[TR_MAKING_ROOT_FILESYSTEM
]))
398 errorbox(ctr
[TR_UNABLE_TO_MAKE_ROOT_FILESYSTEM
]);
402 snprintf(commandstring
, STRING_SIZE
, "%s %s4", mkfscommand
, hdparams
.devnode_part
);
403 if (runcommandwithstatus(commandstring
, ctr
[TR_MAKING_LOG_FILESYSTEM
]))
405 errorbox(ctr
[TR_UNABLE_TO_MAKE_LOG_FILESYSTEM
]);
409 snprintf(commandstring
, STRING_SIZE
, "/bin/mount %s3 /harddisk", hdparams
.devnode_part
);
410 if (runcommandwithstatus(commandstring
, ctr
[TR_MOUNTING_ROOT_FILESYSTEM
]))
412 errorbox(ctr
[TR_UNABLE_TO_MOUNT_ROOT_FILESYSTEM
]);
416 mkdir("/harddisk/boot", S_IRWXU
|S_IRWXG
|S_IRWXO
);
417 mkdir("/harddisk/var", S_IRWXU
|S_IRWXG
|S_IRWXO
);
418 mkdir("/harddisk/var/log", S_IRWXU
|S_IRWXG
|S_IRWXO
);
420 snprintf(commandstring
, STRING_SIZE
, "/bin/mount %s1 /harddisk/boot", hdparams
.devnode_part
);
421 if (runcommandwithstatus(commandstring
, ctr
[TR_MOUNTING_BOOT_FILESYSTEM
]))
423 errorbox(ctr
[TR_UNABLE_TO_MOUNT_BOOT_FILESYSTEM
]);
427 snprintf(commandstring
, STRING_SIZE
, "/sbin/swapon %s2", hdparams
.devnode_part
);
428 if (runcommandwithstatus(commandstring
, ctr
[TR_MOUNTING_SWAP_PARTITION
]))
430 errorbox(ctr
[TR_UNABLE_TO_MOUNT_SWAP_PARTITION
]);
434 snprintf(commandstring
, STRING_SIZE
, "/bin/mount %s4 /harddisk/var", hdparams
.devnode_part
);
435 if (runcommandwithstatus(commandstring
, ctr
[TR_MOUNTING_LOG_FILESYSTEM
]))
437 errorbox(ctr
[TR_UNABLE_TO_MOUNT_LOG_FILESYSTEM
]);
441 snprintf(commandstring
, STRING_SIZE
,
442 "/bin/tar -C /harddisk -xvf /cdrom/" SNAME
"-" VERSION
".tlz --lzma");
444 if (runcommandwithprogress(60, 4, title
, commandstring
, INST_FILECOUNT
,
445 ctr
[TR_INSTALLING_FILES
]))
447 errorbox(ctr
[TR_UNABLE_TO_INSTALL_FILES
]);
451 /* Save language und local settings */
452 write_lang_configs(shortlangname
);
454 /* touch the modules.dep files */
455 snprintf(commandstring
, STRING_SIZE
,
456 "/bin/touch /harddisk/lib/modules/%s-ipfire/modules.dep",
458 mysystem(commandstring
);
459 /* snprintf(commandstring, STRING_SIZE,
460 "/bin/touch /harddisk/lib/modules/%s-ipfire-smp/modules.dep",
462 mysystem(commandstring);
466 rename ("/harddisk/bin/uname.bak", "/harddisk/bin/uname");
468 /* mount proc filesystem */
469 mysystem("mkdir /harddisk/proc");
470 mysystem("/bin/mount --bind /proc /harddisk/proc");
471 mysystem("/bin/mount --bind /dev /harddisk/dev");
472 mysystem("/bin/mount --bind /sys /harddisk/sys");
474 /* Build cache lang file */
475 snprintf(commandstring
, STRING_SIZE
, "/sbin/chroot /harddisk /usr/bin/perl -e \"require '" CONFIG_ROOT
"/lang.pl'; &Lang::BuildCacheLang\"");
476 if (runcommandwithstatus(commandstring
, ctr
[TR_INSTALLING_LANG_CACHE
]))
478 errorbox(ctr
[TR_UNABLE_TO_INSTALL_LANG_CACHE
]);
482 /* Update /etc/fstab */
483 replace("/harddisk/etc/fstab", "DEVICE", hdparams
.devnode_part_run
);
485 if (fstype
== REISER4
) {
486 replace("/harddisk/etc/fstab", "FSTYPE", "reiser4");
487 replace("/harddisk/boot/grub/grub.conf", "MOUNT", "rw");
488 } else if (fstype
== REISERFS
) {
489 replace("/harddisk/etc/fstab", "FSTYPE", "reiserfs");
490 replace("/harddisk/boot/grub/grub.conf", "MOUNT", "ro");
491 } else if (fstype
== EXT3
) {
492 snprintf(commandstring
, STRING_SIZE
, "tune2fs -j %s3", hdparams
.devnode_part
);
493 if (runcommandwithstatus(commandstring
, ctr
[TR_JOURNAL_EXT3
]))
495 errorbox(ctr
[TR_JOURNAL_ERROR
]);
496 replace("/harddisk/etc/fstab", "FSTYPE", "ext2");
499 snprintf(commandstring
, STRING_SIZE
, "tune2fs -j %s4", hdparams
.devnode_part
);
500 if (runcommandwithstatus(commandstring
, ctr
[TR_JOURNAL_EXT3
]))
502 errorbox(ctr
[TR_JOURNAL_ERROR
]);
503 replace("/harddisk/etc/fstab", "FSTYPE", "ext2");
506 replace("/harddisk/etc/fstab", "FSTYPE", "ext3");
508 replace("/harddisk/boot/grub/grub.conf", "MOUNT", "ro");
511 replace("/harddisk/boot/grub/grub.conf", "KVER", KERNEL_VERSION
);
513 /* Build the emergency ramdisk with all drivers */
514 mysystem("cp -f /harddisk/etc/mkinitcpio.conf /harddisk/etc/mkinitcpio.conf.org");
516 replace("/harddisk/etc/mkinitcpio.conf", " autodetect ", " ");
517 snprintf(commandstring
, STRING_SIZE
, "/sbin/chroot /harddisk /sbin/mkinitcpio -g /boot/ipfirerd-%s-emergency.img -k %s-ipfire", KERNEL_VERSION
, KERNEL_VERSION
);
518 runcommandwithstatus(commandstring
, ctr
[TR_BUILDING_INITRD
]);
520 mysystem("cp -f /harddisk/etc/mkinitcpio.conf.org /harddisk/etc/mkinitcpio.conf");
522 /* mkinitcpio has a problem if ide and pata are included */
523 if ( scsi_disk
==1 ) {
524 /* Remove the ide hook if we install sda */
525 replace("/harddisk/etc/mkinitcpio.conf", " ide ", " ");
527 /* Remove the pata & sata hook if we install hda */
528 replace("/harddisk/etc/mkinitcpio.conf", " pata ", " ");
529 replace("/harddisk/etc/mkinitcpio.conf", " sata ", " ");
531 /* Going to make our initrd... */
532 snprintf(commandstring
, STRING_SIZE
, "/sbin/chroot /harddisk /sbin/mkinitcpio -g /boot/ipfirerd-%s.img -k %s-ipfire", KERNEL_VERSION
, KERNEL_VERSION
);
533 runcommandwithstatus(commandstring
, ctr
[TR_BUILDING_INITRD
]);
534 /* snprintf(commandstring, STRING_SIZE, "/sbin/chroot /harddisk /sbin/mkinitcpio -g /boot/ipfirerd-%s-smp.img -k %s-ipfire-smp", KERNEL_VERSION, KERNEL_VERSION );
535 runcommandwithstatus(commandstring, ctr[TR_BUILDING_INITRD]);
539 sprintf(string
, "root=%s3", hdparams
.devnode_part_run
);
540 replace( "/harddisk/boot/grub/grub.conf", "root=ROOT", string
);
541 mysystem("ln -s grub.conf /harddisk/boot/grub/menu.lst");
543 system("sed -e 's#harddisk\\/##g' < /proc/mounts > /harddisk/etc/mtab");
545 snprintf(commandstring
, STRING_SIZE
,
546 "/sbin/chroot /harddisk /usr/sbin/grub-install --no-floppy %s", hdparams
.devnode_disk
);
547 if (runcommandwithstatus(commandstring
, ctr
[TR_INSTALLING_GRUB
])) {
548 errorbox(ctr
[TR_UNABLE_TO_INSTALL_GRUB
]);
552 /* Copy restore file from cdrom */
553 if (unattended
&& (strlen(restore_file
) > 0)) {
554 fprintf(flog
, "unattended: Copy restore file\n");
555 snprintf(commandstring
, STRING_SIZE
,
556 "cp /cdrom/%s /harddisk/var/ipfire/backup", restore_file
);
557 mysystem(commandstring
);
560 mysystem("umount /cdrom");
561 snprintf(commandstring
, STRING_SIZE
, "eject /dev/%s", sourcedrive
);
562 mysystem(commandstring
);
565 sprintf(message
, ctr
[TR_CONGRATULATIONS_LONG
],
567 newtWinMessage(ctr
[TR_CONGRATULATIONS
], ctr
[TR_OK
], message
);
573 fprintf(flog
, "Install program ended.\n");
576 newtWinMessage(title
, ctr
[TR_OK
], ctr
[TR_PRESS_OK_TO_REBOOT
]);
578 freekeyvalues(ethernetkv
);
580 if (allok
&& !allok_fastexit
)
583 fprintf(flog
, "Entering unattended setup\n");
584 if (unattended_setup(unattendedkv
)) {
585 snprintf(commandstring
, STRING_SIZE
, "/bin/sleep 10");
586 runcommandwithstatus(commandstring
, "Unattended installation finished, system will reboot");
588 errorbox("Unattended setup failed.");
598 if (system("/sbin/chroot /harddisk /usr/local/sbin/setup /dev/tty2 INSTALL"))
599 printf("Unable to run setup.\n");
602 if (system("/bin/umount /harddisk/proc"))
603 printf("Unable to umount /harddisk/proc.\n");
613 snprintf(commandstring
, STRING_SIZE
, "/bin/swapoff %s2", hdparams
.devnode_part
);
618 system("/bin/umount /harddisk/proc");
619 system("/bin/umount /harddisk/dev");
620 system("/bin/umount /harddisk/sys");
622 system("/bin/umount /harddisk/var");
623 system("/bin/umount /harddisk/boot");
624 system("/bin/umount /harddisk");