e6afb47eee735ed8b7611de07f881ccad1afa9de
[pkg-fso/files.git] / install.sh
1 #!/bin/bash
2 #
3 # Auto-Installer for Debian on the Openmoko Neo phones (GTA01 and GTA02)
4 #
5 # Copyright 2008 Joachim Breitner <nomeata@debian.org>
6 # Copyright 2008-2009 Luca Capello <luca@pca.it>
7 # Copyright 2009 Steffen Moeller <moeller@debian.org>
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License along
20 # with this program; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22
23
24 set -e
25
26 # This helper function expects names of variables as arguments
27 # and converts all their contents to lower case.
28 lc () {
29         # chosing a rarely seen variable name to avoid masking
30         for i_____i in $*
31         do
32                 cmd="$i_____i=\$(echo \$$i_____i|tr 'A-Z' 'a-z')"
33                 #echo $cmd
34                 eval $cmd
35         done
36 }
37
38 #
39 # INSTALLER CONFIGURATION
40 #
41
42 # user variables
43 APT_OPTIONS=${APT_OPTIONS:- --yes}
44 APT_RECOMMENDS=${APT_RECOMMENDS:-false}
45 BOOTSTRAPPER=${BOOTSTRAPPER:-cdebootstrap}
46 CDEBOOTSTRAP_EXTRA_FLAGS=${CDEBOOTSTRAP_EXTRA_FLAGS:-}
47 DASH_BINSH=${DASH_BINSH:-true}
48 DEBOOTSTRAP_EXTRA_FLAGS=${DEBOOTSTRAP_EXTRA_FLAGS:-}
49 DISPLAY_MANAGER=${DISPLAY_MANAGER:-nodm}
50 FSO_MIRROR=${FSO_MIRROR:-http://pkg-fso.alioth.debian.org/debian}
51 INST_DIR=${INST_DIR:-/mnt/debian}
52 INST_MIRROR=${INST_MIRROR:-http://ftp2.de.debian.org/debian}
53 LOCALEPURGE=${LOCALEPURGE:-}
54 QI=${QI:-false}
55 QI_VERBOSE_BOOT=${QI_VERBOSE_BOOT:-false}
56 ROOT_PASSWORD=${ROOT_PASSWORD:-changeme}
57 SD_DEVICE=${SD_DEVICE:-/dev/mmcblk0}
58 SD_SYS_BLK=${SD_SYS_BLK:-/sys/block/${SD_DEVICE#/dev/}}
59 SD_PART1_FS=${SD_PART1_FS:-ext2}
60 SD_PART1_SIZE=${SD_PART1_SIZE:-8}
61 SD_PART2_FS=${SD_PART2_FS:-ext3}
62 SD_SWAP_SIZE=${SD_SWAP_SIZE:-0}
63 SINGLE_PART=${SINGLE_PART:-$QI}
64 TRAYER=${TRAYER:-trayer}
65 ZHONE=${ZHONE:-true}
66
67 # let us all see the same error messages and possibly suppress some by apt-get
68 export LANG=C.UTF-8
69
70 # hardening for the case that a user uses capitals
71 lc APT_RECOMMENDS BOOTSTRAPPER FSO_DEVICE QI QI_VERBOSE_BOOT SD_PART1_FS SD_PART2_FS 
72
73 # device autodetection if the user has not set FSO_DEVICE
74 FSO_DEVICE_AUTODETECTED=`grep GTA /proc/cpuinfo | awk '{print $3}' | tr "[:upper:]" "[:lower:]"`
75 if [ -z "$FSO_DEVICE" -a -n "$FSO_DEVICE_AUTODETECTED" ]; then
76         FSO_DEVICE=$FSO_DEVICE_AUTODETECTED
77 fi
78
79 # this is an user variable, which by default depends on the FSO_DEVICE value
80 HOSTNAME=${HOSTNAME:-debian}
81 if [ "$HOSTNAME" = debian ]; then
82     HOSTNAME="debian-$FSO_DEVICE"
83 fi
84
85 # general variables
86 VERBOSE=${VERBOSE:-}
87 TESTHOST=${TESTHOST:-www.debian.org}
88 RDATEHOST=${RDATEHOST:-ntp.fu-berlin.de}
89 DROPBEAR_KEYS="/etc/dropbear/dropbear_rsa_host_key /etc/dropbear/dropbear_dss_host_key"
90 MOUNT_PROC=true
91
92 # internal variables
93 internal_version="3.1"          # the version of this file
94 intern_instdir_already_present="" 
95 intern_no_partitioning=""
96
97 # debootstrap and cdebootstrap options
98 # with packages for a basic debian with network connectivity
99 CDEBOOTSTRAP_FLAVOUR=minimal
100 DEBOOTSTRAP_VARIANT=minbase
101 CDEBOOTSTRAP_PACKAGE=$INST_MIRROR/pool/main/c/cdebootstrap/cdebootstrap-static_0.5.9_armel.deb
102 DEBOOTSTRAP_PACKAGE=$INST_MIRROR/pool/main/d/debootstrap/debootstrap_1.0.42_all.deb
103 CDEBOOTSTRAP_EXTRA_PACKAGES=
104 DEBOOTSTRAP_EXTRA_PACKAGES=install-info
105 # This gets passed to boot strappers. currently not used
106 BOOTSTRAP_EXCLUDE_PACKAGES=hello
107
108 # Those packages should be installed with every Debian install, invariant of the bootstrapper used.
109 DEBIAN_EXTRA_PACKAGES=ifupdown,dropbear,udev,procps,netbase,vim-tiny,module-init-tools,wget,openssh-client
110
111 # freesmartphone.org packages
112 # because of some apt-get bug, console-setup must be listed
113 # before any other X.Org package, for more information see
114 # http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=435662#45
115 FSO_PACKAGES="matchbox-window-manager fso-frameworkd openmoko-panel-plugin dbus-x11 $TRAYER fso-utils mtd-utils \
116 console-setup xserver-xorg-core xorg omhacks xserver-xorg-video-glamo xserver-xorg-input-tslib xterm xinit xfonts-base x11-xserver-utils"
117
118 # well-suited software for the FreeRunner
119 PACKAGES_TASK_AUDIO="intone"
120 PACKAGES_TASK_GAMES="mokomaze sgt-puzzles"
121 PACKAGES_TASK_GPS="gpsd foxtrotgps monav navit"
122 PACKAGES_TASK_COM="phoneui-apps pidgin"
123 PACKAGES_TASK_NET="inetutils-ping host iptables"
124 PACKAGES_TASK_TEXT="less"
125 PACKAGES_TASK_WEB="midori epdfview"
126 PACKAGES_TASK_WIFI="wicd dhcp3-client wpasupplicant wireless-tools"
127 PACKAGES_TASK_UTILS="scrot debfoster"
128 # tasks defined
129 intern_tasks_defined="AUDIO,GAMES,GPS,COM,NET,TEXT,WEB,WIFI,UTILS"
130
131 # device-specific values
132 UBOOT_ENV_SIZE=262144
133
134 # mkfs.vfat is not installed by default in most of the Openmoko images
135 DOSFSTOOLS_PACKAGE=$INST_MIRROR/pool/main/d/dosfstools/dosfstools_3.0.1-1_armel.deb
136
137 # FSO-MS5 busybox's tar does not support -z
138 TAR_PACKAGE=$INST_MIRROR/pool/main/t/tar/tar_1.26-2_armel.deb
139 TAR_APPLICATION=tar
140
141
142 #
143 # FUNCTIONS
144 #
145
146 # for the installation of packages, /proc is sometimes required to be
147 # installed. cdebootstrap is installing it itself.
148 ensure_proc_is_mounted () {
149
150         if  [ -z "$MOUNT_PROC" ]; then
151                 if [ ! -r "$INST_DIR"/proc/cpuinfo ]; then
152                         echo "W: /proc not mounted in chroot, install may fail."
153                 fi
154                 return 0;
155         fi
156
157         if [ ! -d "$INST_DIR/proc" ]; then mkdir "$INST_DIR/proc"; fi
158
159         if mount | grep -q "$INST_DIR/proc"; then
160                 if [ -n "$VERBOSE" ]; then
161                         echo "I: /proc is already mounted in chroot"
162                 fi
163                 return 0;
164         fi
165
166         if mount -t proc none "$INST_DIR/proc"; then
167                 echo "I: installed /proc in chroot"
168                 return 0;
169         else
170                 echo "E: could not mount /proc in chroot"
171                 return 1;
172         fi
173
174 }
175
176 # cdebootstrap performs that automatically, but we need to take some
177 # extra precaution when installing packages that would normally
178 # start services. Those might interfere with local services or just
179 # expect bits to be mounted that are not mounted or ... you name it.
180 diverting_invoke_rc_d () {
181         if [ ! -r "$INST_DIR"/usr/sbin/invoke-rc.d.diverted ]; then
182                 mv "$INST_DIR"/usr/sbin/invoke-rc.d "$INST_DIR"/usr/sbin/invoke-rc.d.diverted
183                 echo "I: diverted invoke-rc.d"
184         else
185                 echo "I: invoke-rc.d is already diverted - was previous install interrupted?"
186                 # continuing anyway, since functional existence of dummy is not guaranteed.
187         fi
188         cat > "$INST_DIR"/usr/sbin/invoke-rc.d <<EOINVOKERCD
189 #!/bin/sh
190 echo "I: ignoring call to invoke-rc.d '\$*'"
191 EOINVOKERCD
192         chmod 755 "$INST_DIR"/usr/sbin/invoke-rc.d 
193 }
194
195
196 # this function complements the action of diverting_invoke_rc_d
197 restoring_invoke_rc_d () {
198         if [ -f "$INST_DIR"/usr/sbin/invoke-rc.d.diverted ]; then
199                 mv "$INST_DIR"/usr/sbin/invoke-rc.d.diverted "$INST_DIR"/usr/sbin/invoke-rc.d 
200                 echo "I: restored invoke-rc.d"
201         else
202                 echo "E: no invoke-rc.d found to be put back in place. Investigate!"
203         fi
204 }
205
206
207 # fight against auto-mounting
208 umount_all () {
209         if [ -n "$VERBOSE" ]; then echo "Now unmounting all SD devices."; fi
210         for I in $(seq 0 9); do
211                 cmd="mount | grep -q '^${SD_DEVICE}p$I'"
212                 if [ -n "$VERBOSE" ]; then echo -n " testing #$I ($cmd)"; fi
213                 if mount | grep -q "^${SD_DEVICE}p$I" ; then
214                         if [ -n "$VERBOSE" ]; then 
215                                 echo " unmounting"
216                         else
217                                 echo "W: ${SD_DEVICE}p$I is mounted, unmounting"
218                         fi
219                         umount ${SD_DEVICE}p$I
220                 #       if mount | grep -q ^${SD_DEVICE}p$I; then
221                 #               echo "E: Unmount failed"
222                 #               exit 1
223                 #       fi
224                 elif [ -n "$VERBOSE" ]; then
225                         echo 'not mounted'
226                 fi
227         done
228         if [ -n "$VERBOSE" ]; then echo "All SD devices unmounted."; fi
229 }
230
231 five_seconds_to_quit () {
232         # since dash doesn't support 'read' -t and -n options, this script
233         # should be executed only by /bin/bash.  However, all official
234         # Openmoko images use busybox, which doesn't provide /bin/bash
235         # The nested invocation of the bash circumvents a bug in the bash
236         # distributed with Om2008.12.
237         intern_var=$(read -t 5 -n 1 intern_var || intern_var=continue; echo $intern_var)
238         if [ "$intern_var" != "continue" ]; then
239                 echo "E: aborting on user request"
240                 exit 1
241         fi
242 }
243
244 card_data_warning () {
245
246         cat <<__END__
247 Starting to $1 the microSD card
248
249 ***********
250 * WARNING *
251 ***********
252
253 This will destroy all the data on the microSD card!!!
254
255 Press any key within 5 seconds to quit
256 __END__
257
258         five_seconds_to_quit
259
260 }
261
262 root_password_warning () {
263         cat <<__END__
264 ***********
265 * WARNING *
266 ***********
267
268 Starting from version 3.0, the root password is no more empty,
269 but defaults to 'changeme'.  You can configure it through the
270 ROOT_PASSWORD variable.
271
272 Press any key within 5 seconds to quit
273 __END__
274
275         five_seconds_to_quit
276
277 }
278
279 usage () {
280         cat <<__END__
281
282 NAME
283         install.sh - Auto-Installer for Debian on the Openmoko Neo phones (GTA01 and GTA02) v$internal_version
284
285 SYNOPSIS
286         [variable1=value1 variable2 ...] `basename $0` [options] all
287         [variable1=value1 variable2 ...] `basename $0` [options] <stages>
288
289 DESCRIPTION
290
291         This script is to be executed on an OpenMoko Neo devices
292         (either 1973 or FreeRunner), two openly designed telephones,
293         to install the Debian operating system on an SD card. The
294         system goes through multiple stages, and can be modified to
295         adapt to local preferences.
296
297         The individual stages are described below. The script uses
298         cdebootstrap or debootstrap to prepare a directory to which
299         it can chroot(8) and complete the configuration. 
300
301 ENVIRONMENT
302
303         To influence the program's behaviour, set the following
304         environment variables or leave the default values.
305
306         APT_OPTIONS  extra parameters sent to apt for installing packages (set
307                      to '$APT_OPTIONS').
308         HOSTNAME     the name of the machine (set to '$HOSTNAME').
309         SD_DEVICE    the character device to access the microSD card (set to
310                      '$SD_DEVICE').
311         SD_SYS_BLK   the directory under /sys/block that refers to the microSD
312                      card (set to '$SD_SYS_BLK', or derived from the
313                      value of SD_DEVICE).
314         SD_PART1_FS  the filesystem for the first microSD card partition, i.e.
315                      the one containing the uImage.bin (set to '$SD_PART1_FS', possible
316                      values are ext2/vfat, also ext3 when using Qi as
317                      bootloader).
318         SD_PART1_SIZE
319                      number of megabytes for first partition (set to '$SD_PART1_SIZE').
320         SD_SWAP_SIZE
321                      number of megabytes of swap partition (set to '$SD_SWAP_SIZE').
322         INST_DIR     the directory where the microSD card partitions will be
323                      mounted to (set to '$INST_DIR').
324         INST_MIRROR  the Debian mirror used during installation, it should be
325                      set to a mirror nearby that carries armel binaries for
326                      unstable (set to
327                      '$INST_MIRROR').
328         LOCALEPURGE  set this to true, if you want to install the package of the
329                      same name. It reduces disk space enormously, but needs user
330                      interaction for the install (set to '$LOCALEPURGE').
331         APT_RECOMMENDS
332                      control if APT should install recommends by default (set to
333                      '$APT_RECOMMENDS', possible values are false/true).
334         BOOTSTRAPPER determines the bootstrapper to use, should be set to
335                      deboostrap or cdebootstrap - cdebootstrap is somewhat
336                      faster and you may have to "opkg install perl", but it
337                      seems to work less reliably (set to '$BOOTSTRAPPER').
338                      For debootstrap please do not run anything besides
339                      install.sh as you will need all your memory.
340         CDEBOOTSTRAP_EXTRA_FLAGS
341                      parameters to be added to invocation of cdebootstrap, only
342                      active if BOOTSTRAPPER is not set to debootstrap.
343         DASH_BINSH   configure dash as default /bin/sh (set to '$DASH_BINSH', possible
344                      values are true/false).
345         DEBOOTSTRAP_EXTRA_FLAGS
346                      parameters to be added to invocation of debootstrap, only
347                      active if BOOTSTRAPPER is not set to cdebootstrap.
348         FSO_MIRROR   the Debian freesmartphone.org repository (set to
349                      '$FSO_MIRROR').
350         FSO_DEVICE   the device the installation is being performed on (set to
351                      '$FSO_DEVICE', default to autodetection, possible values are
352                      gta01/gta02).
353         QI           if you use Qi bootloader set this to true (set to '$QI').
354         QI_VERBOSE_BOOT
355                      set this to true if you want to see the kernel messages; do
356                      this only when QI=true (set to '$QI_VERBOSE_BOOT').
357         RDATEHOST    machine with which to sync against with ntp (set to
358                      '$RDATEHOST').
359         SINGLE_PART  set this to true if only one partition should be created;
360                      this is true by default for QI=true and false if
361                      SD_PART1_FS=vfat (set to '$SINGLE_PART').
362         TRAYER       name of package to fill the menu bar, supported are
363                      'trayer' and 'stalonetray' (set to '$TRAYER').
364         TESTHOST     name of machine that should be tested for availability
365                      (set to '$TESTHOST').
366         VERBOSE      increase amount of information that is forwarded to stdout
367                      (set to '$VERBOSE').
368         DISPLAY_MANAGER
369                      choose the Display Manager to be installed (default to
370                      '$DISPLAY_MANAGER', possible values are any *dm package in the Debian
371                      archive, which could require a LOT of disk space; set this
372                      to none if you do not want a Display Manager).
373         ZHONE        set this to true to install the Zhone GUI (set to '$ZHONE').
374         ROOT_PASSWORD
375                      the password for the root account (set to '$ROOT_PASSWORD').
376
377     Stages:
378
379         all          Runs all the stages in the following order:
380                                 testing time partition format mount
381                                 debian apt fso configuration tasks
382                                 kernel cleanup unmount
383         testing      Verifys that everything is in place for this
384                      installer to work
385         time         Fetches the current time from $RDATEHOST via rdate
386         partition    Partitions the microSD card found in $SD_DEVICE
387                      (WARNING, this will destroy all the data on the
388                      microSD card unless the partioning is the same as
389                      used before; this can be used to recover a broken
390                      partiton table due to the suspend/resume problem)
391         format       Formats the microSD card found in '$SD_DEVICE'
392                      (WARNING, this will destroy all the data on the
393                      microSD card)
394         mount        Mounts microSD card partitions under '$INST_DIR'
395         debian       Installs a basic Debian system, including an SSH server
396         apt          Configure APT
397         fso          Install freesmartphone.org packages (by default this
398                      also include the Zhone GUI, see the ZHONE variable)
399         tasks        Installs packages for a series of tasks. Supported are
400                        AUDIO  ($PACKAGES_TASK_AUDIO)
401                        COM    software supporting the communication of
402                               users ($PACKAGES_TASK_COM). 
403                        GAMES  installs games that were designed with
404                               FreeRunner-like devices in mind or that have
405                               been found compatible ($PACKAGES_TASK_GAMES)
406                        GPS    software for GPS, geocaching and navigation
407                               ($PACKAGES_TASK_GPS)
408                        NET    software that is beneficial to investigate
409                               the network setup and/or forward traffic
410                               to USB-attached devices, e.g. to connect a
411                               a laptop to the internet via the freephone's
412                               internet connection (GPRS or Wifi).  See
413                               http://wiki.openmoko.org/wiki/Tethering for
414                               the setup ($PACKAGES_TASK_NET).
415                        TEXT   allows improved handling of text
416                               ($PACKAGES_TASK_TEXT)
417                        WEB    ($PACKAGES_TASK_WEB)
418                        WIFI   ($PACKAGES_TASK_WIFI)
419                        UTILS  whatever was wanted that does not belong to the
420                               other classes ($PACKAGES_TASK_UTILS)
421                        ALL    Selection of all the tasks listed above.
422
423                      To specify the tasks shall be assigned to the TASKS
424                      environment variable as a comma separated list (set to
425                      '$TASKS').
426         configuration    Configures various parts of the system, such as
427                      the X server
428         kernel       Downloads and installs the Debian kernel package (only
429                      when SD_PART1_FS is set to ext2, now '$SD_PART1_FS')
430         cleanup      Remove unnecessary package and clean APT cache
431         unmount      Unmounts the Debian filesystem mounted at '$INST_DIR'
432
433     Unsupported or buggy stages, use at your own risk:
434
435         uboot        Configures the U-Boot environment to boot Debian (it
436                      needs to run between the debian and the unmount stage,
437                      only when SD_PART1_FS is set to ext2, now '$SD_PART1_FS')
438
439 OPTIONS
440         For users uncomfortable with setting environment variables, the following
441         options have been implemented to be set as arguments:
442
443         -h|--help    shows this information
444         --verbose    same as invocation with VERBOSE=true as environment variable
445         --apt-options <options>
446                      sets the APT_OPTIONS variable
447         --hostname <name>
448                      sets the HOSTNAME variable
449         --mount-proc
450                      mounts /proc directory in Debian chroot
451         -np|--no-partitioning
452                      disable the partitioning when otherwise doing "all"
453         --part1-fs <vfat|ext2|ext3>
454                      sets the SD_PART1_FS variable
455         --part1-size <megabytes>
456                      sets the SD_PART1_SIZE variable
457         --swap-size <megabytes>
458                      sets the SD_SWAP_SIZE variable
459         --with-tasks <tasks>
460                      sets the TASKS variable
461         --with-localepurge
462                      will install the localepurge package
463         --trayer <trayer>
464                      sets the TRAYER variable
465         --bootstrapper (debootstrap|cdebootstrap)
466                      sets the BOOTSTRAPPER variable
467
468 EXAMPLE
469
470         Next to your install.sh script you may want to create a file
471         that remembers the parameters for you that you have chosen for
472         your previous setup.
473
474         ./install.sh --part1-fs vfat --part1-size 16  all
475
476         Performs a complete installation. Tasks would be addressed if the
477         TASKS environment variable would have been set.
478
479         TASKS="GAMES,GPS" ./install.sh --part1-fs vfat --part1-size 16  all
480
481         As before, but now extra packages are installed (and configured)
482         to prepare a selection of games and a reasonably complete utilisation
483         of the GPS funcionality.
484
485         TASKS="GAMES,GPS" ./install.sh --part1-fs vfat --part1-size 16  tasks
486
487         This is the delta between the prior two examples.
488
489         TASKS="GAMES,GPS" ./install.sh --part1-fs vfat --part1-size 16
490
491         E: This does nothing since no install target is defined.
492
493 BUGS
494         Only the current (second) generation of devices is fully supported.
495         This script should be functional with the GTA01 except for the
496         installation of the kernel.
497
498         For comments or contributions, you can directly send emails
499         to the list mentioned below. However, please be aware of the
500         pkg-fso git repository that helps orchestrating our efforts. You
501         can check it out with
502           git clone git://git.debian.org/pkg-fso/files.git
503         perform your changes and prepare patches with
504           git format-patch origin/master
505         to send to the authors or to the pkg-fso mailing list
506         pkg-fso-maint@lists.alioth.debian.org .
507
508 AUTHORS
509         Copyright 2008 Joachim Breitner <nomeata@debian.org>
510         Copyright 2008 Luca Capello <luca@pca.it>
511         Copyright 2009 Steffen Moeller <moeller@debian.org>
512
513         This program is licensed under the terms of GNU General Public
514         License either version 2, or (at your option) any later version.
515
516 SEE ALSO
517         This project's homepage: http://wiki.debian.org/DebianOnFreeRunner
518         The OpenMoko project: http://www.openmoko.org
519
520 __END__
521
522         exit 0
523 }
524
525
526
527 vfat_check () {
528 # /boot on vfat is no more supported (http://bugs.debian.org/315493)
529 # http://lists.linuxtogo.org/pipermail/smartphones-userland/2008-November/000502.html
530         if [ "$SD_PART1_FS" = "vfat" ]; then
531                 cat <<__END__
532 ***********
533 * WARNING *
534 ***********
535
536 For the /boot partition you have chosen to use vfat, which is not a
537 POSIX-compliant filesystem.
538
539 Because dpkg, the Debian package manager, does not support non-POSIX
540 filesystems, the Debian kernel package can not be installed.  More
541 information are available at http://bugs.debian.org/315493.
542
543 Press any key within 5 seconds to quit
544 __END__
545                 five_seconds_to_quit
546                 SINGLE_PART="false"
547         fi
548 }
549
550
551 #
552 # Stage all
553 #
554
555 action_all () {
556         cat <<__END__
557 I: Running all stages with these settings:
558 I: hostname set to $HOSTNAME
559 I: microSD card device at $SD_DEVICE
560 I: $SD_PART1_FS as filesystem for the first microSD card partition
561 I: installing into '$INST_DIR'
562 I: using Debian mirror at $INST_MIRROR
563 I: APT recommends set to $APT_RECOMMENDS
564 I: dash as default /bin/sh set to $DASH_BINSH
565
566 __END__
567
568         for f in testing time partition format mount debian apt fso configuration tasks kernel cleanup unmount
569         do
570                 case "$f" in
571                         "partition")
572                                 if [ -n "$intern_no_partitioning" ]; then
573                                         echo " * Skipping partitioning since --no-partitioning flag set"
574                                 else
575                                         action_partition
576                                 fi
577                                 ;;
578                         "tasks")
579                                 if [ -z "$TASKS" ]; then
580                                         echo " * Skipping over installing tasks since none were specified"
581                                 else
582                                         action_tasks
583                                 fi
584                                 ;;
585                         *)
586                                 if [ -n "$VERBOSE" ]; then echo "calling '$f'"; fi
587                                 action_$f
588                                 ;;
589                 esac
590         done
591         cat <<__END__
592
593 I: All done!
594
595 __END__
596
597         if [ "$SD_PART1_FS" = "vfat" ]; then
598                 echo "Now reboot, switch on the Openmoko FreeRunner with both AUX+PWR,"
599                 echo "buttons, choose \"Boot from microSD (FAT+ext2)\" and wait!"
600         else
601                 echo "Now reboot, and hope for the best!"
602         fi
603         echo
604         exit 0
605 }
606
607
608 #
609 # Stage test
610 #
611
612 action_testing () {
613
614         echo "I: Testing system setup"
615
616         intern_packages_required="wget ping fdisk mount umount mkfs.$SD_PART1_FS mkswap gunzip dd sed rdate ar basename dirname"
617         if [ "mkfs.$SD_PART2_FS" != "mkfs.$SD_PART1_FS" ]; then
618                 intern_packages_required="$intern_packages_required mkfs.$SD_PART2_FS"
619         fi
620         if [ "deboostrap" = "$BOOTSTRAPPER" ]; then
621                 intern_packages_required="$intern_packages_required perl"
622         fi
623
624         intern_packages_missing=""
625         intern_extra_information=""
626         for intern_binary in $intern_packages_required; do
627                 if ! which $intern_binary > /dev/null; then
628                         if [ -n "$VERBOSE" ]; then
629                                 echo " * Missing binary '$intern_binary'"
630                         fi
631                         intern_packages_missing="$intern_packages_missing $intern_binary"
632                         if [ "ar" = "$intern_binary" ]; then
633                                 intern_extra_information="${intern_extra_information}\n  the 'ar' binary is expected to ship with a package named 'binutils'"   
634                         fi
635                 fi
636                 if [ -n "$VERBOSE" ]; then
637                         echo " * $intern_binary installed"
638                 fi
639         done
640
641         if [ -n "$intern_packages_missing" ]; then
642                 echo -e "E: Could not find the following binaries: $intern_packages_missing.$intern_extra_information"
643                 exit 1
644         fi
645
646         if ! test -e $SD_DEVICE; then
647                 echo "E: Could not find microSD card device file $SD_DEVICE"
648                 exit 1
649         fi
650         echo " * microSD card device: $SD_DEVICE present"
651
652         if $TAR_APPLICATION --help 2>&1 | grep "Usage: $TAR_APPLICATION \-\[cxtvO\]" >/dev/null; then
653                 echo "W: $TAR_APPLICATION does not support gzip archives"
654                 echo "Downloading tar package"
655                 wget $TAR_PACKAGE -O /tmp/tar.deb
656                 rm -f /tmp/data.tar.gz # FIXME: ar should just overwrite it
657                 ( cd /tmp && ar -x tar.deb data.tar.gz )
658                 mkdir -p /usr/local
659                 gunzip -c /tmp/data.tar.gz | $TAR_APPLICATION -x -C /usr/local/
660                 TAR_APPLICATION=/usr/local/bin/tar
661                 rm -f /tmp/data.tar.gz
662                 rm -f /tmp/tar.deb
663         fi
664
665         if [ "$SD_PART1_FS" = "vfat" ]; then
666                 if ! which mkfs.vfat >/dev/null; then
667                         echo "W: Could not find mkfs.vfat binary"
668                         # it should try first to install dosfstools through opkg
669                         # and then try to install the Debian dosfstools
670                         echo "Downloading dosfstools package"
671                         wget $DOSFSTOOLS_PACKAGE -O /tmp/dosfstools.deb
672                         rm -f /tmp/data.tar.gz # FIXME: ar should just overwrite it
673                         ( cd /tmp && ar -x dosfstools.deb data.tar.gz )
674                         $TAR_APPLICATION -xz -C / -f /tmp/data.tar.gz
675                         rm -f /tmp/data.tar.gz
676                         rm -f /tmp/dosfstools.deb
677                 fi
678
679                 # let's test again for safety reasons
680                 if ! which mkfs.vfat >/dev/null; then
681                         echo "E: Could not find mkfs.vfat binary"
682                         exit 1
683                 else
684                         echo " * mkfs.vfat installed"
685                 fi
686         fi
687
688         if [ $(uname -m) != armv4tl ]; then
689                 echo "E: System is not claiming to be an armv4tl."
690                 echo "   Are you sure that this is your Openmoko FreeRunner (GTA02)?"
691                 exit 1
692         fi
693
694         if ! ping -c 2 $TESTHOST > /dev/null; then
695                 echo "E: Could not ping $TESTHOST, is the network running?"
696                 exit 1
697         fi
698
699         echo " * network running"
700
701         if ps -C qpe >/dev/null; then
702                 echo "E: Qtopia wants to continually access /media/card.  You need to"
703                 echo -n "   stop it with "
704                 if [ -x "/etc/init.d/qpe" ]; then
705                         echo -n "\`/etc/init.d/qpe stop\` or"
706                 elif [ -x "/etc/init.d/qpe.sh" ]; then
707                         echo -n "\`/etc/init.d/qpe.sh stop\` or"
708                 fi
709                 echo " \`killall qpe\`"
710                 echo "   and then restart the installation!"
711                 exit 1
712         fi
713
714         echo "I: Testing system setup - done - looks good"
715 }
716
717 #
718 # Stage time
719 #
720
721 action_time () {
722         echo "Fetching time from $RDATEHOST with rdate"
723         rdate -s "$RDATEHOST" >/dev/null 2>&1
724         echo "I: Time is now `date`"
725 }
726
727 #
728 # Stage partitioning
729 #
730
731 action_partition () {
732
733         echo "I: Partitioning SD"
734
735         card_data_warning partition
736         umount_all
737
738         # put the partition table in a "known state"
739         echo " * Clearing MBR of SD."
740         if ! dd if=/dev/zero of=$SD_DEVICE bs=512 count=1 >/dev/null 2>&1; then
741                 echo "Clearing MBR of SD with dd failed ($?)."
742                 exit 1
743         fi
744
745         # run partitioner
746         bootfstypeno=83
747
748         if [ -z "$SD_SWAP_SIZE" ]; then SD_SWAP_SIZE=0; fi
749
750         mb=$(( $(cat /sys/block/mmcblk0/size) * 8 / 15625 ))
751         # If one were willing to assume that cylinders are always 64 * 512, then we could calculate the
752         # next line thus:   $(( $(cat /sys/block/mmcblk0/size) / 64 ))
753         cylinders=$(echo -e "p\nq" | fdisk ${SD_DEVICE} | grep cylinders|grep heads|cut -f 5 -d\ )
754         lastcylinder=$(($cylinders-($cylinders*$SD_SWAP_SIZE/$mb)))
755
756 ##### TODO: Evaluate the suggestion by x
757 #+#     mb=$(echo pq | fdisk /dev/mmcblk0 | grep Disk | cut -f 3 -d \ )
758 #+      mb=$(sfdisk -s ${SD_DEVICE} 2>/dev/null)
759 #+#     cylinders=$(echo -e "p\nq" | fdisk /dev/mmcblk0 | grep cylinders | grep heads | cut -f 5 -d\ )
760 #+      cylinders=$(sfdisk -G ${SD_DEVICE} 2>/dev/null | grep "^${SD_DEVICE}" | awk "{ print \$2 }")
761 #+      lastcylinder=$(($cylinders-($cylinders*$SD_SWAP_SIZE/($mb/1024))))
762 #
763
764
765         if [ "vfat" = "$SD_PART1_FS" ]; then
766                 bootfstypeno=4
767         fi
768
769         partitionno=0
770         if [ "$SINGLE_PART" = "true" ]; then
771                 echo "Preparing SD as a single Linux device."
772                 partitionno=$(($partitionno+1))
773                 cat <<EOSINGLECONF > /tmp/argsToFdisk$$
774 n
775 p
776 $partitionno
777
778 $lastcylinder
779 EOSINGLECONF
780
781         else
782                 partitionno=$(($partitionno+1))
783                 cat <<EOBOOTCONF > /tmp/argsToFdisk$$
784 n
785 p
786 $partitionno
787
788 +${SD_PART1_SIZE}M
789 t
790 $bootfstypeno
791 EOBOOTCONF
792
793                 partitionno=$(($partitionno+1))
794                 cat <<EOROOTCONF >> /tmp/argsToFdisk$$
795 n
796 p
797 $partitionno
798
799 $lastcylinder
800 t
801 $partitionno
802 83
803 EOROOTCONF
804         fi
805
806         if [ 0 -lt "$SD_SWAP_SIZE" ]; then
807                 partitionno=$(($partitionno+1))
808                 cat <<EOSWAPCONF >> /tmp/argsToFdisk$$
809 n
810 p
811 $partitionno
812
813
814 t
815 $partitionno
816 82
817 p
818 EOSWAPCONF
819         fi
820         echo "w" >> /tmp/argsToFdisk$$
821
822         if fdisk $SD_DEVICE < /tmp/argsToFdisk$$; then
823                 echo "Partitioning was successful."
824                 if [ -n "$VERBOSE" ]; then
825                         echo "The following partitions have been created:"
826                         echo -e "p\nq" | fdisk $SD_DEVICE
827                 fi
828                 #rm /tmp/argsToFdisk$$
829         else
830                 echo "Partitioning failed, could not execute with fdisk:"
831                 cat /tmp/argsToFdisk$$
832                 exit 1
833         fi
834
835
836         echo " * Waiting for partitions to appear"
837         intern_sleep_seconds=3
838         if [ "$SINGLE_PART" = "true" ]; then
839                 intern_devtest="test -e ${SD_DEVICE}p1"
840         else
841                 intern_devtest="test -e ${SD_DEVICE}p1 -a -e ${SD_DEVICE}p2"
842         fi
843
844         while ! $intern_devtest; do
845                 if [ "$intern_sleep_seconds" = 15 ]; then
846                         echo "E: Partitioning has failed, partitions have not been created"
847                         exit 1
848                 fi
849                 sleep 3
850                 intern_sleep_seconds=$(( $intern_sleep_seconds + 3 ))
851         done
852
853         echo "I: Partitioning SD - done - Partition table created"
854 }
855
856 #
857 # Stage format
858 #
859
860 action_format () {
861         echo "I: Formatting SD"
862         card_data_warning format
863         umount_all
864
865         if [ -z "$SD_SWAP_SIZE" ]; then SD_SWAP_SIZE=0; fi
866
867         echo " * Formatting ${SD_PART1_FS} on ${SD_DEVICE}p1"
868         mkfs.$SD_PART1_FS ${SD_DEVICE}p1
869         nextpart=2
870         if [ "$SINGLE_PART" != "true" ]; then
871                 echo " * Formatting ${SD_PART2_FS} on ${SD_DEVICE}p2"
872                 mkfs.$SD_PART2_FS ${SD_DEVICE}p2
873                 nextpart=3
874         fi
875         if [ 0 -lt "$SD_SWAP_SIZE" ]; then
876                 echo " * Formatting swap on ${SD_DEVICE}p${nextpart}"
877                 mkswap ${SD_DEVICE}p${nextpart}
878         fi
879
880         echo "I: Formatting SD - done - microSD card ready"
881 }
882
883 #
884 # Stage mount
885 #
886
887 action_mount () {
888
889         echo "I: Mounting the newly created system"
890
891         if [ ! -d "$INST_DIR" ]; then
892                 mkdir -p "$INST_DIR"; 
893                 intern_instdir_already_present="" 
894         elif [ -n "$VERBOSE" ]; then
895                 echo "Not creating directory '$INST_DIR', it is already existing."
896                 intern_instdir_already_present="true" 
897         fi
898         if [ "$SINGLE_PART" = "true" ]; then
899                 if [ -n "$VERBOSE" ]; then echo "Preparing single ext2 device."; fi
900                 mount -t auto ${SD_DEVICE}p1 "$INST_DIR"
901                 mkdir -p "$INST_DIR/boot"
902         else
903                 if [ -n "$VERBOSE" ]; then echo "Mounting root partition to '$INST_DIR'."; fi
904                 mount -t auto ${SD_DEVICE}p2 "$INST_DIR"
905                 if [ "vfat" = "$SD_PART1_FS" ]; then
906                         echo "You have a vfat boot filesystem. This is not mounted to /boot."
907                 else
908                         if [ ! -d "$INST_DIR/boot" ]; then mkdir "$INST_DIR/boot"; fi
909                         mount -t auto ${SD_DEVICE}p1 "$INST_DIR/boot"
910                 fi
911         fi
912
913         echo "I: Mounting the newly created system - done - microSD card partitions mounted"
914 }
915
916
917 #
918 # Stage debian
919 #
920
921 action_debian () {
922         echo "I: Install a basic Debian system"
923         
924         #For users of local apt proxy
925         mkdir -p $INST_DIR/etc
926         if [ -f /etc/hosts ]; then
927                 cp /etc/hosts $INST_DIR/etc
928                 echo "I: /etc/hosts copied from this machine"
929         fi                 
930
931         intern_bootstrapper_package=
932         if [ "cdebootstrap" = "$BOOTSTRAPPER" ]; then
933                 intern_bootstrapper_package=$CDEBOOTSTRAP_PACKAGE
934         elif [ "debootstrap" = "$BOOTSTRAPPER" ]; then
935                 intern_bootstrapper_package=$DEBOOTSTRAP_PACKAGE
936         else
937                 echo "Unknown bootstrapper: '$BOOTSTRAPPER'"
938                 exit 1
939         fi
940
941         if [ -x /usr/bin/"$BOOTSTRAPPER" ]; then
942                 echo
943                 echo "I: Found $BOOTSTRAPPER installed,"
944                 echo "   will nonetheless download reference version."
945                 echo
946         fi
947         echo " * Downloading bootstrapper package '`basename $intern_bootstrapper_package`'"
948         if [ -n "$VERBOSE" ]; then echo "   from $intern_bootstrapper_package"; fi
949         internal_localpackagename=/tmp/debootstrapOrCdebootstrap.deb
950         if ! wget $intern_bootstrapper_package -O "$internal_localpackagename"; then
951                 echo <<EOWGETFAILURE
952 E: Could not download debian bootstrap tool '$BOOTSTRAPPER'. Check
953    with page http://packages.debian.org/sid/$BOOTSTRAPPER if the
954    version shown is the same as in
955    $intern_bootstrapper_package .
956 EOWGETFAILURE
957                 exit -1
958         fi
959         rm -f /tmp/data.tar.gz # FIXME: ar should just overwrite it
960         (
961                 cd /tmp
962                 if ! ar -x "$internal_localpackagename" data.tar.gz ; then
963                         echo "E: Could not extract files from .deb for bootstrapper '$BOOTSTRAPER'."
964                         exit 1
965                 else
966                         echo "I: Successfully extracted files from bootstrapper's .deb."
967                 fi
968         )
969         rm -f "$internal_localpackagename"
970         intern_taroptions=
971         if [ -n "$VERBOSE" ]; then intern_taroptions="${intern_taroptions}v"; fi
972         if ! $TAR_APPLICATION -x${intern_taroptions}z -C / -f /tmp/data.tar.gz; then
973                 echo "E: Could not untar binaries for bootstrapper from /tmp/data.tar.gz"
974                 exit 1
975         else
976                 echo "I: Successfully extracted binaries for bootstapper."
977         fi
978         rm -f /tmp/data.tar.gz
979
980         echo " * Running '$BOOTSTRAPPER' to install the system (might take a while)"
981         if [ -n "$VERBOSE" ]; then
982                 if [ "cdebootstrap" = "$BOOTSTRAPPER" ]; then
983                         CDEBOOTSTRAP_EXTRA_FLAGS="$CDEBOOTSTRAP_EXTRA_FLAGS -v"
984                         echo "   CDEBOOTSTRAP_VARIANT='$CDEBOOTSTRAP_VARIANT'"
985                         echo "   CDEBOOTSTRAP_EXTRA_FLAGS='$CDEBOOTSTRAP_EXTRA_FLAGS'"
986                         echo "   CDEBOOTSTRAP_EXTRA_PACKAGES='$CDEBOOTSTRAP_EXTRA_PACKAGES'"
987                 elif [ "debootstrap" = "$BOOTSTRAPPER" ]; then
988                         DEBOOTSTRAP_EXTRA_FLAGS="$DEBOOTSTRAP_EXTRA_FLAGS --verbose"
989                         echo "   DEBOOTSTRAP_VARIANT='$DEBOOTSTRAP_VARIANT'"
990                         echo "   DEBOOTSTRAP_EXTRA_FLAGS='$DEBOOTSTRAP_EXTRA_FLAGS'"
991                         echo "   DEBOOTSTRAP_EXTRA_PACKAGES='$DEBOOTSTRAP_EXTRA_PACKAGES'"
992                 fi
993         fi
994
995         if [ "cdebootstrap" = "$BOOTSTRAPPER" ]; then
996                 cdebootstrap-static $CDEBOOTSTRAP_EXTRA_FLAGS --allow-unauthenticated --flavour $CDEBOOTSTRAP_FLAVOUR \
997                         --exclude=$BOOTSTRAP_EXCLUDE_PACKAGES \
998                         --include=$CDEBOOTSTRAP_EXTRA_PACKAGES \
999                         sid "$INST_DIR" $INST_MIRROR
1000         elif [ "debootstrap" = "$BOOTSTRAPPER" ]; then
1001                 debootstrap --arch=armel $DEBOOTSTRAP_EXTRA_FLAGS --variant $DEBOOTSTRAP_VARIANT \
1002                         --exclude=$BOOTSTRAP_EXCLUDE_PACKAGES \
1003                         --include=$DEBOOTSTRAP_EXTRA_PACKAGES \
1004                          sid "$INST_DIR" $INST_MIRROR
1005         else    # redundant, but when things go wrong, then things go wrong, left as precaution
1006                 echo "Unknown bootstrapper: '$BOOTSTRAPPER'"
1007                 exit 1
1008         fi
1009         
1010         # Bug #542955 (won't fix) for cdebootstrap directs us to use apt-get for versioned
1011         # (direct or indirect) dependencies. The DEBIAN_EXTRA_PACKAGES are consequently
1012         # installed in the action_apt
1013
1014         echo "I: Install a basic Debian system - done - Basic Debian system installed - please also install apt"
1015 }
1016
1017
1018 #
1019 # Stage apt
1020 #
1021
1022 action_apt () {
1023         echo "I: Configuring APT"
1024
1025         if ! ensure_proc_is_mounted; then
1026                 echo "  /proc is needed for APT configuration - please investigate."
1027                 exit 1
1028         fi
1029
1030         echo " * Populating /etc/apt/sources.list"
1031         cat >> "$INST_DIR/etc/apt/sources.list" <<__END__
1032 deb $INST_MIRROR unstable main
1033 deb $FSO_MIRROR unstable main
1034 __END__
1035
1036         echo " * Install APT recommends by default: $APT_RECOMMENDS"
1037         echo "APT::Install-Recommends \"$APT_RECOMMENDS\";" > "$INST_DIR/etc/apt/apt.conf.d/99recommends"
1038
1039         echo " * Learning public key for pkg-fso-keyring"
1040         chroot "$INST_DIR" apt-key add - <<__END__
1041 -----BEGIN PGP PUBLIC KEY BLOCK-----
1042 Version: GnuPG v1.4.9 (GNU/Linux)
1043
1044 mQGiBEicYhYRBACvesYUC+rkRpp0j/PTEimmuPkZqjLuRnpXqlCXV9Y1TZxvPmFl
1045 TwoeBHYoh3Uipbj0WypAJ3N989OQPiPo+lyrPNscRYc9+H/k7Q47Oc0ms23EQSWX
1046 AdnuhRezNF98cW/bmykVO9uZo+OjA501R9SCrMkhkbuifB57OMdXA0o67wCg0NWM
1047 EOAqQ/DCoo9Nqnb5sWotwisD/15dgunBY1BCyz8o7Ky1gn+Cel3LfNaqCIHwgCNf
1048 oxdAzHViNHMJa13Hab/nBY1GPpfJ3VIvLNs309JTDoKnYFvUkinvt+OJcY4z/3XN
1049 yloHhUoFBOSuZUy/xqB5PVrlQYJ07WKQHOkjvcp5LCH7pTLjCu8ih+YIIr4uipNl
1050 ZjBwA/9bCGQysO4xaHCR+qEZcQ1J4tW2A1g6Kr8p2CHxvGVCCQW7hhIRVIVGXF83
1051 EViOVobmG8RHZFpAJzdsrJ/jkYBZp62wCs1DTljxLdMfY9vlKzpui3RSPv10fdPX
1052 t2iPG4Qc9Qm2H6LkWDfSJx6FLzDKrcj8tmbLTpr+fdJZtehudLRNcGtnLWZzbyBB
1053 cmNoaXZlIEF1dG9tYXRpYyBTaWduaW5nIEtleSA8cGtnLWZzby1tYWludEBsaXN0
1054 cy5hbGlvdGguZGViaWFuLm9yZz6IYAQTEQIAIAIbAwYLCQgHAwIEFQIIAwQWAgMB
1055 Ah4BAheABQJKfuQXAAoJEOK53mqOezI71/IAnR6ZRbCHzuDRZecGvOtd8qNtcW+Q
1056 AJ9Cbbi6oyybgvrJ2KGjWXkoYTJnfw==
1057 =IG3W
1058 -----END PGP PUBLIC KEY BLOCK-----
1059 __END__
1060
1061         echo " * Updating APT database"
1062         chroot "$INST_DIR" apt-get update
1063
1064         echo " * Installing pkg-fso keyring"
1065         diverting_invoke_rc_d   # to ensure to avoid problem from previously failed installs
1066                                 # to spot this separate apt-get invocation cost me about 2h
1067         chroot "$INST_DIR" apt-get $APT_OPTIONS install pkg-fso-keyring
1068         restoring_invoke_rc_d
1069
1070         echo " * Completing install of Debian basic packages to circumvent bug #542955"
1071
1072
1073         # dropbear at least cannot be installed without preventing the restart to happen
1074         diverting_invoke_rc_d
1075         chroot "$INST_DIR" apt-get $APT_OPTIONS install $(echo $DEBIAN_EXTRA_PACKAGES | tr "," " ")
1076         restoring_invoke_rc_d
1077
1078         echo " * Cleaning APT cache"
1079         chroot "$INST_DIR" apt-get clean
1080
1081         echo "I: Configuring APT - done - APT configured"
1082 }
1083
1084
1085 #
1086 # Stage fso
1087 #
1088
1089 action_fso () {
1090         echo "I: Installing freesmartphone.org software"
1091
1092         ensure_proc_is_mounted
1093
1094         echo "* core packages"
1095         diverting_invoke_rc_d
1096         chroot "$INST_DIR" /bin/sh -e <<__END_CHROOT__
1097 export DEBIAN_FRONTEND=noninteractive
1098 apt-get $APT_OPTIONS install $FSO_PACKAGES
1099 apt-get clean
1100 __END_CHROOT__
1101         restoring_invoke_rc_d
1102
1103         echo "skipped (\$ZHONE=$ZHONE)"
1104
1105         echo "I: Installing freesmartphone.org software - done"
1106 }
1107
1108
1109 #
1110 # Stage tasks
1111 #
1112 #       These steps shall be executed only after all basic packages
1113 #       have been installed.
1114 #
1115
1116 action_tasks () {
1117
1118         if  [ -z "$TASKS" ]; then
1119                 if [ -n "$VERBOSE" ]; then
1120                         echo
1121                         echo "No task selected."
1122                         echo
1123                 fi
1124                 return 0;
1125         fi
1126
1127         # tasks should be defined case-invariantly
1128         TASKS=$(echo "$TASKS"|tr "a-z" "A-Z")
1129
1130         if [ "ALL" = "$TASKS" ]; then
1131                 TASKS="$intern_tasks_defined"
1132         fi
1133
1134         echo "I: Performing selected tasks: $TASKS"
1135
1136         ensure_proc_is_mounted
1137
1138         for intern_task in $( echo $TASKS|tr "," " ")
1139         do
1140                 echo " * task: $intern_task"
1141                 eval chroot "$INST_DIR" apt-get install $APT_OPTIONS \$PACKAGES_TASK_$intern_task
1142                 chroot "$INST_DIR" apt-get clean
1143                 echo "I: Selection of packages for task '$intern_task' successfully installed."
1144
1145                 case "$intern_task" in
1146                   "WIFI")
1147
1148                         echo " * modifying .xsession to autostart wicd-client"
1149                         sed -i '/^exec/i#wicd-client --no-animate &' "$INST_DIR/root/.xsession"
1150                         echo " * Installing wicd for wifi setup"
1151                         if [ -r "$INST_DIR/etc/wicd/manager-settings.conf" ]; then
1152                                 grep -v ^wired_interface "$INST_DIR/etc/wicd/manager-settings.conf" > \
1153                                         "$INST_DIR/etc/wicd/manager-settings.conf"
1154                         else
1155                                 mkdir "$INST_DIR/etc/wicd"
1156                                 echo "[Settings]" >> "$INST_DIR/etc/wicd/manager-settings.conf"
1157                         fi
1158                         echo "wired_interface =" >> "$INST_DIR/etc/wicd/manager-settings.conf"
1159                         chroot "$INST_DIR" apt-get install $APT_OPTIONS wicd
1160                         echo " * All extra activity for task '$intern_task' completed."
1161                         ;;
1162
1163                   *)
1164                         echo " * No extra activity for task '$intern_task'."
1165                         ;;
1166                 esac
1167         done
1168
1169         echo "I: Performing selected tasks - done"
1170 }
1171
1172
1173 #
1174 # Stage configuration
1175 #
1176
1177 action_configuration () {
1178         echo "I: Configuring system files for ${FSO_DEVICE}"
1179
1180         root_password_warning
1181
1182         ensure_proc_is_mounted
1183
1184         if [ "true" = "$LOCALEPURGE" ]; then
1185                 echo " * installing localepurge"
1186                 echo "     ... skipped since Erich reported it to break unsupervised install"
1187                 #chroot "$INST_DIR" apt-get $APT_OPTIONS install localepurge
1188         fi
1189
1190         if [ -z "$SD_SWAP_SIZE" ]; then SD_SWAP_SIZE=0; fi
1191
1192         echo " * Installing the fso-${FSO_DEVICE} package"
1193         chroot "$INST_DIR" apt-get $APT_OPTIONS install fso-${FSO_DEVICE}
1194
1195         echo " * Creating /etc/X11/xorg.conf"
1196         cat > "$INST_DIR/etc/X11/xorg.conf" <<__END__
1197 # Xorg configuration for an Openmoko FreeRunner
1198
1199 Section "Module"
1200        Load    "glx"
1201        Load    "dri2"
1202 EndSection
1203
1204 Section "Monitor"
1205        Identifier      "LCD Panel"
1206 EndSection
1207
1208 Section "Device"
1209        Identifier      "Glamo Graphics Chip"
1210        Driver          "glamo"
1211 EndSection
1212
1213 Section "Screen"
1214        Identifier      "Default Screen"
1215        Device          "Glamo Graphics Chip"
1216        Monitor         "LCD Panel"
1217 EndSection
1218
1219 Section "InputDevice"
1220        Identifier      "Power Button"
1221        Driver          "evdev"
1222        Option          "Device"        "/dev/input/event0"
1223 EndSection
1224
1225
1226 Section "InputDevice"
1227        Identifier      "AUX Button"
1228        Driver          "evdev"
1229        Option          "Device"        "/dev/input/event2"
1230 EndSection
1231
1232
1233 Section "InputDevice"
1234        Identifier      "Touchscreen"
1235        Driver          "evdev"
1236        Option          "Device"        "/dev/input/event1"
1237        Option          "Calibration"  "107 918 911 98"
1238        Option          "SwapAxes"     "On"
1239 EndSection
1240
1241 Section "ServerFlags"
1242        Option "AutoAddDevices" "False"
1243 EndSection
1244
1245 Section "ServerLayout"
1246        Identifier      "Default Layout"
1247        Screen          "Default Screen"
1248        InputDevice     "Power Button"
1249        InputDevice     "AUX Button"
1250        InputDevice     "Touchscreen"
1251 EndSection
1252
1253 __END__
1254
1255         echo " * Creating /etc/inittab (all but tty1 disabled to save memory)"
1256         cat > "$INST_DIR/etc/inittab" <<__END__
1257 id:2:initdefault:
1258 si::sysinit:/etc/init.d/rcS
1259 ~~:S:wait:/sbin/sulogin
1260 l0:0:wait:/etc/init.d/rc 0
1261 l1:1:wait:/etc/init.d/rc 1
1262 l2:2:wait:/etc/init.d/rc 2
1263 l3:3:wait:/etc/init.d/rc 3
1264 l4:4:wait:/etc/init.d/rc 4
1265 l5:5:wait:/etc/init.d/rc 5
1266 l6:6:wait:/etc/init.d/rc 6
1267 z6:6:respawn:/sbin/sulogin
1268 ca:12345:ctrlaltdel:/sbin/shutdown -t1 -a -r now
1269 pf::powerwait:/etc/init.d/powerfail start
1270 pn::powerfailnow:/etc/init.d/powerfail now
1271 po::powerokwait:/etc/init.d/powerfail stop
1272 1:2345:respawn:/sbin/getty 38400 tty1
1273 #2:23:respawn:/sbin/getty 38400 tty2
1274 #3:23:respawn:/sbin/getty 38400 tty3
1275 #4:23:respawn:/sbin/getty 38400 tty4
1276 #5:23:respawn:/sbin/getty 38400 tty5
1277 #6:23:respawn:/sbin/getty 38400 tty6
1278 __END__
1279
1280
1281         if [ -r "$INST_DIR"/etc/frameworkd.conf ]; then
1282                 echo " * Disabling automated screen lock " # FIXME: should become optional
1283                 if [ ! -r "$INST_DIR"/etc/frameworkd.conf.orig ]; then
1284                         chroot "$INST_DIR" cp /etc/frameworkd.conf /etc/frameworkd.conf.orig
1285                 fi
1286                 cat "$INST_DIR"/etc/frameworkd.conf.orig | sed -e 's/^idle_prelock.*/idle_prelock = 0/' -e 's/^lock.*/lock = 0/' > "$INST_DIR"/etc/frameworkd.conf
1287         else
1288                 echo "W: Missing file /etc/frameworkd.conf to disable automated screen lock"
1289         fi
1290
1291         if [ -r "$INST_DIR"/etc/freesmartphone/oevents/rules.yaml ]; then
1292                 echo " * Allow screen unlock to work without shutting off (AUX pressed, followed by Power shortly pressed)"
1293                 if [ ! -r "$INST_DIR"/etc/freesmartphone/oevents/rules.yaml.orig ]; then
1294                         chroot "$INST_DIR" cp /etc/freesmartphone/oevents/rules.yaml /etc/freesmartphone/oevents/rules.yaml.orig
1295                 fi
1296                 cat "$INST_DIR"/etc/freesmartphone/oevents/rules.yaml.orig | sed -e '/Suspend Handling/,/Suspend\(\)/ d' > "$INST_DIR"/etc/freesmartphone/oevents/rules.yaml
1297         else
1298                 echo "W: Missing file /etc/freesmartphone/oevents/rules.yaml to allow screen unlock to work without shutting off"
1299         fi
1300
1301         echo " * Creating /etc/modules"
1302         cat > "$INST_DIR/etc/modules" <<__END__
1303 # USB RNDIS/Ethernet Gadget
1304 g_ether
1305
1306 # bluetooth
1307 ohci-hcd
1308 hci_usb
1309
1310 # buttons
1311 neo1973kbd
1312
1313 __END__
1314         if [ "$FSO_DEVICE" = gta01 ]; then
1315                 cat >> "$INST_DIR/etc/modules" <<__END__
1316 # audio
1317 snd-soc-neo1973-wm8753
1318 __END__
1319         else
1320                 cat >> "$INST_DIR/etc/modules" <<__END__
1321 # audio
1322 snd-soc-neo1973-gta02-wm8753
1323
1324 # leds
1325 leds-neo1973-gta02
1326 __END__
1327         fi
1328
1329         echo " * Setting FSCKFIX=yes in /etc/default/rcS"
1330         sed -i 's/FSCKFIX=.*/FSCKFIX=yes'/ "$INST_DIR/etc/default/rcS"
1331
1332         echo " * Creating /etc/fstab"
1333         mkdir -p "$INST_DIR/mnt/flash"
1334         cat > "$INST_DIR/etc/fstab" <<__END__
1335 rootfs  /               auto    defaults,errors=remount-ro,noatime      0 1
1336 __END__
1337
1338         if [ "$SINGLE_PART" != "true" ]; then
1339                 if [ "ext2" = "$SD_PART1_FS" -o "ext3" = "$SD_PART1_FS" ]; then
1340                         cat >> "$INST_DIR/etc/fstab" <<__END__
1341 /dev/mmcblk0p1  /boot   auto    defaults,noatime                        0 2
1342 __END__
1343
1344                 else
1345                         cat >> "$INST_DIR/etc/fstab" <<__END__
1346 /dev/mmcblk0p1  /boot   auto    defaults,noatime,noauto                 0 2
1347 __END__
1348
1349                 fi
1350
1351                 if [ 0 -lt "$SD_SWAP_SIZE" ]; then
1352                         cat >> "$INST_DIR/etc/fstab" <<__END__
1353 /dev/mmcblk0p3  none    swap     sw                                     0 0
1354 __END__
1355
1356                 fi
1357         else
1358                 # $SINGLE_PART = "true"
1359                 if [ 0 -lt "$SD_SWAP_SIZE" ]; then
1360                         cat >> "$INST_DIR/etc/fstab" <<__END__
1361 /dev/mmcblk0p2  none   swap     sw                                     0 0
1362 __END__
1363                 fi
1364         fi
1365
1366         if [ "$FSO_DEVICE" = gta01 ]; then
1367             cat >> "$INST_DIR/etc/fstab" <<__END__
1368 /dev/mtdblock4  /mnt/flash      jffs2   defaults,noatime,noauto         0 2
1369 __END__
1370         else
1371             cat >> "$INST_DIR/etc/fstab" <<__END__
1372 /dev/mtdblock6  /mnt/flash      jffs2   defaults,noatime,noauto         0 2
1373 __END__
1374         fi
1375
1376         cat >> "$INST_DIR/etc/fstab" <<__END__
1377 proc    /proc           proc    defaults                                0 0
1378 tmpfs   /tmp            tmpfs   defaults,noatime                        0 0
1379 __END__
1380
1381         echo " * Setting /etc/hostname to $HOSTNAME"
1382         echo "$HOSTNAME" > "$INST_DIR/etc/hostname"
1383
1384         for FILE in /etc/network/interfaces /etc/resolv.conf; do
1385                 echo " * $FILE copied from this machine"
1386                 cat > "$INST_DIR$FILE" <$FILE
1387         done
1388
1389         # empty passwords now require modification to PAM
1390         # http://lists.alioth.debian.org/pipermail/pkg-fso-maint/2009-May/001225.html
1391         # http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=525153
1392         echo " * Setting root password to '$ROOT_PASSWORD'"
1393         echo "root:$ROOT_PASSWORD" | chroot "$INST_DIR" chpasswd
1394
1395         for HOST_KEY in $DROPBEAR_KEYS; do
1396                 if [ -f "$HOST_KEY" ]; then
1397                         echo " * copying ssh host key $HOST_KEY"
1398                         intern_d=`dirname $INST_DIR$HOST_KEY`
1399                         if [ ! -d "$intern_d" ]; then
1400                                 echo "   Creating directory '$intern_d'"
1401                                 mkdir -p "$intern_d"
1402                         fi
1403                         cat >"$INST_DIR$HOST_KEY" <$HOST_KEY
1404                 fi
1405         done
1406
1407
1408         echo " * Installing dash as /bin/sh: $DASH_BINSH"
1409         echo 'dash dash/sh string $DASH_BINSH' | chroot "$INST_DIR" debconf-set-selections
1410         chroot "$INST_DIR" apt-get $APT_OPTIONS install dash
1411
1412         echo " * Installing /root/.xsession"
1413         cat > "$INST_DIR"/root/.xsession <<EOXSESSION
1414 #!/bin/sh
1415 $TRAYER &
1416 openmoko-panel-plugin &
1417 EOXSESSION
1418
1419         if [ -x "$INST_DIR"/usr/bin/zhone ]; then
1420                 cat >> "$INST_DIR"/root/.xsession <<EOXSESSION
1421 zhone &
1422 EOXSESSION
1423         else
1424                 echo "   Not finding zhone application to autostart - omitted"
1425         fi
1426
1427         cat >> "$INST_DIR"/root/.xsession <<EOXSESSION
1428 exec matchbox-window-manager -use_titlebar no -use_cursor no
1429 EOXSESSION
1430
1431         if  [ "stalonetray" = "$TRAYER" ]; then
1432                 cat >> "$INST_DIR"/root/.stalonetrayrc <<EOTRAYRC
1433 # see /usr/share/doc/stalonetray/examples/stalonetrayrc.sample.gz
1434 # with ideas for the configuration.
1435 EOTRAYRC
1436         elif [ "trayer" = "$TRAYER" ]; then
1437                 # let the trayer appear at the top of the screen
1438                 sed -i "s/$TRAYER/$TRAYER --edge top/" "$INST_DIR"/root/.xsession
1439         fi
1440
1441         # FIXME: the battery is deconfigured and should be
1442         #        put back on when the problems on .xsession-errors
1443         #        have been resolved.
1444         # Update:  Erich seems to have no problems.  2009/09/20
1445         echo " * Installing /root/.panel-pluginrc"
1446         cat > "$INST_DIR"/root/.panel-pluginrc <<EOPANELPLUGIN
1447 [main]
1448 plugins = buttons, gsm, wifi, gps, keyboard, brightness, screenlock, bluetooth, usb, battery
1449 [buttons]
1450 handleauxshortexternalcommand = exec /usr/bin/matchbox-keyboard
1451 EOPANELPLUGIN
1452
1453         if [ "$DISPLAY_MANAGER" = none ]; then
1454                 echo " * Not installing the Display Manager"
1455         else
1456                 echo " * Installing Display Manager $DISPLAY_MANAGER"
1457                 if [ "$DISPLAY_MANAGER" = nodm ]; then
1458                         echo 'nodm nodm/x_options string -nolisten tcp' | chroot "$INST_DIR" debconf-set-selections
1459                         echo 'nodm nodm/first_vt string 3' | chroot "$INST_DIR" debconf-set-selections
1460                         echo 'nodm nodm/enabled string true' | chroot "$INST_DIR" debconf-set-selections
1461                 fi
1462                 chroot "$INST_DIR" apt-get $APT_OPTIONS install $DISPLAY_MANAGER
1463         fi
1464
1465         echo "I: Configuring system files for ${FSO_DEVICE} - done - System configured"
1466 }
1467
1468
1469 #
1470 # Stage kernel
1471 #
1472
1473 action_kernel () {
1474         echo "I: Installing kernel package"
1475
1476         if [ "$FSO_DEVICE" != gta02 ]; then
1477                 cat <<EOF
1478
1479 ***********
1480 * WARNING *
1481 ***********
1482
1483 Debian does not provide any kernel for the Openmoko Neo 1973.
1484 You should install an official Openmoko kernel from
1485
1486   http://wiki.openmoko.org/wiki/Kernel
1487
1488 EOF
1489         else
1490                 ensure_proc_is_mounted
1491
1492                 intern_cmd="apt-get $APT_OPTIONS install linux-image-2.6.34-openmoko-gta02"
1493
1494                 if [ "$SD_PART1_FS" = "vfat" ]; then
1495                         intern_bootmountpoint=$(mount | grep "^${SD_DEVICE}p1"| cut -f3 -d\ )
1496                         if [ -z "$intern_bootmountpoint" -o "boot" != "$(basename "$intern_bootmountpoint")" ]; then
1497                                 echo " * installing to /boot"
1498                                 chroot "$INST_DIR" $intern_cmd
1499                                 echo " * cleaning apt cache"
1500                                 chroot "$INST_DIR" apt-get clean
1501                                 echo " * copying /boot/uImage.bin to first partition"
1502                                 if [ -z "$intern_bootmountpoint" ]; then
1503                                         if [ -n "$VERBOSE" ]; then
1504                                                 echo "    Mounting first partition to /mnt/tmp_first_part_$$";
1505                                         fi
1506                                         mkdir /mnt/tmp_first_part_$$
1507                                         mount ${SD_DEVICE}p1 /mnt/tmp_first_part_$$
1508
1509                                         if [ -n "$VERBOSE" ]; then
1510                                                 echo "    Copying '$INST_DIR/boot/uImage.bin' to '/mnt/tmp_first_part_$$/'"
1511                                         fi
1512                                         cp "$INST_DIR/boot/uImage.bin" "/mnt/tmp_first_part_$$/"
1513
1514                                         if [ -n "$VERBOSE" ]; then
1515                                                 echo "    Unmounting first partition from /mnt/tmp_first_part_$$"
1516                                         fi
1517                                         umount /mnt/tmp_first_part_$$
1518                                         rmdir /mnt/tmp_first_part_$$
1519                                 else 
1520                                         if [ -n "$VERBOSE" ]; then
1521                                                 echo "    Found first partitional already mounted to '$intern_bootmountpoint'."
1522                                         fi
1523                                         if [ -n "$VERBOSE" ]; then
1524                                                 echo "    Copying '$INST_DIR/boot/uImage.bin' to '$intern_bootmountpoint/'."
1525                                         fi
1526                                         cp "$INST_DIR/boot/uImage.bin" "$intern_bootmountpoint/"
1527                                 fi
1528                         
1529                         else
1530                                 echo "W: when using $SD_PART1_FS as filesystem for the first microSD card"
1531                                 echo "   partition, you should manually install the kernel, since dpkg"
1532                                 echo "   does not support non-POSIX filesystems:"
1533                                 echo "     http://bugs.debian.org/bug=315493"
1534                                 echo
1535                                 echo "   The fix is to cp or mv the files manually. Just have everything"
1536                                 echo "   on regular extX partitions when running the installation, i.e."
1537                                 echo "   make sure, the first partition is not mounted to the /boot"
1538                                 echo "   directory when the installation is started. Then "
1539                                 echo "      '$intern_cmd'"
1540                                 echo "   and copy /boot/uImage.bin to your first partition mounted at some"
1541                                 echo "   other directory. Check the /etc/fstab afterwards. When no longer"
1542                                 echo "   mounted to /boot, you may also rerun "
1543                                 echo "     '$0 --part1-fs vfat kernel'."
1544                                 return 0
1545                         fi
1546                 else
1547                         chroot "$INST_DIR" /bin/sh -e <<__END_CHROOT__
1548 $intern_cmd
1549 apt-get clean
1550 __END_CHROOT__
1551                 fi
1552
1553                 echo "I: Kernel installed"
1554
1555                 if [ "$QI" = "true" ]; then
1556                         echo "Prepare system for booting with Qi"
1557                         BOOT_DIR="$INST_DIR/boot/boot"
1558                         if [ "$SINGLE_PART" = "true" ]; then
1559                                 BOOT_DIR="$INST_DIR/boot"
1560                                 ln -fs uImage.bin $BOOT_DIR/uImage-GTA02.bin
1561                         else
1562                                 mkdir $BOOT_DIR
1563                                 ln -fs ../uImage.bin $BOOT_DIR/uImage-GTA02.bin
1564                         fi
1565         
1566                         if [ "$SINGLE_PART" = "true" ]; then
1567                                 ROOT_PART=${SD_DEVICE}p1
1568                         else
1569                                 ROOT_PART=${SD_DEVICE}p2
1570                         fi
1571
1572                         if [ "$QI_VERBOSE_BOOT" = "true" ]; then
1573                                 echo "root=$ROOT_PART console=tty0 loglevel=8" >$BOOT_DIR/append-GTA02
1574                         else
1575                                 echo "root=$ROOT_PART rootdelay=1" >$BOOT_DIR/append-GTA02
1576                         fi      
1577                 fi
1578         fi
1579
1580         echo "I: Installing kernel package - done"
1581 }
1582
1583
1584 #
1585 # Stage uboot
1586 #
1587
1588 action_uboot () {
1589         echo "I: Modifying U-Boot bootloader menu"
1590
1591         if [ "$SD_PART1_FS" = "vfat" ]; then
1592                 echo "W: When using $SD_PART1_FS as filesystem for the first microSD card"
1593                 echo "   partition, you should not modify the U-Boot environment."
1594                 return 1
1595         fi
1596
1597         cat > "$INST_DIR/root/uboot_menu.in" <<'__END__'
1598 bootcmd=
1599   setenv bootargs
1600     ${bootargs_base} ${mtdparts}
1601     rootfstype=ext2 root=/dev/mmcblk0p2 rootdelay=5;
1602   mmcinit;
1603   ext2load mmc 1 0x32000000 ${sd_image_name};
1604   bootm 0x32000000
1605
1606 menu_1=
1607   Boot from Flash:
1608   setenv bootargs
1609     ${bootargs_base} ${mtdparts};
1610   nand read.e 0x32000000 kernel 0x200000;
1611   bootm 0x32000000
1612
1613 menu_2=
1614   Reboot:
1615   reset
1616
1617 menu_3=
1618   Power off:
1619   neo1973 power-off
1620
1621 menu_4=
1622   Set console to USB:
1623   setenv stdin usbtty;
1624   setenv stdout usbtty;
1625   setenv stderr usbtty
1626
1627 menu_5=
1628   Set console to serial:
1629   setenv stdin serial;
1630   setenv stdout serial;
1631   setenv stderr serial
1632
1633 menu_6=
1634
1635 menu_7=
1636
1637 menu_8=
1638 __END__
1639
1640         echo " * Checking for a sane MTD configuration"
1641         if ! fgrep mtd2 < /proc/mtd |fgrep -q "u-boot_env"; then
1642                 echo "E: U-Boot environment not found at expected location in mtd2"
1643                 exit 1
1644         fi
1645
1646         echo " * Mounting /dev inside the chroot at '$INST_DIR'"
1647         mount -o bind /dev "$INST_DIR/dev"
1648         chroot "$INST_DIR" /bin/sh -e <<__END_CHROOT__
1649 echo " * Dumping current U-Boot environment"
1650 #dd if=/dev/mtdblock2 of=\$INST_DIR/root/env.orig
1651 nanddump /dev/mtd2 -o -b -f /root/env.orig
1652 if [ ! \$(wc -c /root/env.orig | awk '{print \$1}') -eq $UBOOT_ENV_SIZE ]; then
1653         echo "Environment did not have the expected size of $UBOOT_ENV_SIZE"
1654         exit 1
1655 fi
1656 echo " * Merging Debian menu entries into U-Boot environment (can take up to 70s)"
1657 uboot-envedit -i /root/env.orig -f /root/uboot_menu.in -o /root/env.new
1658 if [ ! \$(wc -c /root/env.new | awk '{print \$1}') -eq $UBOOT_ENV_SIZE ]; then
1659         echo "Newly created environment did not have the expected size of $UBOOT_ENV_SIZE"
1660         exit 1
1661 fi
1662 echo " * Writing back U-Boot environment"
1663 flash_eraseall /dev/mtd2
1664 nandwrite /dev/mtd2 /root/env.new
1665 __END_CHROOT__
1666
1667         echo " * Unmounting /dev"
1668         umount "$INST_DIR/dev"
1669
1670         echo "I: Modifying U-Boot bootloader menu - done"
1671 }
1672
1673
1674 #
1675 # Stage cleaunp
1676 #
1677
1678 action_cleanup () {
1679
1680         echo "I: Cleaning the environment"
1681
1682         echo " * Remove additional packages"
1683         intern_delenda=''
1684         intern_proposed_delenda='cdebootstrap-helper-rc.d xserver-xorg-input-synaptics' #carthago'
1685         for pakket in $intern_proposed_delenda; do
1686                 if dpkg -s $pakket > /dev/null 2> /dev/null; then
1687                         intern_delenda="$intern_delenda $pakket"
1688                 fi
1689         done
1690         chroot "$INST_DIR" apt-get --yes --purge remove $intern_delenda
1691
1692         echo " * Cleaning APT cache"
1693         chroot "$INST_DIR" apt-get clean
1694
1695         echo "I: Cleaning the environment - done - the environment is now clean!"
1696 }
1697
1698
1699 #
1700 # Stage unmount
1701 #
1702
1703 action_unmount () {
1704
1705         echo "I: Unmounting - start"
1706
1707         if mount | grep -q "$INST_DIR/proc"; then
1708                 echo -n " * Unmounting proc from install directory"
1709                 if umount "$INST_DIR/proc"; then
1710                         echo " [ok]"
1711                 else
1712                         echo " [failed]"
1713                 fi
1714         elif [ -n "$VERBOSE" ]; then
1715                 echo "I: /proc directory not mounted to install directory"
1716         fi
1717
1718
1719         echo " * Unmounting microSD card partitions"
1720         if [ "$SINGLE_PART" != "true" ]; then
1721                 umount "$INST_DIR/boot"
1722         fi
1723         if ! umount "$INST_DIR"; then
1724                 echo "W: Could not unmount install directory '$INST_DIR'."
1725                 return 1;
1726         fi
1727         echo "I: microSD card partitions unmounted"
1728
1729         intern_fstablines='E: the intern_fstablines remained unset'
1730         intern_line_or_lines='E: the intern_fstablines remained unset'
1731         if [ -z "$intern_instdir_already_present" -o "no" = "$intern_instdir_already_present" ]; then
1732                 if ! rmdir "$INST_DIR"; then
1733                         echo "W: Could not remove directory '$INST_DIR'."
1734                         echo
1735                 fi
1736                 
1737                 if [ "$SINGLE_PART" = "true" ]; then
1738                         intern_fstablines="${SD_DEVICE}p1  $INST_DIR  auto  defaults,async  0  0"
1739                         intern_line_or_lines="line"
1740                 else
1741                         intern_fstablines="${SD_DEVICE}p1  $INST_DIR/boot  auto  defaults,async  0  0
1742 ${SD_DEVICE}p2  $INST_DIR  auto  defaults,async  0  0"
1743                         intern_line_or_lines="lines"
1744                 fi
1745                 
1746                 cat <<EOINSTRUCTIONS
1747
1748 The partition on which Debian was installed is no
1749 longer mounted. You may want to consider to recreate
1750 the directory '$INST_DIR' (if necessary) with
1751
1752 if [ ! -d $INST_DIR ]; then mkdir "$INST_DIR"; fi
1753
1754 and add the $intern_line_or_lines
1755
1756 $intern_fstablines
1757
1758 to the file /etc/fstab, so it will be mounted with subsequent
1759 boots into the current system.
1760
1761 EOINSTRUCTIONS
1762         fi
1763
1764         echo "I: Unmounting - done"
1765         return 0
1766 }
1767
1768 #
1769 #     MAIN PROGRAM
1770 #
1771
1772 if [ $# -eq 0 ]; then
1773   usage
1774 fi
1775
1776 # internal variable
1777
1778 showHelp=""
1779
1780 # parse options
1781
1782 assignVariablename=""
1783 assignParametername=""
1784
1785 vfat_check
1786
1787 for i in $*
1788 do
1789         if [ -z "$assignVariablename" ]; then
1790                 case "$i" in
1791                  "-h"|"--help"|"What's up, doc?")
1792                         showHelp="true"
1793                         ;;
1794                  "--verbose")
1795                         VERBOSE="true"
1796                         ;;
1797                  "-v")
1798                         echo "$internal_version"
1799                         exit 0
1800                         ;;
1801                  "--apt-options")
1802                         assignParametername="$i"
1803                         assignVariablename="APT_OPTIONS"
1804                         ;;
1805                  "--bootstrapper")
1806                         assignParametername="$i"
1807                         assignVariablename="BOOTSTRAPPER"
1808                         assignRegex='^(deboostrap|cdebootstrap)$'
1809                         ;;
1810                  "--hostname")
1811                         assignParametername="$i"
1812                         assignVariablename="HOSTNAME"
1813                         ;;
1814                  "--mount-proc")
1815                         MOUNT_PROC="true"
1816                         ;;
1817                  "--no-mount-proc")
1818                         MOUNT_PROC=""
1819                         ;;
1820                  "--no-partitioning")
1821                         intern_no_partitioning="true"
1822                         ;;
1823                  "--part1-fs")
1824                         assignParametername="$i"
1825                         assignVariablename="SD_PART1_FS"
1826                         ;;
1827                  "--part1-size")
1828                         assignParametername="$i"
1829                         assignNumeric="true"
1830                         assignVariablename="SD_PART1_SIZE"
1831                         ;;
1832                  "--swap-size")
1833                         assignParametername="$i"
1834                         assignNumeric="true"
1835                         assignVariablename="SD_SWAP_SIZE"
1836                         ;;
1837                  "--trayer")
1838                         assignParametername="$i"
1839                         assignVariablename="TRAYER"
1840                         assignRegex='^(trayer|stalonetray)$'
1841                         ;;
1842                  "--with-localepurge")
1843                         LOCALEPURGE="true"
1844                         ;;
1845                  "--with-tasks")
1846                         assignParametername="$i"
1847                         assignVariablename="TASKS"
1848                         ;;
1849                  "all")
1850                         if [ -z "$showHelp" ]; then
1851                                 echo "Running all stages"
1852                                 if [ "$FSO_DEVICE" != gta01 -a "$FSO_DEVICE" != gta02 ]; then
1853                                         echo "E: unsupported FSO_DEVICE '$FSO_DEVICE'"
1854                                         exit 1
1855                                 fi
1856                                 action_all
1857                                 echo
1858                                 echo "I: Done with all stages"
1859                         fi
1860                         ;;
1861                  "testing"|"time"|"partition"|"format"|"mount"|"debian"|"apt"|"fso"|"configuration"|"kernel"|"unmount"|"uboot"|"tasks"|"cleanup")
1862                         if [ -z "$showHelp" ]; then
1863                                 if [ -n "$VERBOSE" ]; then echo "I: Invoking stage $i"; fi
1864                                 if [ "$FSO_DEVICE" != gta01 -a "$FSO_DEVICE" != gta02 ]; then
1865                                         echo "E: unsupported FSO_DEVICE '$FSO_DEVICE'"
1866                                         exit 1
1867                                 fi
1868                                 action_$i
1869                                 if [ -n "$VERBOSE" ]; then echo "I: Done with stage $i"; fi
1870                         fi
1871                         ;;
1872                 *)
1873                         echo "Unknown option '$i'."
1874                         echo
1875                         showHelp="true"
1876                         ;;
1877                 esac
1878         else
1879                 eval $assignVariablename="$i"
1880                 if [ "true" = "$assignNumeric" ]; then
1881                         # Testing if argument is indeed numerical
1882                         if ! echo $i | egrep -q '^[0-9]+$'; then
1883                                 echo
1884                                 echo "Usage error: argument '$assignParametername' needs a numerical argument, not something like '$i'."
1885                                 echo
1886                                 showHelp="true"
1887                         fi
1888                 elif [ -n "$assignRegex" ]; then
1889                         if ! echo "$i" | egrep -q "$assignRegex"; then
1890                                 echo
1891                                 echo "Usage error: argument '$assignParametername' does not match '$assignRegex'."
1892                                 echo 
1893                                 showHelp="true"
1894                         fi
1895                 fi
1896                 assignVariablename=""
1897                 assignParametername=""
1898                 assignNumeric=""
1899                 assignRegex=""
1900         fi
1901 done
1902
1903 if [ -n "$assignParametername" ]; then
1904         echo "Error: expected value for parameter '$assignParametername'."
1905         showHelp="true"
1906 fi
1907
1908 if [ -n "$showHelp" ]; then
1909         usage
1910 fi
1911
1912
1913 exit 0