da25b0e04f4cb2b8bba83ae1ff8dc1d5c36d7ff7
[ext/instance-debootstrap.git] / create
1 #!/bin/bash
2
3 # Copyright (C) 2007, 2008, 2009 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 . common.sh
23
24 CLEANUP=( )
25
26 trap cleanup EXIT
27
28 if [ "$GENERATE_CACHE" = "yes" -a ! -d "$CACHE_DIR" ]; then
29   mkdir -p "$CACHE_DIR"
30 fi
31
32 DPKG_ARCH=${ARCH:-`dpkg --print-architecture`}
33 CACHE_FILE="$CACHE_DIR/cache-${SUITE}-${DPKG_ARCH}.tar"
34
35 # If the target device is not a real block device we'll first losetup it.
36 # This is needed for file disks.
37 if [ ! -b $blockdev ]; then
38   ORIGINAL_BLOCKDEV=$blockdev
39   blockdev=$(losetup -sf $blockdev)
40   CLEANUP+=("losetup -d $blockdev")
41 fi
42
43 if [ "$PARTITION_STYLE" = "none" ]; then
44   filesystem_dev=$blockdev
45 elif [ "$PARTITION_STYLE" = "msdos" ]; then
46   # Create one big partition, and make it bootable
47   format_disk0 $blockdev
48   filesystem_dev=$(map_disk0 $blockdev)
49   CLEANUP+=("unmap_disk0 $blockdev")
50 else
51   echo "Unknown partition style $PARTITION_STYLE"
52   exit 1
53 fi
54
55 mke2fs -Fjq $filesystem_dev
56 root_uuid=$($VOL_ID -u $filesystem_dev )
57
58 if [ -n "$swapdev" ]; then
59   mkswap $swapdev
60   swap_uuid=$($VOL_ID -u $swapdev || true )
61 fi
62
63 TMPDIR=`mktemp -d` || exit 1
64 CLEANUP+=("rmdir $TMPDIR")
65
66 mount $filesystem_dev $TMPDIR
67 CLEANUP+=("umount $TMPDIR")
68
69 # remove the cache file if it's old (> 2 weeks) and writable by the owner (the
70 # default due to the standard umask)
71 if [ "$CLEAN_CACHE" -a -d "$CACHE_DIR" ]; then
72   find "$CACHE_DIR" -name 'cache-*.tar' -type f \
73     -daystart -mtime "+${CLEAN_CACHE}" -print0 | \
74     xargs -r0 rm -f
75 fi
76
77 if [ -f "$CACHE_FILE" ]; then
78   tar xf "$CACHE_FILE" -C $TMPDIR
79 else
80   if [ "$PROXY" ]; then
81     export http_proxy="$PROXY"
82   fi
83   # INCLUDE will be empty if EXTRA_PKGS is null/empty, otherwise we
84   # build the full parameter format from it
85   INCLUDE=${EXTRA_PKGS:+"--include=$EXTRA_PKGS"}
86   debootstrap \
87     --arch "$DPKG_ARCH" \
88     $INCLUDE \
89     "$SUITE" $TMPDIR $MIRROR
90
91   # remove the downloaded debs, as they are no longer needed
92   find "$TMPDIR/var/cache/apt/archives" -type f -name '*.deb' -print0 | \
93     xargs -r0 rm -f
94
95   # remove the persistent-net rules, otherwise it will remember the node's
96   # interfaces as eth0, eth1, ...
97   rm -f "$TMPDIR/etc/udev/rules.d/z25_persistent-net.rules"
98
99   if [ "$GENERATE_CACHE" = "yes" ]; then
100     TMP_CACHE=`mktemp "${CACHE_FILE}.XXXXXX"`
101     tar cf "$TMP_CACHE" -C $TMPDIR .
102     mv -f "$TMP_CACHE" "$CACHE_FILE"
103   fi
104 fi
105
106 cp -p /etc/hosts $TMPDIR/etc/hosts
107 cp -p /etc/resolv.conf $TMPDIR/etc/resolv.conf
108 echo $instance > $TMPDIR/etc/hostname
109 echo $instance > $TMPDIR/etc/mailname
110
111 cat > $TMPDIR/etc/fstab <<EOF
112 # /etc/fstab: static file system information.
113 #
114 # <file system>   <mount point>   <type>  <options>       <dump>  <pass>
115 UUID=$root_uuid   /               ext3    defaults        0       1
116 proc              /proc           proc    defaults        0       0
117 EOF
118
119 [ -n "$swapdev" -a -n "$swap_uuid" ] && cat >> $TMPDIR/etc/fstab <<EOF
120 UUID=$swap_uuid   swap            swap    defaults        0       0
121 EOF
122
123 cat > $TMPDIR/etc/network/interfaces <<EOF
124 auto lo
125 iface lo inet loopback
126 EOF
127
128 if [ -e $TMPDIR/etc/inittab ]; then
129   cat $TMPDIR/etc/inittab | sed -re 's/\stty1$/ console/' \
130     > $TMPDIR/etc/inittab.new
131   mv $TMPDIR/etc/inittab.new $TMPDIR/etc/inittab
132 elif [ -e $TMPDIR/etc/event.d/tty1 ]; then
133   cat $TMPDIR/etc/event.d/tty1 | sed -re 's/tty1/console/' \
134     > $TMPDIR/etc/event.d/console
135   rm $TMPDIR/etc/event.d/tty1
136 fi
137
138 RUN_PARTS=`which run-parts`
139
140 if [ -n "$RUN_PARTS" -a -n "$CUSTOMIZE_DIR" -a -d "$CUSTOMIZE_DIR" ]; then
141   TARGET=$TMPDIR
142   BLOCKDEV=$blockdev
143   FSYSDEV=$filesystem_dev
144   export TARGET SUITE ARCH PARTITION_STYLE EXTRA_PKGS BLOCKDEV FSYSDEV
145   $RUN_PARTS $CUSTOMIZE_DIR
146 fi
147
148 # execute cleanups
149 cleanup
150 trap - EXIT
151
152 exit 0