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
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 - wrapper around debootstrap for installing plain Debian via grml"
37 einfo "Adjust /etc/debootstrap/config and invoke $0 afterwards."
40 while [ "$#" -gt "0" ] ; do
43 einfo "$0 version $VERSION"
44 einfo "Send bug reports to Michael Prokop <mika@grml.org>."
79 eerror "Syntax error."
88 # set/check variables {{{
90 # inside the chroot system the locales might not be available, so use minimum:
94 if [ -z "$STAGES" ] ; then
95 STAGES='/etc/debootstrap/stages'
96 [ -d "$STAGES" ] || mkdir -p "$STAGES"
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
114 # assume we are installing into a directory, don't run mkfs and grub related stuff therefore
125 if [ -n "$ARCH" ] ; then
126 ARCHCMD="--arch $ARCH"
127 ARCHINFO=" (${ARCH})"
129 ARCH="$(dpkg --print-architecture)"
130 ARCHCMD="--arch $ARCH"
131 ARCHINFO=" (${ARCH})"
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
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
149 # helper functions {{{
150 # we want to exit smoothly and clean:
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 $?
166 [ -n "$1" ] && EXIT="$1" || EXIT="1"
167 [ -n "$3" ] && einfo "Notice: just remove $STAGES/$3 to reexecute the stage"
170 trap bailout 1 2 3 15
172 # we want to execute all the functions only once, simple check for it:
174 if [ -n "$2" ] ; then
175 echo "$2" > "$STAGES/$1"
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
184 # user should recheck his configuration {{{
185 einfo "$0 - Please recheck configuration before execution:"
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
193 einfon "Is this ok for you? [y/N] "
196 if ! [ "$a" = 'y' -o "$a" = 'Y' ] ; then
197 eerror "Exiting as requested." ; eend 1
202 # create filesystem {{{
204 if [ -n "$MKFS" ] ; then
205 einfo "Running $MKFS on $TARGET"
212 # modify filesystem settings {{{
214 if [ -n "$TUNE2FS" ] ; then
215 einfo "Disabling automatic filesystem check on $TARGET via tune2fs"
222 # mount the new partition or if it's a directory do nothing at all {{{
224 if [ -n "$DIRECTORY" ] ; then
225 einfo "Running grml-debootstrap on a directory, nothing to mount."
227 if grep -q $TARGET /proc/mounts ; then
228 eerror "$TARGET already mounted, exiting."
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
240 # install main chroot {{{
241 debootstrap_system() {
242 if ! grep -q $MNTPOINT /proc/mounts ; then
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
250 eerror "Error: $MNTPOINT not mounted, can not continue."
256 # prepare chroot via chroot-script {{{
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/
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
268 # make sure we can access network [relevant for cdebootstrap]
269 [ -f "$MNTPOINT/etc/resolv.conf" ] || cp /etc/resolv.conf $MNTPOINT/etc/resolv.conf
271 # setup default locales
272 [ -n "$LOCALES" ] && cp /etc/debootstrap/locale.gen $MNTPOINT/etc/locale.gen
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/
284 # execute chroot-script {{{
286 if ! [ -r "$MNTPOINT/bin/chroot-script" ] ; then
289 if [ -x "$MNTPOINT/bin/chroot-script" ] ; then
290 einfo "Executing chroot-script now"
291 chroot "$MNTPOINT" /bin/chroot-script
294 eerror "Fatal: $MNTPOINT/bin/chroot-script could not be found."
300 # install booloader grub {{{
302 if [ -z "$GRUB" -o -z "$GROOT" ] ; then
303 echo "Notice: \$GRUB or \$GROOT not defined, will not install grub therefor."
305 einfo "Installing grub on ${GRUB}:"
306 grub-install --root-directory="$MNTPOINT" "(${GRUB})"
312 # unmount $MNTPOINRT {{{
314 if [ -n "$PARTITION" ] ; then
315 einfo "Unmount $MNTPOINT"
322 # execute filesystem check {{{
324 if [ "$FSCK" = 'yes' ] ; then
325 [ -n "$FSCKTOOL" ] || FSCKTOOL="fsck.${MKFS#mkfs.}"
326 einfo "Checking filesystem on $TARGET using $FSCKTOOL"
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
337 $i && stage $i done || bailout 2 "i"
343 echo done > $STAGES/grml-debootstrap
346 einfo "Finished execution of $0 - enjoy your Debian system." ; eend 0
348 ## END OF FILE #################################################################
349 # vim: ai tw=100 expandtab foldmethod=marker