Skip to content

Commit 922ef76

Browse files
Marty Sweetke4qqq
authored andcommitted
Added Linux template creation documentation
1 parent 8422633 commit 922ef76

3 files changed

Lines changed: 232 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version='1.0' encoding='utf-8' ?>
2+
<!DOCTYPE section PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
3+
<!ENTITY % BOOK_ENTITIES SYSTEM "cloudstack.ent">
4+
%BOOK_ENTITIES;
5+
]>
6+
7+
<!-- Licensed to the Apache Software Foundation (ASF) under one
8+
or more contributor license agreements. See the NOTICE file
9+
distributed with this work for additional information
10+
regarding copyright ownership. The ASF licenses this file
11+
to you under the Apache License, Version 2.0 (the
12+
"License"); you may not use this file except in compliance
13+
with the License. You may obtain a copy of the License at
14+
http://www.apache.org/licenses/LICENSE-2.0.
15+
Unless required by applicable law or agreed to in writing,
16+
software distributed under the License is distributed on an
17+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18+
KIND, either express or implied. See the License for the
19+
specific language governing permissions and limitations
20+
under the License.
21+
-->
22+
23+
<!-- Please keep the doctype and license declarations above intact in each doc file. -->
24+
<!-- Make your modifications below this line. -->
25+
26+
<section id="create-linux-template">
27+
<title>Creating a Linux Template</title>
28+
<para>Linux templates should be prepared using this documentation in order to prepare your linux VMs for template deployment. For ease of documentation, the VM which you are configuring the template on will be referred to as "Template Master". This guide currently covers legacy setups which do not take advantage of UserData and cloud-init and assumes openssh-server is installed during installation.
29+
</para>
30+
31+
<para>An overview of the procedure is as follow:</para>
32+
<orderedlist>
33+
<listitem><para>Upload your Linux ISO.</para> <para>For more information, see <xref linkend="add-iso"/>.</para></listitem>
34+
<listitem><para>Create a VM Instance with this ISO.</para><para> For more information, see <xref linkend="creating-vms"/>.</para></listitem>
35+
<listitem><para>Prepare the Linux VM</para></listitem>
36+
<listitem><para>Create a template from the VM.</para><para> For more information, see <xref linkend="create-template-from-existing-vm"/>.</para></listitem>
37+
</orderedlist>
38+
39+
<xi:include href="prepare-linux-template.xml" xmlns:xi="http://www.w3.org/2001/XInclude" />
40+
</section>
41+
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
<?xml version='1.0' encoding='utf-8' ?>
2+
<!DOCTYPE section PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
3+
<!ENTITY % BOOK_ENTITIES SYSTEM "cloudstack.ent">
4+
%BOOK_ENTITIES;
5+
]>
6+
7+
<!-- Licensed to the Apache Software Foundation (ASF) under one
8+
or more contributor license agreements. See the NOTICE file
9+
distributed with this work for additional information
10+
regarding copyright ownership. The ASF licenses this file
11+
to you under the Apache License, Version 2.0 (the
12+
"License"); you may not use this file except in compliance
13+
with the License. You may obtain a copy of the License at
14+
15+
http://www.apache.org/licenses/LICENSE-2.0
16+
17+
Unless required by applicable law or agreed to in writing,
18+
software distributed under the License is distributed on an
19+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20+
KIND, either express or implied. See the License for the
21+
specific language governing permissions and limitations
22+
under the License.
23+
-->
24+
<section id="prepare-linux-template">
25+
<title>System preparation for Linux</title>
26+
<para>The following steps will prepare a basic Linux installation for templating.</para>
27+
28+
<orderedlist>
29+
<listitem>
30+
<para><emphasis role="bold">Installation</emphasis></para>
31+
<para>It is good practice to name your VM something generic during installation, this will ensure components such as LVM do not appear unique to a machine. It is recommended that the name of "localhost" is used for installation.</para>
32+
<warning><para>For CentOS, it is necessary to take unique identification out of the interface configuration file, for this edit /etc/sysconfig/network-scripts/ifcfg-eth0 and change the content to the following.</para>
33+
<programlisting>
34+
DEVICE=eth0
35+
TYPE=Ethernet
36+
BOOTPROTO=dhcp
37+
ONBOOT=yes
38+
</programlisting>
39+
</warning>
40+
<para>The next steps updates the packages on the Template Master.</para>
41+
<itemizedlist>
42+
<listitem>
43+
<para>Ubuntu</para>
44+
<programlisting>
45+
sudo -i
46+
apt-get update
47+
apt-get upgrade -y
48+
apt-get install -y acpid ntp
49+
reboot
50+
</programlisting>
51+
</listitem>
52+
<listitem>
53+
<para>CentOS</para>
54+
<programlisting>
55+
ifup eth0
56+
yum update -y
57+
reboot
58+
</programlisting>
59+
</listitem>
60+
</itemizedlist>
61+
</listitem>
62+
<listitem>
63+
<para><emphasis role="bold">Password management</emphasis></para>
64+
<note><para>If preferred, custom users (such as ones created during the Ubuntu installation) should be removed. First ensure the root user account is enabled by giving it a password and then login as root to continue.</para>
65+
<programlisting>
66+
sudo passwd root
67+
logout
68+
</programlisting>
69+
<para>As root, remove any custom user accounts created during the installation process.</para>
70+
<programlisting>
71+
deluser myuser --remove-home
72+
</programlisting>
73+
</note>
74+
<para>See <xref linkend="add-password-management-to-templates"/> for instructions to setup the password management script, this will allow &PRODUCT; to change your root password from the web interface.</para>
75+
</listitem>
76+
<listitem>
77+
<para><emphasis role="bold">Hostname Management</emphasis></para>
78+
<para>CentOS configures the hostname by default on boot. Unfortunately Ubuntu does not have this functionality, for Ubuntu installations use the following steps.</para>
79+
<itemizedlist>
80+
<listitem>
81+
<para>Ubuntu</para>
82+
<para>The hostname of a Templated VM is set by a custom script in /etc/dhcp/dhclient-exit-hooks.d, this script first checks if the current hostname is localhost, if true, it will get the host-name, domain-name and fixed-ip from the DHCP lease file and use those values to set the hostname and append the /etc/hosts file for local hostname resolution. Once this script, or a user has changed the hostname from localhost, it will no longer adjust system files regardless of it's new hostname. The script also recreates openssh-server keys, which should have been deleted before templating (shown below). Save the following script to /etc/dhcp/dhclient-exit-hooks.d/sethostname, and adjust the permissions.
83+
</para>
84+
<programlisting>
85+
#!/bin/sh
86+
# dhclient change hostname script for Ubuntu
87+
oldhostname=$(hostname -s)
88+
if [ $oldhostname = 'localhost' ]
89+
then
90+
sleep 10 # Wait for configuration to be written to disk
91+
hostname=$(cat /var/lib/dhcp/dhclient.eth0.leases | awk ' /host-name/ { host = $3 } END { printf host } ' | sed 's/[";]//g' )
92+
fqdn="$hostname.$(cat /var/lib/dhcp/dhclient.eth0.leases | awk ' /domain-name/ { domain = $3 } END { printf domain } ' | sed 's/[";]//g')"
93+
ip=$(cat /var/lib/dhcp/dhclient.eth0.leases | awk ' /fixed-address/ { lease = $2 } END { printf lease } ' | sed 's/[";]//g')
94+
echo "cloudstack-hostname: Hostname _localhost_ detected. Changing hostname and adding hosts."
95+
echo " Hostname: $hostname \n FQDN: $fqdn \n IP: $ip"
96+
# Update /etc/hosts
97+
awk -v i="$ip" -v f="$fqdn" -v h="$hostname" "/^127/{x=1} !/^127/ &amp;&amp; x { x=0; print i,f,h; } { print $0; }" /etc/hosts > /etc/hosts.dhcp.tmp
98+
mv /etc/hosts /etc/hosts.dhcp.bak
99+
mv /etc/hosts.dhcp.tmp /etc/hosts
100+
# Rename Host
101+
echo $hostname > /etc/hostname
102+
hostname $hostname
103+
# Recreate SSH2
104+
dpkg-reconfig openssh-server
105+
fi
106+
### End of Script ###
107+
108+
chmod 774 /etc/dhcp/dhclient-exit-hooks.d/sethostname
109+
</programlisting>
110+
</listitem>
111+
</itemizedlist>
112+
113+
<warning><para>The following steps should be run when you are ready to template your Template Master. If the Template Master is rebooted during these steps you will have to run all the steps again. At the end of this process the Template Master should be shutdown and the template created in order to create and deploy the final template.</para></warning>
114+
</listitem>
115+
<listitem>
116+
<para><emphasis role="bold">Remove the udev persistent device rules</emphasis></para>
117+
<para>This step removes information unique to your Template Master such as network MAC addresses, lease files and CD block devices, the files are automatically generated on next boot.</para>
118+
<itemizedlist>
119+
<listitem>
120+
<para>Ubuntu</para>
121+
<programlisting>
122+
rm -f /etc/udev/rules.d/70*
123+
rm -f /var/lib/dhcp/dhclient.*
124+
</programlisting>
125+
</listitem>
126+
<listitem>
127+
<para>CentOS</para>
128+
<programlisting>
129+
rm -f /etc/udev/rules.d/70*
130+
rm -f /var/lib/dhclient/*
131+
</programlisting>
132+
</listitem>
133+
</itemizedlist>
134+
</listitem>
135+
<listitem>
136+
<para><emphasis role="bold">Remove SSH Keys</emphasis></para>
137+
<para>This step is to ensure all your Templated VMs do not have the same SSH keys, which would decrease the security of the machines dramatically.</para>
138+
<programlisting>
139+
rm -f /etc/ssh/*key*
140+
</programlisting>
141+
</listitem>
142+
<listitem>
143+
<para><emphasis role="bold">Cleaning log files</emphasis></para>
144+
<para>It is good practice to remove old logs from the Template Master.</para>
145+
<programlisting>
146+
cat /dev/null > /var/log/audit/audit.log 2>/dev/null
147+
cat /dev/null > /var/log/wtmp 2>/dev/null
148+
logrotate -f /etc/logrotate.conf 2>/dev/null
149+
rm -f /var/log/*-* /var/log/*.gz 2>/dev/null
150+
</programlisting>
151+
</listitem>
152+
<listitem>
153+
<para><emphasis role="bold">Setting hostname</emphasis></para>
154+
<para>In order for the Ubuntu DHCP script to function and the CentOS dhclient to set the VM hostname they both require the Template Master's hostname to be "localhost", run the following commands to change the hostname.</para>
155+
<programlisting>
156+
hostname localhost
157+
echo "localhost" > /etc/hostname
158+
</programlisting>
159+
</listitem>
160+
<listitem>
161+
<para><emphasis role="bold">Set user password to expire</emphasis></para>
162+
<para>This step forces the user to change the password of the VM after the template has been deployed.</para>
163+
<programlisting>
164+
passwd --expire root
165+
</programlisting>
166+
</listitem>
167+
<listitem>
168+
<para><emphasis role="bold">Clearing User History</emphasis></para>
169+
<para>The next step clears the bash commands you have just run.</para>
170+
<programlisting>
171+
history -c
172+
unset HISTFILE
173+
</programlisting>
174+
</listitem>
175+
<listitem>
176+
<para><emphasis role="bold">Shutdown the VM</emphasis></para>
177+
<para>Your now ready to shutdown your Template Master and create a template!</para>
178+
<programlisting>
179+
halt -p
180+
</programlisting>
181+
</listitem>
182+
<listitem>
183+
<para><emphasis role="bold">Create the template!</emphasis></para>
184+
<para>You are now ready to create the template, for more information see <xref linkend="create-template-from-existing-vm"/>.</para>
185+
</listitem>
186+
</orderedlist>
187+
<note><para>Templated VMs for both Ubuntu and CentOS may require a reboot after provisioning in order to pickup the hostname.</para></note>
188+
189+
190+
</section>

‎docs/en-US/working-with-templates.xml‎

100644100755
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
<xi:include href="create-template-from-snapshot.xml" xmlns:xi="http://www.w3.org/2001/XInclude" />
3737
<xi:include href="upload-template.xml" xmlns:xi="http://www.w3.org/2001/XInclude" />
3838
<xi:include href="export-template.xml" xmlns:xi="http://www.w3.org/2001/XInclude" />
39+
<xi:include href="create-linux-template.xml" xmlns:xi="http://www.w3.org/2001/XInclude" />
3940
<xi:include href="create-windows-template.xml" xmlns:xi="http://www.w3.org/2001/XInclude" />
4041
<xi:include href="import-ami.xml" xmlns:xi="http://www.w3.org/2001/XInclude" />
4142
<xi:include href="convert-hyperv-vm-to-template.xml" xmlns:xi="http://www.w3.org/2001/XInclude" />

0 commit comments

Comments
 (0)