Statistics
| Branch: | Revision:

root / common.sh.in @ 6f3e718d

History | View | Annotate | Download (14.6 kB)

1 79224631 Lance Albertson
#
2 79224631 Lance Albertson
3 79224631 Lance Albertson
# Copyright (C) 2007, 2008, 2009 Google Inc.
4 79224631 Lance Albertson
#
5 79224631 Lance Albertson
# This program is free software; you can redistribute it and/or modify
6 79224631 Lance Albertson
# it under the terms of the GNU General Public License as published by
7 79224631 Lance Albertson
# the Free Software Foundation; either version 2 of the License, or
8 79224631 Lance Albertson
# (at your option) any later version.
9 79224631 Lance Albertson
#
10 79224631 Lance Albertson
# This program is distributed in the hope that it will be useful, but
11 79224631 Lance Albertson
# WITHOUT ANY WARRANTY; without even the implied warranty of
12 79224631 Lance Albertson
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 79224631 Lance Albertson
# General Public License for more details.
14 79224631 Lance Albertson
#
15 79224631 Lance Albertson
# You should have received a copy of the GNU General Public License
16 79224631 Lance Albertson
# along with this program; if not, write to the Free Software
17 79224631 Lance Albertson
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 79224631 Lance Albertson
# 02110-1301, USA.
19 79224631 Lance Albertson
20 8d0132dc Lance Albertson
AWK="@AWK@"
21 8d0132dc Lance Albertson
DUMP="@DUMP@"
22 8d0132dc Lance Albertson
LOSETUP="@LOSETUP@"
23 8d0132dc Lance Albertson
KPARTX="@KPARTX@"
24 8d0132dc Lance Albertson
SFDISK="@SFDISK@"
25 ab712029 Lance Albertson
QEMU_IMG="@QEMU_IMG@"
26 8d0132dc Lance Albertson
MKDIR_P="@MKDIR_P@"
27 8d0132dc Lance Albertson
28 79224631 Lance Albertson
CLEANUP=( )
29 79224631 Lance Albertson
30 79224631 Lance Albertson
log_error() {
31 79224631 Lance Albertson
  echo "$@" >&2
32 79224631 Lance Albertson
}
33 79224631 Lance Albertson
34 e7a2d1ad Lance Albertson
debug() {
35 9014a39d Lance Albertson
    [ "$IMAGE_DEBUG" == "1" -o "$IMAGE_DEBUG" == "yes" ] &&  $@ || :
36 e7a2d1ad Lance Albertson
}
37 e7a2d1ad Lance Albertson
38 79224631 Lance Albertson
get_api5_arguments() {
39 79224631 Lance Albertson
  GETOPT_RESULT=$*
40 79224631 Lance Albertson
  # Note the quotes around `$TEMP': they are essential!
41 79224631 Lance Albertson
  eval set -- "$GETOPT_RESULT"
42 79224631 Lance Albertson
  while true; do
43 79224631 Lance Albertson
    case "$1" in
44 79224631 Lance Albertson
      -i|-n) instance=$2; shift 2;;
45 79224631 Lance Albertson
46 79224631 Lance Albertson
      -o) old_name=$2; shift 2;;
47 79224631 Lance Albertson
48 79224631 Lance Albertson
      -b) blockdev=$2; shift 2;;
49 79224631 Lance Albertson
50 79224631 Lance Albertson
      -s) swapdev=$2; shift 2;;
51 79224631 Lance Albertson
52 79224631 Lance Albertson
      --) shift; break;;
53 79224631 Lance Albertson
54 79224631 Lance Albertson
      *)  log_error "Internal error!" >&2; exit 1;;
55 79224631 Lance Albertson
    esac
56 79224631 Lance Albertson
  done
57 79224631 Lance Albertson
  if [ -z "$instance" -o -z "$blockdev" ]; then
58 79224631 Lance Albertson
    log_error "Missing OS API Argument (-i, -n, or -b)"
59 79224631 Lance Albertson
    exit 1
60 79224631 Lance Albertson
  fi
61 79224631 Lance Albertson
  if [ "$SCRIPT_NAME" != "export" -a -z "$swapdev"  ]; then
62 79224631 Lance Albertson
    log_error "Missing OS API Argument -s (swapdev)"
63 79224631 Lance Albertson
    exit 1
64 79224631 Lance Albertson
  fi
65 79224631 Lance Albertson
  if [ "$SCRIPT_NAME" = "rename" -a -z "$old_name"  ]; then
66 79224631 Lance Albertson
    log_error "Missing OS API Argument -o (old_name)"
67 79224631 Lance Albertson
    exit 1
68 79224631 Lance Albertson
  fi
