fix stage() function
[grml-debootstrap.git] / grml-debootstrap
1 #!/bin/sh
2 # Filename:      grml-bootstrap
3 # Purpose:       wrapper around debootstrap for installing plain Debian via grml
4 # Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2.
7 # Latest change: Mon Apr 09 23:23:18 CEST 2007 [mika]
8 ################################################################################
9 # http://www.debian.org/releases/stable/i386/index.html.en
10
11 set -e # exit on any error
12
13 . /etc/grml/lsb-functions
14 . /etc/grml/script-functions
15
16 # inside the chroot system the locales might not be available, so use minimum:
17 export LANG=C
18 export LC_ALL=C
19
20 VERSION='0.6'
21
22 case $* in
23    -h*|--h*)
24      einfo "$0 - wrapper around debootstrap for installing plain Debian via grml"
25      einfo "Adjust /etc/debootstrap/config and invoke $0 afterwards."
26      eend 0
27      exit 0
28    ;;
29    -v|--v*)
30      einfo "$0 version $VERSION"
31      einfo "Send bug reports to Michael Prokop <mika@grml.org>."
32      eend 0
33      exit 0
34    ;;
35 esac
36
37 check4progs debootstrap || exit 1
38 check4root || exit 1
39
40 # without config file it won't work
41 if [ -r /etc/debootstrap/config ] ; then
42    . /etc/debootstrap/config
43 else
44    echo "/etc/debootstrap/config could not be read, exiting."
45    exit 1
46 fi
47
48 if [ -z "$STAGES" ] ; then
49    STAGES='/etc/debootstrap/stages'
50    [ -d "$STAGES" ] || mkdir -p "$STAGES"
51 fi
52
53 # make sure at least $TARGET is set [the partition for the new system]
54 if [ -z "$TARGET" ] ; then
55    eerror "Please adjust /etc/debootstrap/config before running ${0}" ; eend 1
56    exit 1
57 fi
58
59 bailout(){
60   # make sure $TARGET is not mounted when exiting grml-debootstrap
61   if [ -n "$TARGET" ] ; then
62      if grep -q $TARGET /proc/mounts ; then
63         # make sure nothing is left inside chroot so we can unmount it
64         [ -x "$TARGET"/etc/init.d/ssh   ] && "$TARGET"/etc/init.d/ssh stop
65         [ -x "$TARGET"/etc/init.d/mdadm ] && "$TARGET"/etc/init.d/mdadm stop
66         chroot "$TARGET" umount /sys  1>/dev/null 2>&1
67         chroot "$TARGET" umount /proc 1>/dev/null 2>&1
68         echo "Unmounting $TARGET"
69         umount "$TARGET"
70      fi
71   fi
72   [ -n "$1" ] && EXIT="$1" || EXIT="1"
73   exit "$EXIT"
74 }
75
76 stage() {
77   # error handling
78   if [ -z "$1" ] ; then
79      eerror 'Error: stage() requires at least one argument.'
80      eend 1
81      return 1
82   fi
83   if [ -z "$STAGES" ] ; then
84      eerror 'Error: $STAGES not set.'
85      eend 1
86      return 1
87   fi
88   # main function
89   if grep -q done "$STAGES/$1" 2>/dev/null ; then
90      ewarn "Notice: stage $1 has been executed already, skipping execution therefore." ; eend 0
91   else
92      echo "$2" > "$STAGES/$1"
93   fi
94 }
95
96 trap bailout 1 2 3 15
97
98 PARTITION=''
99 DIRECTORY=''
100
101 case $TARGET in
102   /dev/*)
103     PARTITION=1
104     ;;
105   *)
106     # assume we are installing into a directory, don't run mkfs and grub related stuff therefore
107     DIRECTORY=1
108     MNTPOINT="$TARGET"
109     MKFS=''
110     TUNE2FS=''
111     FSCK=''
112     GRUB=''
113     GROOT=''
114     ;;
115 esac
116
117 # user should recheck his configuration
118 einfo "$0 - Please recheck configuration before execution:"
119 echo "
120    Target:           $TARGET"
121    case "$MNTPOINT" in "$TARGET") ;; *) echo "   Mount-point:      $MNTPOINT" ;; esac
122    [ -n "$GRUB" ] && echo "   Install grub to:  $GROOT / $GRUB"
123    case "$MNTPOINT" in "$TARGET") ;; *) echo "  Important! Continuing will delete all data from ${TARGET}!" ;; esac
124    echo
125 einfon "Is this ok for you? [y/N] "
126
127 read a
128 if ! [ "$a" = 'y' -o "$a" = 'Y' ] ; then
129    eerror "Exiting as requested." ; eend 1
130    exit 1
131 fi
132
133 if [ -n "$MKFS" ] ; then
134    stage mkfs
135    einfo "Running $MKFS on $TARGET"
136    $MKFS $TARGET
137    eend $?
138    stage mkfs done
139 fi
140
141
142 if [ -n "$TUNE2FS" ] ; then
143    stage tune2fs
144    einfo "Disabling automatic filesystem check on $TARGET via tune2fs"
145    $TUNE2FS $TARGET
146    eend $?
147    stage tune2fs done
148 fi
149
150
151 # now mount the new partition or if it's a directory do nothing at all
152 if [ -n "$DIRECTORY" ] ; then
153    einfo "Running grml-debootstrap on a directory, nothing to mount."
154 else
155    if grep -q $TARGET /proc/mounts ; then
156       eerror "$TARGET already mounted, exiting."
157    else
158      [ -n "$MNTPOINT" ] || MNTPOINT='/mnt/test'
159      [ -d "$MNTPOINT" ] || mkdir -p "$MNTPOINT"
160      einfo "Mounting $TARGET to $MNTPOINT"
161      mount -o rw,suid,dev $TARGET $MNTPOINT
162      eend $?
163    fi
164 fi
165
166 # get main packages from a debian-mirror
167 if [ -n "$ARCH" ] ; then
168    ARCHCMD="--arch $ARCH"
169    ARCHINFO=" (${ARCH})"
170 else
171    ARCH="$(dpkg --print-architecture)"
172    ARCHCMD="--arch $ARCH"
173    ARCHINFO=" (${ARCH})"
174 fi
175
176 stage debootstrap
177 einfo "Running $DEBOOTSTRAP for release ${RELEASE}${ARCHINFO} using mirror $MIRROR"
178 $DEBOOTSTRAP $ARCHCMD $RELEASE $MNTPOINT $MIRROR
179 stage debootstrap done
180 eend $?
181
182 stage preparechroot
183 einfo "Preparing chroot system"
184   cp $CONFFILES/chroot-script $MNTPOINT/bin/chroot-script
185   chmod 755 $MNTPOINT/bin/chroot-script
186   mkdir $MNTPOINT/etc/debootstrap/
187
188   # make sure we have our files for later use via chroot-script
189   cp /etc/debootstrap/config $MNTPOINT/etc/debootstrap/
190   cp /etc/debootstrap/packages $MNTPOINT/etc/debootstrap/packages
191
192   # make sure we can access network [relevant for cdebootstrap]
193   [ -f "$MNTPOINT/etc/resolv.conf" ] || cp /etc/resolv.conf $MNTPOINT/etc/resolv.conf
194
195   # setup default locales
196   [ -n "$LOCALES" ] && cp /etc/debootstrap/locale.gen  $MNTPOINT/etc/locale.gen
197
198   # copy any existing existing files to chroot
199   [ -d /etc/debootstrap/boot  ] && cp -a /etc/debootstrap/boot/*  $MNTPOINT/boot/
200   [ -d /etc/debootstrap/etc   ] && cp -a /etc/debootstrap/etc/*   $MNTPOINT/etc/
201   [ -d /etc/debootstrap/share ] && cp -a /etc/debootstrap/share/* $MNTPOINT/share/
202   [ -d /etc/debootstrap/usr   ] && cp -a /etc/debootstrap/usr/*   $MNTPOINT/usr/
203   [ -d /etc/debootstrap/var   ] && cp -a /etc/debootstrap/var/*   $MNTPOINT/var/
204 eend 0
205 stage preparechroot done
206
207 stage chrootscript
208 einfo "Executing chroot-script now"
209 chroot "$MNTPOINT" /bin/chroot-script
210 eend $?
211 stage chrootscript done
212
213 # einfo "Removing chroot-script"
214 # rm -f  $MNTPOINT/bin/chroot-script
215 # rm -rf $MNTPOINT/etc/debootstrap/
216 # eend $?
217
218 if [ -z "$GRUB" -o -z "$GROOT" ] ; then
219    echo "Notice: \$GRUB or \$GROOT not defined, will not install grub therefor."
220 else
221    stage grubinstall
222    einfo "Installing grub on ${GRUB}:"
223    grub-install --root-directory="$MNTPOINT" "(${GRUB})"
224    eend $?
225    stage grubinstall done
226 fi
227
228 if [ -n "$PARTITION" ] ; then
229    einfo "Unmount $MNTPOINT"
230    umount $MNTPOINT
231    eend $?
232 fi
233
234 if [ "$FSCK" = 'yes' ] ; then
235    stage fscktool
236    [ -n "$FSCKTOOL" ] || FSCKTOOL="fsck.${MKFS#mkfs.}"
237    einfo "Checking filesystem on $TARGET using $FSCKTOOL"
238    $FSCKTOOL $TARGET
239    eend $?
240    stage fscktool done
241 fi
242
243 # finally remove stages on main system so grml-deboostrap can be reexecuted
244   rm -rf "$STAGES"
245
246 einfo "Finished execution of $0 - enjoy your Debian system." ; eend 0
247
248 ## END OF FILE #################################################################