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