]> git.ipfire.org Git - ipfire-2.x.git/blame - tools/mkflash/mkflash
HinzugefĆ¼gt:
[ipfire-2.x.git] / tools / mkflash / mkflash
CommitLineData
cd1a2927
MT
1#!/bin/bash
2#
3# This file is part of the IPCop Firewall.
4#
5# IPCop is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# IPCop is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with IPCop; if not, write to the Free Software
17# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18#
19# Originally by Guy Ellis and Steve Bauer
20# Copyright 2001 Traverse Technologies Australia - http://www.traverse.com.au/
21#
22# Copyright (C) 2002-04-08 Mark Wormgoor <mark@wormgoor.com>
23# - Modified to use loopback filesystem
24# - Modified to easily change partition sizes
25# Copyright (C) 2003-04-04 Nick Shore <nick.shore@multithread.co.uk>
26# - Added disksize calculations
27# Copyright (C) 2003-04-09 Simon Turner <simont@angledata.co.uk>
28# - Modified to easily change partition sizes and fix
29# some bugs.
30# Copyright (C) 2004-01-29 Mark Wormgoor <mark@wormgoor.com>
31# - Modified for IPCop 1.4 (grub, etc)
32# Copyright (C) 2004-08-17 Dale Haag <dhaag@net-defender.net>
33# - Fixed grub stage2 problem for CF disks
34# - Added command line option to select target CF drive
35# - Modified for creating 256mb, 512mb and 1gb CF disks
36# - Modified routines for building sym links
37# - Added ability to define kernel loading parameters needed for large CF disks
38# - Added ability to define grub install parameters needed for large CF disks
39# - Flash file is created as /tmp/[size]flash.img to allow storing multiple development images
40# Copyright (C) 2004-08-30 Dale Haag <dhaag@net-defender.net>
41# - Fixed issue with ramdisk sym link not properly being created for rc.flash.up
42# - Added ability to configure ethernet settings for a LEX with 3 RTL8139 during flash build
43# Copyright (C) 2005-08-13 Gilles Espinasse <g.esp.ipcop@free.fr>
44# - Use a bigger /boot partition to allow easier kernel upgrade and support smp kernel
45# - Define zlog_MB at 30MB for 512 and 1gb like with 256 flash
46# Features
47# - ext3 file system
48# - auto grub install to CF
49# - compressed logs on flash + log to ramdisk
50#
51# Some SiS chipset don't like ide=nodma parameter (bug SF 1098510), remove in case of problem
52
53VERSION="0.4.3"
54SIZE="$1"
55CF="$2"
56
57# See what we're supposed to do
58# 32 & 64 are too small now for this current script and IPCop 1.4.0
59case "$SIZE" in
60#32)
61# echo "`date '+%b %e %T'`: Creating 32MB Compact Flash"
62# flash_MB=30
63# ramdisk_MB=64
64# boot_MB=3
65# zlog_MB=4
66# root_MB=$(( $flash_MB - $boot_MB - $zlog_MB ))
67# heads=8
68# sectors=32
69# ;;
70#64)
71# echo "`date '+%b %e %T'`: Creating 64MB Compact Flash"
72# flash_MB=61
73# ramdisk_MB=64
74# boot_MB=3
75# zlog_MB=4
76# root_MB=$(( $flash_MB - $boot_MB - $zlog_MB ))
77# heads=8
78# sectors=32
79# ;;
80128)
81 echo "`date '+%b %e %T'`: Creating 128MB Compact Flash"
82 flash_MB=122
83 ramdisk_MB=64
84 boot_MB=8
85 zlog_MB=10
86 root_MB=$(( $flash_MB - $boot_MB - $zlog_MB ))
87 heads=8
88 sectors=32
89 ;;
90256)
91 echo "`date '+%b %e %T'`: Creating 256MB Compact Flash"
92 flash_MB=222
93 ramdisk_MB=64
94 boot_MB=8
95 zlog_MB=30
96 root_MB=$(( $flash_MB - $boot_MB - $zlog_MB ))
97 heads=16
98 sectors=32
99 #kernel_PARMS="idebus=100 ide=nodma ide0=0x177-0x177,0x376" #specific to LEX with CF on secondary master
100 #kernel_PARMS="" # Sis chipset workaround, don't use nodma
101 kernel_PARMS="ide=nodma" # Generic
102 grub_PARMS="--force-lba"
103 ;;
104512)
105 echo "`date '+%b %e %T'`: Creating 512MB Compact Flash"
106 flash_MB=485
107 ramdisk_MB=64
108 boot_MB=8
109 zlog_MB=30
110 root_MB=$(( $flash_MB - $boot_MB - $zlog_MB ))
111 heads=16
112 sectors=32
113 #kernel_PARMS="idebus=100 ide=nodma ide0=0x177-0x177,0x376" #specific to LEX with CF on secondary master
114 #kernel_PARMS="" # Sis chipset workaround, don't use nodma
115 kernel_PARMS="ide=nodma" # Generic
116 grub_PARMS="--force-lba"
117 ;;
1181gb)
119 echo "`date '+%b %e %T'`: Creating 1 Gigabyte Compact Flash"
120 flash_MB=978
121 ramdisk_MB=64
122 boot_MB=8
123 zlog_MB=30
124 root_MB=$(( $flash_MB - $boot_MB - $zlog_MB ))
125 heads=16
126 sectors=32
127 #kernel_PARMS="idebus=100 ide=nodma ide0=0x177-0x177,0x376" #specific to LEX with CF on secondary master
128 #kernel_PARMS="" # Sis chipset workaround, don't use nodma
129 kernel_PARMS="ide=nodma" Generic
130 grub_PARMS="--force-lba"
131 ;;
132
133*)
134# echo "Usage: $0 {32|64|128|256|512|1gb} {hda|hdb|hdc|hdd}"
135 echo "Usage: $0 {128|256|512|1gb} {hda|hdb|hdc|hdd}"
136 exit 1
137 ;;
138esac
139
140case "$CF" in
141hda)
142 echo "`date '+%b %e %T'`: Creating hda Compact Flash"
143 drive_ID=hda4
144 ;;
145hdb)
146 echo "`date '+%b %e %T'`: Creating hdb Compact Flash"
147 drive_ID=hdb4
148 ;;
149hdc)
150 echo "`date '+%b %e %T'`: Creating hdc Compact Flash"
151 drive_ID=hdc4
152 ;;
153hdd)
154 echo "`date '+%b %e %T'`: Creating hdd Compact Flash"
155 drive_ID=hdd4
156 ;;
157*)
158 echo "Usage: $0 {32|64|128|256|512|1gb} {hda|hdb|hdc|hdd}"
159 exit 1
160 ;;
161esac
162
163
164# Calculate all the required derived variables...
165bs=512 # do not change!
166
167flash_blocks=$(( $flash_MB * 1024 * 1024 / $bs ))
168
169boot_blocks=$(( $boot_MB * 1024 * 1024 / $bs - 1 ))
170zlog_blocks=$(( $zlog_MB * 1024 * 1024 / $bs ))
171root_blocks=$(( $root_MB * 1024 * 1024 / $bs ))
172
173boot_block_offset=1
174zlog_block_offset=$(( $boot_block_offset + $boot_blocks ))
175root_block_offset=$(( $zlog_block_offset + $zlog_blocks ))
176
177boot_byte_offset=$(( $boot_block_offset * $bs ))
178zlog_byte_offset=$(( $zlog_block_offset * $bs ))
179root_byte_offset=$(( $root_block_offset * $bs ))
180
181cylinders=$(( $flash_blocks / $heads / $sectors ))
182
183# RAM Disk
184ramdisk_KB=$(( $ramdisk_MB * 1024 ))
185
186############################################################################
187# #
188# Loading loopback kernel module #
189# #
190############################################################################
191echo "`date '+%b %e %T'`: Loading loopback kernel module"
192modprobe loop
193
194
195############################################################################
196# #
197# Creating empty flash image in /tmp/cf-image #
198# #
199############################################################################
200echo "`date '+%b %e %T'`: Creating empty flash image in /tmp"
201dd if=/dev/zero of=/tmp/flash.img bs=$bs count=$flash_blocks >/dev/null
202dd if=/dev/zero of=/var/log/part1.img bs=$bs count=$boot_blocks >/dev/null
203dd if=/dev/zero of=/var/log/part2.img bs=$bs count=$zlog_blocks >/dev/null
204dd if=/dev/zero of=/var/log/part3.img bs=$bs count=$root_blocks >/dev/null
205
206
207############################################################################
208# #
209# Making filesystems #
210# #
211############################################################################
212echo "`date '+%b %e %T'`: Making filesystems"
213mke2fs -F -j -m 0 -b 1024 /var/log/part1.img >/dev/null
214mke2fs -F -j -m 0 -b 1024 /var/log/part2.img >/dev/null
215mke2fs -F -j -m 0 -b 1024 /var/log/part3.img >/dev/null
216
217
218############################################################################
219# #
220# Creating and partitioning Compact Flash image #
221# #
222############################################################################
223echo "`date '+%b %e %T'`: Creating and partitioning Compact Flash image"
224sfdisk -H $heads -S $sectors -C $cylinders -uM /tmp/flash.img <<EOF >/dev/null 2>&1
225# Start Size Type Bootable
226,$boot_MB,,* # /boot
227,$zlog_MB,, # /var/log_compressed
228,0,, # Unused
229,$root_MB,, # /
230EOF
231dd if=/var/log/part1.img of=/tmp/flash.img seek=$boot_block_offset bs=$bs
232dd if=/var/log/part2.img of=/tmp/flash.img seek=$zlog_block_offset bs=$bs
233dd if=/var/log/part3.img of=/tmp/flash.img seek=$root_block_offset bs=$bs
234rm -f /var/log/part?.img
235
236
237############################################################################
238# #
239# Mounting loopback flash image under /mnt #
240# #
241############################################################################
242echo "`date '+%b %e %T'`: Mounting loopback flash image under /mnt"
243rm -rf /mnt/flash
244mkdir -p /mnt/flash
245mount -o loop,offset=$root_byte_offset /tmp/flash.img /mnt/flash
246mkdir -p /mnt/flash/boot
247mount -o loop,offset=$boot_byte_offset /tmp/flash.img /mnt/flash/boot
248mkdir -p /mnt/flash/var/log_compressed
249mount -o loop,offset=$zlog_byte_offset /tmp/flash.img /mnt/flash/var/log_compressed
250
251
252############################################################################
253# #
254# Creating flash image directory structure #
255# #
256############################################################################
257echo "`date '+%b %e %T'`: Creating flash image directory structure"
258mkdir -p /mnt/flash/{ram/,proc/,mnt/}
259
260
261############################################################################
262# #
263# Copying files into flash image #
264# #
265############################################################################
266echo "`date '+%b %e %T'`: Copying files into flash image"
267# /boot
268cp -a /boot /mnt/flash/
269
270# /var
271mkdir -p /mnt/flash/var/{log,log_compressed,spool,www/icons}
272cp -a /var/ipcop /var/lib /var/state /var/lock /var/log /mnt/flash/var
273
274#######################################################################################
275# Configure flash image ethernet settings #
276# this allows you to stage on one system and build flash for LEX with 3 RTL8139 NIC's #
277# if a settings file named settings.8139 is placed in your /root directory it will be #
278# used to configure the flash image with the correct ethernet settings for the LEX #
279# a default settings.8139 file is provided for your use #
280#######################################################################################
281if [ -f /root/settings.8139 ]
282then
283 /bin/echo ""
284 /bin/echo "Configuring Flash With LEX RTL8139 Ethernet Settings"
285 /bin/echo ""
286 /bin/echo ""
287 cat /root/settings.8139 > /mnt/flash/var/ipcop/ethernet/settings
288else
289 /bin/echo ""
290 /bin/echo "Flash Is Using Build System Ethernet Settings"
291 /bin/echo ""
292 /bin/echo ""
293
294fi
295
296rm -rf /mnt/flash/var/log/lost+found
297cp -a /var/run /var/empty /mnt/flash/var
298cp -a /var/spool/cron /mnt/flash/var/spool
299ln -s /tmp /mnt/flash/var/patches
300ln -sf /ram/squid /var/log/cache
301rm -f /mnt/flash/var/lib/logrotate.status
302rm -f /mnt/flash/var/state/dhcp/*
303touch /mnt/flash/var/state/dhcp/dhcpd.leases
304find /mnt/flash/var/run \( -type f -o -type s \) -exec rm -f {} \;
305touch /mnt/flash/var/run/utmp
306chmod 644 /mnt/flash/var/run/utmp
307chown root:utmp /mnt/flash/var/run/utmp
308touch /mnt/flash/var/log/wtmp
309chmod 664 /mnt/flash/var/log/wtmp
310chown root:utmp /mnt/flash/var/log/wtmp
311
312# /var/log
313rm -rf /mnt/flash/var/log/snort/*
314find /mnt/flash/var/log -type f -exec rm -f {} \;
315tar -C /mnt/flash -czf /mnt/flash/var/log_compressed/log.tgz var/log/
316rm -rf /mnt/flash/var/log/
317ln -sf /ram/log /mnt/flash/var/log
318
319# Other files
320cp -a /sbin /mnt/flash/
321cp -a /bin /mnt/flash/
322cp -a /lib /mnt/flash/
323cp -a /dev /mnt/flash/
324
325# create the symlinks
326echo "`date '+%b %e %T'`: Creating $CF Sym Link"
327ln -sf /dev/"$CF" /mnt/flash/dev/harddisk
328
329echo "`date '+%b %e %T'`: Creating "$CF'1'" Sym Link"
330ln -sf /dev/$CF'1' /mnt/flash/dev/harddisk1
331
332echo "`date '+%b %e %T'`: Creating "$CF'2'" Sym Link"
333ln -sf /dev/$CF'2' /mnt/flash/dev/harddisk2
334
335echo "`date '+%b %e %T'`: Creating "$CF'3'" Sym Link"
336ln -sf /dev/$CF'3' /mnt/flash/dev/harddisk3
337
338echo "`date '+%b %e %T'`: Creating "$CF'4'" Sym Link"
339ln -sf /dev/$CF'4' /mnt/flash/dev/harddisk4
340
341
342cp -a /root /mnt/flash/
343rm -f /mnt/flash/root/.bash_history
344cp -a /etc /mnt/flash/
345rm -rf /mnt/flash/etc/{httpd/conf/ssl*,makedev.d/}
346rm -f /mnt/flash/etc/ssh/*key
347rm -f /mnt/flash/etc/httpd/server.*
348cp -a /home /mnt/flash/
349rm -f /home/httpd/html/graphs/*
350rm -f /home/httpd/html/sgraph/*
351cp -a /usr /mnt/flash
352rm -rf /mnt/flash/{tmp/,usr/tmp/,var/tmp/}
353ln -sf /ram/tmp/ /mnt/flash/tmp
354ln -sf /ram/tmp/ /mnt/flash/usr/tmp
355ln -sf /ram/tmp/ /mnt/flash/var/tmp
356ln -sf /dev/ram0 /mnt/flash/dev/ramdisk
357
358############################################################################
359# #
360# Installing log backup cron job #
361# #
362############################################################################
363echo "`date '+%b %e %T'`: Installing log backup cron job"
364cat >> /mnt/flash/var/spool/cron/root.orig <<EOF
365
366# Backup logs to flash
36702 * * * * /etc/rc.d/rc.flash.down
368EOF
369chroot /mnt/flash /usr/bin/fcrontab -z >/dev/null 2>&1
370
371
372############################################################################
373# #
374# Reconfiguring logrotate #
375# #
376############################################################################
377echo "`date '+%b %e %T'`: Reconfiguring logrotate"
378cp -pf logrotate.conf /mnt/flash/etc/
379
380
381############################################################################
382# #
383# Installing new fstab #
384# #
385############################################################################
386echo "`date '+%b %e %T'`: Installing new fstab"
387cat /etc/fstab | sed -e 's/log\t/log_compressed/' > /mnt/flash/etc/fstab
388df | grep /mnt/flash
389
390
391############################################################################
392# #
393# Touching /etc/FLASH #
394# #
395############################################################################
396echo "`date '+%b %e %T'`: Touching /etc/FLASH"
397touch /mnt/flash/etc/FLASH
398
399############################################################################
400# #
401# Create the Grub.conf file with our parameters #
402# #
403############################################################################
404cat > /mnt/flash/boot/grub/grub.conf <<EOF
405timeout 5
406default saved
407foreground = 16064e
408background = ffffff
409splashimage (hd0,0)/grub/ipcop.xpm.gz
410title IPCop
411 root (hd0,0)
412 kernel /vmlinuz root=/dev/$drive_ID panic=10 acpi=off ro $kernel_PARMS ramdisk_size=$ramdisk_KB
413 savedefault
414title IPCop SMP
415 root (hd0,0)
416 kernel /vmlinuz-smp root=/dev/$drive_ID panic=10 acpi=off ro $kernel_PARMS ramdisk_size=$ramdisk_KB
417 savedefault
418title IPCop (ACPI enabled)
419 root (hd0,0)
420 kernel /vmlinuz root=/dev/$drive_ID panic=10 ro $kernel_PARMS ramdisk_size=$ramdisk_KB
421 savedefault
422title IPCop SMP (ACPI HT enabled)
423 root (hd0,0)
424 kernel /vmlinuz-smp root=/dev/$drive_ID panic=10 acpi=ht ro $kernel_PARMS ramdisk_size=$ramdisk_KB
425 savedefault
426EOF
427
428
429############################################################################
430# #
431# Installing grub #
432# #
433############################################################################
434echo "`date '+%b %e %T'`: Installing Grub"
435
436/usr/sbin/grub --batch <<EOF >/dev/null 2>&1
437device (hd0) /tmp/flash.img
438geometry (hd0)
439root (hd0,0)
440makeactive
441install $grub_PARMS (hd0,0)/grub/stage1 (hd0) (hd0,0)/grub/stage2 0x8000 p /grub/grub.conf
442quit
443EOF
444
445############################################################################
446# #
447# Cleaning up #
448# #
449############################################################################
450echo "`date '+%b %e %T'`: Cleaning up"
451rm -f verinfo
452umount /mnt/flash/var/log_compressed
453umount /mnt/flash/boot
454umount /mnt/flash
455losetup -d /dev/loop0
456losetup -d /dev/loop1
457losetup -d /dev/loop2
458mv /tmp/flash.img /tmp/$SIZE'flash.img'
459
460echo "`date '+%b %e %T'`: Mkflash For $SIZE Flash Drive On $CF Complete"