move grub code to a place before umount code; fix sed command in chroot-script
[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 Nov 06 12:51:22 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.1'
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 # user should recheck his configuration
51 einfo "$0 - Please recheck configuration before execution:"
52 echo "
53    Target partition: $TARGET
54    Mount-point:      $MNTPOINT
55    Install grub to:  $MBR / $GROOT [if empty it will not be installed]
56
57    Important! Continuing will delete all data from ${TARGET}!
58 "
59 einfon "Is this ok for you? [y/N] "
60
61 read a
62 if ! [ "$a" = 'y' -o "$a" = 'Y' ] ; then
63    eerror "Exiting as requested." ; eend 1
64    exit 1
65 fi
66
67 if [ -n "$MKFS" ] ; then
68    einfo "Running $MKFS on $TARGET"
69    $MKFS $TARGET
70    eend $?
71 fi
72
73 if [ -n "$TUNE2FS" ] ; then
74    einfo "Disabling automatic filesystem check on $TARGET via tune2fs"
75    $TUNE2FS $TARGET
76    eend $?
77 fi
78
79 # now mount the new partition
80 if grep -q $TARGET /proc/mounts ; then
81    eerror "$TARGET already mounted, exiting."
82 else
83   [ -n "$MNTPOINT" ] || MNTPOINT='/mnt/test'
84   einfo "Mounting $TARGET to $MNTPOINT"
85   [ -d "$MNTPOINT" ] || mkdir -p "$MNTPOINT"
86   mount -o rw,suid,dev $TARGET $MNTPOINT
87   eend $?
88 fi
89
90 # get main packages from a debian-mirror
91 einfo "Running $DEBOOTSTRAP for release $RELEASE using mirror $MIRROR"
92 $DEBOOTSTRAP $RELEASE $MNTPOINT $MIRROR
93 eend $?
94
95 einfo "Preparing chroot system"
96   cp $CONFFILES/chroot-script $MNTPOINT/bin/chroot-script
97   chmod 755 $MNTPOINT/bin/chroot-script
98   mkdir $MNTPOINT/etc/debootstrap/
99
100   # make sure we have our files for later use via chroot-script
101   cp /etc/debootstrap/config $MNTPOINT/etc/debootstrap/
102   cp /etc/debootstrap/packages $MNTPOINT/etc/debootstrap/packages
103
104   # make sure we can access network [relevant for cdebootstrap]
105   [ -f "$MNTPOINT/etc/resolv.conf" ] || cp /etc/resolv.conf $MNTPOINT/etc/resolv.conf
106
107   # setup default locales
108   [ -n "$LOCALES" ] && cp /etc/debootstrap/locale.gen  $MNTPOINT/etc/locale.gen
109
110   # copy any existing existing files to chroot
111   [ -d /etc/debootstrap/boot  ] && cp -a /etc/debootstrap/boot/*  $MNTPOINT/boot/
112   [ -d /etc/debootstrap/etc   ] && cp -a /etc/debootstrap/etc/*   $MNTPOINT/etc/
113   [ -d /etc/debootstrap/share ] && cp -a /etc/debootstrap/share/* $MNTPOINT/share/
114   [ -d /etc/debootstrap/usr   ] && cp -a /etc/debootstrap/usr/*   $MNTPOINT/usr/
115   [ -d /etc/debootstrap/var   ] && cp -a /etc/debootstrap/var/*   $MNTPOINT/var/
116 eend 0
117
118 einfo "Executing chroot-script now"
119 chroot $MNTPOINT /bin/chroot-script
120 eend $?
121
122 # einfo "Removing chroot-script"
123 # rm -f  $MNTPOINT/bin/chroot-script
124 # rm -rf $MNTPOINT/etc/debootstrap/
125 # eend $?
126
127 if [ -z "$MBR" ] ; then
128    echo "Notice: \$MBR not set, will not install grub therefor."
129 else
130    einfo "Installing grub on ${MBR}:"
131    grub-install --root-directory="$MNTPOINT" "(${MBR})"
132    eend $?
133 fi
134
135 einfo "Unmount $MNTPOINT"
136 umount $MNTPOINT
137 eend $?
138
139 if [ "$FSCK" = 'yes' ] ; then
140    [ -n "$FSCKTOOL" ] || FSCKTOOL="fsck.${MKFS#mkfs.}"
141    einfo "Checking filesystem on $TARGET using $FSCKTOOL"
142    $FSCKTOOL $TARGET
143    eend $?
144 fi
145
146 einfo "Finished execution of $0 - enjoy your Debian system." ; eend 0
147
148 ## END OF FILE #################################################################