Typo
[linux_from_scratch_hints.git] / OLD / uclibc-bootfloppy.txt
blob61c0cd073060d93ba91a606d34dbe8567ace16e5
1 TITLE:          uClibc-based bootfloppy
2 LFS VERSION:    All (tested against LFS-pre4.0)
3 AUTHOR:         Csaba Henk <ekho@math-inst.hu>
5 SYNOPSIS: 
6         This hints shows a quick and dirty way of creating a
7         cutting-edge bootfloppy from scratch, based on uClibc, a
8         lightweight C library. "Quick and dirty" means using an ad-hoc,
9         non-chroot, but safe development environment. 
11 VERSION:        0.5
13 CONTENTS
15 Introduction
16 What do we need?
17 Installing uClibc on the development platform
18 Setting up the development environment
19 Creating the root filesystem
20 Installing uClibc for the bootdisk
21 Compiling the linux kernel
22 Installing busybox
23 Installing gpm
24 Other programs
25 Creating the bootdisk
26 Credits, contributions, bugs and weirdnesses
27 Appendix A -- the mkbootdisk script
29 PREFACE
31 * For the most recent version check out 
33 http://www.math-inst.hu/~ekho/lowlife/uclibc-bootfloppy.txt
35 * See changelog at 
37 http://www.math-inst.hu/~ekho/lowlife/Changelog
39 * Comments, ideas, critics, flames are welcome.
41 * (If you are an LFS user, you can skip this.) Although this document is
42 formally an LFS hint, you can see use of it without knowing what LFS is
43 (I tried to write it keeping this possibility in mind). Anyway, I
44 recommend you to check out what LFS is. If you are not familiar with
45 compiling source code in unix/linux environment, then there is no use of
46 going on without checking out what LFS is. Consult the following docs
47 first:
49 http://www.linuxfromscratch.org/lfs/intro.shtml
50 http://www.linuxfromscratch.org/faq/
51 http://www.linuxfromscratch.org/view/4.0/chapter02/chapter02.html
53 HINT
55 Introduction
56 ------------
58 I feel that the bootfloppy created in the BLFS-book is not enough
59 customized to its task. It uses the system glibc and the system
60 bootscripts. These are overkill for a bootfloppy. Here we will create a
61 bootfloppy based on the uClibc C library, which is just made for such
62 purposes. 
64 The bootfloppy will be cutting edge: uses uClibc and Busybox which are
65 actively developed projects for the embedded platform. Moreover, I used
66 a kernel from the 2.4.* branch (but feel free to use other kernel
67 releases). It is a good question to ask whether this is useful: many floppy
68 distros use older kernels for reducing resource usage. I can say the
69 following: on the one hand, I can afford using a recent kernel as my
70 bootfloppy merrily runs on my 486 with 8M RAM; on the other hand, from
71 the moment I began to use this up-to-date stuff, the problems with handling
72 the terminal buffer and plip timeouts vanished. So unless you are really
73 tight in resources, I think using a recent kernel is a good idea. 
75 We will also use some kind of development environment, just to stay on
76 the safe side.
78 This hint has a continuation: there we will describe how to install the
79 svga vncviewer on the bootfloppy, which then becomes capable of turning a
80 machine to an X terminal. (Check out the svnc-x_terminal hint:
82 http://hints.linuxfromscratch.org/hints/svnc-x_terminal.txt
83 http://www.math-inst.hu/~ekho/lowlife/svnc-x_terminal.txt
85
87 Additional info and downloadable bootdisk image with svnc can be found
88 at my homepage (or at its mirror):
90 http://www.math-inst.hu/~ekho/lowlife/
91 http://www.personal.ceu.hu/students/01/Csaba_Henk/lowlife/
93 In this hint the assumption of using an x86 PC (both for making and
94 booting the floppy) and gcc is set. You may try to port it to another
95 architecture / compiler. Doing it on another architecture should not
96 be hard. Doing it with another compiler depends on how much does
97 uClibc support that compiler.  
99 If you copy command from this hint to your shell, be careful that
100 line-terminating backslashes (\) will keep their position (no whitespace
101 characters should follow them). A possible solution is open this hint in
102 the Vim GUI, and copy'n'paste from there.
104 What do we need?
105 ----------------
107 We will need the following programs; most recent versions are
108 recommended.
110 * Addons for the development platform:
112 sudo (optional)
113  ftp://ftp.sudo.ws/pub/sudo/
115 * Programs for the bootfloppy:
117 linux kernel 
118  http://www.kernel.org
119 uClibc
120  http://www.uclibc.org/downloads/
121 busybox
122  http://www.busybox.net/downloads/
123 gpm (optional)
124  ftp://arcana.linux.it/pub/gpm
126 Installing uClibc on the development platform
127 ---------------------------------------------
129 Usually we will optimize to size; in case of uClibc it's done
130 automatically.
132 In the uClibc source tree type
134 make menuconfig
136 uClibc now has a configuration interface similar to that of the linux
137 kernel.  Apply the following setting:
139 Library Installation Options  --->
140         (/lib) Shared library loader path 
142 Apart from this, the default configuration is quite suitable, probably you need
143 not change anything (especially you need not use full math support). However,
144 taking a look at the options never hurts; you should make sure that the "Linux
145 kernel header location" field is filled in correctly (if the kernel release you
146 use for the floppy is not the same as the one used on the development platform,
147 you should use the headers of the release used for the floppy), and you may
148 consider fine-tuning the target processor type. After you exit, 
150 make &&
151 make install
153 Further on we assume that you installed uClibc into
154 /usr/i386-linux-uclibc/ (the default installation location). 
156 Setting up the development environment
157 --------------------------------------
159 We definitely need some kind of development environment to protect
160 us from mucking up our system; eg., if we are in the file system
161 which we will put on the floppy, it is very easy to mistake usr with
162 /usr, and without such a protection, you can imagine that this
163 mistake can have serious consequences... 
165 We will create a non-privileged user named bootdisk for doing the
166 job. First choose a home directory for bootdisk and store this value
167 in $BDISKHOME. Then type:
169 groupadd bootdisk &&
170 useradd -g bootdisk -s /bin/bash -d $BDISKHOME bootdisk &&
171 mkdir -p $BDISKHOME &&
172 cat > $BDISKHOME/.bash_profile << "EOF"
173 export CFLAGS=-Os
174 export PATH=/usr/i386-linux-uclibc/bin/:$PATH:$HOME/bin
175 export CC=i386-uclibc-gcc
178 Of course, you may specify more cflags, like -march=i486. The above
179 setting of the $PATH variable assumes that the usual directories to
180 be contained in the path are already set in /etc/profile. 
182 So the idea is that we will work as user bootdisk; however, there are
183 some tasks during the bootdisk creation which require a privileged user
184 -- namely, mounting ext2 files and raw-copying to a floppy. 
186 We can do two things about this problem: 
188 1) Ignore it, and execute these tasks as root.  
190 2) Make a script named mkbootdisk for doing this tasks. This script must
191 be executable only by root (mode 744). With the help of the sudo utility
192 we let the bootdisk user to use this script as well. That is, we put it
193 to $BDISKHOME/bin and then type:
195 echo "
196 bootdisk ALL = NOPASSWD: $BDISKHOME/bin/mkbootdisk" >> /etc/sudoers
197      
198 A realization of mkbootdisk can be found in Appendix A, or at
200 http://www.math-inst.hu/~ekho/lowlife/mkbootdisk
202 Typing "mkbootdisk -h" will print a brief information on usage.
204 We have the desired environment. We go on compiling the programs. Become
205 user bootdisk:
207 su - bootdisk
209 In the rest of the hint we will act as user bootdisk (except if you
210 don't use the mkbootdisk script: then the appropriate actions are to be
211 done as root).
213 Some of the commands below assume that the actual source archive is
214 decompressed in $HOME (which is the same as the prior $BDISKHOME). 
215 Software installation instructions always start at the point when the
216 source tarball is already decompressed and you entered the source
217 directory (unless we explicitely claim to act differently).
219 Creating the root filesystem
220 -----------------------------------------
222 We will put the files of the root filesysem of the floppy to a directory
223 named rfloppy. Firstly we create the directory and a basic tree of
224 subdirectories which vaguley resembles the FHS idea:
226 cd &&
227 mkdir -p rfloppy/{dev,proc,etc/init.d,sbin,bin,lib,mnt,usr,var/log} &&
228 cat > rfloppy/etc/init.d/rcS << "EOF" &&
229 #!/bin/sh
230 mount proc /proc -t proc
233 The etc/init.d/rcS file will be the startup script for the floppy. Put
234 there what you want to be done in the beginnig -- eg., syslogd, klogd,
235 loading modules, starting gpm, etc.  
237 A very minimal etc directory is used in this setup. Enriching it (with
238 files like passwd, group, fstab, modules.conf, and so on) is up to you
239 -- this minimalist solution works anyway. 
241 Installing uClibc for the bootdisk 
242 ----------------------------------
244 We copy the necessary libraries of uClibc to the bootdisk.
246 cp -a /usr/i386-linux-uclibc/lib/{ld-uClibc*,libc.so.0,libuClibc-*} \
247  ~/rfloppy/lib 
249 Compiling the linux kernel
250 ---------------------------
252 We will compile a network-aware kernel optimized to size. We don't give
253 a complete reference, we just highlight some crucial points.If you need
254 not network-awareness, you may omit TCP/IP netowrking and network
255 drivers; but even in this case it is strongly recommended to include
256 "Unix domain sockets". 
258 In the kernel source tree type:
260 sed -e 's%-O2%-Os%g' -e '/^CFLAGS_KERNEL/s%\(^.*$\)%\1 -Os%' Makefile > \
261  Makefile.tmp &&
262 mv Makefile.tmp Makefile
264 The above commands set optimization for size in the Makefile. Edit the
265 Makefile if you want further optimizations. 
267 Now you should configure the kernel with "make menuconfig". Choose
268 carefully the value in the "Processor type and features  --->  Processor
269 family" meupoint. When choosing options, it is advised to include only
270 those features you really need. Some important ones:
272 Networking options  --->
273         <*> Unix domain sockets
274         [*] TCP/IP networking
276 In the "Network device support  --->" menupoint choose those network
277 drivers you intend to use. If you want to use plip, choose it as a
278 module, and also set:
280 Parallel port support  --->
281         <*> Parallel port support
282         <M>   PC-style hardware
284 This is advised because of the following: as I experienced, if no
285 option is given, the kernel initializes the parallel port (PC-style)
286 IRQ-less!  At boot time we won't have the possibility to pass options
287 to the kernel, as we won't use a boot loader or initrd. Thus we can
288 set the appropriate IRQ value only if we load the parallel port
289 driver as module. Also, to gain more control over the NIC drivers,
290 consider building them as modules.
292 In this hint we use the devfs facility, so choose 
294 File systems  --->
295         [*] /dev file system support
296         [*]   Automatically mount at boot
298 (See a more detailed description in the devfs hint:
300 http://hints.linuxfromscratch.org/hints/devfs.txt
302 ). 
304 And don't forget to include support for the mouse type you will use with
305 the bootfloppy (if you will use any). 
307 Now compile the kernel with the 
309 make dep && make bzImage && make modules
311 commands. Then type
313 mkdir $HOME/rfloppy/lib/modules
315 and copy the NIC driver modules from the drivers/net directory to
316 $HOME/rfloppy/lib/modules, and also the other modules you built. Eg., if
317 you use plip, you will need the drivers/parport/parport_pc.o
318 module.
320 We assume that you do not delete the kernel source. If you do want
321 to delete it, copy arch/i368/boot/bzImage to a safe place before doing
324 If you have troubles with configuring the kernel properly, my .config file
325 might be of your help:
327 http://www.math-inst.hu/~ekho/lowlife/linux-2.4.19/.config
329 Installing busybox
330 ------------------
332 Before making it, adjust the Config.h file according to your needs. To
333 make busybox work with devfs, be sure that the 
335 #define BB_FEATURE_DEVFS
337 line is uncommented (not prefixed with "//"); and it is advised to
338 enable standard Unix utilities, moreover if you want to use network with
339 the floppy, consider uncommenting: 
341 #define BB_IFCONFIG
342 #define BB_TELNET
343 #define BB_TFTP
344 #define BB_FEATURE_IFCONFIG_STATUS
346 Then install it with
348 make CROSS=i386-uclibc- &&
349 make PREFIX=$HOME/rfloppy install
351 If you have troubles with configuring busybox properly, my Config.h file
352 might be of your help:
354 http://www.math-inst.hu/~ekho/lowlife/busybox-0.60.5/Config.h
356 Installing gpm
357 --------------
359 Having mouse at the console is not necessary, but very comfortable for a
360 bootfloppy as well. If you want it, install gpm by running the following
361 commands:
363 export LDFLAGS="-lm" &&
364 ./configure &&
365 make && 
366 unset LDFLAGS &&
367 strip src/gpm &&
368 mkdir -p $HOME/rfloppy/{usr/sbin,var/run} &&
369 cp src/gpm $HOME/rfloppy/usr/sbin &&
370 cp -a /usr/i386-linux-uclibc/lib/{libm-*,libm.so.0} ~/rfloppy/lib
372 [The /var/run directory is needed for gpm at runtime.]
374 Other programs
375 --------------
377 Now if there is anything more you want to have on the floppy, compile it
378 and put it to the appropriate place under $HOME/rfloppy. A list of some
379 possible extensions: 
381 * devfsd (
383 http://ftp.kernel.org/pub/linux/daemons/devfsd/
385 ) is not necessary for the bootfloppy, devfs work fine without it. So
386 install it only if you know what you are doing. However, it needs to be
387 hacked to get it compiled against uClibc. There is a patch for devfsd at 
389 http://www.math-inst.hu/~ekho/lowlife/devfsd-1.3.25-uclibc.patch
391 and you can find some explanation on it at
393 http://www.math-inst.hu/~ekho/lowlife/
395 Note that devfsd depends on the libdl.so.0 library of uClibc (which is a
396 symlink to libdl-0.9.*.so). You have to put these to the /lib of your
397 filesystem.
399 * The svnc-x_terminal hint tells you how to compile and install svgalib
400 and the svga vncviewer to the floppy, giving the bootfloppy the
401 capabilities of an X terminal.
403 * You can put tinylogin (
405 http://tinylogin.busybox.net/
407 ) to the floppy if you want a correct login system on it (with the
408 configuration described in this hint you just get a prompt after
409 booting).
411 * You can put utelnetd ( 
413 http://www.pengutronix.de/software/utelnetd_en.html
415 ) to the floppy if you want to access it remotely. However, don't forget
416 that communication is not encrypted under telnet!
418 * One more useful program is hdparm (
420 http://ftp.ibiblio.org/pub/Linux/system/hardware
422 ): if you boot with this floppy, the harddisk is probably not used, but
423 still is a source of noise by its spinning. You can stop it with
424 hdparm. (See its -y option.)
426 Creating the bootdisk
427 ---------------------
429 Put a floppy to the floppy drive. If you use the mkbootdisk script,
430 check whether the device name of the floppy drive is set correctly in
431 the script (it is set to /dev/fd0 and no option can change it, in order
432 to prevent the bootdisk user in being able to muck up the development
433 platform), and whether the $MKE2FSAPP, $RDEVAPP variables in the script
434 store the correct path to the mke2fs, rdev utilities in your system
435 (they should if you follow standards). If everything is fine, simply run 
437 cd &&
438 sudo mkbootdisk
440 If you don't use the mkbootdisk script, become root, store the name of
441 your floppy device (typically /dev/fd0) in the variable $DISK. Now its
442 time to find out how big the root filesystem of the floppy should be,
443 and how much inodes should it have. Concerning the size, I think the
444 size of the stuff in the rfloppy directory + 150k is enough; concerning
445 the number of inodes, I think the number of files in rfloppy + 100 is
446 enough. But you should know. Store the chosen filesystem size in the
447 variable $SIZE (the number of kilobytes), and the chosen number of
448 inodes in the variable $INODES. Then type the following:
450 cd $BDISKHOME &&
451 # We we create and compress the root filesystem of the floppy:
452 dd if=/dev/zero of=rootfs bs=1k count=$SIZE &&
453 yes | mke2fs -m 0 -N $INODES rootfs &&
454 mkdir -p loop &&
455 mount rootfs -o loop loop &&
456 rmdir loop/lost+found &&
457 cp -a rfloppy/* loop &&
458 chown -R 0:0 loop/* &&
459 umount loop &&
460 dd if=rootfs bs=1k | gzip -v9 > rootfs.gz 
462 Now check whether rootfs.gz and your kernel image
463 (linux-2.4.*/arch/i386/boot/bzImage) fit on a floppy together (a floppy
464 is of 1440k usually but it can be formatted to bigger sizes as well). If
465 everything is fine, go on:
467 # We copy the kernel to the floppy:
468 let KERNELSIZE=`dd bs=1k of=$DISK < linux-2.4.*/arch/i386/boot/bzImage 2>&1 | 
469  sed -n '1s%\([0-9][0-9]*\).*%\1%p'`+1 &&
470 # We perform some adjustments on the kernel copied to the floppy:
471 rdev $DISK 0,0 &&
472 rdev -R $DISK 0 &&
473 rdev -r $DISK `expr 16384 + $KERNELSIZE` &&
474 # Finally we copy the compressed filesystem to its appropriate place \
475 # on the floppy:
476 dd if=rootfs.gz of=$DISK bs=1k seek=$KERNELSIZE
478 [Explanation -- also for those who wonder how the mkbootdisk script
479 works
481 "yes | mke2fs -m 0 -N $INODES rootfs":
483 This pipe construct is a common trick for answering stupid questions
484 non-interactively.
486 "# We copy the kernel to the floppy" :
488 In the command after this comment we not only copy the kernel to floppy,
489 but we also store the number of transferred kb's in the variable
490 $KERNELSIZE.
492 "# We perform some adjustments on the kernel copied to the floppy" :
494 We won't have a bootloader to tell the kernel where to find its root
495 filesystem. The commands after this comment set some specified bits
496 in the kernel, thus hardwiring the location of the root filesystem to
497 it: firstly, we tell the kernel to seek for the filesystem in the
498 floppy, secondly, we tell the kernel that a ramdisk is to be made and
499 the filesystem is to be decompressed to it, thirdly, we tell the
500 kernel the location of the filesystem within the floppy. The number
501 16384 = 2^14 is used for shifting within the range of bits in the
502 kernel devoted for describing these data. For more information
503 consult with the Bootdisk HOWTO available at tldp.org. 
505 "# Finally we copy [...]" :
507 In the dd command after this comment we use the seek option to copy the
508 compressed filesystem nicely after the kernel image.]
510 Now you have the floppy, boot & enjoy! 
512 Credits, contributions, bugs and weirdnesses
513 --------------------------------------------
515 See it in the svnc-x_terminal hint.
519 * * * Appendices * * *
523 Appendix A -- the mkbootdisk script
524 -----------------------------------
526 If you copy and paste the script below to a separate file, be careful
527 that line-terminating backslashes (\) will keep their position in the
528 separate file (no whitespace characters should follow them).
530 %%%<mkbootdisk>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
531 #!/bin/bash
534 # A script for making the bootdisk creation process easy and safe 
536 # If you develop your own floppy linux, then it is advised to do it as a 
537 # non-privileged user (in order to not suck if you happen to do
538 # something  stupid :), and use this (root owned and executable) script
539 # via the sudo utility. 
541 # Below are some values set. Adjust them if necessary.
544 DISK=/dev/fd0
545 DEVICE="$DISK"
546 FSDIR=rfloppy
547 unset FSGZ
548 KERNEL=linux-`uname -r`/arch/i386/boot/bzImage
549 TOADD=16384 # Comes from kernel internals, do no change!
550 MKE2FSAPP=/sbin/mke2fs
551 RDEVAPP=/usr/sbin/rdev # The $MKE2FSAPP and $RDEVAPP variables contain \
552 # an absolute path because of the following: \
553 # by my idea this script is used by a non-privileged user (via sudo) \
554 # whose path does not contain the mke2fs, rdev executables 
555 MKBOOTREC="$HOME"/.mkbootdisk
556 EXTRA_SIZE=150 # Free space left on root filesystem of floppy
557 EXTRA_INODES=100 # Free inodes left on filesystem of floppy
558 FLOPPYSIZE=1440
559 VERSION="0.5"
561 # Do not edit what follows unless your intention is hacking!
563 firstcheck=yes  
564 compress=yes # Any value other than "no" defaults to production of a \
565 #gzipped rootfs 
566 KERNELSIZE=x
567 [ -s "$MKBOOTREC" ] && KERNELSIZE=`cat "$MKBOOTREC"`
568 dokernelcopy=no # Becomes yes if $KERNELSIZE set to a numerical value
569 unset TMPDIR
570 manuallysetkernelsize=no # This variable tracks down whether -r option is used
573 # Usage
576 if [ "$1" = "-h" -o "$1" = "--help" ]; then 
577         echo "\
578 ----------------
579 Bootdisk creation utility, version $VERSION. Usage:
580 With -h or --help being the 1st arg, this help is shown; otherwise
582 `basename "$0"` -k kernelimg -f filesys_dir -s filesys_size -i filesys_inodes \\
583 -r kernelimg_size -c floppy_size [-g gzipped_filesys -d]
585 where \"kernelimg\" is the linux kernel image to be booted by the floppy,
586 and \"filesys_dir\" contains the files to be put in the root filesystem.
588 Default values are: 
589 -k $KERNEL 
590 -f $FSDIR 
591 -s <size of files in filesys_dir + ${EXTRA_SIZE}k> 
592 -i <number of files in filesys_dir + ${EXTRA_INODES}>
593 -r \`cat ~/`basename "$MKBOOTREC"`\`, or x if ~/`basename \
594 "$MKBOOTREC"` is empty or does not exist 
595 -c $FLOPPYSIZE
597 Explanations of options:
598 -r kernelimg_size       a kernelimg of the given size (in kb) is supposed to be
599                         on the disk and kernel copying is skipped, unless size 
600                         is x instead of a number
601 -g gzipped_filesys      gzipped_filesys is copied to the disk
602                         (instead of the contents of filesys_dir)
603 -d                      the floppyimage is written to stdout instead of $DISK
604 -c floppy_size          if the floppy image were bigger than floppy_size 
605                         (in kb), the process is aborted, unless floppy_size is 
606                         x instead of a number
608 Further comments:
610 If kernel is copied and -r was used with a non-numerical value, its size
611 is stored in ~/`basename "${MKBOOTREC}"` (delete that file before using a
612 new kernel image, or use -r x !!)
614 For sake of safety, `basename "$0"` utilizes mktemp; if you don't have it
615 temporary file creation is still done as safely as it's possible
617 Example:
618 * A compressed filesystem is produced from the contents of filesys_dir by
619         `basename "$0"` -r 0 -d > rootfs.gz
620 * A floppy can be made using this compressed filesystem by
621         `basename "$0"` -g rootfs.gz
622 ----------------"
623         exit 1
628 # Getting options
631 while getopts "g:k:f:dr:c:s:i:" option; do
632         case $option in 
633                 g) FSGZ="$OPTARG"
634                    compress=no;;
635                 k) KERNEL="$OPTARG";;
636                 f) FSDIR="$OPTARG";;
637                 d) DEVICE="&1";;
638                 r) KERNELSIZE="$OPTARG"
639                    [ "$OPTARG" -ge 0 ] &>/dev/null && 
640                    manuallysetkernelsize=yes;;
641                 c) FLOPPYSIZE="$OPTARG";;
642                 i) INODES="$OPTARG";; 
643                 s) SIZE="$OPTARG";;
644                 *) exit 1;;
645          esac
646 done
652 # Functions
656 # Auxiliary fnc's
659 # gzipimp -- the concrete way of doing the compression
661 gzipimp()
663         dd if=$1 bs=1k | gzip -v9 > $2
664         # I could not figure out why it is the way to do gzipping, but this is
665         # what is suggested by the clever guys. I'd be happy to be informed
666         # about it...
669 #cleanup -- removes temporary files
671 cleanup()
673         [ -e "$TMPDIR" ] && echo "Removing temporary files..." >&2 
674         rm -rf "$TMPDIR" >&2 
677 #error -- if something goes wrong... 
679 error()
681         echo Error: "$1" >&2
682         cleanup
683         exit 1
686 #maketmpdir -- creates a tmp dir as safely as possible
688 maketmpdir()
690         if ! TMPDIR=`mktemp -d /tmp/mkbootdisk.$$.XXXXXXXXXX 2>/dev/null`
691         then
692                 TMPDIR=/tmp/mkbootdisk.$$.$RANDOM$RANDOM &&
693                 rm -rf "$TMPDIR" &&
694                 mkdir -m 700 "$TMPDIR"
695         fi ||
696         error "Unable to create temporary directory"     
699 #findoutFSGZ -- finds out the appropriate value of $FSGZ
701 findoutFSGZ()
703         [ $compress = yes ] && FSGZ="$TMPDIR"/mkbootdisk-gzipped_fs
707 # Important fnc's
710 # check -- checks the validity of arguments
712 check()
714         for v in RDEVAPP MKE2FSAPP; do #Checking whether these apps can be found
715                 [ -x "`eval echo \\$$v`" ] ||
716                 error \
717 "the value of \$$v is wrong -- `eval echo \\$$v` is not an executable"
718         done
720         for v in SIZE INODES; do # Syntax check of variables
721                 [ -z "`eval echo \\$$v`" ] ||
722                 [ "`eval echo \\$$v`" -ge 0 ] &>/dev/null || 
723                 error \
724 "wrong value for option -- \$$v is not a non-negative integer"  
725         done
727         for v in FLOPPYSIZE KERNELSIZE; do 
728                 [ "`eval echo \\$$v`" = x ] ||
729                 [ "`eval echo \\$$v`" -ge 0 ] &>/dev/null ||
730                 error \
731 "Wrong value for option -- \$$v is neither x, nor non-negative integer"
732         done
734         if [ "$compress" != no ]; then # checking whether $FSDIR is a directory
735                 [ "`file -bL "$FSDIR"`" = directory ] ||
736                 error "$FSDIR is not a directory."
737         fi
739         if [ $KERNELSIZE = x ]; then #checking whether the kernelimg exists
740                 [ "`file -bL "$KERNEL"`" = 'x86 boot sector' ] || 
741                 error "$KERNEL is not a kernelimg." 
742         else 
743                 [ "$firstcheck" = yes ] && 
744                 echo \
745 "A kernelimg of size $KERNELSIZE is supposed to be on the disk,
746 kernel copying is skipped" >&2 
747         fi
749         if [ "$compress" = no ]; then #checking whether the gzipped fs exists
750                 file -bL "$FSGZ" | grep \
751                 'gzip compressed data' > /dev/null || 
752                 error "$FSGZ is not a gzipped file"
753                 [ "$firstcheck" = yes ] && 
754                 echo \
755 "An existing compressed filesystem is used as root filesystem,
756 filesystem creation is skipped." >&2 
757         fi
759         firstcheck= 
762 # getfsdata -- Finds out size and inode number param's of the filesystem
763 # to be created
765 getfsdata()
767         if [ $compress = yes ]; then
768                 [ -z "$SIZE" ] &&
769                 SIZE=$(expr $EXTRA_SIZE + `du -sD "$FSDIR" | awk '{print $1}'`)
770         
771                 [ -z "$INODES" ] &&
772                 INODES=$(expr $EXTRA_INODES + `find "$FSDIR" -follow | wc -l`)
774         fi
777 # compressfs -- Adjusts and compresses the filesystem
778 # (Now also creates the filesys but the name is kept)
780 compressfs()
782         [ "$compress" = no ] && return 0
783         compress=no
785         tmpfs="$TMPDIR"/mkbootdisk-rfloppy
786         tmpmountpt="$TMPDIR"/mkbootdisk-mountpt
788         echo \
789 "Creating an ext2 filesystem of size ${SIZE}k and with $INODES inodes" >&2
790         dd if=/dev/zero of="$tmpfs" bs=1k count=$SIZE
791         yes | "$MKE2FSAPP" -m 0 -N $INODES "$tmpfs" > /dev/null 
792         mkdir -p "$tmpmountpt"
793         mount "$tmpfs" -o loop "$tmpmountpt"
794         rmdir "$tmpmountpt"/lost+found 
795         cp -a "$FSDIR"/* "$tmpmountpt"
796         chown -R 0:0 "$tmpmountpt"/*
797         if umount "$tmpmountpt"; then
798                 echo "Compressing the filesystem..." >&2
799                 gzipimp "$tmpfs" "$FSGZ"
800         else
801                 error "some problem occured with unmounting the file system."
802         fi
806 # floppysizecheck -- checks whether will be enough space on floppy
808 floppysizecheck()
810         [ $FLOPPYSIZE = x ] && return 0
811         FSGZSIZE=$(( `dd if="$FSGZ" of=/dev/null bs=1k 2>&1 |
812          sed -n '1s%\([0-9][0-9]*\).*%\1%p'` + 1 ))
813         [ $(($KERNELSIZE + $FSGZSIZE)) -gt $FLOPPYSIZE ] &&
814         error "
815 size of kernel:                $KERNELSIZE
816 size of compressed filesystem: $FSGZSIZE
817 are altogether:                $KERNELSIZE + $FSGZSIZE = \
818 $(($KERNELSIZE + $FSGZSIZE))
819 which exceeds your floppy size ($FLOPPYSIZE)"
822 # kernelcopy -- copies and installs the kernelimg to disk
824 kernelcopy()
826         [ $KERNELSIZE = x ] || return 0
828         tmpdiskimg="$TMPDIR"/mkbootdisk-diskimg 
829         echo Copying kernel to diskimage file... >&2
830         KERNELSIZE=$(( `dd if="$KERNEL" of="$tmpdiskimg" bs=1k 2>&1 |
831          sed -n '1s%\([0-9][0-9]*\).*%\1%p'` + 1 )) ||
832         error "cannot create disk image file" 
833         echo $(( $KERNELSIZE - 1 ))+1 records in/out >&2
834         [ $manuallysetkernelsize = yes ] || echo $KERNELSIZE > "$MKBOOTREC" 
836         echo Adjusting the kernelimg to mount the file system as rootfs... >&2 
837         "$RDEVAPP" "$tmpdiskimg" 0,0 
838         "$RDEVAPP" -R "$tmpdiskimg" 0
839         "$RDEVAPP" -r "$tmpdiskimg" `expr $TOADD + $KERNELSIZE`
840         dokernelcopy=yes
843 diskwrite()
845         if [ $dokernelcopy = yes ]; then 
846                 echo "Completing the diskimage..." >&2
847                 dd if="$FSGZ" of="$tmpdiskimg" bs=1k seek=$KERNELSIZE 
848                 echo Writing the diskimage to device... >&2
849                 eval "dd if=$tmpdiskimg bs=1k >$DEVICE" 
850         elif [ $dokernelcopy = no ]; then
851                 echo Writing the compressed file system to device... >&2
852                 eval "dd bs=1k seek=$KERNELSIZE >$DEVICE" < "$FSGZ"
853         else
854                 error 'bogus value for $dokernelcopy' 
855         fi || 
856         error "it seems that there is some problem with the target device."
861 # Program body
864 maketmpdir
865 findoutFSGZ
866 check
867 getfsdata
868 compressfs
869 kernelcopy
870 floppysizecheck
871 check
872 diskwrite
873 cleanup
874 exit 0
875 %%%</mkbootdisk>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
878 END of "uClibc-based bootfloppy" hint