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
[])
38 char *langnames
[] = { "Deutsch", "English", "Français", "Español", NULL
};
39 char *shortlangnames
[] = { "de", "en", "fr", "es", NULL
};
40 char **langtrs
[] = { de_tr
, en_tr
, fr_tr
, es_tr
, NULL
};
42 char harddrive
[30], sourcedrive
[5]; /* Device holder. */
43 struct devparams hdparams
, cdromparams
; /* Params for CDROM and HD */
45 char commandstring
[STRING_SIZE
];
46 char mkfscommand
[STRING_SIZE
];
47 char *fstypes
[] = { "ext2", "ext3", "ReiserFS", NULL
};
48 int fstype
= REISERFS
;
53 char shortlangname
[10];
55 char title
[STRING_SIZE
];
59 struct keyvalue
*ethernetkv
= initkeyvalues();
60 FILE *handle
, *cmdfile
;
61 char line
[STRING_SIZE
];
62 char string
[STRING_SIZE
];
63 long memory
= 0, disk
= 0, free
;
64 long system_partition
, boot_partition
, root_partition
, swap_file
;
66 char *yesnoharddisk
[3]; // char *yesnoharddisk = { "NO", "YES", NULL };
69 struct keyvalue
*unattendedkv
= initkeyvalues();
71 char restore_file
[STRING_SIZE
] = "";
73 setlocale (LC_ALL
, "");
74 sethostname( SNAME
, 10);
76 memset(&hdparams
, 0, sizeof(struct devparams
));
77 memset(&cdromparams
, 0, sizeof(struct devparams
));
79 /* Log file/terminal stuff. */
82 if (!(flog
= fopen(argv
[1], "w+")))
90 fprintf(flog
, "Install program started.\n");
95 newtDrawRootText(14, 0, NAME
" " VERSION
" - " SLOGAN
);
96 sprintf (title
, "%s %s - %s", NAME
, VERSION
, SLOGAN
);
98 if (! (cmdfile
= fopen("/proc/cmdline", "r")))
100 fprintf(flog
, "Couldn't open commandline: /proc/cmdline\n");
102 fgets(line
, STRING_SIZE
, cmdfile
);
104 // check if we have to make an unattended install
105 if (strstr (line
, "unattended") != NULL
) {
107 runcommandwithstatus("/bin/sleep 10", "WARNING: Unattended installation will start in 10 seconds...");
111 // Load common modules
112 mysystem("/sbin/modprobe iso9660"); // CDROM
113 mysystem("/sbin/modprobe ext2"); // Boot patition
114 mysystem("/sbin/modprobe vfat"); // USB key
116 /* German is the default */
117 for (choice
= 0; langnames
[choice
]; choice
++)
119 if (strcmp(langnames
[choice
], "Deutsch") == 0)
122 if (!langnames
[choice
])
126 rc
= newtWinMenu("Language selection", "Select the language you wish to use for the " NAME
".", 50, 5, 5, 8,
127 langnames
, &choice
, "Ok", NULL
);
130 ctr
= langtrs
[choice
];
131 strcpy(shortlangname
, shortlangnames
[choice
]);
133 newtPushHelpLine(ctr
[TR_HELPLINE
]);
135 sprintf(message
, ctr
[TR_WELCOME
], NAME
);
136 newtWinMessage(title
, ctr
[TR_OK
], message
);
138 switch (mysystem("/bin/mountsource.sh")) {
142 errorbox(ctr
[TR_NO_CDROM
]);
146 /* read source drive letter */
147 if ((handle
= fopen("/tmp/source_device", "r")) == NULL
) {
148 errorbox(ctr
[TR_ERROR_PROBING_CDROM
]);
151 fgets(sourcedrive
, 5, handle
);
152 fprintf(flog
, "Source drive: %s\n", sourcedrive
);
158 fprintf(flog
, "Harddisk scan pass %i\n", i
);
160 switch (mysystem("/bin/mountdest.sh") % 255) {
161 case 0: // Found IDE disk
166 case 1: // Found SCSI disk
171 case 2: // Found RAID disk
176 case 10: // No harddisk found
178 errorbox(ctr
[TR_NO_HARDDISK
]);
181 // Do this if the kudzu-scan fails...
182 runcommandwithstatus("/bin/probehw.sh deep-scan", ctr
[TR_PROBING_HARDWARE
]);
187 if ((handle
= fopen("/tmp/dest_device", "r")) == NULL
) {
188 errorbox(ctr
[TR_NO_HARDDISK
]);
191 fgets(harddrive
, 30, handle
);
194 /* load unattended configuration */
196 fprintf(flog
, "unattended: Reading unattended.conf\n");
198 (void) readkeyvalues(unattendedkv
, UNATTENDED_CONF
);
199 findkey(unattendedkv
, "RESTORE_FILE", restore_file
);
202 /* Make the hdparms struct and print the contents.
203 With USB-KEY install and SCSI disk, while installing, the disk
204 is named 'sdb,sdc,...' (following keys)
205 On reboot, it will become 'sda'
206 To avoid many test, all names are built in the struct.
208 sprintf(hdparams
.devnode_disk
, "/dev/%s", harddrive
);
209 /* Address the partition or raid partition (eg dev/sda or /dev/sdap1 */
210 sprintf(hdparams
.devnode_part
, "/dev/%s%s", harddrive
,raid_disk
? "p" : "");
211 /* Now the names after the machine is booted. Only scsi is affected
212 and we only install on the first scsi disk. */
214 fprintf(flog
, "Destination drive: %s\n", hdparams
.devnode_disk
);
216 sprintf(message
, ctr
[TR_PREPARE_HARDDISK
], hdparams
.devnode_disk
);
220 yesnoharddisk
[0] = ctr
[TR_NO
];
221 yesnoharddisk
[1] = ctr
[TR_YES
];
222 yesnoharddisk
[2] = NULL
;
226 rc
= newtWinMenu(title
, message
,
227 50, 5, 5, 6, yesnoharddisk
,
229 ctr
[TR_CANCEL
], NULL
);
237 sprintf(message
, ctr
[TR_CHOOSE_FILESYSTEM
]);
238 rc
= newtWinMenu( ctr
[TR_CHOOSE_FILESYSTEM
], message
,
239 50, 5, 5, 6, fstypes
, &fstype
, ctr
[TR_OK
],
240 ctr
[TR_CANCEL
], NULL
);
248 /* Calculate amount of memory in machine */
249 if ((handle
= fopen("/proc/meminfo", "r")))
251 while (fgets(line
, STRING_SIZE
-1, handle
)) {
252 if (sscanf (line
, "MemTotal: %s kB", string
)) {
253 memory
= atoi(string
) / 1024 ;
259 /* Partition, mkswp, mkfs.
260 * before partitioning, first determine the sizes of each
261 * partition. In order to do that we need to know the size of
264 /* Don't use mysystem here so we can redirect output */
265 sprintf(commandstring
, "/sbin/sfdisk -s /dev/%s > /tmp/disksize 2> /dev/null", harddrive
);
266 system(commandstring
);
268 /* Calculate amount of disk space */
269 if ((handle
= fopen("/tmp/disksize", "r"))) {
270 fgets(line
, STRING_SIZE
-1, handle
);
271 if (sscanf (line
, "%s", string
)) {
272 disk
= atoi(string
) / 1024;
277 fprintf(flog
, "Disksize = %ld, memory = %ld", disk
, memory
);
279 /* Calculating Swap-Size dependend of Ram Size */
282 else if (memory
<= 1024 && memory
> 256)
285 swap_file
= memory
/ 4;
287 /* Calculating Root-Size dependend of Max Disk Space */
289 root_partition
= 200;
290 else if ( disk
>= 756 && disk
<= 3072 )
291 root_partition
= 512;
293 root_partition
= 2048;
296 /* Calculating the amount of free space */
297 boot_partition
= 20; /* in MB */
298 system_partition
= disk
- ( root_partition
+ swap_file
+ boot_partition
);
300 fprintf(flog
, ", boot = %ld, swap = %ld, mylog = %ld, root = %ld\n",
301 boot_partition
, swap_file
, system_partition
, root_partition
);
304 if ( (!unattended
) && (((disk
- (root_partition
+ swap_file
+ boot_partition
)) < 256 ) && ((disk
- (root_partition
+ boot_partition
)) > 256)) ) {
305 rc
= newtWinChoice(title
, ctr
[TR_OK
], ctr
[TR_CANCEL
], ctr
[TR_CONTINUE_NO_SWAP
]);
308 system_partition
= disk
- ( root_partition
+ swap_file
+ boot_partition
);
309 fprintf(flog
, "Changing Swap Size to 0 MB.\n");
312 fprintf(flog
, "Disk is too small.\n");
313 errorbox(ctr
[TR_DISK_TOO_SMALL
]);goto EXIT
;
316 else if (disk
- (root_partition
+ swap_file
+ boot_partition
) >= 256) {
320 fprintf(flog
, "Disk is too small.\n");
321 errorbox(ctr
[TR_DISK_TOO_SMALL
]);goto EXIT
;
324 handle
= fopen("/tmp/partitiontable", "w");
328 fprintf(handle
, ",%ld,L,*\n,%ld,S,\n,%ld,L,\n,,L,\n",
329 boot_partition
, swap_file
, root_partition
);
331 fprintf(handle
, ",%ld,L,*\n,0,0,\n,%ld,L,\n,,L,\n",
332 boot_partition
, root_partition
);
337 snprintf(commandstring
, STRING_SIZE
, "/sbin/sfdisk -L -uM %s < /tmp/partitiontable", hdparams
.devnode_disk
);
338 if (runcommandwithstatus(commandstring
, ctr
[TR_PARTITIONING_DISK
]))
340 errorbox(ctr
[TR_UNABLE_TO_PARTITION
]);
344 if (fstype
== EXT2
) {
345 mysystem("/sbin/modprobe ext2");
346 sprintf(mkfscommand
, "/sbin/mke2fs -T ext2");
347 } else if (fstype
== REISERFS
) {
348 mysystem("/sbin/modprobe reiserfs");
349 sprintf(mkfscommand
, "/sbin/mkreiserfs -f");
350 } else if (fstype
== EXT3
) {
351 mysystem("/sbin/modprobe ext3");
352 sprintf(mkfscommand
, "/sbin/mke2fs -T ext3");
355 snprintf(commandstring
, STRING_SIZE
, "/sbin/mke2fs -T ext2 -I 128 %s1", hdparams
.devnode_part
);
356 if (runcommandwithstatus(commandstring
, ctr
[TR_MAKING_BOOT_FILESYSTEM
]))
358 errorbox(ctr
[TR_UNABLE_TO_MAKE_BOOT_FILESYSTEM
]);
363 snprintf(commandstring
, STRING_SIZE
, "/sbin/mkswap %s2", hdparams
.devnode_part
);
364 if (runcommandwithstatus(commandstring
, ctr
[TR_MAKING_SWAPSPACE
]))
366 errorbox(ctr
[TR_UNABLE_TO_MAKE_SWAPSPACE
]);
371 snprintf(commandstring
, STRING_SIZE
, "%s %s3", mkfscommand
, hdparams
.devnode_part
);
372 if (runcommandwithstatus(commandstring
, ctr
[TR_MAKING_ROOT_FILESYSTEM
]))
374 errorbox(ctr
[TR_UNABLE_TO_MAKE_ROOT_FILESYSTEM
]);
378 snprintf(commandstring
, STRING_SIZE
, "%s %s4", mkfscommand
, hdparams
.devnode_part
);
379 if (runcommandwithstatus(commandstring
, ctr
[TR_MAKING_LOG_FILESYSTEM
]))
381 errorbox(ctr
[TR_UNABLE_TO_MAKE_LOG_FILESYSTEM
]);
385 snprintf(commandstring
, STRING_SIZE
, "/bin/mount %s3 /harddisk", hdparams
.devnode_part
);
386 if (runcommandwithstatus(commandstring
, ctr
[TR_MOUNTING_ROOT_FILESYSTEM
]))
388 errorbox(ctr
[TR_UNABLE_TO_MOUNT_ROOT_FILESYSTEM
]);
392 mkdir("/harddisk/boot", S_IRWXU
|S_IRWXG
|S_IRWXO
);
393 mkdir("/harddisk/var", S_IRWXU
|S_IRWXG
|S_IRWXO
);
394 mkdir("/harddisk/var/log", S_IRWXU
|S_IRWXG
|S_IRWXO
);
396 snprintf(commandstring
, STRING_SIZE
, "/bin/mount %s1 /harddisk/boot", hdparams
.devnode_part
);
397 if (runcommandwithstatus(commandstring
, ctr
[TR_MOUNTING_BOOT_FILESYSTEM
]))
399 errorbox(ctr
[TR_UNABLE_TO_MOUNT_BOOT_FILESYSTEM
]);
403 snprintf(commandstring
, STRING_SIZE
, "/sbin/swapon %s2", hdparams
.devnode_part
);
404 if (runcommandwithstatus(commandstring
, ctr
[TR_MOUNTING_SWAP_PARTITION
]))
406 errorbox(ctr
[TR_UNABLE_TO_MOUNT_SWAP_PARTITION
]);
410 snprintf(commandstring
, STRING_SIZE
, "/bin/mount %s4 /harddisk/var", hdparams
.devnode_part
);
411 if (runcommandwithstatus(commandstring
, ctr
[TR_MOUNTING_LOG_FILESYSTEM
]))
413 errorbox(ctr
[TR_UNABLE_TO_MOUNT_LOG_FILESYSTEM
]);
417 snprintf(commandstring
, STRING_SIZE
,
418 "/bin/tar -C /harddisk -xvf /cdrom/" SNAME
"-" VERSION
".tlz --lzma 2>/dev/null");
420 if (runcommandwithprogress(60, 4, title
, commandstring
, INST_FILECOUNT
,
421 ctr
[TR_INSTALLING_FILES
]))
423 errorbox(ctr
[TR_UNABLE_TO_INSTALL_FILES
]);
427 /* Save language und local settings */
428 write_lang_configs(shortlangname
);
431 rename ("/harddisk/bin/uname.bak", "/harddisk/bin/uname");
433 /* mount proc filesystem */
434 mysystem("mkdir /harddisk/proc");
435 mysystem("/bin/mount --bind /proc /harddisk/proc");
436 mysystem("/bin/mount --bind /dev /harddisk/dev");
437 mysystem("/bin/mount --bind /sys /harddisk/sys");
439 /* Build cache lang file */
440 snprintf(commandstring
, STRING_SIZE
, "/usr/sbin/chroot /harddisk /usr/bin/perl -e \"require '" CONFIG_ROOT
"/lang.pl'; &Lang::BuildCacheLang\"");
441 if (runcommandwithstatus(commandstring
, ctr
[TR_INSTALLING_LANG_CACHE
]))
443 errorbox(ctr
[TR_UNABLE_TO_INSTALL_LANG_CACHE
]);
447 /* Update /etc/fstab */
448 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
);
449 system(commandstring
);
450 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
);
451 system(commandstring
);
452 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
);
453 system(commandstring
);
454 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
);
455 system(commandstring
);
457 if (fstype
== EXT2
) {
458 replace("/harddisk/etc/fstab", "FSTYPE", "ext2");
459 replace("/harddisk/boot/grub/grub.conf", "MOUNT", "ro");
460 } else if (fstype
== REISERFS
) {
461 replace("/harddisk/etc/fstab", "FSTYPE", "reiserfs");
462 replace("/harddisk/boot/grub/grub.conf", "MOUNT", "ro");
463 } else if (fstype
== EXT3
) {
464 replace("/harddisk/etc/fstab", "FSTYPE", "ext3");
466 replace("/harddisk/boot/grub/grub.conf", "MOUNT", "ro");
469 replace("/harddisk/boot/grub/grub.conf", "KVER", KERNEL_VERSION
);
471 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
);
472 system(commandstring
);
474 mysystem("ln -s grub.conf /harddisk/boot/grub/menu.lst");
476 system("/bin/sed -e 's#/harddisk#/#g' -e 's#//#/#g' < /proc/mounts > /harddisk/etc/mtab");
478 snprintf(commandstring
, STRING_SIZE
,
479 "/usr/sbin/chroot /harddisk /usr/sbin/grub-install --no-floppy %s", hdparams
.devnode_disk
);
480 if (runcommandwithstatus(commandstring
, ctr
[TR_INSTALLING_GRUB
])) {
481 errorbox(ctr
[TR_UNABLE_TO_INSTALL_GRUB
]);
485 /* Copy restore file from cdrom */
486 if (unattended
&& (strlen(restore_file
) > 0)) {
487 fprintf(flog
, "unattended: Copy restore file\n");
488 snprintf(commandstring
, STRING_SIZE
,
489 "cp /cdrom/%s /harddisk/var/ipfire/backup", restore_file
);
490 mysystem(commandstring
);
493 mysystem("umount /cdrom");
494 snprintf(commandstring
, STRING_SIZE
, "/usr/bin/eject /dev/%s", sourcedrive
);
495 mysystem(commandstring
);
498 sprintf(message
, ctr
[TR_CONGRATULATIONS_LONG
],
500 newtWinMessage(ctr
[TR_CONGRATULATIONS
], ctr
[TR_OK
], message
);
506 fprintf(flog
, "Install program ended.\n");
509 newtWinMessage(title
, ctr
[TR_OK
], ctr
[TR_PRESS_OK_TO_REBOOT
]);
511 freekeyvalues(ethernetkv
);
513 if (allok
&& !allok_fastexit
)
516 fprintf(flog
, "Entering unattended setup\n");
517 if (unattended_setup(unattendedkv
)) {
518 snprintf(commandstring
, STRING_SIZE
, "/bin/sleep 10");
519 runcommandwithstatus(commandstring
, "Unattended installation finished, system will reboot");
521 errorbox("Unattended setup failed.");
530 // if (!unattended) {
531 // if (system("/usr/sbin/chroot /harddisk /usr/local/sbin/setup /dev/tty2 INSTALL"))
532 // printf("Unable to run setup.\n");
535 if (system("/bin/umount /harddisk/proc"))
536 printf("Unable to umount /harddisk/proc.\n");
546 snprintf(commandstring
, STRING_SIZE
, "/bin/swapoff %s2", hdparams
.devnode_part
);
551 system("/bin/umount /harddisk/proc");
552 system("/bin/umount /harddisk/dev");
553 system("/bin/umount /harddisk/sys");
555 system("/bin/umount /harddisk/var");
556 system("/bin/umount /harddisk/boot");
557 system("/bin/umount /harddisk");