69 79224631 Lance Albertson
}
70 79224631 Lance Albertson
71 79224631 Lance Albertson
get_api10_arguments() {
72 79224631 Lance Albertson
  if [ -z "$INSTANCE_NAME" -o -z "$HYPERVISOR" -o -z "$DISK_COUNT" ]; then
73 79224631 Lance Albertson
    log_error "Missing OS API Variable:"
74 79224631 Lance Albertson
    log_error "(INSTANCE_NAME HYPERVISOR or DISK_COUNT)"
75 79224631 Lance Albertson
    exit 1
76 79224631 Lance Albertson
  fi
77 79224631 Lance Albertson
  instance=$INSTANCE_NAME
78 79224631 Lance Albertson
  if [ $DISK_COUNT -lt 1 -o -z "$DISK_0_PATH" ]; then
79 79224631 Lance Albertson
    log_error "At least one disk is needed"
80 79224631 Lance Albertson
    exit 1
81 79224631 Lance Albertson
  fi
82 79224631 Lance Albertson
  if [ "$SCRIPT_NAME" = "export" ]; then
83 79224631 Lance Albertson
    if [ -z "$EXPORT_DEVICE" ]; then
84 79224631 Lance Albertson
      log_error "Missing OS API Variable EXPORT_DEVICE"
85 79224631 Lance Albertson
    fi
86 79224631 Lance Albertson
    blockdev=$EXPORT_DEVICE
87 79224631 Lance Albertson
  elif [ "$SCRIPT_NAME" = "import" ]; then
88 79224631 Lance Albertson
    if [ -z "$IMPORT_DEVICE" ]; then
89 79224631 Lance Albertson
       log_error "Missing OS API Variable IMPORT_DEVICE"
90 79224631 Lance Albertson
    fi
91 79224631 Lance Albertson
    blockdev=$IMPORT_DEVICE
92 79224631 Lance Albertson
  else
93 79224631 Lance Albertson
    blockdev=$DISK_0_PATH
94 79224631 Lance Albertson
  fi
95 79224631 Lance Albertson
  if [ "$SCRIPT_NAME" = "rename" -a -z "$OLD_INSTANCE_NAME" ]; then
96 79224631 Lance Albertson
    log_error "Missing OS API Variable OLD_INSTANCE_NAME"
97 79224631 Lance Albertson
  fi
98 79224631 Lance Albertson
  old_name=$OLD_INSTANCE_NAME
99 79224631 Lance Albertson
}
100 79224631 Lance Albertson
101 ab462591 Constantinos Venetsanopoulos
get_api20_parameters() {
102 8e5449f1 Constantinos Venetsanopoulos
  if [ -z "$OSP_IMG_ID" -o -z "$OSP_IMG_FORMAT" -o -z "$OSP_IMG_PASSWD" ]; then
103 ab462591 Constantinos Venetsanopoulos
    log_error "Missing OS API Parameter:"
104 8e5449f1 Constantinos Venetsanopoulos
    log_error "(OSP_IMG_ID or OSP_IMG_FORMAT or OSP_IMG_PASSWD)"
105 ab462591 Constantinos Venetsanopoulos
    exit 1
106 ab462591 Constantinos Venetsanopoulos
  fi
107 ab462591 Constantinos Venetsanopoulos
  IMG_ID=$OSP_IMG_ID
108 8e5449f1 Constantinos Venetsanopoulos
  IMG_FORMAT=$OSP_IMG_FORMAT
109 ab462591 Constantinos Venetsanopoulos
  IMG_PASSWD=$OSP_IMG_PASSWD
110 ab462591 Constantinos Venetsanopoulos
}
111 ab462591 Constantinos Venetsanopoulos
112 f6f223b8 Lance Albertson
get_os_type() {
113 abade4ea Lance Albertson
    target=$1
114 abade4ea Lance Albertson
    if [ -z "${target}" ] ; then
115 abade4ea Lance Albertson
        log_error "target is not set in get_os_type"
116 abade4ea Lance Albertson
        exit 1
117 abade4ea Lance Albertson
    fi
118 abade4ea Lance Albertson
    if [ -e ${target}/etc/redhat-release ] ; then
119 f6f223b8 Lance Albertson
        OS_TYPE="redhat"
120 abade4ea Lance Albertson
    elif [ -e ${target}/etc/debian_version ] ; then
121 f6f223b8 Lance Albertson
        OS_TYPE="debian"
122 abade4ea Lance Albertson
    elif [ -e ${target}/etc/gentoo-release ] ; then
123 f6f223b8 Lance Albertson
        OS_TYPE="gentoo"
124 abade4ea Lance Albertson
    elif [ -e ${target}/etc/SuSE-release ] ; then
125 f6f223b8 Lance Albertson
        OS_TYPE="suse"
126 f6f223b8 Lance Albertson
    fi
127 f6f223b8 Lance Albertson
}
128 f6f223b8 Lance Albertson
129 abade4ea Lance Albertson
get_os() {
130 abade4ea Lance Albertson
    target=$1
131 abade4ea Lance Albertson
    if [ -z "${target}" ] ; then
132 abade4ea Lance Albertson
        log_error "target is not set in get_os"
133 abade4ea Lance Albertson
        exit 1
134 abade4ea Lance Albertson
    fi
135 67951e4e Nikos Skalkotos
    if [ -e ${target}/etc/debian_version ] ; then
136 67951e4e Nikos Skalkotos
        if [ -e ${target}/etc/lsb-release ] ; then
137 67951e4e Nikos Skalkotos
            ID=$(grep ^DISTRIB_ID= ${target}/etc/lsb-release | cut -d= -f2)
138 67951e4e Nikos Skalkotos
        fi
139 67951e4e Nikos Skalkotos
        if [ "a$ID" = "aUbuntu" ] ; then
140 67951e4e Nikos Skalkotos
            OPERATING_SYSTEM="ubuntu"
141 67951e4e Nikos Skalkotos
        else
142 67951e4e Nikos Skalkotos
            OPERATING_SYSTEM="debian"
143 67951e4e Nikos Skalkotos
        fi
144 abade4ea Lance Albertson
    elif [ -e ${target}/etc/gentoo-release ] ; then
145 abade4ea Lance Albertson
        OPERATING_SYSTEM="gentoo"
146 abade4ea Lance Albertson
    elif [ -e ${target}/etc/fedora-release ] ; then
147 abade4ea Lance Albertson
        OPERATING_SYSTEM="fedora"
148 9d35a15e Lance Albertson
    elif [ -e ${target}/etc/redhat-release ] ; then
149 abade4ea Lance Albertson
        if [ -n "$(grep -i centos ${target}/etc/redhat-release)" ] ; then
150 abade4ea Lance Albertson
            OPERATING_SYSTEM="centos"
151 abade4ea Lance Albertson
        else
152 abade4ea Lance Albertson
            OPERATING_SYSTEM="redhat"
153 abade4ea Lance Albertson
        fi
154 abade4ea Lance Albertson
    fi
155 abade4ea Lance Albertson
}
156 abade4ea Lance Albertson
157 abade4ea Lance Albertson
get_os_release() {
158 abade4ea Lance Albertson
    target=$1
159 abade4ea Lance Albertson
    if [ -z "${target}" ] ; then
160 abade4ea Lance Albertson
        log_error "target is not set in get_os_release"
161 abade4ea Lance Albertson
        exit 1
162 abade4ea Lance Albertson
    fi
163 67951e4e Nikos Skalkotos
    if [ -e ${target}/etc/debian_version ] ; then
164 abade4ea Lance Albertson
        OS_RELEASE="$(cat ${target}/etc/debian_version)"
165 abade4ea Lance Albertson
    elif [ -e ${target}/etc/fedora-release ] ; then
166 abade4ea Lance Albertson
        OS_RELEASE="$(cat ${target}/etc/fedora-release | awk '{print $3}')"
167 abade4ea Lance Albertson
    elif [ -e ${$target}/etc/redhat-release ] ; then
168 abade4ea Lance Albertson
        OS_RELEASE="$(cat ${target}/etc/redhat-release | awk '{print $3}')"
169 abade4ea Lance Albertson
    fi
170 abade4ea Lance Albertson
}
171 abade4ea Lance Albertson
172 79224631 Lance Albertson
format_disk0() {
173 99efa839 Lance Albertson
    local sfdisk_cmd="$SFDISK -uM -H 255 -S 63 --quiet --Linux --DOS $1"
174 df24aef4 Nikos Skalkotos
    if [  "${SWAP}" = "yes" -a "${BOOT}" = "yes" ] ; then
175 e21b8802 Lance Albertson
        # Create three partitions:
176 e21b8802 Lance Albertson
        # 1 - 100MB /boot, bootable
177 e21b8802 Lance Albertson
        # 2 - Size of Memory, swap
178 e21b8802 Lance Albertson
        # 3 - Rest
179 78422bb3 Lance Albertson
        $sfdisk_cmd > /dev/null <<EOF
180 22b570d6 Lance Albertson
,100,L,*
181 fd786991 Lance Albertson
,${SWAP_SIZE},S
182 22b570d6 Lance Albertson
,,L
183 79224631 Lance Albertson
EOF
184 df24aef4 Nikos Skalkotos
    elif [  "${SWAP}" = "no" -a "${BOOT}" = "yes" ] ; then
185 e21b8802 Lance Albertson
        # Create two partitions:
186 e21b8802 Lance Albertson
        # 1 - 100MB /boot, bootable
187 e21b8802 Lance Albertson
        # 2 - Rest
188 78422bb3 Lance Albertson
        $sfdisk_cmd > /dev/null <<EOF
189 e21b8802 Lance Albertson
,100,L,*
190 e21b8802 Lance Albertson
,,L
191 e21b8802 Lance Albertson
EOF
192 df24aef4 Nikos Skalkotos
    elif [  "${SWAP}" = "yes" -a "${BOOT}" = "no" ] ; then
193 77449e7c Lance Albertson
        # Create two partitions:
194 77449e7c Lance Albertson
        # 1 - Size of Memory, swap
195 77449e7c Lance Albertson
        # 2 - Rest
196 78422bb3 Lance Albertson
        $sfdisk_cmd > /dev/null <<EOF
197 fd786991 Lance Albertson
,$SWAP_SIZE,S
198 77449e7c Lance Albertson
,,L
199 77449e7c Lance Albertson
EOF
200 df24aef4 Nikos Skalkotos
    elif [  "${SWAP}" = "no" -a "${BOOT}" = "no" ] ; then
201 77449e7c Lance Albertson
        # Create two partitions:
202 77449e7c Lance Albertson
        # 1 - Whole
203 78422bb3 Lance Albertson
        $sfdisk_cmd > /dev/null <<EOF
204 77449e7c Lance Albertson
,,L
205 77449e7c Lance Albertson
EOF
206 e21b8802 Lance Albertson
    fi
207 79224631 Lance Albertson
}
208 79224631 Lance Albertson
209 cc927a76 Lance Albertson
mkfs_disk0() {
210 4c96e490 Lance Albertson
    local mkfs="mkfs.${FILESYSTEM}"
211 cc927a76 Lance Albertson
    # Format /
212 78422bb3 Lance Albertson
    $mkfs -Fq -L / $root_dev > /dev/null
213 77449e7c Lance Albertson
    # Format /boot
214 77449e7c Lance Albertson
    if [ -n "${boot_dev}" ] ; then
215 78422bb3 Lance Albertson
        $mkfs -Fq -L /boot $boot_dev > /dev/null
216 77449e7c Lance Albertson
    fi
217 77449e7c Lance Albertson
    # Format swap
218 77449e7c Lance Albertson
    if [ -n "${swap_dev}" ] ; then
219 cc927a76 Lance Albertson
        # Format swap
220 78422bb3 Lance Albertson
        mkswap -f $swap_dev > /dev/null
221 cc927a76 Lance Albertson
    fi
222 0bde4a2a Lance Albertson
    # During reinstalls, ext4 needs a little time after a mkfs so add it here
223 2146dd61 Lance Albertson
    # and also run a sync to be sure.
224 2146dd61 Lance Albertson
    sync
225 0bde4a2a Lance Albertson
    sleep 2
226 cc927a76 Lance Albertson
}
227 cc927a76 Lance Albertson
228 b05b1ab6 Lance Albertson
mount_disk0() {
229 77449e7c Lance Albertson
    local target=$1
230 3173a4bc Lance Albertson
    mount $root_dev $target
231 3173a4bc Lance Albertson
    CLEANUP+=("umount $target")
232 77449e7c Lance Albertson
    if [ -n "${boot_dev}" ] ; then
233 77449e7c Lance Albertson
        $MKDIR_P $target/boot
234 77449e7c Lance Albertson
        mount $boot_dev $target/boot
235 77449e7c Lance Albertson
        CLEANUP+=("umount $target/boot")
236 77449e7c Lance Albertson
    fi
237 1c571a34 Lance Albertson
    # sync the file systems before unmounting to ensure everything is flushed
238 1c571a34 Lance Albertson
    # out
239 1c571a34 Lance Albertson
    CLEANUP+=("sync")
240 b05b1ab6 Lance Albertson
}
241 b05b1ab6 Lance Albertson
242 79224631 Lance Albertson
map_disk0() {
243 77449e7c Lance Albertson
    blockdev="$1"
244 77449e7c Lance Albertson
    filesystem_dev_base=`$KPARTX -l -p- $blockdev | \
245 77449e7c Lance Albertson
                            grep -m 1 -- "-1.*$blockdev" | \
246 77449e7c Lance Albertson
                            $AWK '{print $1}'`
247 77449e7c Lance Albertson
    if [ -z "$filesystem_dev_base" ]; then
248 77449e7c Lance Albertson
        log_error "Cannot interpret kpartx output and get partition mapping"
249 77449e7c Lance Albertson
        exit 1
250 77449e7c Lance Albertson
    fi
251 77449e7c Lance Albertson
    $KPARTX -a -p- $blockdev > /dev/null
252 77449e7c Lance Albertson
    filesystem_dev="/dev/mapper/${filesystem_dev_base/%-1/}"
253 77449e7c Lance Albertson
    if [ ! -b "/dev/mapper/$filesystem_dev_base" ]; then
254 77449e7c Lance Albertson
        log_error "Can't find kpartx mapped partition: /dev/mapper/$filesystem_dev_base"
255 77449e7c Lance Albertson
        exit 1
256 77449e7c Lance Albertson
    fi
257 494927a8 Lance Albertson
    echo "$filesystem_dev"
258 494927a8 Lance Albertson
}
259 494927a8 Lance Albertson
260 494927a8 Lance Albertson
map_partition() {
261 494927a8 Lance Albertson
    filesystem_dev="$1"
262 494927a8 Lance Albertson
    partition="$2"
263 df24aef4 Nikos Skalkotos
    if [ "${SWAP}" = "yes" -a "${BOOT}" = "yes" ] ; then
264 494927a8 Lance Albertson
        boot_dev="${filesystem_dev}-1"
265 494927a8 Lance Albertson
        swap_dev="${filesystem_dev}-2"
266 494927a8 Lance Albertson
        root_dev="${filesystem_dev}-3"
267 df24aef4 Nikos Skalkotos
    elif [ "${SWAP}" = "no" -a "${BOOT}" = "yes" ] ; then
268 494927a8 Lance Albertson
        boot_dev="${filesystem_dev}-1"
269 494927a8 Lance Albertson
        root_dev="${filesystem_dev}-2"
270 df24aef4 Nikos Skalkotos
    elif [ "${SWAP}" = "yes" -a "${BOOT}" = "no" ] ; then
271 494927a8 Lance Albertson
        swap_dev="${filesystem_dev}-1"
272 494927a8 Lance Albertson
        root_dev="${filesystem_dev}-2"
273 df24aef4 Nikos Skalkotos
    elif [ "${SWAP}" = "no" -a "${BOOT}" = "no" ] ; then
274 494927a8 Lance Albertson
        root_dev="${filesystem_dev}-1"
275 77449e7c Lance Albertson
    fi
276 494927a8 Lance Albertson
    echo "$(eval "echo \${$(echo ${partition}_dev)"})"
277 79224631 Lance Albertson
}
278 79224631 Lance Albertson
279 79224631 Lance Albertson
unmap_disk0() {
280 8d0132dc Lance Albertson
  $KPARTX -d -p- $1
281 79224631 Lance Albertson
}
282 79224631 Lance Albertson
283 3903b7f1 Lance Albertson
setup_fstab() {
284 4c96e490 Lance Albertson
    local target=$1 fs=${FILESYSTEM}
285 ecd82584 Lance Albertson
    get_os_type $target
286 3903b7f1 Lance Albertson
    cat > $target/etc/fstab <<EOF
287 3903b7f1 Lance Albertson
# /etc/fstab: static file system information.
288 3903b7f1 Lance Albertson
#
289 3903b7f1 Lance Albertson
# <file system>   <mount point>   <type>  <options>       <dump>  <pass>
290 4c96e490 Lance Albertson
UUID=$root_uuid   /               $fs     defaults        0       1
291 3903b7f1 Lance Albertson
proc              /proc           proc    defaults        0       0
292 3903b7f1 Lance Albertson
EOF
293 3903b7f1 Lance Albertson
294 0877f9c0 Lance Albertson
if [ -n "$boot_dev" -a -n "$boot_uuid" ] ; then
295 0877f9c0 Lance Albertson
    cat >> $target/etc/fstab <<EOF
296 4c96e490 Lance Albertson
UUID=$boot_uuid   /boot           $fs     defaults        1       2
297 3903b7f1 Lance Albertson
EOF
298 0877f9c0 Lance Albertson
fi
299 3903b7f1 Lance Albertson
300 0877f9c0 Lance Albertson
if [ -n "$swap_dev" -a -n "$swap_uuid" ] ; then
301 0877f9c0 Lance Albertson
    cat >> $target/etc/fstab <<EOF
302 3903b7f1 Lance Albertson
UUID=$swap_uuid   swap            swap    defaults        0       0
303 3903b7f1 Lance Albertson
EOF
304 0877f9c0 Lance Albertson
fi
305 3903b7f1 Lance Albertson
306 3903b7f1 Lance Albertson
# OS Specific fstabs
307 0877f9c0 Lance Albertson
if [ "$OS_TYPE" = "redhat" ] ; then
308 0877f9c0 Lance Albertson
    cat >> $target/etc/fstab <<EOF
309 3903b7f1 Lance Albertson
tmpfs             /dev/shm        tmpfs   defaults        0       0
310 3903b7f1 Lance Albertson
devpts            /dev/pts        devpts  gid=5,mode=620  0       0
311 3903b7f1 Lance Albertson
sysfs             /sys            sysfs   defaults        0       0
312 3903b7f1 Lance Albertson
EOF
313 0877f9c0 Lance Albertson
fi
314 3903b7f1 Lance Albertson
315 0877f9c0 Lance Albertson
if [ "$OS_TYPE" = "gentoo" ] ; then
316 0877f9c0 Lance Albertson
    cat >> $target/etc/fstab <<EOF
317 3903b7f1 Lance Albertson
shm               /dev/shm        tmpfs   nodev,nosuid,noexec 0   0
318 3903b7f1 Lance Albertson
EOF
319 0877f9c0 Lance Albertson
fi
320 3903b7f1 Lance Albertson
}
321 3903b7f1 Lance Albertson
322 9f4b2c31 Lance Albertson
setup_console() {
323 9f4b2c31 Lance Albertson
    local target=$1
324 9f4b2c31 Lance Albertson
    if [ -z "$target" ] ; then
325 9f4b2c31 Lance Albertson
        log_error "target not set for setup_console"
326 9f4b2c31 Lance Albertson
        exit 1
327 9f4b2c31 Lance Albertson
    fi
328 9f4b2c31 Lance Albertson
    # Upstart is on this system, so do this instead
329 9f4b2c31 Lance Albertson
    if [ -e ${target}/etc/event.d/tty1 ] ; then
330 9f4b2c31 Lance Albertson
        cat ${target}/etc/event.d/tty1 | sed -re 's/tty1/ttyS0/' \
331 9f4b2c31 Lance Albertson
            > ${target}/etc/event.d/ttyS0
332 9f4b2c31 Lance Albertson
        return
333 9f4b2c31 Lance Albertson
    fi
334 0b56457d Lance Albertson
    # upstart in karmic and newer
335 0b56457d Lance Albertson
    if [ -e ${target}/etc/init/tty1.conf ] ; then
336 0b56457d Lance Albertson
        cat ${target}/etc/init/tty1.conf | \
337 230410df Lance Albertson
        sed -re 's/^exec.*/exec \/sbin\/getty -L 115200 ttyS0 vt102/' \
338 0b56457d Lance Albertson
            > ${target}/etc/init/ttyS0.conf
339 230410df Lance Albertson
        sed -ie 's/tty1/ttyS0/g' ${target}/etc/init/ttyS0.conf
340 0b56457d Lance Albertson
        return
341 0b56457d Lance Albertson
    fi
342 9f4b2c31 Lance Albertson
    get_os $target
343 9f4b2c31 Lance Albertson
    case $OPERATING_SYSTEM in
344 9f4b2c31 Lance Albertson
        gentoo)
345 9f4b2c31 Lance Albertson
            sed -i -e 's/.*ttyS0.*/s0:12345:respawn:\/sbin\/agetty 115200 ttyS0 vt100/' \
346 9f4b2c31 Lance Albertson
                ${target}/etc/inittab
347 9f4b2c31 Lance Albertson
            ;;
348 9f4b2c31 Lance Albertson
        centos)
