From 44c3ff7ecef550303c1653a33105ea44b4ab8c5d Mon Sep 17 00:00:00 2001 From: Chris Alfonso Date: Wed, 21 Aug 2013 14:18:01 -0400 Subject: [PATCH] Add rhel installation element If you want to have the installation update packages, you'll need to register the system log in to rhn and subscribe to an available subscription. export DIB_RHSM_USER to your rhn username export DIB_RHSM_PASSWORD to your rhn password To get the qcow2 image, log into rhn.redhat.com and download the image from https://rhn.redhat.com/rhn/software/channel/downloads/Download.do?cid=16952 Then export DIB_CLOUD_IMAGES to whereever you're hosting the qcow2. Change-Id: Idb547f4ffe75514b1e3f6b34f5f347493b132925 --- elements/rhel/README.md | 10 ++++ elements/rhel/bin/install-packages | 38 ++++++++++++++ elements/rhel/bin/map-packages | 30 +++++++++++ elements/rhel/element-deps | 3 ++ elements/rhel/finalise.d/60-rhsm-unregister | 7 +++ elements/rhel/pre-install.d/00-rhsm | 16 ++++++ elements/rhel/root.d/10-rhel-cloud-image | 57 +++++++++++++++++++++ 7 files changed, 161 insertions(+) create mode 100644 elements/rhel/README.md create mode 100644 elements/rhel/bin/install-packages create mode 100755 elements/rhel/bin/map-packages create mode 100644 elements/rhel/element-deps create mode 100755 elements/rhel/finalise.d/60-rhsm-unregister create mode 100755 elements/rhel/pre-install.d/00-rhsm create mode 100755 elements/rhel/root.d/10-rhel-cloud-image diff --git a/elements/rhel/README.md b/elements/rhel/README.md new file mode 100644 index 00000000..ee817888 --- /dev/null +++ b/elements/rhel/README.md @@ -0,0 +1,10 @@ +Overrides: +* Set DIB_CLOUD_IMAGES to a URL for downloading base Red Hat Enterprise Linux cloud image. +* Set DIB_CLOUD_RELEASE to a use a non-default name for the Red Hat Enterprise Linux cloud image. +* Set DIB_RHSM_USER and DIB_RHSM_PASSOWRD for the RHN user to be used for a subscription registration. + If these are set, the image building process will register the system with RHN + and apply the associated Red Hat Enterprise Linux Server subscription so the + latest package updates can be applied. At the end of the image building + process, the system will be unregistered from RHN. +* Set DIB_RHSM_POOL to a subscription pool if you want the system to not use + the auto attach feature of subscription-manager diff --git a/elements/rhel/bin/install-packages b/elements/rhel/bin/install-packages new file mode 100644 index 00000000..d65b4c43 --- /dev/null +++ b/elements/rhel/bin/install-packages @@ -0,0 +1,38 @@ +#!/bin/sh +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +set -e + +# Update packages for the distro +if [ "$1" = "-u" ] ; then + #yum -y update + exit 0 +fi + +# Packages that aren't available in the distro but requested for installation +# can be ignored by adding them to the exclude list +BLACKLIST=(ccache dkms) +WHITELIST=() +for i in "$@" +do + if [[ ! ${BLACKLIST[*]} =~ $i ]]; then + WHITELIST+="$i " + else + echo "The package $i is not available and will not be installed" + fi +done +if [ -n "$WHITELIST" ]; then + rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release + yum -y install $(map-packages $WHITELIST) +fi diff --git a/elements/rhel/bin/map-packages b/elements/rhel/bin/map-packages new file mode 100755 index 00000000..e113336c --- /dev/null +++ b/elements/rhel/bin/map-packages @@ -0,0 +1,30 @@ +#!/bin/env python + +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import sys + +# Manually maintained for brevity; consider making this compiled from +# distromatch or other rich data sources. +# Debian name on the left, RHEL on the right. +package_map = { + 'grub-pc': 'grub', + 'extlinux': 'syslinux-extlinux', + 'open-iscsi': 'iscsi-initiator-utils', + 'vlan': 'vconfig', + } + +for arg in sys.argv[1:]: + print(package_map.get(arg, arg)) +sys.exit(0) diff --git a/elements/rhel/element-deps b/elements/rhel/element-deps new file mode 100644 index 00000000..fcb39be0 --- /dev/null +++ b/elements/rhel/element-deps @@ -0,0 +1,3 @@ +dib-run-parts +cache-url +rpm-distro diff --git a/elements/rhel/finalise.d/60-rhsm-unregister b/elements/rhel/finalise.d/60-rhsm-unregister new file mode 100755 index 00000000..27d7884f --- /dev/null +++ b/elements/rhel/finalise.d/60-rhsm-unregister @@ -0,0 +1,7 @@ +#!/bin/bash + +set -e + +if [ -n $DIB_RHSM_USER ] && [ -n $DIB_RHSM_PASSWORD ]; then + subscription-manager unregister +fi diff --git a/elements/rhel/pre-install.d/00-rhsm b/elements/rhel/pre-install.d/00-rhsm new file mode 100755 index 00000000..cbfefb43 --- /dev/null +++ b/elements/rhel/pre-install.d/00-rhsm @@ -0,0 +1,16 @@ +#!/bin/bash + +set -e + +if [ -n $DIB_RHSM_USER ] && [ -n $DIB_RHSM_PASSWORD ] + then + subscription-manager register --user $DIB_RHSM_USER --password $DIB_RHSM_PASSWORD + # wait a second to ensure consumer certificate is finished writing to disk + sleep 1 + if [ -z $DIB_RHSM_POOL ]; then + subscription-manager attach --auto + else + subscription-manager attach --pool $DIB_RHSM_POOL + fi + subscription-manager repos --enable rhel-6-server-optional-rpms +fi diff --git a/elements/rhel/root.d/10-rhel-cloud-image b/elements/rhel/root.d/10-rhel-cloud-image new file mode 100755 index 00000000..bfeee889 --- /dev/null +++ b/elements/rhel/root.d/10-rhel-cloud-image @@ -0,0 +1,57 @@ +#!/bin/bash + +set -eu + +[ -n "$ARCH" ] +[ -n "$TARGET_ROOT" ] + +if [ 'amd64' = "$ARCH" ] ; then + ARCH="x86_64" +fi + +IMG_PATH=~/.cache/image-create +DIB_RELEASE=${DIB_RELEASE:-latest} +DIB_CLOUD_IMAGES=${DIB_CLOUD_IMAGES:-http://rhn.redhat.com} +BASE_IMAGE_FILE=${BASE_IMAGE_FILE:-rhel-server-x86_64-kvm-6.4_20130130.0-4.qcow2} +BASE_IMAGE_TAR=$DIB_RELEASE-rhel-server-$ARCH-latest.tgz +CACHED_TAR=$IMG_PATH/$BASE_IMAGE_TAR + +if [ -n "$DIB_OFFLINE" -a -f "$CACHED_TAR" ] ; then + echo "Not checking freshness of cached $CACHED_TAR." +else + echo "Fetching Base Image" + $TMP_HOOKS_PATH/bin/cache-url $DIB_CLOUD_IMAGES/$BASE_IMAGE_FILE $IMG_PATH/$BASE_IMAGE_FILE + + if [ ! -f $CACHED_TAR -o \ + $IMG_PATH/$BASE_IMAGE_FILE -nt $CACHED_TAR ] ; then + echo "Repacking base image as tarball." + WORKING=$(mktemp -d) + EACTION="rm -r $WORKING" + trap "$EACTION" EXIT + RAW_FILE=$(basename $BASE_IMAGE_FILE) + RAW_FILE=${RAW_FILE#.qcow2}.raw + qemu-img convert -f qcow2 -O raw $IMG_PATH/$BASE_IMAGE_FILE $WORKING/$RAW_FILE + MAGIC_BIT=p1 + # NOTE: On RHEL, partprobe of /dev/loop0 does not create /dev/loop0p2, + # while kpartx at least creates /dev/mapper/loop0p2. + LOOPDEV=$(sudo kpartx -av $WORKING/$RAW_FILE | awk "/loop[0-9]+$MAGIC_BIT/ {print \$3}") + export LOOPDEV=$LOOPDEV + echo "Loop device is set to: $LOOPDEV" + if ! timeout 5 sh -c "while ! [ -e /dev/mapper/$LOOPDEV ]; do sleep 1; done"; then + echo "Error: Could not find /dev/mapper/$LOOPDEV" + exit 1 + fi + EACTION="sudo kpartx -d $WORKING/$RAW_FILE;$EACTION" + trap "$EACTION" EXIT + mkdir $WORKING/mnt + sudo mount /dev/mapper/$LOOPDEV $WORKING/mnt + EACTION="sudo umount -f $WORKING/mnt;$EACTION" + trap "$EACTION" EXIT + # Chroot in so that we get the correct uid/gid + sudo chroot $WORKING/mnt bin/tar -cz . > $WORKING/tmp.tar + mv $WORKING/tmp.tar $IMG_PATH/$BASE_IMAGE_TAR + fi +fi +# Extract the base image +sudo tar -C $TARGET_ROOT -xzf $IMG_PATH/$BASE_IMAGE_TAR +sudo rmdir $TARGET_ROOT/lost+found