Kickstarting Ubuntu In SpaceWalk

Preparation

You have to use apt-mirror for getting the packages to local repository, so that you could directly install the system via http method. The configuration file for apt-mirror is listed as following:

$ cat /etc/apt/mirror.list
set base_path /mnt/myrepo
set nthreads     20
set _tilde 0
#################Trusty Repository Starts ########################
deb-amd64 http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse main/debian-installer restricted/debian-installer multiverse/debian-installer universe/debian-installer
deb-amd64 http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse main/debian-installer restricted/debian-installer universe/debian-installer
deb-amd64 http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse main/debian-installer restricted/debian-installer universe/debian-installer
deb-amd64 http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse
deb-amd64 http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse

deb-i386 http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse main/debian-installer restricted/debian-installer multiverse/debian-installer universe/debian-installer
deb-i386 http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse main/debian-installer restricted/debian-installer universe/debian-installer
deb-i386 http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse main/debian-installer restricted/debian-installer universe/debian-installer
deb-i386 http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse
deb-i386 http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse
#################Trusty Repository ends ########################

#################Precise Repository starts ########################
deb-amd64 http://mirrors.aliyun.com/ubuntu precise main main/debian-installer restricted restricted/debian-installer universe multiverse multiverse/debian-installer universe/debian-installer
deb-amd64 http://mirrors.aliyun.com/ubuntu precise-security main restricted universe multiverse main/debian-installer restricted/debian-installer universe/debian-installer
deb-amd64 http://mirrors.aliyun.com/ubuntu precise-updates main restricted universe multiverse main/debian-installer restricted/debian-installer universe/debian-installer
deb-amd64 http://mirrors.aliyun.com/ubuntu precise-proposed main restricted universe multiverse
deb-amd64 http://mirrors.aliyun.com/ubuntu precise-backports main restricted universe multiverse

deb-i386 http://mirrors.aliyun.com/ubuntu precise main main/debian-installer restricted restricted/debian-installer universe multiverse multiverse/debian-installer universe/debian-installer
deb-i386 http://mirrors.aliyun.com/ubuntu precise-security main restricted universe multiverse main/debian-installer restricted/debian-installer universe/debian-installer
deb-i386 http://mirrors.aliyun.com/ubuntu precise-updates main restricted universe multiverse main/debian-installer restricted/debian-installer universe/debian-installer
deb-i386 http://mirrors.aliyun.com/ubuntu precise-proposed main restricted universe multiverse
deb-i386 http://mirrors.aliyun.com/ubuntu precise-backports main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu precise-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu precise-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu precise-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu precise-backports main restricted universe multiverse
#################Precise Repository ends ########################

Sync your local repository via apt-mirror, the sync procedure could takes many days, depends on your bandwidth.

Make Your Own KickStart File

You have to install system-config-kickstart for generating the kickstart file. But the official program conflicts with hwdata, we manually downgrade it and install specified version, then sytem-config-kickstart could be installed.

# apt-get remove hwda
# wget ftp://mirror.ovh.net/mirrors/ftp.debian.org/debian/pool/main/h/hwdata/hwdata_0.234-1_all.deb
# dpkg -i hwdata_0.267-1_all.deb
# sudo apt-get install system-config-kickstart

Run it and its effects is like following:

/images/2015_08_20_12_07_59_637x562.jpg

Select the installation method via:

/images/2015_08_20_12_09_02_541x361.jpg

Save your kickstart file, and save it into some webserver’s directory, since PXE always have the network connection, we could put it the reachable webserver.

PXE Menu Configuration

Just give out an example of the PXE Menu on SpaceWalk:

# cat /var/lib/tftpboot/pxelinux.cfg/default 
DEFAULT menu
PROMPT 0
MENU TITLE Cobbler | http://fedorahosted.org/cobbler
TIMEOUT 200
TOTALTIMEOUT 6000
ONTIMEOUT local

LABEL local
        MENU LABEL (local)
        MENU DEFAULT
        LOCALBOOT 0

LABEL precise-ia32:1:SpacewalkDefaultOrganization
        kernel /images/precise-ia32:1:SpacewalkDefaultOrganization/vmlinuz
        MENU LABEL precise-ia32:1:SpacewalkDefaultOrganization
        append ks=http://192.168.0.79/ubuntu1204kickstart1.cfg initrd=/images/precise-ia32:1:SpacewalkDefaultOrganization/initrd.gz ksdevice=eth0 --
        ipappend 2
MENU end

We use the kickstart file located in a webserver, using this we could install the system unattendly.

An example of the this kickstart file is listed:

# cat ubuntu1204kickstart1.cfg  | more
#Generated by Kickstart Configurator
#platform=x86

