]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blob - tools/mkflash/mkflash
git-svn-id: http://svn.ipfire.org/svn/ipfire/IPFire/source@16 ea5c0bd1-69bd-2848...
[people/teissler/ipfire-2.x.git] / tools / mkflash / mkflash
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
53 VERSION="0.4.3"
54 SIZE="$1"
55 CF="$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
59 case "$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 # ;;
80 128)
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 ;;
90 256)
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 ;;
104 512)
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 ;;
118 1gb)
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 ;;
138 esac
139
140 case "$CF" in
141 hda)
142 echo "`date '+%b %e %T'`: Creating hda Compact Flash"
143 drive_ID=hda4
144 ;;
145 hdb)
146 echo "`date '+%b %e %T'`: Creating hdb Compact Flash"
147 drive_ID=hdb4
148 ;;
149 hdc)
150 echo "`date '+%b %e %T'`: Creating hdc Compact Flash"
151 drive_ID=hdc4
152 ;;
153 hdd)
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 ;;
161 esac
162
163
164 # Calculate all the required derived variables...
165 bs=512 # do not change!
166
167 flash_blocks=$(( $flash_MB * 1024 * 1024 / $bs ))
168
169 boot_blocks=$(( $boot_MB * 1024 * 1024 / $bs - 1 ))
170 zlog_blocks=$(( $zlog_MB * 1024 * 1024 / $bs ))
171 root_blocks=$(( $root_MB * 1024 * 1024 / $bs ))
172
173 boot_block_offset=1
174 zlog_block_offset=$(( $boot_block_offset + $boot_blocks ))
175 root_block_offset=$(( $zlog_block_offset + $zlog_blocks ))
176
177 boot_byte_offset=$(( $boot_block_offset * $bs ))
178 zlog_byte_offset=$(( $zlog_block_offset * $bs ))
179 root_byte_offset=$(( $root_block_offset * $bs ))
180
181 cylinders=$(( $flash_blocks / $heads / $sectors ))
182
183 # RAM Disk
184 ramdisk_KB=$(( $ramdisk_MB * 1024 ))
185
186 ############################################################################
187 # #
188 # Loading loopback kernel module #
189 # #
190 ############################################################################
191 echo "`date '+%b %e %T'`: Loading loopback kernel module"
192 modprobe loop
193
194
195 ############################################################################
196 # #
197 # Creating empty flash image in /tmp/cf-image #
198 # #
199 ############################################################################
200 echo "`date '+%b %e %T'`: Creating empty flash image in /tmp"
201 dd if=/dev/zero of=/tmp/flash.img bs=$bs count=$flash_blocks >/dev/null
202 dd if=/dev/zero of=/var/log/part1.img bs=$bs count=$boot_blocks >/dev/null
203 dd if=/dev/zero of=/var/log/part2.img bs=$bs count=$zlog_blocks >/dev/null
204 dd if=/dev/zero of=/var/log/part3.img bs=$bs count=$root_blocks >/dev/null
205
206
207 ############################################################################
208 # #
209 # Making filesystems #
210 # #
211 ############################################################################
212 echo "`date '+%b %e %T'`: Making filesystems"
213 mke2fs -F -j -m 0 -b 1024 /var/log/part1.img >/dev/null
214 mke2fs -F -j -m 0 -b 1024 /var/log/part2.img >/dev/null
215 mke2fs -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 ############################################################################
223 echo "`date '+%b %e %T'`: Creating and partitioning Compact Flash image"
224 sfdisk -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,, # /
230 EOF
231 dd if=/var/log/part1.img of=/tmp/flash.img seek=$boot_block_offset bs=$bs
232 dd if=/var/log/part2.img of=/tmp/flash.img seek=$zlog_block_offset bs=$bs
233 dd if=/var/log/part3.img of=/tmp/flash.img seek=$root_block_offset bs=$bs
234 rm -f /var/log/part?.img
235
236
237 ############################################################################
238 # #
239 # Mounting loopback flash image under /mnt #
240 # #
241 ############################################################################
242 echo "`date '+%b %e %T'`: Mounting loopback flash image under /mnt"
243 rm -rf /mnt/flash
244 mkdir -p /mnt/flash
245 mount -o loop,offset=$root_byte_offset /tmp/flash.img /mnt/flash
246 mkdir -p /mnt/flash/boot
247 mount -o loop,offset=$boot_byte_offset /tmp/flash.img /mnt/flash/boot
248 mkdir -p /mnt/flash/var/log_compressed
249 mount -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 ############################################################################
257 echo "`date '+%b %e %T'`: Creating flash image directory structure"
258 mkdir -p /mnt/flash/{ram/,proc/,mnt/}
259
260
261 ############################################################################
262 # #
263 # Copying files into flash image #
264 # #
265 ############################################################################
266 echo "`date '+%b %e %T'`: Copying files into flash image"
267 # /boot
268 cp -a /boot /mnt/flash/
269
270 # /var
271 mkdir -p /mnt/flash/var/{log,log_compressed,spool,www/icons}
272 cp -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 #######################################################################################
281 if [ -f /root/settings.8139 ]
282 then
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
288 else
289 /bin/echo ""
290 /bin/echo "Flash Is Using Build System Ethernet Settings"
291 /bin/echo ""
292 /bin/echo ""
293
294 fi
295
296 rm -rf /mnt/flash/var/log/lost+found
297 cp -a /var/run /var/empty /mnt/flash/var
298 cp -a /var/spool/cron /mnt/flash/var/spool
299 ln -s /tmp /mnt/flash/var/patches
300 ln -sf /ram/squid /var/log/cache
301 rm -f /mnt/flash/var/lib/logrotate.status
302 rm -f /mnt/flash/var/state/dhcp/*
303 touch /mnt/flash/var/state/dhcp/dhcpd.leases
304 find /mnt/flash/var/run \( -type f -o -type s \) -exec rm -f {} \;
305 touch /mnt/flash/var/run/utmp
306 chmod 644 /mnt/flash/var/run/utmp
307 chown root:utmp /mnt/flash/var/run/utmp
308 touch /mnt/flash/var/log/wtmp
309 chmod 664 /mnt/flash/var/log/wtmp
310 chown root:utmp /mnt/flash/var/log/wtmp
311
312 # /var/log
313 rm -rf /mnt/flash/var/log/snort/*
314 find /mnt/flash/var/log -type f -exec rm -f {} \;
315 tar -C /mnt/flash -czf /mnt/flash/var/log_compressed/log.tgz var/log/
316 rm -rf /mnt/flash/var/log/
317 ln -sf /ram/log /mnt/flash/var/log
318
319 # Other files
320 cp -a /sbin /mnt/flash/
321 cp -a /bin /mnt/flash/
322 cp -a /lib /mnt/flash/
323 cp -a /dev /mnt/flash/
324
325 # create the symlinks
326 echo "`date '+%b %e %T'`: Creating $CF Sym Link"
327 ln -sf /dev/"$CF" /mnt/flash/dev/harddisk
328
329 echo "`date '+%b %e %T'`: Creating "$CF'1'" Sym Link"
330 ln -sf /dev/$CF'1' /mnt/flash/dev/harddisk1
331
332 echo "`date '+%b %e %T'`: Creating "$CF'2'" Sym Link"
333 ln -sf /dev/$CF'2' /mnt/flash/dev/harddisk2
334
335 echo "`date '+%b %e %T'`: Creating "$CF'3'" Sym Link"
336 ln -sf /dev/$CF'3' /mnt/flash/dev/harddisk3
337
338 echo "`date '+%b %e %T'`: Creating "$CF'4'" Sym Link"
339 ln -sf /dev/$CF'4' /mnt/flash/dev/harddisk4
340
341
342 cp -a /root /mnt/flash/
343 rm -f /mnt/flash/root/.bash_history
344 cp -a /etc /mnt/flash/
345 rm -rf /mnt/flash/etc/{httpd/conf/ssl*,makedev.d/}
346 rm -f /mnt/flash/etc/ssh/*key
347 rm -f /mnt/flash/etc/httpd/server.*
348 cp -a /home /mnt/flash/
349 rm -f /home/httpd/html/graphs/*
350 rm -f /home/httpd/html/sgraph/*
351 cp -a /usr /mnt/flash
352 rm -rf /mnt/flash/{tmp/,usr/tmp/,var/tmp/}
353 ln -sf /ram/tmp/ /mnt/flash/tmp
354 ln -sf /ram/tmp/ /mnt/flash/usr/tmp
355 ln -sf /ram/tmp/ /mnt/flash/var/tmp
356 ln -sf /dev/ram0 /mnt/flash/dev/ramdisk
357
358 ############################################################################
359 # #
360 # Installing log backup cron job #
361 # #
362 ############################################################################
363 echo "`date '+%b %e %T'`: Installing log backup cron job"
364 cat >> /mnt/flash/var/spool/cron/root.orig <<EOF
365
366 # Backup logs to flash
367 02 * * * * /etc/rc.d/rc.flash.down
368 EOF
369 chroot /mnt/flash /usr/bin/fcrontab -z >/dev/null 2>&1
370
371
372 ############################################################################
373 # #
374 # Reconfiguring logrotate #
375 # #
376 ############################################################################
377 echo "`date '+%b %e %T'`: Reconfiguring logrotate"
378 cp -pf logrotate.conf /mnt/flash/etc/
379
380
381 ############################################################################
382 # #
383 # Installing new fstab #
384 # #
385 ############################################################################
386 echo "`date '+%b %e %T'`: Installing new fstab"
387 cat /etc/fstab | sed -e 's/log\t/log_compressed/' > /mnt/flash/etc/fstab
388 df | grep /mnt/flash
389
390
391 ############################################################################
392 # #
393 # Touching /etc/FLASH #
394 # #
395 ############################################################################
396 echo "`date '+%b %e %T'`: Touching /etc/FLASH"
397 touch /mnt/flash/etc/FLASH
398
399 ############################################################################
400 # #
401 # Create the Grub.conf file with our parameters #
402 # #
403 ############################################################################
404 cat > /mnt/flash/boot/grub/grub.conf <<EOF
405 timeout 5
406 default saved
407 foreground = 16064e
408 background = ffffff
409 splashimage (hd0,0)/grub/ipcop.xpm.gz
410 title 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
414 title 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
418 title 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
422 title 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
426 EOF
427
428
429 ############################################################################
430 # #
431 # Installing grub #
432 # #
433 ############################################################################
434 echo "`date '+%b %e %T'`: Installing Grub"
435
436 /usr/sbin/grub --batch <<EOF >/dev/null 2>&1
437 device (hd0) /tmp/flash.img
438 geometry (hd0)
439 root (hd0,0)
440 makeactive
441 install $grub_PARMS (hd0,0)/grub/stage1 (hd0) (hd0,0)/grub/stage2 0x8000 p /grub/grub.conf
442 quit
443 EOF
444
445 ############################################################################
446 # #
447 # Cleaning up #
448 # #
449 ############################################################################
450 echo "`date '+%b %e %T'`: Cleaning up"
451 rm -f verinfo
452 umount /mnt/flash/var/log_compressed
453 umount /mnt/flash/boot
454 umount /mnt/flash
455 losetup -d /dev/loop0
456 losetup -d /dev/loop1
457 losetup -d /dev/loop2
458 mv /tmp/flash.img /tmp/$SIZE'flash.img'
459
460 echo "`date '+%b %e %T'`: Mkflash For $SIZE Flash Drive On $CF Complete"