Statistics
| Branch: | Revision:

root / create @ 79224631

History | View | Annotate | Download (4.2 kB)

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
if [ "$GENERATE_CACHE" = "yes" -a ! -d "$CACHE_DIR" ]; then
25
  mkdir -p "$CACHE_DIR"
26
fi
27

    
28
DPKG_ARCH=${ARCH:-`dpkg --print-architecture`}
29
CACHE_FILE="$CACHE_DIR/cache-${SUITE}-${DPKG_ARCH}.tar"
30

    
31
# If the target device is not a real block device we'll first losetup it.
32
# This is needed for file disks.
33
if [ ! -b $blockdev ]; then
34
  ORIGINAL_BLOCKDEV=$blockdev
35
  blockdev=$(losetup -sf $blockdev)
36
  CLEANUP+=("losetup -d $blockdev")
37
fi
38

    
39
if [ "$PARTITION_STYLE" = "none" ]; then
40
  filesystem_dev=$blockdev
41
elif [ "$PARTITION_STYLE" = "msdos" ]; then
42
  # Create one big partition, and make it bootable
43
  format_disk0 $blockdev
44
  filesystem_dev=$(map_disk0 $blockdev)
45
  CLEANUP+=("unmap_disk0 $blockdev")
46
else
47
  echo "Unknown partition style $PARTITION_STYLE"
48
  exit 1
49
fi
50

    
51
mke2fs -Fjq $filesystem_dev
52
root_uuid=$($VOL_ID $filesystem_dev )
53

    
54
if [ -n "$swapdev" ]; then
55
  mkswap $swapdev
56
  swap_uuid=$($VOL_ID $swapdev || true )
57
fi
58

    
59
TMPDIR=`mktemp -d` || exit 1
60
CLEANUP+=("rmdir $TMPDIR")
61

    
62
mount $filesystem_dev $TMPDIR
63
CLEANUP+=("umount $TMPDIR")
64

    
65
# remove the cache file if it's old (> 2 weeks) and writable by the owner (the
66
# default due to the standard umask)
67
if [ "$CLEAN_CACHE" -a -d "$CACHE_DIR" ]; then
68
  find "$CACHE_DIR" -name 'cache-*.tar' -type f \
69
    -daystart -mtime "+${CLEAN_CACHE}" -print0 | \
70
    xargs -r0 rm -f
71
fi
72

    
73
if [ -f "$CACHE_FILE" ]; then
74
  tar xf "$CACHE_FILE" -C $TMPDIR
75
else
76
  if [ "$PROXY" ]; then
77
    export http_proxy="$PROXY"
78
  fi
79
  # INCLUDE will be empty if EXTRA_PKGS is null/empty, otherwise we
80
  # build the full parameter format from it
81
  INCLUDE=${EXTRA_PKGS:+"--include=$EXTRA_PKGS"}
82
  debootstrap \
83
    --arch "$DPKG_ARCH" \
84
    $INCLUDE \
85
    "$SUITE" $TMPDIR $MIRROR
86

    
87
  # remove the downloaded debs, as they are no longer needed
88
  find "$TMPDIR/var/cache/apt/archives" -type f -name '*.deb' -print0 | \
89
    xargs -r0 rm -f
90

    
91
  # remove the persistent-net rules, otherwise it will remember the node's
92
  # interfaces as eth0, eth1, ...
93
  rm -f "$TMPDIR/etc/udev/rules.d/z25_persistent-net.rules"
94

    
95
  if [ "$GENERATE_CACHE" = "yes" ]; then
96
    TMP_CACHE=`mktemp "${CACHE_FILE}.XXXXXX"`
97
    tar cf "$TMP_CACHE" -C $TMPDIR .
98
    mv -f "$TMP_CACHE" "$CACHE_FILE"
99
  fi
100
fi
101

    
102
cp -p /etc/hosts $TMPDIR/etc/hosts
103
cp -p /etc/resolv.conf $TMPDIR/etc/resolv.conf
104
echo $instance > $TMPDIR/etc/hostname
105
echo $instance > $TMPDIR/etc/mailname
106

    
107
cat > $TMPDIR/etc/fstab <<EOF
108
# /etc/fstab: static file system information.
109
#
110
# <file system>   <mount point>   <type>  <options>       <dump>  <pass>
111
UUID=$root_uuid   /               ext3    defaults        0       1
112
proc              /proc           proc    defaults        0       0
113
EOF
114

    
115
[ -n "$swapdev" -a -n "$swap_uuid" ] && cat >> $TMPDIR/etc/fstab <<EOF
116
UUID=$swap_uuid   swap            swap    defaults        0       0
117
EOF
118

    
119
cat > $TMPDIR/etc/network/interfaces <<EOF
120
auto lo
121
iface lo inet loopback
122
EOF
123

    
124
if [ -e $TMPDIR/etc/inittab ]; then
125
  cat $TMPDIR/etc/inittab | sed -re 's/\stty1$/ console/' \
126
    > $TMPDIR/etc/inittab.new
127
  mv $TMPDIR/etc/inittab.new $TMPDIR/etc/inittab
128
elif [ -e $TMPDIR/etc/event.d/tty1 ]; then
129
  cat $TMPDIR/etc/event.d/tty1 | sed -re 's/tty1/console/' \
130
    > $TMPDIR/etc/event.d/console
131
  rm $TMPDIR/etc/event.d/tty1
132
fi
133

    
134
RUN_PARTS=`which run-parts`
135

    
136
if [ -n "$RUN_PARTS" -a -n "$CUSTOMIZE_DIR" -a -d "$CUSTOMIZE_DIR" ]; then
137
  TARGET=$TMPDIR
138
  BLOCKDEV=$blockdev
139
  FSYSDEV=$filesystem_dev
140
  export TARGET SUITE ARCH PARTITION_STYLE EXTRA_PKGS BLOCKDEV FSYSDEV
141
  $RUN_PARTS $CUSTOMIZE_DIR
142
fi
143

    
144
# execute cleanups
145
cleanup
146
trap - EXIT
147

    
148
exit 0