Skip to content

Instantly share code, notes, and snippets.

@davydotcom
Created September 27, 2017 19:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davydotcom/ccd8b899f89ed96b36016b54ebc9f6fa to your computer and use it in GitHub Desktop.
Save davydotcom/ccd8b899f89ed96b36016b54ebc9f6fa to your computer and use it in GitHub Desktop.
Image Builder Tutorial for Morpheus

Creating an Image Build Task for CentOS 7.3

Morpheus 3 provides a brand new image builder service. This allows the user to build a new virtual image from scratch using only a base install CD and some scripts. This guide will help one better setup a Cent OS 7.3 Community Edition base image.

Requirements

  • Vmware VSPhere Cloud
  • Hypervisor Console Enabled
  • Sufficient storage space (100G recommended) in /var/opt/morpheus
  • DHCP Enabled Network within Vcenter

Getting Started

Before we get too far we need to get a few things together. Firstly we need to have an ISO of Cent OS 7.3 installation. Morpheus supports URL upload directly in Provisioning -> Virtual Images. So first go there and hit 'Add Virtual Image' and pick the ISO image type. I named my image CENTOS-7-x86_64-DVD-1611. Next under 'Create Image By' choose the URL option and pate in the following URL:

http://mirrors.sonic.net/centos/7.3.1611/isos/x86_64/CentOS-7-x86_64-DVD-1611.iso

Save changes and then search for your image name. You will see a live uploading progress radial in the list. Once its complete we need to edit the image and turn off 'Cloud Init' as well as 'Install Agent'.

Now that we have our base ISO we need to make sure a few other prerequisites are in place. When we provision onto a target cloud we will have to be able to use a hypervisor console. So be sure that the cloud we will be provisioning onto has "Hypervisor Console Enabled" checked.

Scripts

We will need to create some scripts we can use post install to install additional components and prep the image for being a template. There are 2 scripts to be mindful of that are called centos-7-setup.sh and centos-7-cleanup.sh. Lets go add these in Provisioning -> Library -> Scripts.

Add a new script and ensure the phase is set to "Provision" for centos-7-setup.sh:

#!/bin/bash -eux

# Turn off DNS lookups for SSH
echo "UseDNS no" >> /etc/ssh/sshd_config

# Add vagrant user to sudoers.
echo 'cloud-user ALL=(ALL) NOPASSWD:ALL' > /etc/sudoers.d/cloud-user
sed -i "s/^.*requiretty/#Defaults requiretty/" /etc/sudoers

yum -y update && yum -y upgrade

# Install packages
yum -y install epel-release

# centos 7 style growroot
yum install -y cloud-init cloud-utils cloud-utils-growpart

Finally add centos-7-cleanup.sh:

#!/bin/bash -eux

# Uninstall Ansible and remove PPA.
yum clean all

for nic in /etc/sysconfig/network-scripts/ifcfg-eth*; do sed -i /HWADDR/d $nic; done

