'msdos' is the default partition style from api10
[ext/instance-debootstrap.git] / create.in
1 #!/bin/bash
2
3 # Copyright (C) 2007 Google Inc.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 # 02110-1301, USA.
19
20 set -e
21
22 CLEANUP=( )
23
24 cleanup() {
25   if [ ${#CLEANUP[*]} -gt 0 ]; then
26     LAST_ELEMENT=$((${#CLEANUP[*]}-1))
27     REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
28     for i in $REVERSE_INDEXES; do
29       ${CLEANUP[$i]}
30     done
31   fi
32 }
33
34 trap cleanup EXIT
35
36 DEFAULT_FILE="@sysconfdir@/default/ganeti-instance-debootstrap"
37 if [ -f "$DEFAULT_FILE" ]; then
38     . "$DEFAULT_FILE"
39 fi
40
41 # note: we don't set a default mirror since debian and ubuntu have
42 # different defaults, and it's better to use the default
43
44 # only if the user want to specify a mirror in the defaults file we
45 # will use it, this declaration is to make sure the variable is set
46 : ${MIRROR:=""}
47 : ${PROXY:=""}
48 : ${SUITE:="lenny"}
49 : ${ARCH:=""}
50 : ${EXTRA_PKGS:=""}
51 : ${CUSTOMIZE_DIR:="@sysconfdir@/ganeti/instance-debootstrap.d"}
52 : ${GENERATE_CACHE:="yes"}
53 : ${CLEAN_CACHE:="14"} # number of days to keep a cache file
54 if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
55   DEFAULT_PARTITION_STYLE="none"
56 else
57   DEFAULT_PARTITION_STYLE="msdos"
58 fi
59 : ${PARTITION_STYLE:=$DEFAULT_PARTITION_STYLE} # disk partition style
60
61 CACHE_DIR="@localstatedir@/cache/ganeti-instance-debootstrap"
62
63 if [ "$GENERATE_CACHE" = "yes" -a ! -d "$CACHE_DIR" ]; then
64   mkdir -p "$CACHE_DIR"
65 fi
66
67 DPKG_ARCH=${ARCH:-`dpkg --print-architecture`}
68 CACHE_FILE="$CACHE_DIR/cache-${SUITE}-${DPKG_ARCH}.tar"
69
70 . common.sh
71
72 # If the target device is not a real block device we'll first losetup it.
73 # This is needed for file disks.
74 if [ ! -b $blockdev ]; then
75   ORIGINAL_BLOCKDEV=$blockdev
76   blockdev=$(losetup -sf $blockdev)
77   CLEANUP+=("losetup -d $blockdev")
78 fi
79
80 if [ "$PARTITION_STYLE" = "none" ]; then
81   filesystem_dev=$blockdev
82 elif [ "$PARTITION_STYLE" = "msdos" ]; then
83   # Create one big partition, and make it bootable
84   sfdisk --quiet --Linux $blockdev <<EOF
85 0,,L,*
86 EOF
87   block_base=`basename $blockdev`
88   filesystem_dev_base=`kpartx -l -p- $blockdev | \
89                        grep -m 1 "^$block_base.*$blockdev" | \
90                        awk '{print $1}'`
91   kpartx -a -p- $blockdev
92   CLEANUP+=("kpartx -d -p- $blockdev")
93   filesystem_dev="/dev/mapper/$filesystem_dev_base"
94 else
95   echo "Unknown partition style $PARTITION_STYLE"
96   exit 1
97 fi
98
99 mke2fs -Fjq $filesystem_dev
100 root_uuid=$($VOL_ID -u $filesystem_dev )
101
102 if [ -n "$swapdev" ]; then
103   mkswap $swapdev
104   swap_uuid=$($VOL_ID -u $swapdev || true )
105 fi
106
107 TMPDIR=`mktemp -d` || exit 1
108 CLEANUP+=("rmdir $TMPDIR")
109
110 mount $filesystem_dev $TMPDIR
111 CLEANUP+=("umount $TMPDIR")
112
113 # remove the cache file if it's old (> 2 weeks) and writable by the owner (the
114 # default due to the standard umask)
115 if [ "$CLEAN_CACHE" -a -d "$CACHE_DIR" ]; then
116   find "$CACHE_DIR" -name 'cache-*.tar' -type f \
117     -daystart -mtime "+${CLEAN_CACHE}" -print0 | \
118     xargs -r0 rm -f
119 fi
120
121 if [ -f "$CACHE_FILE" ]; then
122   tar xf "$CACHE_FILE" -C $TMPDIR
123 else
124   if [ "$PROXY" ]; then
125     export http_proxy="$PROXY"
126   fi
127   # INCLUDE will be empty if EXTRA_PKGS is null/empty, otherwise we
128   # build the full parameter format from it
129   INCLUDE=${EXTRA_PKGS:+"--include=$EXTRA_PKGS"}
130   debootstrap \
131     --arch "$DPKG_ARCH" \
132     $INCLUDE \
133     "$SUITE" $TMPDIR $MIRROR
134
135   # remove the downloaded debs, as they are no longer needed
136   find "$TMPDIR/var/cache/apt/archives" -type f -name '*.deb' -print0 | \
137     xargs -r0 rm -f
138
139   # remove the persistent-net rules, otherwise it will remember the node's
140   # interfaces as eth0, eth1, ...
141   rm -f "$TMPDIR/etc/udev/rules.d/z25_persistent-net.rules"
142
143   if [ "$GENERATE_CACHE" = "yes" ]; then
144     TMP_CACHE=`mktemp "${CACHE_FILE}.XXXXXX"`
145     tar cf "$TMP_CACHE" -C $TMPDIR .
146     mv -f "$TMP_CACHE" "$CACHE_FILE"
147   fi
148 fi
149
150 cp -p /etc/hosts $TMPDIR/etc/hosts
151 cp -p /etc/resolv.conf $TMPDIR/etc/resolv.conf
152 echo $instance > $TMPDIR/etc/hostname
153 echo $instance > $TMPDIR/etc/mailname
154
155 cat > $TMPDIR/etc/fstab <<EOF
156 # /etc/fstab: static file system information.
157 #
158 # <file system>   <mount point>   <type>  <options>       <dump>  <pass>
159 UUID=$root_uuid   /               ext3    defaults        0       1
160 proc              /proc           proc    defaults        0       0
161 EOF
162
163 [ -n "$swapdev" -a -n "$swap_uuid" ] && cat >> $TMPDIR/etc/fstab <<EOF
164 UUID=$swap_uuid   swap            swap    defaults        0       0
165 EOF
166
167 cat > $TMPDIR/etc/network/interfaces <<EOF
168 auto lo
169 iface lo inet loopback
170 EOF
171
172 if [ -e $TMPDIR/etc/inittab ]; then
173   cat $TMPDIR/etc/inittab | sed -re 's/\stty1$/ console/' \
174     > $TMPDIR/etc/inittab.new
175   mv $TMPDIR/etc/inittab.new $TMPDIR/etc/inittab
176 elif [ -e $TMPDIR/etc/event.d/tty1 ]; then
177   cat $TMPDIR/etc/event.d/tty1 | sed -re 's/tty1/console/' \
178     > $TMPDIR/etc/event.d/console
179   rm $TMPDIR/etc/event.d/tty1
180 fi
181
182 RUN_PARTS=`which run-parts`
183
184 if [ -n "$RUN_PARTS" -a -n "$CUSTOMIZE_DIR" -a -d "$CUSTOMIZE_DIR" ]; then
185   TARGET=$TMPDIR
186   BLOCKDEV=$blockdev
187   FSYSDEV=$filesystem_dev
188   export TARGET SUITE ARCH PARTITION_STYLE EXTRA_PKGS BLOCKDEV FSYSDEV
189   $RUN_PARTS $CUSTOMIZE_DIR
190 fi
191
192 # execute cleanups
193 cleanup
194 trap - EXIT
195
196 exit 0