Difference between revisions of "Debian template creation"
m (→Preparing for and packing template cache: Info about removing DNS information) |
(Updated for Debian Etch release) |
||
Line 1: | Line 1: | ||
− | These are rough instructions of how to manually create | + | These are rough instructions of how to manually create basic Debian Etch (4.0) template cache, which can be used to create OpenVZ [[VE]]s based on Debian Etch (4.0). |
− | + | '''Notes:''' | |
− | + | * You shouldn't be running as root, but as a user that is permitted to use sudo instead. It's a dangerous idea, run as root at your peril. | |
+ | * Anywhere you see <tt>/vz</tt>, you might really need to use <tt>/var/lib/vz</tt> instead, especially on a Debian Etch host. | ||
+ | * Anywhere you see <tt>http://debian.osuosl.org/debian/</tt>, you can substitute your favorite Debian mirror. ([http://www.debian.org/mirror/list List of official Debian Mirrors]) | ||
− | |||
− | |||
− | + | == Prerequisites == | |
+ | You need to have a working copy of <tt>debootstrap</tt> running on your hardware node. | ||
− | + | For Debian: | |
− | + | sudo apt-get install debootstrap | |
− | For | + | For Gentoo: |
− | + | sudo emerge debootstrap | |
− | debootstrap | ||
− | |||
− | For | + | For other distros you might need to install it from sources, or search for an appropriate package for your distribution. An RPM is available on the [http://forum.openvz.org/index.php?t=tree&th=142&mid=584 OpenVZ Forum]. |
− | |||
− | |||
− | |||
− | + | == Bootstrapping Debian == | |
− | < | + | Download Debian Etch to a directory called "etch-temp". Specify your architecture instead of <tt>i386</tt> if you're using something other than i386/x86. For example, for AMD64/x86_64, use <tt>amd64</tt> or for ia64, use <tt>ia64</tt>. We use VE ID of 777 for this example; surely it can be any other unused ID. |
− | debootstrap --arch | + | sudo debootstrap --arch i386 etch /vz/private/777 http://debian.osuosl.org/debian/ |
− | |||
== Preparing and starting the VE == | == Preparing and starting the VE == | ||
Line 31: | Line 26: | ||
=== Setting VE config === | === Setting VE config === | ||
First, we need a config for the [[VE]]: | First, we need a config for the [[VE]]: | ||
− | + | sudo vzctl set 777 --applyconfig vps.basic --save | |
− | vzctl set 777 --applyconfig vps.basic --save | ||
− | |||
=== Setting VE OSTEMPLATE === | === Setting VE OSTEMPLATE === | ||
Also, we need <tt>OSTEMPLATE</tt> to be set in VE configuration file, for the [[vzctl]] to work properly. | Also, we need <tt>OSTEMPLATE</tt> to be set in VE configuration file, for the [[vzctl]] to work properly. | ||
− | + | sudo sh -c 'echo "OSTEMPLATE=debian-4.0" >> /etc/vz/conf/777.conf' | |
− | echo "OSTEMPLATE=debian- | ||
− | |||
=== Setting VE IP address === | === Setting VE IP address === | ||
For the [[VE]] to be able to download updates from the Internet, we need a valid IP address for it: | For the [[VE]] to be able to download updates from the Internet, we need a valid IP address for it: | ||
− | + | sudo vzctl set 777 --ipadd x.x.x.x --save | |
− | vzctl set 777 --ipadd x.x.x.x --save | ||
− | |||
{{Note|if you use private IP for the VE, you have to set up NAT as described in [[Using NAT for VE with private IPs]].}} | {{Note|if you use private IP for the VE, you have to set up NAT as described in [[Using NAT for VE with private IPs]].}} | ||
Line 52: | Line 41: | ||
=== Setting DNS server for VE === | === Setting DNS server for VE === | ||
For the [[VE]] to be able to download updates from the Internet, we also need to specify a DNS for it: | For the [[VE]] to be able to download updates from the Internet, we also need to specify a DNS for it: | ||
− | + | sudo vzctl set 777 --nameserver x.x.x.x --save | |
− | vzctl set 777 --nameserver x.x.x.x --save | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
=== Starting VE === | === Starting VE === | ||
Now start the VE: | Now start the VE: | ||
− | + | sudo vzctl start 777 | |
− | vzctl start 777 | ||
− | |||
== Customizing the installation == | == Customizing the installation == | ||
+ | A few things need to be done inside a newly created VE for it to become suitable for OpenVZ. All those things are done inside the VE, so first command is: | ||
+ | sudo vzctl enter 777 | ||
+ | export PATH=/sbin:/usr/sbin:/bin:/usr/bin | ||
− | + | {{Note|'''Warning!''' Do not run the commands below on the hardware node, they are only to be run within the VE!}} | |
− | {{Note|Do not run | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | === | + | === Set Debian repositories === |
− | < | + | cat <<EOF > /etc/apt/sources.list |
− | + | deb http://debian.osuosl.org/debian/ etch main contrib | |
− | + | deb http://security.debian.org etch/updates main contrib | |
+ | EOF | ||
=== Get new security updates === | === Get new security updates === | ||
− | + | apt-get update | |
− | apt-get update | + | apt-get upgrade |
− | apt-get upgrade | ||
− | |||
=== Install some more packages === | === Install some more packages === | ||
− | + | Installing packages could be an interactive process so the system might ask some questions. You can install more packages if you'd like. For example: | |
− | + | apt-get install ssh quota | |
− | |||
− | apt-get install ssh quota | ||
− | |||
− | === | + | === Set sane permissions for <tt>/root</tt> directory === |
− | + | chmod 700 /root | |
− | |||
− | |||
− | + | === Disable root login === | |
+ | This will disable root login by default. | ||
+ | usermod -L root | ||
=== Disable getty === | === Disable getty === | ||
Disable running <tt>getty</tt>s on terminals as a VE does not have any: | Disable running <tt>getty</tt>s on terminals as a VE does not have any: | ||
− | + | sed -i -e '/getty/d' /etc/inittab | |
− | sed -i -e '/getty/d' /etc/inittab | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
=== Disable <tt>sync()</tt> for syslog === | === Disable <tt>sync()</tt> for syslog === | ||
Turn off doing <tt>sync()</tt> on every write for <tt>syslog</tt>'s log files, to improve I/O performance: | Turn off doing <tt>sync()</tt> on every write for <tt>syslog</tt>'s log files, to improve I/O performance: | ||
− | + | sed -i -e 's@\([[:space:]]\)\(/var/log/\)@\1-\2@' /etc/syslog.conf | |
− | sed -i -e 's@\([[:space:]]\)\(/var/log/\)@\1-\2@' /etc/syslog.conf | ||
− | |||
=== Fix <tt>/etc/mtab</tt> === | === Fix <tt>/etc/mtab</tt> === | ||
− | Link <tt>/etc/mtab</tt> to <tt>/proc/mounts</tt>, so <tt>df</tt> and | + | Link <tt>/etc/mtab</tt> to <tt>/proc/mounts</tt>, so <tt>df</tt> and friends will work: |
− | + | rm -f /etc/mtab | |
− | rm -f /etc/mtab | + | ln -s /proc/mounts /etc/mtab |
− | ln -s /proc/mounts /etc/mtab | ||
− | |||
=== Remove some unneeded packages === | === Remove some unneeded packages === | ||
− | + | If you have any packages you'd like to remove, now's the time for it. Here's an example: | |
− | + | dpkg --purge modutils ppp pppoeconf pppoe pppconfig | |
− | dpkg --purge modutils | ||
− | |||
− | |||
=== Disable services === | === Disable services === | ||
Do not start some services, stick to bare minimum: | Do not start some services, stick to bare minimum: | ||
− | + | update-rc.d -f klogd remove | |
− | update-rc.d -f klogd remove | + | update-rc.d -f quotarpc remove |
− | update-rc.d -f quotarpc remove | + | update-rc.d -f exim4 remove |
− | update-rc.d -f exim4 remove | + | update-rc.d -f inetd remove |
− | update-rc.d -f inetd remove | ||
− | |||
=== Fix SSH host keys === | === Fix SSH host keys === | ||
− | SSH host keys | + | This is only useful if you installed SSH. Each individual [[VE]] should have its own pair of SSH host keys. The code below will wipe out the existing SSH keys and instruct the newly-created [[VE]] to create new SSH keys on first boot. |
− | + | rm -f /etc/ssh/ssh_host_* | |
− | rm -f /etc/ssh/ssh_host_* | + | cat << EOF > /etc/rc2.d/S15ssh_gen_host_keys |
− | cat << EOF > /etc/rc2.d/S15ssh_gen_host_keys | + | #!/bin/bash |
− | #!/bin/bash | + | ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N '' |
− | ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N '' | + | ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N '' |
− | ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N '' | + | rm -f \$0 |
− | rm -f \$0 | + | EOF |
− | EOF | + | chmod a+x /etc/rc2.d/S15ssh_gen_host_keys |
− | chmod a+x /etc/rc2.d/S15ssh_gen_host_keys | ||
− | |||
=== Clean packages === | === Clean packages === | ||
− | + | After installing packages, you'll have some junk packages laying around in your cache. Since you don't want your template to have those, this command will wipe them out. | |
− | apt-get clean | + | apt-get clean |
− | |||
− | Now everything is done. Exit from the | + | Now everything is done. Exit from the template and go back to the hardware node. |
+ | exit | ||
== Preparing for and packing template cache == | == Preparing for and packing template cache == | ||
We don't need an IP for the VE anymore, and we definitely do not need it in template cache, so remove it: | We don't need an IP for the VE anymore, and we definitely do not need it in template cache, so remove it: | ||
− | + | sudo vzctl set 777 --ipdel all --save | |
− | vzctl set 777 --ipdel all --save | + | |
− | + | Also, remove DNS server and search domain information from ''/etc/resolv.conf'' file '''in VE''': | |
− | Also, remove DNS server and search domain information from ''/etc/resolv.conf'' file '''in VE'''. | + | sudo nano /vz/private/777/etc/resolv.conf |
Stop the VE: | Stop the VE: | ||
− | + | sudo vzctl stop 777 | |
− | vzctl stop 777 | ||
− | |||
Go to the VE directory: | Go to the VE directory: | ||
− | + | cd /vz/private/777 | |
− | cd /vz/private/777 | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | Now create a cached OS tarball. In the command below, you'll want to replace <tt>i386</tt> with your architecture (i386, amd64, ia64, etc). | |
− | < | + | sudo tar -zcf /vz/template/cache/debian-4.0-i386-minimal.tar.gz . |
− | tar | ||
− | |||
Look at the resulting tarball to see its size is sane: | Look at the resulting tarball to see its size is sane: | ||
− | + | # ls -lh /vz/template/cache | |
− | # ls -lh /vz/template/cache | + | -rw-r--r-- 1 root root 51M Apr 10 03:16 debian-4.0-i386-minimal.tar.gz |
− | -rw-r--r-- 1 root root | ||
− | |||
− | |||
== Checking if template cache works == | == Checking if template cache works == | ||
+ | We can now create a VE based on the just-created template cache. Be sure to change <tt>i386</tt> to your architecture just like you did when you named the tarball above. | ||
+ | sudo vzctl create 123456 --ostemplate debian-4.0-i386-minimal | ||
− | + | Now make sure that it works: | |
− | + | sudo vzctl start 123456 | |
− | + | sudo vzctl exec 123456 ps ax | |
− | |||
− | vzctl | ||
− | |||
− | |||
− | |||
− | |||
− | vzctl | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
You should see that a few processes are running. | You should see that a few processes are running. | ||
− | == Final | + | == Final cleanup == |
− | + | Stop and remove the test VE you just created: | |
− | + | sudo vzctl stop 123456 | |
− | + | sudo vzctl destroy 123456 | |
− | vzctl stop | + | sudo rm /etc/vz/conf/123456.conf.destroyed |
− | vzctl destroy | ||
− | |||
Finally, let's remove the VE we used for OS template cache creation: | Finally, let's remove the VE we used for OS template cache creation: | ||
− | + | sudo vzctl destroy 777 | |
− | vzctl destroy 777 | + | sudo rm /etc/vz/conf/777.conf.destroyed |
− | |||
[[Category: HOWTO]] | [[Category: HOWTO]] | ||
[[Category: Templates]] | [[Category: Templates]] |
Revision as of 05:13, 10 April 2007
These are rough instructions of how to manually create basic Debian Etch (4.0) template cache, which can be used to create OpenVZ VEs based on Debian Etch (4.0).
Notes:
- You shouldn't be running as root, but as a user that is permitted to use sudo instead. It's a dangerous idea, run as root at your peril.
- Anywhere you see /vz, you might really need to use /var/lib/vz instead, especially on a Debian Etch host.
- Anywhere you see http://debian.osuosl.org/debian/, you can substitute your favorite Debian mirror. (List of official Debian Mirrors)
Contents
- 1 Prerequisites
- 2 Bootstrapping Debian
- 3 Preparing and starting the VE
- 4 Customizing the installation
- 4.1 Set Debian repositories
- 4.2 Get new security updates
- 4.3 Install some more packages
- 4.4 Set sane permissions for /root directory
- 4.5 Disable root login
- 4.6 Disable getty
- 4.7 Disable sync() for syslog
- 4.8 Fix /etc/mtab
- 4.9 Remove some unneeded packages
- 4.10 Disable services
- 4.11 Fix SSH host keys
- 4.12 Clean packages
- 5 Preparing for and packing template cache
- 6 Checking if template cache works
- 7 Final cleanup
Prerequisites
You need to have a working copy of debootstrap running on your hardware node.
For Debian:
sudo apt-get install debootstrap
For Gentoo:
sudo emerge debootstrap
For other distros you might need to install it from sources, or search for an appropriate package for your distribution. An RPM is available on the OpenVZ Forum.
Bootstrapping Debian
Download Debian Etch to a directory called "etch-temp". Specify your architecture instead of i386 if you're using something other than i386/x86. For example, for AMD64/x86_64, use amd64 or for ia64, use ia64. We use VE ID of 777 for this example; surely it can be any other unused ID.
sudo debootstrap --arch i386 etch /vz/private/777 http://debian.osuosl.org/debian/
Preparing and starting the VE
Setting VE config
First, we need a config for the VE:
sudo vzctl set 777 --applyconfig vps.basic --save
Setting VE OSTEMPLATE
Also, we need OSTEMPLATE to be set in VE configuration file, for the vzctl to work properly.
sudo sh -c 'echo "OSTEMPLATE=debian-4.0" >> /etc/vz/conf/777.conf'
Setting VE IP address
For the VE to be able to download updates from the Internet, we need a valid IP address for it:
sudo vzctl set 777 --ipadd x.x.x.x --save
Note: if you use private IP for the VE, you have to set up NAT as described in Using NAT for VE with private IPs. |
Setting DNS server for VE
For the VE to be able to download updates from the Internet, we also need to specify a DNS for it:
sudo vzctl set 777 --nameserver x.x.x.x --save
Starting VE
Now start the VE:
sudo vzctl start 777
Customizing the installation
A few things need to be done inside a newly created VE for it to become suitable for OpenVZ. All those things are done inside the VE, so first command is:
sudo vzctl enter 777 export PATH=/sbin:/usr/sbin:/bin:/usr/bin
Note: Warning! Do not run the commands below on the hardware node, they are only to be run within the VE! |
Set Debian repositories
cat <<EOF > /etc/apt/sources.list deb http://debian.osuosl.org/debian/ etch main contrib deb http://security.debian.org etch/updates main contrib EOF
Get new security updates
apt-get update apt-get upgrade
Install some more packages
Installing packages could be an interactive process so the system might ask some questions. You can install more packages if you'd like. For example:
apt-get install ssh quota
Set sane permissions for /root directory
chmod 700 /root
Disable root login
This will disable root login by default.
usermod -L root
Disable getty
Disable running gettys on terminals as a VE does not have any:
sed -i -e '/getty/d' /etc/inittab
Disable sync() for syslog
Turn off doing sync() on every write for syslog's log files, to improve I/O performance:
sed -i -e 's@\(space:\)\(/var/log/\)@\1-\2@' /etc/syslog.conf
Fix /etc/mtab
Link /etc/mtab to /proc/mounts, so df and friends will work:
rm -f /etc/mtab ln -s /proc/mounts /etc/mtab
Remove some unneeded packages
If you have any packages you'd like to remove, now's the time for it. Here's an example:
dpkg --purge modutils ppp pppoeconf pppoe pppconfig
Disable services
Do not start some services, stick to bare minimum:
update-rc.d -f klogd remove update-rc.d -f quotarpc remove update-rc.d -f exim4 remove update-rc.d -f inetd remove
Fix SSH host keys
This is only useful if you installed SSH. Each individual VE should have its own pair of SSH host keys. The code below will wipe out the existing SSH keys and instruct the newly-created VE to create new SSH keys on first boot.
rm -f /etc/ssh/ssh_host_* cat << EOF > /etc/rc2.d/S15ssh_gen_host_keys #!/bin/bash ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N rm -f \$0 EOF chmod a+x /etc/rc2.d/S15ssh_gen_host_keys
Clean packages
After installing packages, you'll have some junk packages laying around in your cache. Since you don't want your template to have those, this command will wipe them out.
apt-get clean
Now everything is done. Exit from the template and go back to the hardware node.
exit
Preparing for and packing template cache
We don't need an IP for the VE anymore, and we definitely do not need it in template cache, so remove it:
sudo vzctl set 777 --ipdel all --save
Also, remove DNS server and search domain information from /etc/resolv.conf file in VE:
sudo nano /vz/private/777/etc/resolv.conf
Stop the VE:
sudo vzctl stop 777
Go to the VE directory:
cd /vz/private/777
Now create a cached OS tarball. In the command below, you'll want to replace i386 with your architecture (i386, amd64, ia64, etc).
sudo tar -zcf /vz/template/cache/debian-4.0-i386-minimal.tar.gz .
Look at the resulting tarball to see its size is sane:
# ls -lh /vz/template/cache -rw-r--r-- 1 root root 51M Apr 10 03:16 debian-4.0-i386-minimal.tar.gz
Checking if template cache works
We can now create a VE based on the just-created template cache. Be sure to change i386 to your architecture just like you did when you named the tarball above.
sudo vzctl create 123456 --ostemplate debian-4.0-i386-minimal
Now make sure that it works:
sudo vzctl start 123456 sudo vzctl exec 123456 ps ax
You should see that a few processes are running.
Final cleanup
Stop and remove the test VE you just created:
sudo vzctl stop 123456 sudo vzctl destroy 123456 sudo rm /etc/vz/conf/123456.conf.destroyed
Finally, let's remove the VE we used for OS template cache creation:
sudo vzctl destroy 777 sudo rm /etc/vz/conf/777.conf.destroyed