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