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 20:39:13 CEST 2007 [mika]
8 ################################################################################
9 # http://www.debian.org/releases/stable/i386/index.html.en
11 set -e # exit on any error
15 # source core functions {{{
16 . /etc/grml/lsb-functions
17 . /etc/grml/script-functions
20 # make sure we have what we need {{{
21 check4progs debootstrap || exit 1
25 # without config file it won't work {{{
26 if [ -r /etc/debootstrap/config ] ; then
27 . /etc/debootstrap/config
29 eerror "/etc/debootstrap/config could not be read, exiting." ; eend 1
34 # cmdline handling {{{
36 einfo "$0 - version $VERSION"
37 echo " A wrapper around debootstrap for installing plain Debian via grml"
39 einfo "Usage: $0 [options]"
40 echo " Adjust /etc/debootstrap/config and invoke $0 afterwards or use the cmdline option:"
42 einfo "Valid options:"
44 -h|--help Print this usage information and exit.
45 -v|--version Show summary of options and exit.
47 -t|--target <target> Target partition (/dev/...) or directory.
48 -r|--release <release> Specify release of new Debian system. Supported relases: sarge, etch, lenny and sid.
49 -m|--mirror <URL> Specify mirror which should be used for apt-get/aptitude.
50 -p|--mntpoint <mnt> Specify mountpoint that should be used for mounting the target system.
51 --groot <device> Specify root device for usage in grub (corresponds with \$TARGET).
52 --grub <device> Where do you want to install grub to? Use grub syntax for specifying.
53 --password <pwd> Use specified password as password for user root. Use with caution.
58 while [ "$#" -gt "0" ] ; do
61 einfo "$0 - version $VERSION"
62 einfo "Send bug reports to Michael Prokop <mika@grml.org>."
101 eerror "Syntax error."
110 # set/check variables {{{
112 # inside the chroot system the locales might not be available, so use minimum:
116 if [ -z "$STAGES" ] ; then
117 STAGES='/etc/debootstrap/stages'
118 [ -d "$STAGES" ] || mkdir -p "$STAGES"
121 if [ -r $STAGES/grml-debootstrap ] ; then
122 if grep -q done $STAGES/grml-debootstrap ; then
123 eerror "Error: grml-debootstrap has been executed already, won't continue therefore."
124 eerror "If you want to re-execute grml-debootstrap just manually remove ${STAGES}" ; eend 1
136 # assume we are installing into a directory, don't run mkfs and grub related stuff therefore
147 if [ -n "$ARCH" ] ; then
148 ARCHCMD="--arch $ARCH"
149 ARCHINFO=" (${ARCH})"
151 ARCH="$(dpkg --print-architecture)"
152 ARCHCMD="--arch $ARCH"
153 ARCHINFO=" (${ARCH})"
156 # provide variables to chroot system
157 touch /etc/debootstrap/variables
158 chmod 600 /etc/debootstrap/variables # make sure nobody except root can read it
159 [ -n "$ARCH" ] && echo "ARCH=$ARCH" > /etc/debootstrap/variables
160 [ -n "$GRUB" ] && echo "GRUB=$GRUB" >> /etc/debootstrap/variables
161 [ -n "$GROOT" ] && echo "GROOT=$GROOT" >> /etc/debootstrap/variables
162 [ -n "$TARGET" ] && echo "TARGET=$TARGET" >> /etc/debootstrap/variables
163 [ -n "$MIRROR" ] && echo "MIRROR=$MIRROR" >> /etc/debootstrap/variables
164 [ -n "$MIRROR" ] && echo "CHROOTMIRROR=$MIRROR" >> /etc/debootstrap/variables
165 [ -n "$ROOTPASSWORD" ] && echo "ROOTPASSWORD=$ROOTPASSWORD" >> /etc/debootstrap/variables
167 # make sure at least $TARGET is set [the partition for the new system]
168 if [ -z "$TARGET" ] ; then
169 eerror "Please adjust /etc/debootstrap/config before running ${0}" ; eend 1
174 # helper functions {{{
175 # we want to exit smoothly and clean:
177 # make sure $TARGET is not mounted when exiting grml-debootstrap
178 if [ -n "$MNTPOINT" ] ; then
179 if grep -q $MNTPOINT /proc/mounts ; then
180 # make sure nothing is left inside chroot so we can unmount it
181 [ -x "$MNTPOINT"/etc/init.d/ssh ] && "$MNTPOINT"/etc/init.d/ssh stop
182 [ -x "$MNTPOINT"/etc/init.d/mdadm ] && "$MNTPOINT"/etc/init.d/mdadm stop
183 # ugly, but make sure we really don't leav anything
184 [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount /sys 1>/dev/null 2>&1
185 [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount -a 1>/dev/null 2>&1
186 [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount /proc 1>/dev/null 2>&1
187 [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount /proc 1>/dev/null 2>&1
188 einfo "Unmounting $MNTPOINT" ; umount "$MNTPOINT" ; eend $?
191 [ -n "$1" ] && EXIT="$1" || EXIT="1"
192 [ -n "$3" ] && einfo "Notice: just remove $STAGES/$3 to reexecute the stage"
195 trap bailout 1 2 3 15
197 # we want to execute all the functions only once, simple check for it:
199 if [ -n "$2" ] ; then
200 echo "$2" > "$STAGES/$1"
202 elif grep -q done "$STAGES/$1" 2>/dev/null ; then
203 ewarn "Notice: stage $1 has been executed already, skipping execution therefore." ; eend 0
209 # user should recheck his configuration {{{
210 # support full automatic installation:
212 dialog --timeout 10 --title "$0" \
213 --yesno "Do you want to stop at this stage?
215 Notice: you are running grml-debootstrap in non-interactive mode.
216 grml-debootstrap will install Debian ${RELEASE} on ${TARGET}.
217 Last chance to quit. Timeout of 10 seconds running....
219 Do you want to stop now?" 0 0 2>/dev/null
222 if [ -n "$AUTOINSTALL" ] ; then
223 if checkforrun ; then
224 eerror "Exiting as requested" ; eend 0
227 else # if not running automatic installation display configuration and prompt for execution:
228 einfo "$0 - Please recheck configuration before execution:"
231 case "$MNTPOINT" in "$TARGET") ;; *) echo " Mount-point: $MNTPOINT" ;; esac
232 [ -n "$GRUB" ] && echo " Install grub to: $GROOT / $GRUB"
233 [ -n "$MIRROR" ] && echo " Using mirror: $MIRROR"
234 case "$MNTPOINT" in "$TARGET") ;; *) echo " Important! Continuing will delete all data from ${TARGET}!" ;; esac
236 einfon "Is this ok for you? [y/N] "
238 if ! [ "$a" = 'y' -o "$a" = 'Y' ] ; then
239 eerror "Exiting as requested." ; eend 1
245 # create filesystem {{{
247 if [ -n "$MKFS" ] ; then
248 einfo "Running $MKFS on $TARGET"
255 # modify filesystem settings {{{
257 if [ -n "$TUNE2FS" ] ; then
258 einfo "Disabling automatic filesystem check on $TARGET via tune2fs"
265 # mount the new partition or if it's a directory do nothing at all {{{
267 if [ -n "$DIRECTORY" ] ; then
268 einfo "Running grml-debootstrap on a directory, nothing to mount."
270 if grep -q $TARGET /proc/mounts ; then
271 eerror "$TARGET already mounted, exiting."
273 [ -n "$MNTPOINT" ] || MNTPOINT='/mnt/test'
274 [ -d "$MNTPOINT" ] || mkdir -p "$MNTPOINT"
275 einfo "Mounting $TARGET to $MNTPOINT"
276 mount -o rw,suid,dev $TARGET $MNTPOINT
283 # install main chroot {{{
284 debootstrap_system() {
285 if ! grep -q $MNTPOINT /proc/mounts ; then
288 if grep -q $MNTPOINT /proc/mounts ; then
289 einfo "Running $DEBOOTSTRAP for release ${RELEASE}${ARCHINFO} using mirror $MIRROR"
290 $DEBOOTSTRAP $ARCHCMD $RELEASE $MNTPOINT $MIRROR
293 eerror "Error: $MNTPOINT not mounted, can not continue."
299 # prepare chroot via chroot-script {{{
301 einfo "Preparing chroot system"
302 cp $CONFFILES/chroot-script $MNTPOINT/bin/chroot-script
303 chmod 755 $MNTPOINT/bin/chroot-script
304 mkdir $MNTPOINT/etc/debootstrap/
306 # make sure we have our files for later use via chroot-script
307 cp /etc/debootstrap/config $MNTPOINT/etc/debootstrap/
308 cp /etc/debootstrap/packages $MNTPOINT/etc/debootstrap/packages
309 cp /etc/debootstrap/variables $MNTPOINT/etc/debootstrap/variables
311 # make sure we can access network [relevant for cdebootstrap]
312 [ -f "$MNTPOINT/etc/resolv.conf" ] || cp /etc/resolv.conf $MNTPOINT/etc/resolv.conf
314 # setup default locales
315 [ -n "$LOCALES" ] && cp /etc/debootstrap/locale.gen $MNTPOINT/etc/locale.gen
317 # copy any existing existing files to chroot
318 [ -d /etc/debootstrap/boot ] && cp -a /etc/debootstrap/boot/* $MNTPOINT/boot/
319 [ -d /etc/debootstrap/etc ] && cp -a /etc/debootstrap/etc/* $MNTPOINT/etc/
320 [ -d /etc/debootstrap/share ] && cp -a /etc/debootstrap/share/* $MNTPOINT/share/
321 [ -d /etc/debootstrap/usr ] && cp -a /etc/debootstrap/usr/* $MNTPOINT/usr/
322 [ -d /etc/debootstrap/var ] && cp -a /etc/debootstrap/var/* $MNTPOINT/var/
327 # execute chroot-script {{{
329 if ! [ -r "$MNTPOINT/bin/chroot-script" ] ; then
332 if [ -x "$MNTPOINT/bin/chroot-script" ] ; then
333 einfo "Executing chroot-script now"
334 chroot "$MNTPOINT" /bin/chroot-script
337 eerror "Fatal: $MNTPOINT/bin/chroot-script could not be found."
343 # install booloader grub {{{
345 if [ -z "$GRUB" -o -z "$GROOT" ] ; then
346 echo "Notice: \$GRUB or \$GROOT not defined, will not install grub therefor."
348 einfo "Installing grub on ${GRUB}:"
349 [ -x /usr/sbin/grub-install ] && GRUBINSTALL=/usr/sbin/grub-install || GRUBINSTALL=/sbin/grub-install
350 $GRUBINSTALL --root-directory="$MNTPOINT" "(${GRUB})"
356 # unmount $MNTPOINRT {{{
358 if [ -n "$PARTITION" ] ; then
359 einfo "Unmount $MNTPOINT"
366 # execute filesystem check {{{
368 if [ "$FSCK" = 'yes' ] ; then
369 [ -n "$FSCKTOOL" ] || FSCKTOOL="fsck.${MKFS#mkfs.}"
370 einfo "Checking filesystem on $TARGET using $FSCKTOOL"
377 # now execute all the functions {{{
378 for i in mkfs tunefs mount_target debootstrap_system preparechroot \
379 chrootscript grub_install umount_chroot fscktool ; do
381 $i && stage $i done || bailout 2 "i"
387 echo done > $STAGES/grml-debootstrap
390 if [ -n "$AUTOINSTALL" ] ; then
391 dialog --title "$0" --msgbox \
392 "Finished execution of ${0}.
393 Enjoy your Debian system." 6 60
395 einfo "Finished execution of $0 - enjoy your Debian system." ; eend 0
398 ## END OF FILE #################################################################
399 # vim: ai tw=100 expandtab foldmethod=marker