# Delete unneeded files.
rm -f /home/vagrant/*.sh
rm -rf /tmp/*
rm -rf /etc/udev/rules.d/70-persistent-net.rules
#mkdir /etc/udev/rules.d/70-persistent-net.rules
rm -f /lib/udev/rules.d/75-persistent-net-generator.rules
rm -rf /dev/.udev/ /var/lib/dhcp/*

sed -i -e 's/quiet/quiet net.ifnames=0 biosdevname=0/' /etc/default/grub
grub2-mkconfig -o /boot/grub2/grub.cfg
export iface_file=$(basename "$(find /etc/sysconfig/network-scripts/ -name 'ifcfg*' -not -name 'ifcfg-lo' | head -n 1)")
export iface_name=${iface_file:6}
echo $iface_file
echo $iface_name
sudo mv /etc/sysconfig/network-scripts/$iface_file /etc/sysconfig/network-scripts/ifcfg-eth0
sudo sed -i -e "s/$iface_name/eth0/" /etc/sysconfig/network-scripts/ifcfg-eth0
sudo bash -c 'echo NM_CONTROLLED=\"no\" >> /etc/sysconfig/network-scripts/ifcfg-eth0'

sed 's/#PasswordAuthentication yes/PasswordAuthentication yes/' -i /etc/ssh/sshd_config

# disable network manager
service NetworkManager stop
chkconfig NetworkManager off
service network start
chkconfig network on

# Zero out the rest of the free space using dd, then delete the written file.
/sbin/swapoff -a
dd if=/dev/zero of=/EMPTY bs=1M
rm -f /EMPTY

# Add `sync` so Packer doesn't quit too early, before the large file is deleted.
sync

Boot Command and Preseed/Kickstart

A Boot command is a set of keyboard commands to type on the boot prompt as if physically using a keyboard during installation of a base operating system. The boot command can be used to specify to the installer what kickstart file to be used during installation automation.

To create a reusable boot command go to Services -> Image builder -> Boot Scripts and add a new script called centos-7:

<wait5><tab> text ks=<%=preseedUrl%><enter>

NOTE: The <%=preseedUrl%> is a special script injection that allows one to reference the kickstart file we are about to add.

Once this is added we need to create a reusable preseed script which can be done by going to Services -> Image builder -> Preseed Scripts.

Lets create a new one called centos-7-ks.cfg:

# CentOS 7.x kickstart file - ks.cfg
#
# For more information on kickstart syntax and commands, refer to the
# CentOS Installation Guide:
# https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/Installation_Guide/sect-kickstart-syntax.html
#
# For testing, you can fire up a local http server temporarily.
# cd to the directory where this ks.cfg file resides and run the following:
#    $ python -m SimpleHTTPServer
# You don't have to restart the server every time you make changes.  Python
# will reload the file from disk every time.  As long as you save your changes
# they will be reflected in the next HTTP download.  Then to test with
# a PXE boot server, enter the following on the PXE boot prompt:
#    > linux text ks=http://<your_ip>:8000/ks.cfg

# Required settings
lang en_US.UTF-8
keyboard us
rootpw password
authconfig --enableshadow --enablemd5
timezone UTC

# Optional settings
install
cdrom
user --name=cloud-user --plaintext --password password
unsupported_hardware
network --bootproto=dhcp
firewall --disabled
selinux --permissive
bootloader --location=mbr --append="biosdevname=0 net.ifnames=0"
text
skipx
zerombr
clearpart --all --initlabel
autopart --type=plain
firstboot --disabled
reboot

%packages --nobase --ignoremissing --excludedocs
openssh-clients
# Prerequisites for installing VMware Tools or VirtualBox guest additions.
# Put in kickstart to ensure first version installed is from install disk,
# not latest from a mirror.
kernel-headers
kernel-devel
gcc
make
perl
curl
wget
bzip2
dkms
patch
net-tools
git
# Core selinux dependencies installed on 7.x, no need to specify
# Other stuff
sudo
nfs-utils
open-vm-tools
-fprintd-pam
-intltool
-biosdevname

# unnecessary firmware
-aic94xx-firmware
-atmel-firmware
-b43-openfwwf
-bfa-firmware
-ipw*-firmware
-irqbalance
-ivtv-firmware
-iwl*-firmware
-libertas-usb8388-firmware
-ql*-firmware
-rt61pci-firmware
-rt73usb-firmware
-xorg-x11-drv-ati-firmware
-zd1211-firmware
%end

%post
# configure vagrant user in sudoers
echo "%cloud-user ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers.d/cloud-user
chmod 0440 /etc/sudoers.d/cloud-user
cp /etc/sudoers /etc/sudoers.orig
sed -i "s/^\(.*requiretty\)$/#\1/" /etc/sudoers
# keep proxy settings through sudo
echo 'Defaults env_keep += "HTTP_PROXY HTTPS_PROXY FTP_PROXY RSYNC_PROXY NO_PROXY"' >> /etc/sudoers
%end

Now we have all of our prerequisites taken care of for setting up a CentOS 7.3 image build job.

Creating the Image Build

[[TODO: FILL IN]]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment