From f1c7aa38c14ab14387f13e06282425adc832c9ba Mon Sep 17 00:00:00 2001 From: Ivan Mikushin Date: Fri, 14 Aug 2015 17:26:16 +0500 Subject: [PATCH] add rancher/os (os-installer) Dockerfile and installer scripts --- .dockerignore | 1 + .dockerignore.docker | 11 ++ Dockerfile | 9 + Dockerfile.build | 3 +- Makefile.docker | 5 +- scripts/installer/bootstrap | 6 + scripts/installer/conf/cloud-config-local.yml | 4 + scripts/installer/conf/vagrant.yml | 3 + scripts/installer/lay-down-os | 181 ++++++++++++++++++ scripts/installer/seed-data | 22 +++ scripts/installer/set-disk-partitions | 43 +++++ 11 files changed, 285 insertions(+), 3 deletions(-) create mode 100644 .dockerignore.docker create mode 100644 Dockerfile create mode 100755 scripts/installer/bootstrap create mode 100644 scripts/installer/conf/cloud-config-local.yml create mode 100755 scripts/installer/conf/vagrant.yml create mode 100755 scripts/installer/lay-down-os create mode 100755 scripts/installer/seed-data create mode 100755 scripts/installer/set-disk-partitions diff --git a/.dockerignore b/.dockerignore index 962cb50c..fe5669be 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,6 +1,7 @@ .DS_Store .git .idea +.dockerignore bin gopath tmp diff --git a/.dockerignore.docker b/.dockerignore.docker new file mode 100644 index 00000000..b36c0d26 --- /dev/null +++ b/.dockerignore.docker @@ -0,0 +1,11 @@ +.DS_Store +.git +.idea +.dockerignore +bin +gopath +tmp +state +build +assets +Godeps/_workspace/pkg diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..9bf4e5fd --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM debian:jessie +COPY ./scripts/installer /scripts +COPY ./scripts/version /scripts/ +RUN /scripts/bootstrap + +COPY ./dist/artifacts/vmlinuz /dist/vmlinuz +COPY ./dist/artifacts/initrd /dist/initrd + +ENTRYPOINT ["/scripts/lay-down-os"] diff --git a/Dockerfile.build b/Dockerfile.build index d0389b28..ffff4c42 100644 --- a/Dockerfile.build +++ b/Dockerfile.build @@ -1,5 +1,4 @@ FROM ros-build-base -ENV CONTAINED=1 - COPY . ./ +COPY .dockerignore.docker .dockerignore diff --git a/Makefile.docker b/Makefile.docker index 8694dea5..8e350d95 100644 --- a/Makefile.docker +++ b/Makefile.docker @@ -49,8 +49,11 @@ $(DIST)/artifacts/iso-checksums.txt: $(DIST)/artifacts/rancheros.iso version: @echo $(VERSION) +installer: $(DIST)/artifacts/initrd $(DIST)/artifacts/vmlinuz + docker build -t rancher/os:$(VERSION) . build-all: \ + installer \ bin/rancheros \ $(DIST)/artifacts/initrd \ $(DIST)/artifacts/vmlinuz \ @@ -58,4 +61,4 @@ build-all: \ $(DIST)/artifacts/iso-checksums.txt -.PHONY: build-all version bin/rancheros +.PHONY: build-all installer version bin/rancheros diff --git a/scripts/installer/bootstrap b/scripts/installer/bootstrap new file mode 100755 index 00000000..426ed80f --- /dev/null +++ b/scripts/installer/bootstrap @@ -0,0 +1,6 @@ +#!/bin/bash + +export DEBIAN_FRONTEND=noninteractive + +apt-get update +apt-get install -y grub2 parted diff --git a/scripts/installer/conf/cloud-config-local.yml b/scripts/installer/conf/cloud-config-local.yml new file mode 100644 index 00000000..4067ba6e --- /dev/null +++ b/scripts/installer/conf/cloud-config-local.yml @@ -0,0 +1,4 @@ +rancher: + cloud_init: + datasources: + - file:/var/lib/rancher/conf/user_config.yml diff --git a/scripts/installer/conf/vagrant.yml b/scripts/installer/conf/vagrant.yml new file mode 100755 index 00000000..73800bca --- /dev/null +++ b/scripts/installer/conf/vagrant.yml @@ -0,0 +1,3 @@ +#cloud-config +ssh_authorized_keys: + - ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ== vagrant insecure public key diff --git a/scripts/installer/lay-down-os b/scripts/installer/lay-down-os new file mode 100755 index 00000000..3b631915 --- /dev/null +++ b/scripts/installer/lay-down-os @@ -0,0 +1,181 @@ +#!/bin/bash +set -e -x + +. $(dirname $0)/version + +while getopts "i:f:c:d:t:r:o:p:" OPTION +do + case $OPTION in + i) DIST="$OPTARG" ;; + f) FILES="$OPTARG" ;; + c) CLOUD_CONFIG="$OPTARG" ;; + d) DEVICE="$OPTARG" ;; + o) OEM="$OPTARG" ;; + p) PARTITION="$OPTARG" ;; + r) ROLLBACK_VERSION="$OPTARG" ;; + t) ENV="$OPTARG" ;; + *) exit 1 ;; + esac +done + +DIST=${DIST:-/dist} +BASE_DIR="/mnt/new_img" +# TODO: Change this to a number so that users can specify. +# Will need to make it so that our builds and packer APIs remain consistent. +PARTITION=${PARTITION:=${DEVICE}1} + +device_defined() +{ + if [[ -z "$1" ]]; then + echo "Need to Pass a device name -d ." 1>&2 + exit 1 + fi +} + +format_device() +{ + device_defined ${DEVICE} + mkfs.ext4 -F -i 4096 -L RANCHER_STATE ${PARTITION} +} + +mount_device() +{ + local label=RANCHER_STATE + local raw="${1:-false}" + + mkdir -p ${BASE_DIR} + + if [ "$(lsblk -o name|grep RANCHER_BOOT | wc -l)" -eq "1" ]; then + label=RANCHER_BOOT + fi + + local mount_opts="-L ${label}" + if [ "${raw}" == "true" ]; then + device_defined ${DEVICE} + mount_opts=${PARTITION} + fi + + mount $mount_opts ${BASE_DIR} + trap "umount ${BASE_DIR}" EXIT +} + +create_boot_dirs() +{ + mkdir -p ${BASE_DIR}/boot/grub +} + +install_grub() { + grub-install --boot-directory=${BASE_DIR}/boot ${DEVICE} +} + +grub2_config(){ + local grub_cfg=${BASE_DIR}/boot/grub/grub.cfg + local append_line="${1}" +cat >$grub_cfg <>$grub_cfg < $grub_file<> $grub_file<&2 + exit 1 + ;; + esac +fi + +grub2_config "${KERNEL_ARGS}" +pvgrub_config "${KERNEL_ARGS}" +install_rancher diff --git a/scripts/installer/seed-data b/scripts/installer/seed-data new file mode 100755 index 00000000..5199f293 --- /dev/null +++ b/scripts/installer/seed-data @@ -0,0 +1,22 @@ +#!/bin/bash + +set -e -x + +BASE_DIR=${1} +CLOUD_DATA=${2} +IFS=',' read -ra FILES <<< "${3}" + +if [ -z ${BASE_DIR} ]; then + echo "Need base directory to place files" 1>&2 + exit 1 +fi + +mkdir -p ${BASE_DIR}/lib/rancher/conf/cloud-config.d + +cp ${CLOUD_DATA} ${BASE_DIR}/lib/rancher/conf/cloud-config.d/ + +for f in ${FILES[@]}; do + IFS=":" read s d <<< "${f}" + mkdir -p $(dirname ${BASE_DIR}/${d}) + cp -a -T ${s} ${BASE_DIR}/${d} +done diff --git a/scripts/installer/set-disk-partitions b/scripts/installer/set-disk-partitions new file mode 100755 index 00000000..d546f3d3 --- /dev/null +++ b/scripts/installer/set-disk-partitions @@ -0,0 +1,43 @@ +#!/bin/bash + +set -e +set -x + +DEVICE=${1} + +if [[ -z $DEVICE ]]; then + echo "Need to Pass a device name as arg1." 1>&2 + exit 1 +fi + +PARTITION_COUNT=$(grep $(echo $DEVICE | cut -d '/' -f3) /proc/partitions | wc -l) +if [ "$PARTITION_COUNT" -gt "1" ]; then + echo "Device ${DEVICE} already partitioned!" + echo "Checking to see if it is mounted" + + # Check this container first... + if grep -q "${DEVICE}" /proc/mounts; then + echo "Device is mounted, we can not repartition" 1>&2 + exit 1 + fi + + # Check other system containers... + for container in $(system-docker ps -q); do + if system-docker exec $container grep -q "${DEVICE}" /proc/mounts; then + echo "Device is mounted in system container ${container}, we can not repartition" 1>&2 + exit 1 + fi + done + + dd if=/dev/zero of=${DEVICE} bs=512 count=1 + partprobe ${DEVICE} +fi + +fdisk ${DEVICE} <