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 8400
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
36 int main(int argc
, char *argv
[])
39 char discl_msg
[40000] = "Disclaimer\n";
41 char *langnames
[] = { "Deutsch", "English", "Français", "Español", NULL
};
42 char *shortlangnames
[] = { "de", "en", "fr", "es", NULL
};
43 char **langtrs
[] = { de_tr
, en_tr
, fr_tr
, es_tr
, NULL
};
45 char harddrive
[30], sourcedrive
[5]; /* Device holder. */
46 struct devparams hdparams
, cdromparams
; /* Params for CDROM and HD */
48 char commandstring
[STRING_SIZE
];
49 char mkfscommand
[STRING_SIZE
];
50 char *fstypes
[] = { "ext2", "ext3", "ReiserFS", NULL
};
51 int fstype
= REISERFS
;
56 char shortlangname
[10];
58 char title
[STRING_SIZE
];
62 struct keyvalue
*ethernetkv
= initkeyvalues();
63 FILE *handle
, *cmdfile
, *copying
;
64 char line
[STRING_SIZE
];
65 char string
[STRING_SIZE
];
66 long memory
= 0, disk
= 0, free
;
67 long system_partition
, boot_partition
, root_partition
, swap_file
;
69 char *yesnoharddisk
[3]; // char *yesnoharddisk = { "NO", "YES", NULL };
72 struct keyvalue
*unattendedkv
= initkeyvalues();
74 char restore_file
[STRING_SIZE
] = "";
76 setlocale (LC_ALL
, "");
77 sethostname( SNAME
, 10);
79 memset(&hdparams
, 0, sizeof(struct devparams
));
80 memset(&cdromparams
, 0, sizeof(struct devparams
));
82 /* Log file/terminal stuff. */
85 if (!(flog
= fopen(argv
[1], "w+")))
93 fprintf(flog
, "Install program started.\n");
98 newtDrawRootText(14, 0, NAME
" " VERSION
" - " SLOGAN
);
99 sprintf (title
, "%s %s - %s", NAME
, VERSION
, SLOGAN
);
101 if (! (cmdfile
= fopen("/proc/cmdline", "r")))
103 fprintf(flog
, "Couldn't open commandline: /proc/cmdline\n");
105 fgets(line
, STRING_SIZE
, cmdfile
);
107 // check if we have to make an unattended install
108 if (strstr (line
, "unattended") != NULL
) {
110 runcommandwithstatus("/bin/sleep 10", "WARNING: Unattended installation will start in 10 seconds...");
115 if (! (copying
= fopen("/COPYING", "r")))
117 fprintf(flog
, "Couldn't open gpl (/COPYING)\n");
118 sprintf(discl_msg
, "Couldn't open gpl (/COPYING)\n");
120 fread(discl_msg
, 1, 40000, copying
);
124 // Load common modules
125 mysystem("/sbin/modprobe iso9660"); // CDROM
126 mysystem("/sbin/modprobe ext2"); // Boot patition
127 mysystem("/sbin/modprobe vfat"); // USB key
129 /* German is the default */
130 for (choice
= 0; langnames
[choice
]; choice
++)
132 if (strcmp(langnames
[choice
], "Deutsch") == 0)
135 if (!langnames
[choice
])
139 rc
= newtWinMenu("Language selection", "Select the language you wish to use for the " NAME
".", 50, 5, 5, 8,
140 langnames
, &choice
, "Ok", NULL
);
143 ctr
= langtrs
[choice
];
144 strcpy(shortlangname
, shortlangnames
[choice
]);
146 newtPushHelpLine(ctr
[TR_HELPLINE
]);
148 sprintf(message
, ctr
[TR_WELCOME
], NAME
);
149 newtWinMessage(title
, ctr
[TR_OK
], message
);
152 if (disclaimerbox(discl_msg
)==0) {
153 errorbox(ctr
[TR_LICENSE_NOT_ACCEPTED
]);
158 switch (mysystem("/bin/mountsource.sh")) {
162 errorbox(ctr
[TR_NO_CDROM
]);
166 /* read source drive letter */
167 if ((handle
= fopen("/tmp/source_device", "r")) == NULL
) {
168 errorbox(ctr
[TR_ERROR_PROBING_CDROM
]);
171 fgets(sourcedrive
, 5, handle
);
172 fprintf(flog
, "Source drive: %s\n", sourcedrive
);
178 fprintf(flog
, "Harddisk scan pass %i\n", i
);
180 switch (mysystem("/bin/mountdest.sh") % 255) {
181 case 0: // Found IDE disk
186 case 1: // Found SCSI disk
191 case 2: // Found RAID disk
196 case 10: // No harddisk found
198 errorbox(ctr
[TR_NO_HARDDISK
]);
201 // Do this if the kudzu-scan fails...
202 runcommandwithstatus("/bin/probehw.sh deep-scan", ctr
[TR_PROBING_HARDWARE
]);
207 if ((handle
= fopen("/tmp/dest_device", "r")) == NULL
) {
208 errorbox(ctr
[TR_NO_HARDDISK
]);
211 fgets(harddrive
, 30, handle
);
214 /* load unattended configuration */
216 fprintf(flog
, "unattended: Reading unattended.conf\n");
218 (void) readkeyvalues(unattendedkv
, UNATTENDED_CONF
);
219 findkey(unattendedkv
, "RESTORE_FILE", restore_file
);
222 /* Make the hdparms struct and print the contents.
223 With USB-KEY install and SCSI disk, while installing, the disk
224 is named 'sdb,sdc,...' (following keys)
225 On reboot, it will become 'sda'
226 To avoid many test, all names are built in the struct.
228 sprintf(hdparams
.devnode_disk
, "/dev/%s", harddrive
);
229 /* Address the partition or raid partition (eg dev/sda or /dev/sdap1 */
230 sprintf(hdparams
.devnode_part
, "/dev/%s%s", harddrive
,raid_disk
? "p" : "");
231 /* Now the names after the machine is booted. Only scsi is affected
232 and we only install on the first scsi disk. */
234 fprintf(flog
, "Destination drive: %s\n", hdparams
.devnode_disk
);
236 sprintf(message
, ctr
[TR_PREPARE_HARDDISK
], hdparams
.devnode_disk
);
240 yesnoharddisk
[0] = ctr
[TR_NO
];
241 yesnoharddisk
[1] = ctr
[TR_YES
];
242 yesnoharddisk
[2] = NULL
;
246 rc
= newtWinMenu(title
, message
,
247 50, 5, 5, 6, yesnoharddisk
,
249 ctr
[TR_CANCEL
], NULL
);
257 sprintf(message
, ctr
[TR_CHOOSE_FILESYSTEM
]);
258 rc
= newtWinMenu( ctr
[TR_CHOOSE_FILESYSTEM
], message
,
259 50, 5, 5, 6, fstypes
, &fstype
, ctr
[TR_OK
],
260 ctr
[TR_CANCEL
], NULL
);
268 /* Calculate amount of memory in machine */
269 if ((handle
= fopen("/proc/meminfo", "r")))
271 while (fgets(line
, STRING_SIZE
-1, handle
)) {
272 if (sscanf (line
, "MemTotal: %s kB", string
)) {
273 memory
= atoi(string
) / 1024 ;
279 /* Partition, mkswp, mkfs.
280 * before partitioning, first determine the sizes of each
281 * partition. In order to do that we need to know the size of
284 /* Don't use mysystem here so we can redirect output */
285 sprintf(commandstring
, "/sbin/sfdisk -s /dev/%s > /tmp/disksize 2> /dev/null", harddrive
);
286 system(commandstring
);
288 /* Calculate amount of disk space */
289 if ((handle
= fopen("/tmp/disksize", "r"))) {
290 fgets(line
, STRING_SIZE
-1, handle
);
291 if (sscanf (line
, "%s", string
)) {
292 disk
= atoi(string
) / 1024;
297 fprintf(flog
, "Disksize = %ld, memory = %ld", disk
, memory
);
299 /* Calculating Swap-Size dependend of Ram Size */
302 else if (memory
<= 1024 && memory
> 256)
305 swap_file
= memory
/ 4;
307 /* Calculating Root-Size dependend of Max Disk Space */
309 root_partition
= 200;
310 else if ( disk
>= 756 && disk
<= 3072 )
311 root_partition
= 512;
313 root_partition
= 2048;
316 /* Calculating the amount of free space */
317 boot_partition
= 20; /* in MB */
318 system_partition
= disk
- ( root_partition
+ swap_file
+ boot_partition
);
320 fprintf(flog
, ", boot = %ld, swap = %ld, mylog = %ld, root = %ld\n",
321 boot_partition
, swap_file
, system_partition
, root_partition
);
324 if ( (!unattended
) && (((disk
- (root_partition
+ swap_file
+ boot_partition
)) < 256 ) && ((disk
- (root_partition
+ boot_partition
)) > 256)) ) {
325 rc
= newtWinChoice(title
, ctr
[TR_OK
], ctr
[TR_CANCEL
], ctr
[TR_CONTINUE_NO_SWAP
]);
328 system_partition
= disk
- ( root_partition
+ swap_file
+ boot_partition
);
329 fprintf(flog
, "Changing Swap Size to 0 MB.\n");
332 fprintf(flog
, "Disk is too small.\n");
333 errorbox(ctr
[TR_DISK_TOO_SMALL
]);goto EXIT
;
336 else if (disk
- (root_partition
+ swap_file
+ boot_partition
) >= 256) {
340 fprintf(flog
, "Disk is too small.\n");
341 errorbox(ctr
[TR_DISK_TOO_SMALL
]);goto EXIT
;
344 handle
= fopen("/tmp/partitiontable", "w");
348 fprintf(handle
, ",%ld,L,*\n,%ld,S,\n,%ld,L,\n,,L,\n",
349 boot_partition
, swap_file
, root_partition
);
351 fprintf(handle
, ",%ld,L,*\n,0,0,\n,%ld,L,\n,,L,\n",
352 boot_partition
, root_partition
);
357 snprintf(commandstring
, STRING_SIZE
, "/sbin/sfdisk -L -uM %s < /tmp/partitiontable", hdparams
.devnode_disk
);
358 if (runcommandwithstatus(commandstring
, ctr
[TR_PARTITIONING_DISK
]))
360 errorbox(ctr
[TR_UNABLE_TO_PARTITION
]);
364 if (fstype
== EXT2
) {
365 mysystem("/sbin/modprobe ext2");
366 sprintf(mkfscommand
, "/sbin/mke2fs -T ext2");
367 } else if (fstype
== REISERFS
) {
368 mysystem("/sbin/modprobe reiserfs");
369 sprintf(mkfscommand
, "/sbin/mkreiserfs -f");
370 } else if (fstype
== EXT3
) {
371 mysystem("/sbin/modprobe ext3");
372 sprintf(mkfscommand
, "/sbin/mke2fs -T ext3");
375 snprintf(commandstring
, STRING_SIZE
, "/sbin/mke2fs -T ext2 -I 128 %s1", hdparams
.devnode_part
);
376 if (runcommandwithstatus(commandstring
, ctr
[TR_MAKING_BOOT_FILESYSTEM
]))
378 errorbox(ctr
[TR_UNABLE_TO_MAKE_BOOT_FILESYSTEM
]);
383 snprintf(commandstring
, STRING_SIZE
, "/sbin/mkswap %s2", hdparams
.devnode_part
);
384 if (runcommandwithstatus(commandstring
, ctr
[TR_MAKING_SWAPSPACE
]))
386 errorbox(ctr
[TR_UNABLE_TO_MAKE_SWAPSPACE
]);
391 snprintf(commandstring
, STRING_SIZE
, "%s %s3", mkfscommand
, hdparams
.devnode_part
);
392 if (runcommandwithstatus(commandstring
, ctr
[TR_MAKING_ROOT_FILESYSTEM
]))
394 errorbox(ctr
[TR_UNABLE_TO_MAKE_ROOT_FILESYSTEM
]);
398 snprintf(commandstring
, STRING_SIZE
, "%s %s4", mkfscommand
, hdparams
.devnode_part
);
399 if (runcommandwithstatus(commandstring
, ctr
[TR_MAKING_LOG_FILESYSTEM
]))
401 errorbox(ctr
[TR_UNABLE_TO_MAKE_LOG_FILESYSTEM
]);
405 snprintf(commandstring
, STRING_SIZE
, "/bin/mount %s3 /harddisk", hdparams
.devnode_part
);
406 if (runcommandwithstatus(commandstring
, ctr
[TR_MOUNTING_ROOT_FILESYSTEM
]))
408 errorbox(ctr
[TR_UNABLE_TO_MOUNT_ROOT_FILESYSTEM
]);
412 mkdir("/harddisk/boot", S_IRWXU
|S_IRWXG
|S_IRWXO
);
413 mkdir("/harddisk/var", S_IRWXU
|S_IRWXG
|S_IRWXO
);
414 mkdir("/harddisk/var/log", S_IRWXU
|S_IRWXG
|S_IRWXO
);
416 snprintf(commandstring
, STRING_SIZE
, "/bin/mount %s1 /harddisk/boot", hdparams
.devnode_part
);
417 if (runcommandwithstatus(commandstring
, ctr
[TR_MOUNTING_BOOT_FILESYSTEM
]))
419 errorbox(ctr
[TR_UNABLE_TO_MOUNT_BOOT_FILESYSTEM
]);
423 snprintf(commandstring
, STRING_SIZE
, "/sbin/swapon %s2", hdparams
.devnode_part
);
424 if (runcommandwithstatus(commandstring
, ctr
[TR_MOUNTING_SWAP_PARTITION
]))
426 errorbox(ctr
[TR_UNABLE_TO_MOUNT_SWAP_PARTITION
]);
430 snprintf(commandstring
, STRING_SIZE
, "/bin/mount %s4 /harddisk/var", hdparams
.devnode_part
);
431 if (runcommandwithstatus(commandstring
, ctr
[TR_MOUNTING_LOG_FILESYSTEM
]))
433 errorbox(ctr
[TR_UNABLE_TO_MOUNT_LOG_FILESYSTEM
]);
437 snprintf(commandstring
, STRING_SIZE
,
438 "/bin/tar -C /harddisk -xvf /cdrom/" SNAME
"-" VERSION
".tlz --lzma 2>/dev/null");
440 if (runcommandwithprogress(60, 4, title
, commandstring
, INST_FILECOUNT
,
441 ctr
[TR_INSTALLING_FILES
]))
443 errorbox(ctr
[TR_UNABLE_TO_INSTALL_FILES
]);
447 /* Save language und local settings */
448 write_lang_configs(shortlangname
);
451 rename ("/harddisk/bin/uname.bak", "/harddisk/bin/uname");
453 /* mount proc filesystem */
454 mysystem("mkdir /harddisk/proc");
455 mysystem("/bin/mount --bind /proc /harddisk/proc");
456 mysystem("/bin/mount --bind /dev /harddisk/dev");
457 mysystem("/bin/mount --bind /sys /harddisk/sys");
459 /* Build cache lang file */
460 snprintf(commandstring
, STRING_SIZE
, "/usr/sbin/chroot /harddisk /usr/bin/perl -e \"require '" CONFIG_ROOT
"/lang.pl'; &Lang::BuildCacheLang\"");
461 if (runcommandwithstatus(commandstring
, ctr
[TR_INSTALLING_LANG_CACHE
]))
463 errorbox(ctr
[TR_UNABLE_TO_INSTALL_LANG_CACHE
]);
467 /* Update /etc/fstab */
468 snprintf(commandstring
, STRING_SIZE
, "/bin/sed -i -e \"s#DEVICE1#UUID=$(/sbin/blkid %s1 -sUUID | /usr/bin/cut -d'\"' -f2)#g\" /harddisk/etc/fstab", hdparams
.devnode_part
);
469 system(commandstring
);
470 snprintf(commandstring
, STRING_SIZE
, "/bin/sed -i -e \"s#DEVICE2#UUID=$(/sbin/blkid %s2 -sUUID | /usr/bin/cut -d'\"' -f2)#g\" /harddisk/etc/fstab", hdparams
.devnode_part
);
471 system(commandstring
);
472 snprintf(commandstring
, STRING_SIZE
, "/bin/sed -i -e \"s#DEVICE3#UUID=$(/sbin/blkid %s3 -sUUID | /usr/bin/cut -d'\"' -f2)#g\" /harddisk/etc/fstab", hdparams
.devnode_part
);
473 system(commandstring
);
474 snprintf(commandstring
, STRING_SIZE
, "/bin/sed -i -e \"s#DEVICE4#UUID=$(/sbin/blkid %s4 -sUUID | /usr/bin/cut -d'\"' -f2)#g\" /harddisk/etc/fstab", hdparams
.devnode_part
);
475 system(commandstring
);
477 if (fstype
== EXT2
) {
478 replace("/harddisk/etc/fstab", "FSTYPE", "ext2");
479 replace("/harddisk/boot/grub/grub.conf", "MOUNT", "ro");
480 } else if (fstype
== REISERFS
) {
481 replace("/harddisk/etc/fstab", "FSTYPE", "reiserfs");
482 replace("/harddisk/boot/grub/grub.conf", "MOUNT", "ro");
483 } else if (fstype
== EXT3
) {
484 replace("/harddisk/etc/fstab", "FSTYPE", "ext3");
486 replace("/harddisk/boot/grub/grub.conf", "MOUNT", "ro");
489 replace("/harddisk/boot/grub/grub.conf", "KVER", KERNEL_VERSION
);
491 snprintf(commandstring
, STRING_SIZE
, "/bin/sed -i -e \"s#root=ROOT#root=UUID=$(/sbin/blkid %s3 -sUUID | /usr/bin/cut -d'\"' -f2)#g\" /harddisk/boot/grub/grub.conf", hdparams
.devnode_part
);
492 system(commandstring
);
494 mysystem("ln -s grub.conf /harddisk/boot/grub/menu.lst");
496 system("/bin/sed -e 's#/harddisk#/#g' -e 's#//#/#g' < /proc/mounts > /harddisk/etc/mtab");
498 snprintf(commandstring
, STRING_SIZE
,
499 "/usr/sbin/chroot /harddisk /usr/sbin/grub-install --no-floppy %s", hdparams
.devnode_disk
);
500 if (runcommandwithstatus(commandstring
, ctr
[TR_INSTALLING_GRUB
])) {
501 errorbox(ctr
[TR_UNABLE_TO_INSTALL_GRUB
]);
505 /* Copy restore file from cdrom */
506 if (unattended
&& (strlen(restore_file
) > 0)) {
507 fprintf(flog
, "unattended: Copy restore file\n");
508 snprintf(commandstring
, STRING_SIZE
,
509 "cp /cdrom/%s /harddisk/var/ipfire/backup", restore_file
);
510 mysystem(commandstring
);
513 mysystem("umount /cdrom");
514 snprintf(commandstring
, STRING_SIZE
, "/usr/bin/eject /dev/%s", sourcedrive
);
515 mysystem(commandstring
);
518 sprintf(message
, ctr
[TR_CONGRATULATIONS_LONG
],
520 newtWinMessage(ctr
[TR_CONGRATULATIONS
], ctr
[TR_OK
], message
);
526 fprintf(flog
, "Install program ended.\n");
529 newtWinMessage(title
, ctr
[TR_OK
], ctr
[TR_PRESS_OK_TO_REBOOT
]);
531 freekeyvalues(ethernetkv
);
533 if (allok
&& !allok_fastexit
)
536 fprintf(flog
, "Entering unattended setup\n");
537 if (unattended_setup(unattendedkv
)) {
538 snprintf(commandstring
, STRING_SIZE
, "/bin/sleep 10");
539 runcommandwithstatus(commandstring
, "Unattended installation finished, system will reboot");
541 errorbox("Unattended setup failed.");
550 // if (!unattended) {
551 // if (system("/usr/sbin/chroot /harddisk /usr/local/sbin/setup /dev/tty2 INSTALL"))
552 // printf("Unable to run setup.\n");
555 if (system("/bin/umount /harddisk/proc"))
556 printf("Unable to umount /harddisk/proc.\n");
566 snprintf(commandstring
, STRING_SIZE
, "/bin/swapoff %s2", hdparams
.devnode_part
);
571 system("/bin/umount /harddisk/proc");
572 system("/bin/umount /harddisk/dev");
573 system("/bin/umount /harddisk/sys");
575 system("/bin/umount /harddisk/var");
576 system("/bin/umount /harddisk/boot");
577 system("/bin/umount /harddisk");