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