#System language
lang en_US
#Language modules to install
langsupport en_US
#System keyboard
keyboard us
#System mouse
mouse
#System timezone
timezone Asia/Shanghai
#Root password
rootpw --iscrypted xxxxxxxxxxxxxxxxxxxxxxxxxxx
#Initial user
user xxxx --fullname "xxxx" --iscrypted --password xxxxxxxxxxxxxxxxxxxxxx
#Reboot after installation
reboot
#Use text mode install
text
#Install OS instead of upgrade
install
#Use Web installation
url --url http://192.168.0.79/ubuntu/
#System bootloader configuration
bootloader --location=mbr 
#Clear the Master Boot Record
zerombr yes
#Partition clearing information
clearpart --all --initlabel 
#Disk partitioning information
part swap --size 1024 
part / --fstype ext4 --size 1 --grow 
#System authorization infomation
auth  --useshadow  --enablemd5 
#Network information
network --bootproto=dhcp --device=eth0
#Firewall configuration
firewall --disabled 
#Do not configure the X Window System
skipx

%packages
ubuntu-minimal
openssh-server
screen
curl
wget

%post

# update fstab for the root partition
perl -pi -e 's/(errors=remount-ro)/noatime,nodiratime,$1,barrier=0/' /etc/fstab

# point sh to bash instead of dash
rm /bin/sh
ln -s /bin/bash /bin/sh

# add normal apt source list
(
cat <<'EOP'
deb http://192.168.0.79/ubuntu/ precise main restricted universe
deb http://192.168.0.79/ubuntu/ precise-security main restricted universe
deb http://192.168.0.79/ubuntu/ precise-updates main restricted universe
EOP
) > /etc/apt/sources.list
apt-get update
apt-get upgrade -y

# install some additional packages
# apt-get install -y xenstore-utils

# set up xenserver automation scripts
# AUTOMATER_REPO=https://raw.github.com/krobertson/xenserver-automater
# curl $AUTOMATER_REPO/master/usr/sbin/xe-set-hostname > /usr/sbin/xe-set-hostname
# curl $AUTOMATER_REPO/master/usr/sbin/xe-set-network > /usr/sbin/xe-set-network
# curl $AUTOMATER_REPO/master/usr/sbin/generate-sshd-keys > /usr/sbin/generate-sshd-keys
# curl $AUTOMATER_REPO/master/etc/init/xe-automator.conf > /etc/init/xe-automator.conf
# chmod a+x /usr/sbin/xe-set-hostname
# chmod a+x /usr/sbin/xe-set-network
# chmod a+x /usr/sbin/generate-sshd-keys

# setup locales
locale-gen en_US.UTF-8
update-locale LANG="en_US.UTF-8"
echo 'LANG=en_US.UTF-8' >> /etc/environment
echo 'LC_ALL=en_US.UTF-8' >> /etc/environment

# install xe tools
# cd /tmp
# wget http://some/url/to/xe-guest-utilities_6.0.0-743_amd64.deb
# dpkg -i xe-guest-utilities_6.0.0-743_amd64.deb

# install paravirt kernel image
apt-get install -f -y linux-virtual
dpkg -l | grep generic | grep linux | awk '{print $2}' | xargs apt-get remove -y

