#!/bin/bash # Copyright (C) 2011 GRNET S.A. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA # 02110-1301, USA. set -e set -o pipefail set -o errtrace . @osdir@/common.sh usage() { local rc="$1" cat <&1 < /dev/null echo "done" echo "Creating partitions..." blockdev=$("$LOSETUP" -sf $helper_img) add_cleanup "$LOSETUP" -d "$blockdev" sleep 1 # sometimes losetup returns and the device is still busy.. format_disk0 "$blockdev" "extdump" 2>&1 | sed -e 's/^/CFDISK: /g' root_dev=$(map_disk0 "$blockdev")-1 add_cleanup unmap_disk0 "$blockdev" echo Creating and configuring filesystem... mkfs.ext3 "$root_dev" 2>&1 | sed -e 's/^/MKFS.EXT3: /g' # The helper vm should never do filesystem checks... tune2fs -i 0 -c 0 "$root_dev" 2>&1 | sed -e 's/^/TUNE2FS: /g' root_uuid=$(blkid -s UUID -o value "$root_dev") target=$(mktemp -d) add_cleanup rmdir "$target" mount "$root_dev" "$target" add_cleanup umount "$root_dev" do_multistrap "$target" # Save the package list chroot "$target" dpkg-query -W > "$HELPER_DIR/packages" echo -n "Configuring the helper image..." echo snf-image-helper > "$target/etc/hostname" cat > "$target/etc/fstab" < UUID=$root_uuid / ext3 defaults 0 1 proc /proc proc defaults 0 0 EOF # We need this since we mount the helper VM ro ln -sf /proc/mounts "$target/etc/mtab" echo "done" echo -n "Extracting kernel..." if [ ! -L "$target/vmlinuz" -o ! -L "$target/vmlinuz" ]; then echo -e "\033[1;31mfailed\033[0m" log_error "vmlinuz or initrd.img link in root is missing." log_error "I don't know how to find a usable kernel/initrd pair." exit 1 fi echo "done" # Make sure extended globbing is enabled shopt -s extglob kernel=$(ls "$target"/boot/vmlinuz-+([0-9.])-+([a-z0-9])-amd64) initrd=$(ls "$target"/boot/initrd.img-+([0-9.])-+([a-z0-9])-amd64) echo "Moving $(basename "$kernel") and $(basename "$initrd") to \`$HELPER_DIR'" cp "$kernel" "$initrd" "$HELPER_DIR" kernel=$(basename "$kernel") initrd=$(basename "$initrd") (cd "$HELPER_DIR"; ln -fs "$kernel" kernel; ln -fs "$initrd" initrd) pkg_installed=$(grep snf-image-helper "$HELPER_DIR/packages" > /dev/null && echo yes) if [ -z "$HELPER_PKG" -a -z "$pkg_installed" ]; then log_error "No helper package was specified and non was found by the apt." exit 1 fi if [ -r "$HELPER_PKG" ]; then echo "Installing snf-image-helper pkg in the new image..." cp "$HELPER_PKG" "$target/tmp/" pkg_name=$(basename "$HELPER_PKG") add_cleanup rm "$target/tmp/$pkg_name" chroot "$target" dpkg -i "/tmp/$pkg_name" 2>&1 | sed -e 's/^/DPKG: /g' # Recreate package list chroot "$target" dpkg-query -W > "$HELPER_DIR/packages" else echo "snf-image-helper pkg was installed from the apt repository." fi helper_version="$(grep ^snf-image-helper[[:space:]] "$HELPER_DIR/packages" | cut -f2)" host_version="$(dpkg-query -W -f "\${Version}\n" snf-image)" if [ "$VERSION_CHECK" == yes -a -z "$HELPER_PKG" ]; then if [ "$host_version" != "$helper_version" ]; then log_error "snf-image version (=$host_version) and " \ "snf-image-helper version (=$helper_version) don't match." exit 1 fi fi mv "$helper_img" "$HELPER_DIR/image" echo "$SNF_IMAGE_VERSION" > "$HELPER_DIR/version" echo "Files in \`$HELPER_DIR' were updated successfully" >&2 exit 0 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :