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.
14 #define CDROM_INSTALL 0
16 #define DISK_INSTALL 2
17 #define INST_FILECOUNT 7000
18 #define UNATTENDED_CONF "/cdrom/boot/unattended.conf"
26 extern char url
[STRING_SIZE
];
33 char line
[STRING_SIZE
];
36 if ((fd
= fopen("/proc/cpuinfo", "r")) == NULL
) {
39 while (fgets(line
, STRING_SIZE
, fd
) != NULL
) {
40 if (strstr(line
, "processor") == line
) {
45 return (cpu_count
> 1);
48 long calc_swapsize(long memory
, long disk
) {
59 int main(int argc
, char *argv
[])
61 char *langnames
[] = { "Deutsch", "English", NULL
};
62 char *shortlangnames
[] = { "de", "en", NULL
};
63 char **langtrs
[] = { de_tr
, en_tr
, NULL
};
65 char harddrive
[5], sourcedrive
[5]; /* Device holder. */
66 struct devparams hdparams
, cdromparams
; /* Params for CDROM and HD */
67 int cdmounted
= 0; /* Loop flag for inserting a cd. */
69 char commandstring
[STRING_SIZE
];
70 char *installtypes
[] = { "CDROM/USB", "HTTP/FTP", NULL
};
71 int installtype
= CDROM_INSTALL
;
73 char shortlangname
[10];
75 char title
[STRING_SIZE
];
78 struct keyvalue
*ethernetkv
= initkeyvalues();
79 FILE *handle
, *cmdfile
;
80 char line
[STRING_SIZE
];
81 char string
[STRING_SIZE
];
82 long maximum_free
= 0, current_free
;
84 long system_partition
, boot_partition
, root_partition
, swap_file
;
86 char *yesnoharddisk
[] = { "NO", "YES", NULL
};
88 struct keyvalue
*unattendedkv
= initkeyvalues();
91 setlocale (LC_ALL
, "");
92 sethostname( SNAME
, 10);
94 memset(&hdparams
, 0, sizeof(struct devparams
));
95 memset(&cdromparams
, 0, sizeof(struct devparams
));
97 /* Log file/terminal stuff. */
100 if (!(flog
= fopen(argv
[1], "w+")))
108 fprintf(flog
, "Install program started.\n");
113 /* Do usb detection first for usb keyboard */
114 if (! (cmdfile
= fopen("/proc/cmdline", "r")))
116 fprintf(flog
, "Couldn't open commandline: /proc/cmdline\n");
118 fgets(line
, STRING_SIZE
, cmdfile
);
119 if (strstr (line
, "nousb") == NULL
) {
120 fprintf(flog
, "Initializing USB controllers.\n");
123 fprintf(flog
, "Skipping USB detection.\n");
125 // check if we have to make an unattended install
126 if (strstr (line
, "unattended") != NULL
) {
132 runcommandwithstatus("/bin/sleep 10", "WARNING: Unattended installation will start in 10 seconds...");
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",
146 "Select the language you wish to use for the " NAME
".", 50, 5, 5, 8,
147 langnames
, &choice
, "Ok", NULL
);
150 ctr
= langtrs
[choice
];
151 strcpy(shortlangname
, shortlangnames
[choice
]);
153 mysystem("/bin/setfont lat0-16");
155 newtDrawRootText(14, 0, NAME
" " VERSION
" - " SLOGAN
);
156 newtPushHelpLine(ctr
[TR_HELPLINE
]);
157 sprintf (title
, "%s %s - %s", NAME
, VERSION
, SLOGAN
);
160 sprintf(message
, ctr
[TR_WELCOME
], NAME
);
161 newtWinMessage(title
, ctr
[TR_OK
], message
);
163 sprintf(message
, ctr
[TR_SELECT_INSTALLATION_MEDIA_LONG
], NAME
);
164 rc
= newtWinMenu(ctr
[TR_SELECT_INSTALLATION_MEDIA
], message
,
165 50, 5, 5, 6, installtypes
, &installtype
, ctr
[TR_OK
],
166 ctr
[TR_CANCEL
], NULL
);
169 installtype
= CDROM_INSTALL
;
175 // Starting hardware detection
176 runcommandwithstatus("/bin/probehw.sh", ctr
[TR_PROBING_HARDWARE
]);
179 if (installtype
== CDROM_INSTALL
) {
180 switch (mysystem("/bin/mountsource.sh")) {
182 installtype
= CDROM_INSTALL
;
186 installtype
= DISK_INSTALL
;
189 errorbox(ctr
[TR_NO_CDROM
]);
193 /* read source drive letter */
194 if ((handle
= fopen("/tmp/source_device", "r")) == NULL
) {
195 errorbox(ctr
[TR_ERROR_PROBING_CDROM
]);
198 fgets(sourcedrive
, 5, handle
);
199 fprintf(flog
, "Source drive: %s\n", sourcedrive
);
202 snprintf(cdromparams
.devnode
, STRING_SIZE
, "/dev/%s", sourcedrive
);
203 cdromparams
.module
= 0;
204 fprintf(flog
, "Source device: %s\n", cdromparams
.devnode
);
207 /* Configure the network now! */
208 if (installtype
== URL_INSTALL
) {
209 /* Network driver and params. */
210 if (!(networkmenu(ethernetkv
))) {
211 errorbox(ctr
[TR_NETWORK_SETUP_FAILED
]);
215 /* Check for ipfire-<VERSION>.tbz2 */
216 if (checktarball(SNAME
"-" VERSION
".tbz2", ctr
[TR_ENTER_URL
])) {
217 errorbox(ctr
[TR_NO_IPCOP_TARBALL_FOUND
]);
222 /* Get device for the HD. This has to succeed. */
223 if (!(hdletter
= findidetype(IDE_HD
)))
225 /* Need to clean this up at some point */
226 if (!try_scsi("sda") || strstr(sourcedrive
, "sda") != NULL
) {
227 if (!try_scsi("ida/c0d0")) {
228 if (!try_scsi("cciss/c0d0")) {
229 if (!try_scsi("rd/c0d0")) {
230 if (!try_scsi("ataraid/d0")) {
231 errorbox(ctr
[TR_NO_HARDDISK
]);
235 sprintf(harddrive
, "ataraid/d0");
239 sprintf(harddrive
, "rd/c0d0");
243 sprintf(harddrive
, "cciss/c0d0");
247 sprintf(harddrive
, "ida/c0d0");
250 if (strstr(sourcedrive
, "sda") != NULL
) {
251 // probably installing from usb stick, try sdb
252 if (try_scsi("sdb")) {
253 sprintf(harddrive
, "sdb");
256 errorbox(ctr
[TR_NO_HARDDISK
]);
261 sprintf(harddrive
, "sda");
266 sprintf(harddrive
, "hd%c", hdletter
);
268 fprintf(flog
, "Destination drive: %s\n", harddrive
);
270 /* load unattended configuration */
272 fprintf(flog
, "unattended: Reading unattended.conf\n");
274 (void) readkeyvalues(unattendedkv
, UNATTENDED_CONF
);
277 /* Make the hdparms struct and print the contents. */
278 snprintf(hdparams
.devnode
, STRING_SIZE
, "/dev/%s", harddrive
);
281 sprintf(message
, ctr
[TR_PREPARE_HARDDISK
], hdparams
.devnode
);
288 rc
= newtWinMenu(title
, message
,
289 50, 5, 5, 6, yesnoharddisk
,
291 ctr
[TR_CANCEL
], NULL
);
299 /* Calculate amount of memory in machine */
300 if ((handle
= fopen("/proc/meminfo", "r")))
302 while (fgets(line
, STRING_SIZE
-1, handle
)) {
303 if (sscanf (line
, "MemTotal: %s kB", string
)) {
304 memory
= atoi(string
) / 1024 ;
310 /* Partition, mkswp, mkfs.
311 * before partitioning, first determine the sizes of each
312 * partition. In order to do that we need to know the size of
315 /* Don't use mysystem here so we can redirect output */
316 sprintf(commandstring
, "/bin/sfdisk -s /dev/%s > /tmp/disksize 2> /dev/null", harddrive
);
317 system(commandstring
);
319 /* Calculate amount of disk space */
320 if ((handle
= fopen("/tmp/disksize", "r")))
322 fgets(line
, STRING_SIZE
-1, handle
);
323 if (sscanf (line
, "%s", string
)) {
324 maximum_free
= atoi(string
) / 1024;
329 fprintf(flog
, "maximum_free = %ld, memory = %ld",
330 maximum_free
, memory
);
332 swap_file
= calc_swapsize(memory
, maximum_free
);
334 if (maximum_free
< 512 + swap_file
) {
335 if (maximum_free
< 512) {
336 errorbox(ctr
[TR_DISK_TOO_SMALL
]);
341 rc
= newtWinChoice(title
, ctr
[TR_OK
], ctr
[TR_CANCEL
], ctr
[TR_CONTINUE_NO_SWAP
]);
352 boot_partition
= 20; /* in MB */
353 current_free
= maximum_free
- boot_partition
- swap_file
;
355 root_partition
= 2048 ;
356 if (current_free
< 512) {
357 errorbox(ctr
[TR_DISK_TOO_SMALL
]);
361 current_free
= current_free
- root_partition
;
363 root_partition
= root_partition
+ swap_file
;
366 system_partition
= current_free
;
368 fprintf(flog
, "boot = %ld, swap = %ld, mylog = %ld, root = %ld\n",
369 boot_partition
, swap_file
, system_partition
, root_partition
);
371 handle
= fopen("/tmp/partitiontable", "w");
375 fprintf(handle
, ",%ld,L,*\n,%ld,S,\n,%ld,L,\n,,L,\n",
376 boot_partition
, swap_file
, root_partition
);
378 fprintf(handle
, ",%ld,L,*\n,0,0,\n,%ld,L,\n,,L,\n",
379 boot_partition
, root_partition
);
384 snprintf(commandstring
, STRING_SIZE
, "/bin/sfdisk -L -uM %s < /tmp/partitiontable", hdparams
.devnode
);
385 if (runcommandwithstatus(commandstring
, ctr
[TR_PARTITIONING_DISK
]))
387 errorbox(ctr
[TR_UNABLE_TO_PARTITION
]);
391 mysystem("/sbin/udevstart");
394 snprintf(commandstring
, STRING_SIZE
, "/bin/mke2fs -T ext2 -c %sp1", hdparams
.devnode
);
396 snprintf(commandstring
, STRING_SIZE
, "/bin/mke2fs -T ext2 -c %s1", hdparams
.devnode
);
397 if (runcommandwithstatus(commandstring
, ctr
[TR_MAKING_BOOT_FILESYSTEM
]))
399 errorbox(ctr
[TR_UNABLE_TO_MAKE_BOOT_FILESYSTEM
]);
405 snprintf(commandstring
, STRING_SIZE
, "/sbin/mkswap %sp2", hdparams
.devnode
);
407 snprintf(commandstring
, STRING_SIZE
, "/sbin/mkswap %s2", hdparams
.devnode
);
408 if (runcommandwithstatus(commandstring
, ctr
[TR_MAKING_SWAPSPACE
]))
410 errorbox(ctr
[TR_UNABLE_TO_MAKE_SWAPSPACE
]);
416 snprintf(commandstring
, STRING_SIZE
, "/sbin/mkreiserfs -f %sp3", hdparams
.devnode
);
418 snprintf(commandstring
, STRING_SIZE
, "/sbin/mkreiserfs -f %s3", hdparams
.devnode
);
420 if (runcommandwithstatus(commandstring
, ctr
[TR_MAKING_ROOT_FILESYSTEM
]))
422 errorbox(ctr
[TR_UNABLE_TO_MAKE_ROOT_FILESYSTEM
]);
427 snprintf(commandstring
, STRING_SIZE
, "/sbin/mkreiserfs -f %sp4", hdparams
.devnode
);
429 snprintf(commandstring
, STRING_SIZE
, "/sbin/mkreiserfs -f %s4", hdparams
.devnode
);
431 if (runcommandwithstatus(commandstring
, ctr
[TR_MAKING_LOG_FILESYSTEM
]))
433 errorbox(ctr
[TR_UNABLE_TO_MAKE_ROOT_FILESYSTEM
]);
437 /* Mount harddisk. */
439 snprintf(commandstring
, STRING_SIZE
, "/bin/mount %sp3 /harddisk", hdparams
.devnode
);
441 snprintf(commandstring
, STRING_SIZE
, "/bin/mount %s3 /harddisk", hdparams
.devnode
);
442 if (runcommandwithstatus(commandstring
, ctr
[TR_MOUNTING_ROOT_FILESYSTEM
]))
444 errorbox(ctr
[TR_UNABLE_TO_MOUNT_ROOT_FILESYSTEM
]);
448 mkdir("/harddisk/boot", S_IRWXU
|S_IRWXG
|S_IRWXO
);
449 mkdir("/harddisk/var", S_IRWXU
|S_IRWXG
|S_IRWXO
);
450 mkdir("/harddisk/var/log", S_IRWXU
|S_IRWXG
|S_IRWXO
);
453 snprintf(commandstring
, STRING_SIZE
, "/bin/mount %sp1 /harddisk/boot", hdparams
.devnode
);
455 snprintf(commandstring
, STRING_SIZE
, "/bin/mount %s1 /harddisk/boot", hdparams
.devnode
);
457 if (runcommandwithstatus(commandstring
, ctr
[TR_MOUNTING_BOOT_FILESYSTEM
]))
459 errorbox(ctr
[TR_UNABLE_TO_MOUNT_BOOT_FILESYSTEM
]);
464 snprintf(commandstring
, STRING_SIZE
, "/sbin/swapon %sp2", hdparams
.devnode
);
466 snprintf(commandstring
, STRING_SIZE
, "/sbin/swapon %s2", hdparams
.devnode
);
467 if (runcommandwithstatus(commandstring
, ctr
[TR_MOUNTING_SWAP_PARTITION
]))
469 errorbox(ctr
[TR_UNABLE_TO_MOUNT_SWAP_PARTITION
]);
474 snprintf(commandstring
, STRING_SIZE
, "/bin/mount %sp4 /harddisk/var", hdparams
.devnode
);
476 snprintf(commandstring
, STRING_SIZE
, "/bin/mount %s4 /harddisk/var", hdparams
.devnode
);
477 if (runcommandwithstatus(commandstring
, ctr
[TR_MOUNTING_LOG_FILESYSTEM
]))
479 errorbox(ctr
[TR_UNABLE_TO_MOUNT_LOG_FILESYSTEM
]);
483 if (installtype
== URL_INSTALL
) {
484 snprintf(commandstring
, STRING_SIZE
,
485 "/bin/wget -q -O - %s/" SNAME
"-" VERSION
".tbz2 | /bin/tar -C /harddisk -xvjf -", url
);
488 if (installtype
== CDROM_INSTALL
) {
489 snprintf(commandstring
, STRING_SIZE
,
490 "/bin/tar -C /harddisk -xvjf /cdrom/" SNAME
"-" VERSION
".tbz2");
493 if (runcommandwithprogress(60, 4, title
, commandstring
, INST_FILECOUNT
,
494 ctr
[TR_INSTALLING_FILES
]))
496 errorbox(ctr
[TR_UNABLE_TO_INSTALL_FILES
]);
500 /* Save USB controller type to modules.conf */
501 write_usb_modules_conf();
503 /* touch the modules.dep files */
504 snprintf(commandstring
, STRING_SIZE
,
505 "/bin/touch /harddisk/lib/modules/%s-ipfire/modules.dep",
507 mysystem(commandstring
);
508 snprintf(commandstring
, STRING_SIZE
,
509 "/bin/touch /harddisk/lib/modules/%s-ipfire-smp/modules.dep",
511 mysystem(commandstring
);
514 rename ("/harddisk/bin/uname.bak", "/harddisk/bin/uname");
516 /* mount proc filesystem */
517 mysystem("mkdir /harddisk/proc");
518 mysystem("/bin/mount -t proc none /harddisk/proc");
519 mysystem("/bin/mount --bind /dev /harddisk/dev");
521 /* Build cache lang file */
522 snprintf(commandstring
, STRING_SIZE
, "/sbin/chroot /harddisk /usr/bin/perl -e \"require '" CONFIG_ROOT
"/lang.pl'; &Lang::BuildCacheLang\"");
523 if (runcommandwithstatus(commandstring
, ctr
[TR_INSTALLING_LANG_CACHE
]))
525 errorbox(ctr
[TR_UNABLE_TO_INSTALL_LANG_CACHE
]);
529 /* Update /etc/fstab */
530 replace("/harddisk/etc/fstab", "DEVICE", hdparams
.devnode
);
532 /* if we detected SCSI/USB then fixup */
533 mysystem("/bin/probecntrl.sh");
534 if ((handle
= fopen("/tmp/cntrldriver", "r")))
537 fgets(line
, STRING_SIZE
-1, handle
);
539 line
[strlen(line
) - 1] = 0;
540 driver
= strtok(line
, ".");
541 if (strlen(driver
) > 1) {
542 fprintf(flog
, "Fixing up ipfirerd.img\n");
543 mkdir("/harddisk/initrd", S_IRWXU
|S_IRWXG
|S_IRWXO
);
544 snprintf(commandstring
, STRING_SIZE
, "/sbin/chroot /harddisk /sbin/mkinitrd --with=scsi_mod %s --with=sd_mod --with=sr_mod /boot/ipfirerd.img %s-ipfire", driver
, KERNEL_VERSION
);
545 runcommandwithstatus(commandstring
, ctr
[TR_BUILDING_INITRD
]);
546 snprintf(commandstring
, STRING_SIZE
, "/sbin/chroot /harddisk /sbin/mkinitrd --with=scsi_mod %s --with=sd_mod --with=sr_mod /boot/ipfirerd-smp.img %s-ipfire-smp", driver
, KERNEL_VERSION
);
547 runcommandwithstatus(commandstring
, ctr
[TR_BUILDING_INITRD
]);
548 mysystem("/sbin/chroot /harddisk /bin/mv /boot/grub/scsigrub.conf /boot/grub/grub.conf");
553 sprintf(string
, "root=%sp3", hdparams
.devnode
);
555 sprintf(string
, "root=%s3", hdparams
.devnode
);
556 replace( "/harddisk/boot/grub/grub.conf", "root=ROOT", string
);
557 replace( "/harddisk/boot/grub/grubbatch", "DEVICE", hdparams
.devnode
);
559 /* restore permissions */
560 chmod("/harddisk/boot/grub/grubbatch", S_IXUSR
| S_IRUSR
| S_IXGRP
| S_IRGRP
| S_IXOTH
| S_IROTH
);
562 snprintf(commandstring
, STRING_SIZE
,
563 "/sbin/chroot /harddisk /boot/grub/grubbatch");
564 if (runcommandwithstatus(commandstring
, ctr
[TR_INSTALLING_GRUB
])) {
565 errorbox(ctr
[TR_UNABLE_TO_INSTALL_GRUB
]);
569 /* Install bootsplash */
570 mysystem("/bin/installbootsplash.sh");
572 mysystem("ln -s grub.conf /harddisk/boot/grub/menu.lst");
573 mysystem("umount /harddisk/proc");
574 mysystem("umount /harddisk/dev");
577 sprintf(message
, ctr
[TR_CONGRATULATIONS_LONG
],
578 NAME
, SNAME
, SNAME
, NAME
, NAME
, NAME
);
579 newtWinMessage(ctr
[TR_CONGRATULATIONS
], ctr
[TR_OK
], message
);
585 fprintf(flog
, "Install program ended.\n");
588 newtWinMessage(title
, ctr
[TR_OK
], ctr
[TR_PRESS_OK_TO_REBOOT
]);
590 freekeyvalues(ethernetkv
);
592 if (allok
&& !allok_fastexit
)
594 /* /proc is needed by the module checker. We have to mount it
595 * so it can be seen by setup, which is run chrooted. */
596 if (system("/bin/mount proc -t proc /harddisk/proc"))
597 printf("Unable to mount proc in /harddisk.");
601 fprintf(flog
, "Entering unattended setup\n");
602 if (unattended_setup(unattendedkv
)) {
603 snprintf(commandstring
, STRING_SIZE
, "/bin/sleep 10");
604 runcommandwithstatus(commandstring
, "Unattended installation finished, system will reboot");
606 errorbox("Unattended setup failed.");
616 if (system("/sbin/chroot /harddisk /usr/local/sbin/setup /dev/tty2 INSTALL"))
617 printf("Unable to run setup.\n");
620 if (system("/bin/umount /harddisk/proc"))
621 printf("Unable to umount /harddisk/proc.\n");
628 snprintf(commandstring
, STRING_SIZE
, "/bin/swapoff %sp2", hdparams
.devnode
);
630 snprintf(commandstring
, STRING_SIZE
, "/bin/swapoff %s2", hdparams
.devnode
);
635 system("/bin/umount /harddisk/var");
636 system("/bin/umount /harddisk/boot");
637 system("/bin/umount /harddisk");