# clean up some stuff
rm -f /etc/ssh/ssh_host_*
rm -f /var/cache/apt/archives/*.deb
rm -f /var/cache/apt/*cache.bin
rm -f /var/lib/apt/lists/*_Packages

A Known Bug

The initrd.gz file and vmlinuz file should be picked from the installation iso. While the spacewalk uses a CentOS based initrd.gz file for starting the system. thus you will met “cdrom” error.

$ ls -l /var/lib/tftpboot/images/precise-ia32:1:SpacewalkDefaultOrganization/
total 31788
-r--r--r-- 1 root root 18434355 Aug 20 11:36 initrd.gz
-r--r--r-- 3 root root  8188836 Aug  7  2014 initrd.gz.back
-r--r--r-- 3 root root  5919280 Jul 29  2014 vmlinuz

Auto Configure SpaceWalk Ubuntu Agent

Create the Auto-Deployment Material

Directory structure is listed as following:

adminubuntu@bee3:~/Code$ ls -F
deb/  deb_osad/  deb_remote/  first.sh*  second.sh*  sources.list  xmlrpclib.py
adminubuntu@bee3:~/Code$ ls *
autoChange.sh  autoChange.sh~  hostname  sources.list  xmlrpclib.py

deb:
apt-transport-spacewalk_1.0.6-4.1_all.deb  python-rhn_2.5.55-2_all.deb         rhnsd_5.0.4-3_i386.deb
python-ethtool_0.11-2_i386.deb             rhn-client-tools_1.8.26-4_i386.deb

deb_osad:
osad_5.11.27-1ubuntu1~precise5_all.deb  pyjabber_0.5.0-1.4ubuntu3~precise1_all.deb

deb_remote:
rhncfg_5.10.14-1ubuntu1~precise2_all.deb

The deb files in deb should be compiled manually:

apt-get install debhelper build-essential gcc devscripts git intltool quilt automake python-all-dev libnl-route-3-dev asciidoc pkg-config libxml2-utils docbook-xml xsltproc sgml-data docbook-xsl
apt-get -f install

git clone git://anonscm.debian.org/collab-maint/spacewalk/rhnlib.git
git clone git://anonscm.debian.org/collab-maint/spacewalk/rhn-client-tools.git rhn-client-tools
git clone git://anonscm.debian.org/collab-maint/spacewalk/python-ethtool.git python-ethtool
git clone git://anonscm.debian.org/collab-maint/spacewalk/rhnsd.git rhnsd
git clone git://anonscm.debian.org/collab-maint/spacewalk/apt-spacewalk.git

# now in every of these directories do:
debuild -i -us -uc -b

Automatically Registration

The first.sh listed as following:

#!/bin/sh
### 1. TODO.
# You must be root, so input the root priviledge check here. 

### 2. Check Parameter to make sure you have inputs. 
if [ $# != 1 ]
then
  echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
  echo "!!         Parameters error       !!"
  echo "!! Example: ./createvm.sh name    !!"
  echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
  exit 1
fi

### 3. Change the hostname of the system
#echo $1
echo $1>/etc/hostname

### 4. Modify the hostname listed in /etc/hosts.
# The default name is always "packer-ubuntu-1204-server-i386", use sed for replacing it.
sed -i "s/packer-ubuntu-1204-server-i386/$1/g" /etc/hosts
echo "10.11.11.3 spacewalk">>/etc/hosts
reboot

The second.sh listed as following:

### 5. Change the Repository, and install the packages. 
cp ./sources.list /etc/apt/
apt-get update
cd deb/
dpkg -i *.deb
apt-get -f install
cd ../

### 6. Apply the bug-fix for osad. 
cp ./xmlrpclib.py /usr/lib/python2.7/xmlrpclib.py

### 7. Registration of the system. 
mkdir /var/lock/subsys
apt-get install python-libxml2
rhnreg_ks --activationkey=1-precise-ia32 --serverUrl=http://spacewalk/XMLRPC

### 8. Update Repository for using SpaceWalk. 
echo 'deb spacewalk://spacewalk/XMLRPC channels: precise-main precise-updates' > /etc/apt/sources.list.d/spacewalk.list
mv /etc/apt/sources.list /etc/apt/sources.list.back
apt-get update && apt-get update

### 9. Enable "push" to registed client. 
# wget https://launchpad.net/~mj-casalogic/+archive/ubuntu/spacewalk-ubuntu/+files/pyjabber_0.5.0-1.4ubuntu3~precise1_all.deb
# wget https://launchpad.net/~mj-casalogic/+archive/ubuntu/spacewalk-ubuntu/+files/osad_5.11.27-1ubuntu1~precise5_all.deb
cd deb_osad/
dpkg -i *.deb
cd /usr/share/rhn
wget http://spacewalk/pub/RHN-ORG-TRUSTED-SSL-CERT
service osad start
update-rc.d osad defaults


### 10. Enable Remote Command
cd /home/adminubuntu/Code/deb_remote/
dpkg -i *.deb
# this is important and touches the file /etc/sysconfig/rhn/allowed-actions/script/run
rhn-actions-control --enable-all
# Now create the missing directory, the command script gets placed there temporarily
mkdir /var/spool/rhn/


### 99. Finally , reboot the system.

Run the scripts then you could easily got system registed into the spacewalk server.

Preseed Partition Configuration

Regular Partition

Use regular configuration of the partion:

d-i partman-auto/disk string /dev/vda
d-i partman-auto/method string regular
d-i partman-auto/choose_recipe \
       select All files in one partition (recommended for new users)
d-i partman/confirm_write_new_label boolean true
#
d-i partman/choose_partition \
       select Finish partitioning and write changes to disk
d-i partman/confirm boolean true
d-i partman/confirm_nooverwrite boolean true

Build nbd Kernel Module On CentOS7

Get Source Code

First check your kernel version via:

$ uname -r
3.10.0-229.7.2.el7.x86_64

Then find the corresponding kernel source rpm under vault.centos.org, download its rpm and install it.

$ wget http://vault.centos.org/7.1.1503/updates/Source/SPackages/kernel-3.10.0-229.7.2.el7.src.rpm
# useradd builder
# groupadd builder
$ rpm -ivh kernel-3.10.0-229.7.2.el7.src.rpm

Build Preparation

As a normal user, do following:

$ mkdir -p ~/rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS}
$ echo '%_topdir %(echo $HOME)/rpmbuild' > ~/.rpmmacros
$ cd ~/rpmbuild/SPECS
$ rpmbuild -bp --target=$(uname -m) kernel.spec
$ cd ~/rpmbuild/BUILD/kernel-3.10.0-229.7.2.el7/linux-3.10.0-229.7.2.el7.centos.x86_64/
$ ls
arch     COPYING  Documentation  fs       ipc      kernel       Makefile  README          scripts   tools
block    CREDITS  drivers        include  Kbuild   lib          mm        REPORTING-BUGS  security  usr
configs  crypto   firmware       init     Kconfig  MAINTAINERS  net       samples         sound     virt

Now the source code tree is available.

Build

In the kernel source directory, type make menuconfig for configurating the kernel configuration, select :

Device Driver -> Block devices -> Set “M” On “Network block device support”

Save the configuration and exit, now begin to make via:

$ make prepare && make modules_prepare && make

Now makeout the kernel module and copy it to modules directory:

$ make M=drivers/block -j8
$ modinfo drivers/block/nbd.ko
$ sudo cp drivers/block/nbd.ko /lib/modules/3.10.0-229.7.2.el7.x86_64/extra/
$ sudo depmod -a && sudo modprobe nbd

Finally met some problems of nbd, don’t know why, later will debug on.

Automatically Create Virtual Machine

Just record the whole scripts for create/define/start the vm machine.

#!/bin/bash
# $1: The name of the virtual machine. 

### 1. Check Input Parameters. 
if [ $# != 1 ]
then
  echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
  echo "!!         Parameters error       !!"
  echo "!! Example: ./createvm.sh name    !!"
  echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
  exit 1
fi

### 2. Create the qcow2 file in local directory. 
echo $1
qemu-img create -f qcow2 -b /home/juju/img/WolfHunter/SpaceWalk/Base/packer-ubuntu-1204-server-i386 $1.qcow2

### 3. Generate Random MAC Address.
printf -v macaddr "52:54:%02x:%02x:%02x:%02x" $(( $RANDOM & 0xff)) $(( $RANDOM & 0xff )) $(( $RANDOM & 0xff)) $(( $RANDOM & 0xff ))
# echo $macaddr
### 3.1 Check whether this MAC Address is in the host's existing items. 
declare maclist

### 3.2 Get the MAC Address List
for vm in $(virsh list --all | tail -n +3 |  awk '{print $2}')
do
        # Multiple NIC need this. 
    for mac_item in $(virsh dumpxml $vm | grep "mac address" | awk -F "'" '{print $2}')
                do
                        # echo $mac_item
                        maclist+=($mac_item)
                done
done

# echo ${maclist[*]} 
# echo ${#maclist[@]}

### 3.3 Processing possible duplicated problem. 
### We don't hope the generate random mac address to be duplicated, so write whatever you want to see under the while loop for debugging
### Judge if the generated random mac address duplicated. 
while [[ ${maclist[*]} =~ ${macaddr} ]]
do
        echo "yes, you entered while for changing the mac address!"
        printf -v macaddr "52:54:%02x:%02x:%02x:%02x" $(( $RANDOM & 0xff)) $(( $RANDOM & 0xff )) $(( $RANDOM & 0xff)) $(( $RANDOM & 0xff ))
        echo $macaddr
done

### 3.4 Only for debugging purpose, view result
### For debugging purpose
### if [[ ${maclist[*]} =~ ${macaddr} ]]
### then
###     echo "Yes, it's in the existing mac list"
### else
###     echo "Congratulations, it's safe to use this mac!"
### fi
### echo $macaddr

### 4. Create new xml file and modify the xml file.
### 4.1 Create the file
mkdir -p xml
cp ./Template.xml ./xml/$1.xml
### 4.2 Remove the uuid(Virt-manager will automatically generate it)
sed -i '/uuid/d' ./xml/$1.xml
### 4.3 Replace the MAC Address
sed -i -r "s/(.*)([a-zA-Z0-9]{2}:[a-zA-Z0-9]{2}:[a-zA-Z0-9]{2}:[a-zA-Z0-9]{2}:[a-zA-Z0-9]{2}:[a-zA-Z0-9]{2})(.*)/\1$macaddr\3/g" ./xml/$1.xml 
### 4.4 Replace the virt disk file
imagepath=($PWD/$1.qcow2)
echo $imagepath
sed -i "s#<source file.*#<source file='$imagepath'/>#g"  ./xml/$1.xml

### 5. Change the VM Name
sed -i "s#<name.*#<name>$1</name>#g" ./xml/$1.xml

### 6. Define and Start VM Machine
virsh define $PWD/xml/$1.xml
virsh start $1

Steps for create the machine:

# ./createvm.sh zz_bee1