OpenVZ - Create Custom Ubuntu Template

Introduction

Creating a custom OpenVZ template for a debian or ubuntu is really easy. It is simply a case of deploying a base container and putting the stuff on that you want before then zipping it all back up again under a different name. Please note that these steps are not the same for if you want to create a custom CentOS template, and this tutorial was performed on a minimal ubuntu 12.04 x86 container.

    Create Container

    To create a fresh ubuntu container, simply copy the following command into a file and execute it with bash. Feel free to update the variables appropriately, btu the VM_ID must not go below 101.
    VM_ID=101
    IP=192.168.1.1
    HOSTNAME=client1
    vzctl create $VM_ID --ostemplate ubuntu-14.04-x86_64 --conf basic --ipadd $IP --hostname $HOSTNAME
    All the templates can be found here here.
    Enter the container.
    vzctl enter $VM_ID
    Perform any actions that you want to perform to make it your own custom template. For example, do what I did and update it before installing Java 7 and downloading the minecraft server jar file. All files/changes will be kept.
    Exit the container (leave it), so that now you are in the host shell.
    exit
    Prepare the container for creating a template from:
    vzctl stop $VM_ID
    vzctl set $VM_ID --ipdel all --save
    Create the new template from the container.
    cd /vz/private/$VM_ID
    NEW_TEMPLATE_NAME=custom_template
    tar --numeric-owner -czf /vz/template/cache/$NEW_TEMPLATE_NAME.tar.gz .

    Optional Additional Steps

    Feel free to now remove the old container that you created the custom template from.
    vzctl destroy $VM_ID
    rm -f /etc/vz/conf/$VM_ID.conf.destroyed
    It may be a good idea to compare the two templates sizes:
    ls -lh /vz/template/cache/*
    Deploy from your new custom template to check that it works.
    VM_ID=101
    IP=192.168.1.1
    HOSTNAME=client1
    vzctl create $VM_ID --ostemplate $NEW_TEMPLATE_NAME --conf basic --ipadd $IP --hostname $HOSTNAME

References

No comments:

Post a Comment