Use vol_id to calculate volume UUIDs
[ext/instance-debootstrap.git] / create.in
1 #!/bin/sh
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 DEFAULT_FILE="@sysconfdir@/default/ganeti-instance-debootstrap"
23 if [ -f "$DEFAULT_FILE" ]; then
24     . "$DEFAULT_FILE"
25 fi
26
27 # note: we don't set a default mirror since debian and ubuntu have
28 # different defaults, and it's better to use the default
29
30 # only if the user want to specify a mirror in the defaults file we
31 # will use it, this declaration is to make sure the variable is set
32 : ${MIRROR:=""}
33 : ${PROXY:=""}
34 : ${SUITE:="lenny"}
35 : ${ARCH:=""}
36 : ${EXTRA_PKGS:=""}
37 : ${GENERATE_CACHE:="yes"}
38 : ${CLEAN_CACHE:="14"} # number of days to keep a cache file
39
40 CACHE_DIR="@localstatedir@/cache/ganeti-instance-debootstrap"
41
42 if [ "$GENERATE_CACHE" = "yes" -a ! -d "$CACHE_DIR" ]; then
43   mkdir -p "$CACHE_DIR"
44 fi
45
46 DPKG_ARCH=${ARCH:-`dpkg --print-architecture`}
47 CACHE_FILE="$CACHE_DIR/cache-${SUITE}-${DPKG_ARCH}.tar"
48
49 TEMP=`getopt -o i:b:s: -n '$0' -- "$@"`
50
51 if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
52
53 # Note the quotes around `$TEMP': they are essential!
54 eval set -- "$TEMP"
55
56 while true; do
57   case "$1" in
58     -i) instance=$2; shift 2;;
59
60     -b) blockdev=$2; shift 2;;
61
62     -s) swapdev=$2; shift 2;;
63
64     --) shift; break;;
65
66     *)  echo "Internal error!"; exit 1;;
67   esac
68 done
69 if [ -z "$instance" -o -z "$blockdev" -o -z "$swapdev" ]; then
70   echo "Missing -i or -b or -s argument!"
71   exit 1
72 fi
73
74 mkswap $swapdev
75 mke2fs -Fjq $blockdev
76 swap_uuid=$(/sbin/vol_id --uuid $swapdev )
77 root_uuid=$(/sbin/vol_id --uuid $blockdev )
78
79 TMPDIR=`mktemp -d` || exit 1
80 trap "umount $TMPDIR; rmdir $TMPDIR" EXIT
81
82 # If it's not a block device try to mount it via loopback device.
83 # This is needed for file disks.
84 MOUNT_OPTIONS=""
85 if [ ! -b $blockdev ]; then
86   MOUNT_OPTIONS="$MOUNT_OPTIONS -o loop"
87 fi
88 mount $MOUNT_OPTIONS $blockdev $TMPDIR
89
90 # remove the cache file if it's old (> 2 weeks) and writable by the owner (the
91 # default due to the standard umask)
92 if [ "$CLEAN_CACHE" -a -d "$CACHE_DIR" ]; then
93   find "$CACHE_DIR" -name 'cache-*.tar' -type f \
94     -daystart -mtime "+${CLEAN_CACHE}" -print0 | \
95     xargs -r0 rm -f
96 fi
97
98 if [ -f "$CACHE_FILE" ]; then
99   tar xf "$CACHE_FILE" -C $TMPDIR
100 else
101   if [ "$PROXY" ]; then
102     export http_proxy="$PROXY"
103   fi
104   # INCLUDE will be empty if EXTRA_PKGS is null/empty, otherwise we
105   # build the full parameter format from it
106   INCLUDE=${EXTRA_PKGS:+"--include=$EXTRA_PKGS"}
107   debootstrap \
108     --arch "$DPKG_ARCH" \
109     $INCLUDE \
110     "$SUITE" $TMPDIR $MIRROR
111
112   # remove the downloaded debs, as they are no longer needed
113   find "$TMPDIR/var/cache/apt/archives" -type f -name '*.deb' -print0 | \
114     xargs -r0 rm -f
115
116   # remove the persistent-net rules, otherwise it will remember the node's
117   # interfaces as eth0, eth1, ...
118   rm -f "$TMPDIR/etc/udev/rules.d/z25_persistent-net.rules"
119
120   if [ "$GENERATE_CACHE" = "yes" ]; then
121     TMP_CACHE=`mktemp "${CACHE_FILE}.XXXXXX"`
122     tar cf "$TMP_CACHE" -C $TMPDIR .
123     mv -f "$TMP_CACHE" "$CACHE_FILE"
124   fi
125 fi
126
127 cp -p /etc/hosts $TMPDIR/etc/hosts
128 cp -p /etc/resolv.conf $TMPDIR/etc/resolv.conf
129 echo $instance > $TMPDIR/etc/hostname
130 echo $instance > $TMPDIR/etc/mailname
131
132 cat > $TMPDIR/etc/fstab <<EOF
133 # /etc/fstab: static file system information.
134 #
135 # <file system>   <mount point>   <type>  <options>       <dump>  <pass>
136 UUID=$root_uuid   /               ext3    defaults        0       1
137 UUID=$swap_uuid   swap            swap    defaults        0       0
138 proc              /proc           proc    defaults        0       0
139 EOF
140
141 cat > $TMPDIR/etc/network/interfaces <<EOF
142 auto lo
143 iface lo inet loopback
144 EOF
145
146 if [ -e $TMPDIR/etc/inittab ]; then
147   cat $TMPDIR/etc/inittab | sed -re 's/\stty1$/ console/' \
148     > $TMPDIR/etc/inittab.new
149   mv $TMPDIR/etc/inittab.new $TMPDIR/etc/inittab
150 elif [ -e $TMPDIR/etc/event.d/tty1 ]; then
151   cat $TMPDIR/etc/event.d/tty1 | sed -re 's/tty1/console/' \
152     > $TMPDIR/etc/event.d/console
153   rm $TMPDIR/etc/event.d/tty1
154 fi
155
156 umount $TMPDIR
157 rmdir $TMPDIR
158 trap - EXIT
159
160 exit 0