349 6f7ac139 Lance Albertson
            echo "s0:12345:respawn:/sbin/agetty 115200 ttyS0 vt100" >> \
350 9f4b2c31 Lance Albertson
                ${target}/etc/inittab
351 9f4b2c31 Lance Albertson
            ;;
352 9f4b2c31 Lance Albertson
        debian|ubuntu)
353 a3e24c37 Lance Albertson
            sed -i -e 's/.*T0.*/T0:23:respawn:\/sbin\/getty -L ttyS0 115200 vt100/' \
354 9f4b2c31 Lance Albertson
                ${target}/etc/inittab
355 9f4b2c31 Lance Albertson
            ;;
356 9f4b2c31 Lance Albertson
        *)
357 9f4b2c31 Lance Albertson
            echo "No support for your OS in instance-image, skipping..."
358 9f4b2c31 Lance Albertson
            ;;
359 9f4b2c31 Lance Albertson
    esac
360 9f4b2c31 Lance Albertson
}
361 9f4b2c31 Lance Albertson
362 6f3e718d Nikos Skalkotos
epilogue() {
363 47e547ca Nikos Skalkotos
    local target=$1
364 47e547ca Nikos Skalkotos
    if [ -z "$target" ] ; then
365 47e547ca Nikos Skalkotos
        log_error "target not set for filesystem_check"
366 47e547ca Nikos Skalkotos
        exit 1
367 47e547ca Nikos Skalkotos
    fi
368 47e547ca Nikos Skalkotos
369 6f3e718d Nikos Skalkotos
    get_os_type $target
370 6f3e718d Nikos Skalkotos
    if [ "${OS_TYPE}" = "redhat" ]; then
371 47e547ca Nikos Skalkotos
        # we have to force a filesystem relabeling for SELinux after messing
372 6f3e718d Nikos Skalkotos
        # around with the filesystem in redhat-derived OSs
373 47e547ca Nikos Skalkotos
        echo "Enforce an automatic relabeling in the initial boot process..."
374 47e547ca Nikos Skalkotos
        touch $target/.autorelabel
375 47e547ca Nikos Skalkotos
    fi
376 47e547ca Nikos Skalkotos
}
377 47e547ca Nikos Skalkotos
378 79224631 Lance Albertson
cleanup() {
379 171aea62 Nikos Skalkotos
  # if something fails here, it souldn't call cleanup again...
380 171aea62 Nikos Skalkotos
  trap - EXIT
381 171aea62 Nikos Skalkotos
382 79224631 Lance Albertson
  if [ ${#CLEANUP[*]} -gt 0 ]; then
383 79224631 Lance Albertson
    LAST_ELEMENT=$((${#CLEANUP[*]}-1))
384 79224631 Lance Albertson
    REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
385 79224631 Lance Albertson
    for i in $REVERSE_INDEXES; do
386 171aea62 Nikos Skalkotos
        # If something fails here, it's better to retry it for a few times
387 171aea62 Nikos Skalkotos
        # before we give up with an error. This is needed for kpartx when
388 171aea62 Nikos Skalkotos
        # dealing with ntfs partitions mounted through fuse. umount is not
389 171aea62 Nikos Skalkotos
        # synchronous and may return while the partition is still busy. A
390 171aea62 Nikos Skalkotos
        # premature attempt to delete partition mappings through kpartx on a
391 171aea62 Nikos Skalkotos
        # device that hosts previously mounted ntfs partition may fail with an 
392 171aea62 Nikos Skalkotos
        # `device-mapper: remove ioctl failed: Device or resource busy'
393 171aea62 Nikos Skalkotos
        # error. A sensible workaround for this is to wait for a while and then
394 171aea62 Nikos Skalkotos
        # try again.
395 171aea62 Nikos Skalkotos
        local cmd=${CLEANUP[$i]}
396 171aea62 Nikos Skalkotos
        $cmd || for interval in 0.25 0.5 1 2 4; do
397 171aea62 Nikos Skalkotos
            echo "Command $cmd failed!"
398 171aea62 Nikos Skalkotos
            echo "I'll wait for $interval secs and will retry..."
399 171aea62 Nikos Skalkotos
            sleep $interval
400 171aea62 Nikos Skalkotos
            $cmd && break
401 171aea62 Nikos Skalkotos
        done
402 171aea62 Nikos Skalkotos
        test $? -eq 1 && { echo "Giving Up..."; exit 1; }
403 79224631 Lance Albertson
    done
404 79224631 Lance Albertson
  fi
405 171aea62 Nikos Skalkotos
  echo "Clean UP executed"
406 79224631 Lance Albertson
}
407 79224631 Lance Albertson
408 79224631 Lance Albertson
trap cleanup EXIT
409 79224631 Lance Albertson
410 9cdb305c Lance Albertson
DEFAULT_FILE="@DEFAULT_DIR@/ganeti-instance-image"
411 79224631 Lance Albertson
if [ -f "$DEFAULT_FILE" ]; then
412 79224631 Lance Albertson
    . "$DEFAULT_FILE"
413 79224631 Lance Albertson
fi
414 79224631 Lance Albertson
415 79224631 Lance Albertson
# note: we don't set a default mirror since debian and ubuntu have
416 79224631 Lance Albertson
# different defaults, and it's better to use the default
417 79224631 Lance Albertson
418 79224631 Lance Albertson
# only if the user want to specify a mirror in the defaults file we
419 79224631 Lance Albertson
# will use it, this declaration is to make sure the variable is set
420 828670a1 Lance Albertson
: ${CDINSTALL:="no"}
421 df24aef4 Nikos Skalkotos
: ${BOOT:="no"}
422 e21b8802 Lance Albertson
: ${SWAP:="yes"}
423 fd786991 Lance Albertson
: ${SWAP_SIZE:="${INSTANCE_BE_memory}"}
424 4c96e490 Lance Albertson
: ${FILESYSTEM:="ext3"}
425 be751693 Lance Albertson
: ${KERNEL_ARGS=""}
426 84472880 Lance Albertson
: ${OVERLAY=""}
427 134c11c3 Lance Albertson
: ${IMAGE_NAME:=""}
428 be751693 Lance Albertson
: ${IMAGE_TYPE:="dump"}
429 8727c57d Lance Albertson
: ${NOMOUNT:="no"}
430 79224631 Lance Albertson
: ${ARCH:=""}
431 549118f0 Lance Albertson
: ${CUSTOMIZE_DIR:="@sysconfdir@/ganeti/instance-image/hooks"}
432 f5aa7725 Lance Albertson
: ${VARIANTS_DIR:="@sysconfdir@/ganeti/instance-image/variants"}
433 7193962d Lance Albertson
: ${NETWORKS_DIR:="@sysconfdir@/ganeti/instance-image/networks"}
434 84472880 Lance Albertson
: ${OVERLAYS_DIR:="@sysconfdir@/ganeti/instance-image/overlays"}
435 2e0988ec Lance Albertson
: ${IMAGE_DIR:="@localstatedir@/cache/ganeti-instance-image"}
436 9014a39d Lance Albertson
: ${IMAGE_DEBUG:="no"}
437 ab462591 Constantinos Venetsanopoulos
: ${TOOLS_DIR:="@OS_DIR@/@OS_NAME@/tools"}
438 f7959feb Lance Albertson
439 79224631 Lance Albertson
SCRIPT_NAME=$(basename $0)
440 77449e7c Lance Albertson
KERNEL_PATH="$INSTANCE_HV_kernel_path"
441 79224631 Lance Albertson
442 103d261e Lance Albertson
if [ -f /sbin/blkid -a -x /sbin/blkid ]; then
443 93fa6b1e Lance Albertson
    VOL_ID="/sbin/blkid -c /dev/null -o value -s UUID"
444 93fa6b1e Lance Albertson
    VOL_TYPE="/sbin/blkid -c /dev/null -o value -s TYPE"
445 103d261e Lance Albertson
else
446 103d261e Lance Albertson
    for dir in /lib/udev /sbin; do
447 103d261e Lance Albertson
        if [ -f $dir/vol_id -a -x $dir/vol_id ]; then
448 103d261e Lance Albertson
            VOL_ID="$dir/vol_id -u"
449 103d261e Lance Albertson
            VOL_TYPE="$dir/vol_id -t"
450 103d261e Lance Albertson
        fi
451 103d261e Lance Albertson
    done
452 103d261e Lance Albertson
fi
453 103d261e Lance Albertson
454 103d261e Lance Albertson
if [ -z "$VOL_ID" ]; then
455 103d261e Lance Albertson
    log_error "vol_id or blkid not found, please install udev or util-linux"
456 103d261e Lance Albertson
    exit 1
457 103d261e Lance Albertson
fi
458 103d261e Lance Albertson
459 103d261e Lance Albertson
460 79224631 Lance Albertson
if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
461 79224631 Lance Albertson
  OS_API_VERSION=5
462 79224631 Lance Albertson
  GETOPT_RESULT=`getopt -o o:n:i:b:s: -n '$0' -- "$@"`
463 79224631 Lance Albertson
  if [ $? != 0 ] ; then log_error "Terminating..."; exit 1 ; fi
464 79224631 Lance Albertson
  get_api5_arguments $GETOPT_RESULT
465 79224631 Lance Albertson
elif [ "$OS_API_VERSION" = "10" -o "$OS_API_VERSION" = "15" ]; then
466 79224631 Lance Albertson
  get_api10_arguments
467 ab462591 Constantinos Venetsanopoulos
elif [ "$OS_API_VERSION" = "20" ]; then
468 ab462591 Constantinos Venetsanopoulos
  get_api10_arguments
469 ab462591 Constantinos Venetsanopoulos
  get_api20_parameters
470 ab462591 Constantinos Venetsanopoulos
  IMAGE_NAME=$IMG_ID
471 79224631 Lance Albertson
else
472 79224631 Lance Albertson
  log_error "Unknown OS API VERSION $OS_API_VERSION"
473 79224631 Lance Albertson
  exit 1
474 79224631 Lance Albertson
fi
475 79224631 Lance Albertson
476 79224631 Lance Albertson
if [ -n "$OS_VARIANT" ]; then
477 79224631 Lance Albertson
  if [ ! -d "$VARIANTS_DIR" ]; then
478 79224631 Lance Albertson
    log_error "OS Variants directory $VARIANTS_DIR doesn't exist"
479 79224631 Lance Albertson
    exit 1
480 79224631 Lance Albertson
  fi
481 79224631 Lance Albertson
  VARIANT_CONFIG="$VARIANTS_DIR/$OS_VARIANT.conf"
482 79224631 Lance Albertson
  if [ -f "$VARIANT_CONFIG" ]; then
483 79224631 Lance Albertson
    . "$VARIANT_CONFIG"
484 79224631 Lance Albertson
  else
485 79224631 Lance Albertson
    if grep -qxF "$OS_VARIANT" variants.list; then
486 f5aa7725 Lance Albertson
      log_error "ERROR: instance-image configuration error"
487 79224631 Lance Albertson
      log_error "  Published variant $OS_VARIANT is missing its config file"
488 79224631 Lance Albertson
      log_error "  Please create $VARIANT_CONFIG or unpublish the variant"
489 79224631 Lance Albertson
      log_error "  (by removing $OS_VARIANT from variants.list)"
490 79224631 Lance Albertson
    else
491 79224631 Lance Albertson
      log_error "Unofficial variant $OS_VARIANT is unsupported"
492 79224631 Lance Albertson
      log_error "Most probably this is a user error, forcing a wrong name"
493 79224631 Lance Albertson
      log_error "To support this variant please create file $VARIANT_CONFIG"
494 79224631 Lance Albertson
    fi
495 79224631 Lance Albertson
    exit 1
496 79224631 Lance Albertson
  fi
497 79224631 Lance Albertson
fi