Don't clean up stages after execution
[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: Don Apr 12 11:39:22 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 VERSION='0.6'
14
15 # source core functions {{{
16 . /etc/grml/lsb-functions
17 . /etc/grml/script-functions
18 # }}}
19
20 # make sure we have what we need {{{
21 check4progs debootstrap || exit 1
22 check4root || exit 1
23 # }}}
24
25 # cmdline handling {{{
26 case $* in
27    -h*|--h*)
28      einfo "$0 - wrapper around debootstrap for installing plain Debian via grml"
29      einfo "Adjust /etc/debootstrap/config and invoke $0 afterwards."
30      eend 0
31      exit 0
32    ;;
33    -v|--v*)
34      einfo "$0 version $VERSION"
35      einfo "Send bug reports to Michael Prokop <mika@grml.org>."
36      eend 0
37      exit 0
38    ;;
39 esac
40 # }}}
41
42 # without config file it won't work {{{
43 if [ -r /etc/debootstrap/config ] ; then
44    . /etc/debootstrap/config
45 else
46    eerror "/etc/debootstrap/config could not be read, exiting." ; eend 1
47    exit 1
48 fi
49 # }}}
50
51 # set/check variables {{{
52
53 # inside the chroot system the locales might not be available, so use minimum:
54 export LANG=C
55 export LC_ALL=C
56
57 if [ -z "$STAGES" ] ; then
58    STAGES='/etc/debootstrap/stages'
59    [ -d "$STAGES" ] || mkdir -p "$STAGES"
60 fi
61
62 if grep -q done $STAGES/grml-debootstrap ; then
63    eerror "Error: grml-debootstrap has been executed already, won't continue therefore."
64    eerror "If you want to re-execute grml-debootstrap just manually remove ${STAGES}" ; eend 1
65 fi
66
67 PARTITION=''
68 DIRECTORY=''
69
70 case $TARGET in
71   /dev/*)
72     PARTITION=1
73     ;;
74   *)
75     # assume we are installing into a directory, don't run mkfs and grub related stuff therefore
76     DIRECTORY=1
77     MNTPOINT="$TARGET"
78     MKFS=''
79     TUNE2FS=''
80     FSCK=''
81     GRUB=''
82     GROOT=''
83     ;;
84 esac
85
86 if [ -n "$ARCH" ] ; then
87    ARCHCMD="--arch $ARCH"
88    ARCHINFO=" (${ARCH})"
89 else
90    ARCH="$(dpkg --print-architecture)"
91    ARCHCMD="--arch $ARCH"
92    ARCHINFO=" (${ARCH})"
93 fi
94
95 # make sure at least $TARGET is set [the partition for the new system]
96 if [ -z "$TARGET" ] ; then
97    eerror "Please adjust /etc/debootstrap/config before running ${0}" ; eend 1
98    exit 1
99 fi
100 # }}}
101
102 # helper functions {{{
103 # we want to exit smoothly and clean:
104 bailout(){
105   # make sure $TARGET is not mounted when exiting grml-debootstrap
106   if [ -n "$TARGET" ] ; then
107      if grep -q $TARGET /proc/mounts ; then
108         # make sure nothing is left inside chroot so we can unmount it
109         [ -x "$TARGET"/etc/init.d/ssh   ] && "$TARGET"/etc/init.d/ssh stop
110         [ -x "$TARGET"/etc/init.d/mdadm ] && "$TARGET"/etc/init.d/mdadm stop
111         chroot "$TARGET" umount /sys  1>/dev/null 2>&1
112         chroot "$TARGET" umount /proc 1>/dev/null 2>&1
113         echo "Unmounting $TARGET"
114         umount "$TARGET"
115      fi
116   fi
117   [ -n "$1" ] && EXIT="$1" || EXIT="1"
118   exit "$EXIT"
119 }
120 trap bailout 1 2 3 15
121
122 # we want to execute all the functions only once, simple check for it:
123 stage() {
124   if grep -q done "$STAGES/$1" 2>/dev/null ; then
125      ewarn "Notice: stage $1 has been executed already, skipping execution therefore." ; eend 0
126      return 1
127   else
128      echo "$2" > "$STAGES/$1"
129      return 0
130   fi
131 }
132 # }}}
133
134 # user should recheck his configuration {{{
135 einfo "$0 - Please recheck configuration before execution:"
136 echo "
137    Target:           $TARGET"
138    case "$MNTPOINT" in "$TARGET") ;; *) echo "   Mount-point:      $MNTPOINT" ;; esac
139    [ -n "$GRUB" ] && echo "   Install grub to:  $GROOT / $GRUB"
140    case "$MNTPOINT" in "$TARGET") ;; *) echo "  Important! Continuing will delete all data from ${TARGET}!" ;; esac
141    echo
142 einfon "Is this ok for you? [y/N] "
143
144 read a
145 if ! [ "$a" = 'y' -o "$a" = 'Y' ] ; then
146    eerror "Exiting as requested." ; eend 1
147    exit 1
148 fi
149 # }}}
150
151 # create filesystem {{{
152 mkfs() {
153   if [ -n "$MKFS" ] ; then
154      einfo "Running $MKFS on $TARGET"
155      $MKFS $TARGET
156      eend $?
157   fi
158 }
159 # }}}
160
161 # modify filesystem settings {{{
162 tunefs() {
163   if [ -n "$TUNE2FS" ] ; then
164      einfo "Disabling automatic filesystem check on $TARGET via tune2fs"
165      $TUNE2FS $TARGET
166      eend $?
167   fi
168 }
169 # }}}
170
171 # mount the new partition or if it's a directory do nothing at all {{{
172 mount_target() {
173   if [ -n "$DIRECTORY" ] ; then
174      einfo "Running grml-debootstrap on a directory, nothing to mount."
175   else
176      if grep -q $TARGET /proc/mounts ; then
177         eerror "$TARGET already mounted, exiting."
178      else
179        [ -n "$MNTPOINT" ] || MNTPOINT='/mnt/test'
180        [ -d "$MNTPOINT" ] || mkdir -p "$MNTPOINT"
181        einfo "Mounting $TARGET to $MNTPOINT"
182        mount -o rw,suid,dev $TARGET $MNTPOINT
183        eend $?
184      fi
185   fi
186 }
187 # }}}
188
189 # install main chroot {{{
190 debootstrap_system() {
191   einfo "Running $DEBOOTSTRAP for release ${RELEASE}${ARCHINFO} using mirror $MIRROR"
192   $DEBOOTSTRAP $ARCHCMD $RELEASE $MNTPOINT $MIRROR
193   eend $?
194 }
195 # }}}
196
197 # prepare chroot via chroot-script {{{
198 preparechroot() {
199   einfo "Preparing chroot system"
200   cp $CONFFILES/chroot-script $MNTPOINT/bin/chroot-script
201   chmod 755 $MNTPOINT/bin/chroot-script
202   mkdir $MNTPOINT/etc/debootstrap/
203
204   # make sure we have our files for later use via chroot-script
205   cp /etc/debootstrap/config $MNTPOINT/etc/debootstrap/
206   cp /etc/debootstrap/packages $MNTPOINT/etc/debootstrap/packages
207
208   # make sure we can access network [relevant for cdebootstrap]
209   [ -f "$MNTPOINT/etc/resolv.conf" ] || cp /etc/resolv.conf $MNTPOINT/etc/resolv.conf
210
211   # setup default locales
212   [ -n "$LOCALES" ] && cp /etc/debootstrap/locale.gen  $MNTPOINT/etc/locale.gen
213
214   # copy any existing existing files to chroot
215   [ -d /etc/debootstrap/boot  ] && cp -a /etc/debootstrap/boot/*  $MNTPOINT/boot/
216   [ -d /etc/debootstrap/etc   ] && cp -a /etc/debootstrap/etc/*   $MNTPOINT/etc/
217   [ -d /etc/debootstrap/share ] && cp -a /etc/debootstrap/share/* $MNTPOINT/share/
218   [ -d /etc/debootstrap/usr   ] && cp -a /etc/debootstrap/usr/*   $MNTPOINT/usr/
219   [ -d /etc/debootstrap/var   ] && cp -a /etc/debootstrap/var/*   $MNTPOINT/var/
220   eend 0
221 }
222 # }}}
223
224 # execute chroot-script {{{
225 chrootscript() {
226   einfo "Executing chroot-script now"
227   chroot "$MNTPOINT" /bin/chroot-script
228   eend $?
229 }
230 # }}}
231
232 # install booloader grub {{{
233 grub_install() {
234   if [ -z "$GRUB" -o -z "$GROOT" ] ; then
235      echo "Notice: \$GRUB or \$GROOT not defined, will not install grub therefor."
236   else
237      einfo "Installing grub on ${GRUB}:"
238      grub-install --root-directory="$MNTPOINT" "(${GRUB})"
239      eend $?
240   fi
241 }
242 # }}}
243
244 # unmount $MNTPOINRT {{{
245 umount_chroot() {
246   if [ -n "$PARTITION" ] ; then
247      einfo "Unmount $MNTPOINT"
248      umount $MNTPOINT
249      eend $?
250   fi
251 }
252 # }}}
253
254 # execute filesystem check {{{
255 fscktool() {
256   if [ "$FSCK" = 'yes' ] ; then
257      [ -n "$FSCKTOOL" ] || FSCKTOOL="fsck.${MKFS#mkfs.}"
258      einfo "Checking filesystem on $TARGET using $FSCKTOOL"
259      $FSCKTOOL $TARGET
260      eend $?
261   fi
262 }
263 # }}}
264
265 # now execute all the functions {{{
266   stage mkfs               && mkfs               && stage mkfs done
267   stage tunefs             && tunefs             && stage tunefs done
268   stage mount_target       && mount_target       && stage mount_target done
269   stage debootstrap_system && debootstrap_system && stage debootstrap_system done
270   stage preparechroot      && preparechroot      && stage preparechroot done
271   stage chrootscript       && chrootscript       && stage chrootscript done
272   stage grub_install       && grub_install       && stage grub_install done
273   stage umount_chroot      && umount_chroot      && stage umount_chroot done
274   stage fscktool           && fscktool           && stage fscktool done
275 # }}}
276
277 # stages {{{
278   echo done > $STAGES/grml-debootstrap
279 # }}}
280
281   einfo "Finished execution of $0 - enjoy your Debian system." ; eend 0
282
283 ## END OF FILE #################################################################
284 # vim: ai tw=100 expandtab foldmethod=marker