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