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();
70 setlocale (LC_ALL
, "");
71 sethostname( SNAME
, 10);
73 memset(&hdparams
, 0, sizeof(struct devparams
));
74 memset(&cdromparams
, 0, sizeof(struct devparams
));
76 /* Log file/terminal stuff. */
79 if (!(flog
= fopen(argv
[1], "w+")))
87 fprintf(flog
, "Install program started.\n");
92 /* Do usb detection first for usb keyboard */
93 if (! (cmdfile
= fopen("/proc/cmdline", "r")))
95 fprintf(flog
, "Couldn't open commandline: /proc/cmdline\n");
97 fgets(line
, STRING_SIZE
, cmdfile
);
99 // check if we have to make an unattended install
100 if (strstr (line
, "unattended") != NULL
) {
102 runcommandwithstatus("/bin/sleep 10", "WARNING: Unattended installation will start in 10 seconds...");
106 mysystem("/sbin/modprobe ide-generic");
107 mysystem("/sbin/modprobe generic");
108 mysystem("/sbin/modprobe ide-cd");
109 mysystem("/sbin/modprobe ide-disk");
110 mysystem("/sbin/modprobe uhci-hcd");
111 mysystem("/sbin/modprobe ohci-hcd");
112 mysystem("/sbin/modprobe ehci-hcd");
113 mysystem("/sbin/modprobe ohci1394");
114 mysystem("/sbin/modprobe sd_mod");
115 mysystem("/sbin/modprobe sr_mod");
116 mysystem("/sbin/modprobe usb-storage");
117 mysystem("/sbin/modprobe usbhid");
119 mysystem("/sbin/modprobe iso9660"); // CDROM
120 mysystem("/sbin/modprobe ext2"); // Boot patition
121 mysystem("/sbin/modprobe vfat"); // USB key
123 /* German is the default */
124 for (choice
= 0; langnames
[choice
]; choice
++)
126 if (strcmp(langnames
[choice
], "Deutsch") == 0)
129 if (!langnames
[choice
])
133 rc
= newtWinMenu("Language selection", "Select the language you wish to use for the " NAME
".", 50, 5, 5, 8,
134 langnames
, &choice
, "Ok", NULL
);
137 ctr
= langtrs
[choice
];
138 strcpy(shortlangname
, shortlangnames
[choice
]);
140 newtDrawRootText(14, 0, NAME
" " VERSION
" - " SLOGAN
);
141 newtPushHelpLine(ctr
[TR_HELPLINE
]);
142 sprintf (title
, "%s %s - %s", NAME
, VERSION
, SLOGAN
);
144 // Starting hardware detection
145 runcommandwithstatus("/bin/probehw.sh", ctr
[TR_PROBING_HARDWARE
]);
147 sprintf(message
, ctr
[TR_WELCOME
], NAME
);
148 newtWinMessage(title
, ctr
[TR_OK
], message
);
150 switch (mysystem("/bin/mountsource.sh")) {
154 errorbox(ctr
[TR_NO_CDROM
]);
158 /* read source drive letter */
159 if ((handle
= fopen("/tmp/source_device", "r")) == NULL
) {
160 errorbox(ctr
[TR_ERROR_PROBING_CDROM
]);
163 fgets(sourcedrive
, 5, handle
);
164 fprintf(flog
, "Source drive: %s\n", sourcedrive
);
170 fprintf(flog
, "Harddisk scan pass %i\n", i
);
172 switch (mysystem("/bin/mountdest.sh") % 255) {
173 case 0: // Found IDE disk
178 case 1: // Found SCSI disk
183 case 2: // Found RAID disk
188 case 10: // No harddisk found
190 errorbox(ctr
[TR_NO_HARDDISK
]);
193 // Do this if the kudzu-scan fails...
194 runcommandwithstatus("/bin/probehw.sh deep-scan", ctr
[TR_PROBING_HARDWARE
]);
199 if ((handle
= fopen("/tmp/dest_device", "r")) == NULL
) {
200 errorbox(ctr
[TR_NO_HARDDISK
]);
203 fgets(harddrive
, 30, handle
);
206 /* load unattended configuration */
208 fprintf(flog
, "unattended: Reading unattended.conf\n");
210 (void) readkeyvalues(unattendedkv
, UNATTENDED_CONF
);
213 /* Make the hdparms struct and print the contents.
214 With USB-KEY install and SCSI disk, while installing, the disk
215 is named 'sdb,sdc,...' (following keys)
216 On reboot, it will become 'sda'
217 To avoid many test, all names are built in the struct.
219 sprintf(hdparams
.devnode_disk
, "/dev/%s", harddrive
);
220 /* Address the partition or raid partition (eg dev/sda or /dev/sdap1 */
221 sprintf(hdparams
.devnode_part
, "/dev/%s%s", harddrive
,raid_disk
? "p" : "");
222 /* Now the names after the machine is booted. Only scsi is affected
223 and we only install on the first scsi disk. */
225 strcpy(tmp
, scsi_disk
? "sda" : harddrive
);
226 sprintf(hdparams
.devnode_disk_run
, "/dev/%s", tmp
);
227 sprintf(hdparams
.devnode_part_run
, "/dev/%s%s", tmp
, raid_disk
? "p" : "");
230 fprintf(flog
, "Destination drive: %s\n", hdparams
.devnode_disk
);
232 sprintf(message
, ctr
[TR_PREPARE_HARDDISK
], hdparams
.devnode_disk
);
236 yesnoharddisk
[0] = ctr
[TR_NO
];
237 yesnoharddisk
[1] = ctr
[TR_YES
];
238 yesnoharddisk
[2] = NULL
;
242 rc
= newtWinMenu(title
, message
,
243 50, 5, 5, 6, yesnoharddisk
,
245 ctr
[TR_CANCEL
], NULL
);
253 sprintf(message
, ctr
[TR_CHOOSE_FILESYSTEM
]);
254 rc
= newtWinMenu( ctr
[TR_CHOOSE_FILESYSTEM
], message
,
255 50, 5, 5, 6, fstypes
, &fstype
, ctr
[TR_OK
],
256 ctr
[TR_CANCEL
], NULL
);
264 /* Calculate amount of memory in machine */
265 if ((handle
= fopen("/proc/meminfo", "r")))
267 while (fgets(line
, STRING_SIZE
-1, handle
)) {
268 if (sscanf (line
, "MemTotal: %s kB", string
)) {
269 memory
= atoi(string
) / 1024 ;
275 /* Partition, mkswp, mkfs.
276 * before partitioning, first determine the sizes of each
277 * partition. In order to do that we need to know the size of
280 /* Don't use mysystem here so we can redirect output */
281 sprintf(commandstring
, "/bin/sfdisk -s /dev/%s > /tmp/disksize 2> /dev/null", harddrive
);
282 system(commandstring
);
284 /* Calculate amount of disk space */
285 if ((handle
= fopen("/tmp/disksize", "r"))) {
286 fgets(line
, STRING_SIZE
-1, handle
);
287 if (sscanf (line
, "%s", string
)) {
288 disk
= atoi(string
) / 1024;
293 fprintf(flog
, "Disksize = %ld, memory = %ld", disk
, memory
);
295 /* Calculating Swap-Size dependend of Ram Size */
298 else if (memory
>= 1024)
303 /* Calculating Root-Size dependend of Max Disk Space */
305 root_partition
= 200;
306 else if ( disk
>= 756 && disk
<= 3072 )
307 root_partition
= 512;
309 root_partition
= 2048;
312 /* Calculating the amount of free space */
313 boot_partition
= 20; /* in MB */
314 system_partition
= disk
- ( root_partition
+ swap_file
+ boot_partition
);
316 fprintf(flog
, ", boot = %ld, swap = %ld, mylog = %ld, root = %ld\n",
317 boot_partition
, swap_file
, system_partition
, root_partition
);
320 if ( (!unattended
) && (((disk
- (root_partition
+ swap_file
+ boot_partition
)) < 256 ) && ((disk
- (root_partition
+ boot_partition
)) > 256)) ) {
321 rc
= newtWinChoice(title
, ctr
[TR_OK
], ctr
[TR_CANCEL
], ctr
[TR_CONTINUE_NO_SWAP
]);
324 system_partition
= disk
- ( root_partition
+ swap_file
+ boot_partition
);
325 fprintf(flog
, "Changing Swap Size to 0 MB.\n");
328 fprintf(flog
, "Disk is too small.\n");
329 errorbox(ctr
[TR_DISK_TOO_SMALL
]);goto EXIT
;
332 else if (disk
- (root_partition
+ swap_file
+ boot_partition
) >= 256) {
336 fprintf(flog
, "Disk is too small.\n");
337 errorbox(ctr
[TR_DISK_TOO_SMALL
]);goto EXIT
;
340 handle
= fopen("/tmp/partitiontable", "w");
344 fprintf(handle
, ",%ld,L,*\n,%ld,S,\n,%ld,L,\n,,L,\n",
345 boot_partition
, swap_file
, root_partition
);
347 fprintf(handle
, ",%ld,L,*\n,0,0,\n,%ld,L,\n,,L,\n",
348 boot_partition
, root_partition
);
353 snprintf(commandstring
, STRING_SIZE
, "/bin/sfdisk -L -uM %s < /tmp/partitiontable", hdparams
.devnode_disk
);
354 if (runcommandwithstatus(commandstring
, ctr
[TR_PARTITIONING_DISK
]))
356 errorbox(ctr
[TR_UNABLE_TO_PARTITION
]);
360 if (fstype
== REISER4
) {
361 mysystem("/sbin/modprobe reiser4");
362 sprintf(mkfscommand
, "/sbin/mkfs.reiser4 -y");
363 } else if (fstype
== REISERFS
) {
364 mysystem("/sbin/modprobe reiserfs");
365 sprintf(mkfscommand
, "/sbin/mkreiserfs -f");
366 } else if (fstype
== EXT3
) {
367 mysystem("/sbin/modprobe ext3");
368 sprintf(mkfscommand
, "/sbin/mke2fs -T ext3 -c");
371 snprintf(commandstring
, STRING_SIZE
, "/sbin/mke2fs -T ext2 -c %s1", hdparams
.devnode_part
);
372 if (runcommandwithstatus(commandstring
, ctr
[TR_MAKING_BOOT_FILESYSTEM
]))
374 errorbox(ctr
[TR_UNABLE_TO_MAKE_BOOT_FILESYSTEM
]);
379 snprintf(commandstring
, STRING_SIZE
, "/sbin/mkswap %s2", hdparams
.devnode_part
);
380 if (runcommandwithstatus(commandstring
, ctr
[TR_MAKING_SWAPSPACE
]))
382 errorbox(ctr
[TR_UNABLE_TO_MAKE_SWAPSPACE
]);
387 snprintf(commandstring
, STRING_SIZE
, "%s %s3", mkfscommand
, hdparams
.devnode_part
);
388 if (runcommandwithstatus(commandstring
, ctr
[TR_MAKING_ROOT_FILESYSTEM
]))
390 errorbox(ctr
[TR_UNABLE_TO_MAKE_ROOT_FILESYSTEM
]);
394 snprintf(commandstring
, STRING_SIZE
, "%s %s4", mkfscommand
, hdparams
.devnode_part
);
395 if (runcommandwithstatus(commandstring
, ctr
[TR_MAKING_LOG_FILESYSTEM
]))
397 errorbox(ctr
[TR_UNABLE_TO_MAKE_LOG_FILESYSTEM
]);
401 snprintf(commandstring
, STRING_SIZE
, "/bin/mount %s3 /harddisk", hdparams
.devnode_part
);
402 if (runcommandwithstatus(commandstring
, ctr
[TR_MOUNTING_ROOT_FILESYSTEM
]))
404 errorbox(ctr
[TR_UNABLE_TO_MOUNT_ROOT_FILESYSTEM
]);
408 mkdir("/harddisk/boot", S_IRWXU
|S_IRWXG
|S_IRWXO
);
409 mkdir("/harddisk/var", S_IRWXU
|S_IRWXG
|S_IRWXO
);
410 mkdir("/harddisk/var/log", S_IRWXU
|S_IRWXG
|S_IRWXO
);
412 snprintf(commandstring
, STRING_SIZE
, "/bin/mount %s1 /harddisk/boot", hdparams
.devnode_part
);
413 if (runcommandwithstatus(commandstring
, ctr
[TR_MOUNTING_BOOT_FILESYSTEM
]))
415 errorbox(ctr
[TR_UNABLE_TO_MOUNT_BOOT_FILESYSTEM
]);
419 snprintf(commandstring
, STRING_SIZE
, "/sbin/swapon %s2", hdparams
.devnode_part
);
420 if (runcommandwithstatus(commandstring
, ctr
[TR_MOUNTING_SWAP_PARTITION
]))
422 errorbox(ctr
[TR_UNABLE_TO_MOUNT_SWAP_PARTITION
]);
426 snprintf(commandstring
, STRING_SIZE
, "/bin/mount %s4 /harddisk/var", hdparams
.devnode_part
);
427 if (runcommandwithstatus(commandstring
, ctr
[TR_MOUNTING_LOG_FILESYSTEM
]))
429 errorbox(ctr
[TR_UNABLE_TO_MOUNT_LOG_FILESYSTEM
]);
433 snprintf(commandstring
, STRING_SIZE
,
434 "/bin/tar -C /harddisk -xvjf /cdrom/" SNAME
"-" VERSION
".tbz2");
436 if (runcommandwithprogress(60, 4, title
, commandstring
, INST_FILECOUNT
,
437 ctr
[TR_INSTALLING_FILES
]))
439 errorbox(ctr
[TR_UNABLE_TO_INSTALL_FILES
]);
443 /* Save language und local settings */
444 write_lang_configs(shortlangname
);
446 /* touch the modules.dep files */
447 snprintf(commandstring
, STRING_SIZE
,
448 "/bin/touch /harddisk/lib/modules/%s-ipfire/modules.dep",
450 mysystem(commandstring
);
451 /* snprintf(commandstring, STRING_SIZE,
452 "/bin/touch /harddisk/lib/modules/%s-ipfire-smp/modules.dep",
454 mysystem(commandstring);
457 rename ("/harddisk/bin/uname.bak", "/harddisk/bin/uname");
459 /* mount proc filesystem */
460 mysystem("mkdir /harddisk/proc");
461 mysystem("/bin/mount --bind /proc /harddisk/proc");
462 mysystem("/bin/mount --bind /dev /harddisk/dev");
463 mysystem("/bin/mount --bind /sys /harddisk/sys");
465 /* Build cache lang file */
466 snprintf(commandstring
, STRING_SIZE
, "/sbin/chroot /harddisk /usr/bin/perl -e \"require '" CONFIG_ROOT
"/lang.pl'; &Lang::BuildCacheLang\"");
467 if (runcommandwithstatus(commandstring
, ctr
[TR_INSTALLING_LANG_CACHE
]))
469 errorbox(ctr
[TR_UNABLE_TO_INSTALL_LANG_CACHE
]);
473 /* Update /etc/fstab */
474 replace("/harddisk/etc/fstab", "DEVICE", hdparams
.devnode_part_run
);
476 if (fstype
== REISER4
) {
477 replace("/harddisk/etc/fstab", "FSTYPE", "reiser4");
478 replace("/harddisk/boot/grub/grub.conf", "MOUNT", "rw");
479 } else if (fstype
== REISERFS
) {
480 replace("/harddisk/etc/fstab", "FSTYPE", "reiserfs");
481 replace("/harddisk/boot/grub/grub.conf", "MOUNT", "ro");
482 } else if (fstype
== EXT3
) {
483 snprintf(commandstring
, STRING_SIZE
, "tune2fs -j %s3", hdparams
.devnode_part
);
484 if (runcommandwithstatus(commandstring
, ctr
[TR_JOURNAL_EXT3
]))
486 errorbox(ctr
[TR_JOURNAL_ERROR
]);
487 replace("/harddisk/etc/fstab", "FSTYPE", "ext2");
490 snprintf(commandstring
, STRING_SIZE
, "tune2fs -j %s4", hdparams
.devnode_part
);
491 if (runcommandwithstatus(commandstring
, ctr
[TR_JOURNAL_EXT3
]))
493 errorbox(ctr
[TR_JOURNAL_ERROR
]);
494 replace("/harddisk/etc/fstab", "FSTYPE", "ext2");
497 replace("/harddisk/etc/fstab", "FSTYPE", "ext3");
499 replace("/harddisk/boot/grub/grub.conf", "MOUNT", "ro");
502 replace("/harddisk/boot/grub/grub.conf", "KVER", KERNEL_VERSION
);
504 /* Build the emergency ramdisk with all drivers */
505 mysystem("cp -f /harddisk/etc/mkinitcpio.conf /harddisk/etc/mkinitcpio.conf.org");
507 replace("/harddisk/etc/mkinitcpio.conf", " autodetect ", " ");
508 snprintf(commandstring
, STRING_SIZE
, "/sbin/chroot /harddisk /sbin/mkinitcpio -g /boot/ipfirerd-%s-emergency.img -k %s-ipfire", KERNEL_VERSION
, KERNEL_VERSION
);
509 runcommandwithstatus(commandstring
, ctr
[TR_BUILDING_INITRD
]);
511 mysystem("cp -f /harddisk/etc/mkinitcpio.conf.org /harddisk/etc/mkinitcpio.conf");
513 /* mkinitcpio has a problem if ide and pata are included */
514 if ( scsi_disk
==1 ) {
515 /* Remove the ide hook if we install sda */
516 replace("/harddisk/etc/mkinitcpio.conf", " ide ", " ");
518 /* Remove the pata & sata hook if we install hda */
519 replace("/harddisk/etc/mkinitcpio.conf", " pata ", " ");
520 replace("/harddisk/etc/mkinitcpio.conf", " sata ", " ");
522 /* Going to make our initrd... */
523 snprintf(commandstring
, STRING_SIZE
, "/sbin/chroot /harddisk /sbin/mkinitcpio -g /boot/ipfirerd-%s.img -k %s-ipfire", KERNEL_VERSION
, KERNEL_VERSION
);
524 runcommandwithstatus(commandstring
, ctr
[TR_BUILDING_INITRD
]);
525 /* snprintf(commandstring, STRING_SIZE, "/sbin/chroot /harddisk /sbin/mkinitcpio -g /boot/ipfirerd-%s-smp.img -k %s-ipfire-smp", KERNEL_VERSION, KERNEL_VERSION );
526 runcommandwithstatus(commandstring, ctr[TR_BUILDING_INITRD]);
528 sprintf(string
, "root=%s3", hdparams
.devnode_part_run
);
529 replace( "/harddisk/boot/grub/grub.conf", "root=ROOT", string
);
530 mysystem("ln -s grub.conf /harddisk/boot/grub/menu.lst");
532 system("sed -e 's#harddisk\\/##g' < /proc/mounts > /harddisk/etc/mtab");
534 snprintf(commandstring
, STRING_SIZE
,
535 "/sbin/chroot /harddisk /usr/sbin/grub-install --no-floppy %s", hdparams
.devnode_disk
);
536 if (runcommandwithstatus(commandstring
, ctr
[TR_INSTALLING_GRUB
])) {
537 errorbox(ctr
[TR_UNABLE_TO_INSTALL_GRUB
]);
541 mysystem("umount /cdrom");
542 snprintf(commandstring
, STRING_SIZE
, "eject /dev/%s", sourcedrive
);
543 mysystem(commandstring
);
546 sprintf(message
, ctr
[TR_CONGRATULATIONS_LONG
],
548 newtWinMessage(ctr
[TR_CONGRATULATIONS
], ctr
[TR_OK
], message
);
554 fprintf(flog
, "Install program ended.\n");
557 newtWinMessage(title
, ctr
[TR_OK
], ctr
[TR_PRESS_OK_TO_REBOOT
]);
559 freekeyvalues(ethernetkv
);
561 if (allok
&& !allok_fastexit
)
564 fprintf(flog
, "Entering unattended setup\n");
565 if (unattended_setup(unattendedkv
)) {
566 snprintf(commandstring
, STRING_SIZE
, "/bin/sleep 10");
567 runcommandwithstatus(commandstring
, "Unattended installation finished, system will reboot");
569 errorbox("Unattended setup failed.");
579 if (system("/sbin/chroot /harddisk /usr/local/sbin/setup /dev/tty2 INSTALL"))
580 printf("Unable to run setup.\n");
583 if (system("/bin/umount /harddisk/proc"))
584 printf("Unable to umount /harddisk/proc.\n");
594 snprintf(commandstring
, STRING_SIZE
, "/bin/swapoff %s2", hdparams
.devnode_part
);
599 system("/bin/umount /harddisk/proc");
600 system("/bin/umount /harddisk/dev");
601 system("/bin/umount /harddisk/sys");
603 system("/bin/umount /harddisk/var");
604 system("/bin/umount /harddisk/boot");
605 system("/bin/umount /harddisk");