e842828815edbaa5eb8b2aff53acec81854cc143
[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: Fre Nov 17 23:16:04 CET 2006 [mika]
8 ################################################################################
9 # http://www.debian.org/releases/stable/i386/apcs04.html.en
10
11 set -e # exit on any error
12
13 . /etc/grml/lsb-functions
14 . /etc/grml/script-functions
15
16 VERSION='0.2'
17
18 case $* in
19    -h*|--h*)
20      einfo "$0 - wrapper around debootstrap for installing plain Debian via grml"
21      einfo "Configure via /etc/debootstrap/config and invoke $0 afterwards."
22      eend 0
23      exit 0
24    ;;
25    -v|--v*)
26      einfo "$0 version $VERSION"
27      einfo "Send bug reports to Michael Prokop <mika@grml.org>."
28      eend 0
29      exit 0
30    ;;
31 esac
32
33 check4progs debootstrap || exit 1
34 check4root || exit 1
35
36 # without config file it won't work
37 if [ -r /etc/debootstrap/config ] ; then
38    . /etc/debootstrap/config
39 else
40    echo "/etc/debootstrap/config could not be read, exiting."
41    exit 1
42 fi
43
44 # make sure at least $TARGET is set [the partition for the new system]
45 if [ -z "$TARGET" ] ; then
46    eerror "Please adjust /etc/debootstrap/config before running ${0}" ; eend 1
47    exit 1
48 fi
49
50 function bailout(){
51   # make sure $TARGET is not mounted when exiting grml-debootstrap
52   if [ -n "$TARGET" ] ; then
53      if grep -q $TARGET /proc/mounts ; then
54         echo "Unmounting $TARGET"
55         umount "$TARGET"
56      fi
57   fi
58   [ -n "$1" ] && EXIT="$1" || EXIT="1"
59   exit "$EXIT"
60 }
61
62 trap bailout 1 2 3 15
63
64 PARTITION=''
65 DIRECTORY=''
66
67 case $TARGET in
68   /dev/*)
69     PARTITION=1
70        ;;
71        *)
72        # assume we are installing into a directory, don't run mkfs and grub related stuff therefore
73     DIRECTORY=1
74     MNTPOINT="$TARGET"
75     MKFS=''
76     TUNE2FS=''
77     GRUB=''
78     GROOT=''
79        ;;
80 esac
81
82 # user should recheck his configuration
83 einfo "$0 - Please recheck configuration before execution:"
84 echo "
85    Target:           $TARGET"
86    case "$MNTPOINT" in "$TARGET") ;; *) echo "   Mount-point:      $MNTPOINT" ;; esac
87    [ -n "$GRUB" ] && echo "   Install grub to:  $GROOT / $GRUB"
88    case "$MNTPOINT" in "$TARGET") ;; *) echo "  Important! Continuing will delete all data from ${TARGET}!" ;; esac
89 einfon "Is this ok for you? [y/N] "
90
91 read a
92 if ! [ "$a" = 'y' -o "$a" = 'Y' ] ; then
93    eerror "Exiting as requested." ; eend 1
94    exit 1
95 fi
96
97 if [ -n "$MKFS" ] ; then
98    einfo "Running $MKFS on $TARGET"
99    $MKFS $TARGET
100    eend $?
101 fi
102
103 if [ -n "$TUNE2FS" ] ; then
104    einfo "Disabling automatic filesystem check on $TARGET via tune2fs"
105    $TUNE2FS $TARGET
106    eend $?
107 fi
108
109
110 # now mount the new partition or if it's a directory do nothing at all
111 if [ -n "$DIRECTORY" ] ; then
112    einfo "Running grml-debootstrap on a directory, nothing to mount."
113 else
114    if grep -q $TARGET /proc/mounts ; then
115       eerror "$TARGET already mounted, exiting."
116    else
117      [ -n "$MNTPOINT" ] || MNTPOINT='/mnt/test'
118      [ -d "$MNTPOINT" ] || mkdir -p "$MNTPOINT"
119      einfo "Mounting $TARGET to $MNTPOINT"
120      mount -o rw,suid,dev $TARGET $MNTPOINT
121      eend $?
122    fi
123 fi
124
125 # get main packages from a debian-mirror
126 if [ -n "$ARCH" ] ; then
127    ARCHCMD="--arch $ARCH"
128    ARCHINFO=" (${ARCH})"
129 fi
130 einfo "Running $DEBOOTSTRAP for release ${RELEASE}${ARCHINFO} using mirror $MIRROR"
131 $DEBOOTSTRAP $ARCHCMD $RELEASE $MNTPOINT $MIRROR
132 eend $?
133
134 einfo "Preparing chroot system"
135   cp $CONFFILES/chroot-script $MNTPOINT/bin/chroot-script
136   chmod 755 $MNTPOINT/bin/chroot-script
137   mkdir $MNTPOINT/etc/debootstrap/
138
139   # make sure we have our files for later use via chroot-script
140   cp /etc/debootstrap/config $MNTPOINT/etc/debootstrap/
141   cp /etc/debootstrap/packages $MNTPOINT/etc/debootstrap/packages
142
143   # make sure we can access network [relevant for cdebootstrap]
144   [ -f "$MNTPOINT/etc/resolv.conf" ] || cp /etc/resolv.conf $MNTPOINT/etc/resolv.conf
145
146   # setup default locales
147   [ -n "$LOCALES" ] && cp /etc/debootstrap/locale.gen  $MNTPOINT/etc/locale.gen
148
149   # copy any existing existing files to chroot
150   [ -d /etc/debootstrap/boot  ] && cp -a /etc/debootstrap/boot/*  $MNTPOINT/boot/
151   [ -d /etc/debootstrap/etc   ] && cp -a /etc/debootstrap/etc/*   $MNTPOINT/etc/
152   [ -d /etc/debootstrap/share ] && cp -a /etc/debootstrap/share/* $MNTPOINT/share/
153   [ -d /etc/debootstrap/usr   ] && cp -a /etc/debootstrap/usr/*   $MNTPOINT/usr/
154   [ -d /etc/debootstrap/var   ] && cp -a /etc/debootstrap/var/*   $MNTPOINT/var/
155 eend 0
156
157 einfo "Executing chroot-script now"
158 chroot $MNTPOINT /bin/chroot-script
159 eend $?
160
161 # einfo "Removing chroot-script"
162 # rm -f  $MNTPOINT/bin/chroot-script
163 # rm -rf $MNTPOINT/etc/debootstrap/
164 # eend $?
165
166 if [ -z "$GRUB" -o -z "$GROOT" ] ; then
167    echo "Notice: \$GRUB or \$GROOT not defined, will not install grub therefor."
168 else
169    einfo "Installing grub on ${GRUB}:"
170    grub-install --root-directory="$MNTPOINT" "(${GRUB})"
171    eend $?
172 fi
173
174 if [ -n "$PARTITION" ] ; then
175    einfo "Unmount $MNTPOINT"
176    umount $MNTPOINT
177    eend $?
178 fi
179
180 if [ "$FSCK" = 'yes' ] ; then
181    [ -n "$FSCKTOOL" ] || FSCKTOOL="fsck.${MKFS#mkfs.}"
182    einfo "Checking filesystem on $TARGET using $FSCKTOOL"
183    $FSCKTOOL $TARGET
184    eend $?
185 fi
186
187 einfo "Finished execution of $0 - enjoy your Debian system." ; eend 0
188
189 ## END OF FILE #################################################################