Creating an OpenStack image

We can now create our custom OpenStack image, however, it is advisable to do so outside of our OpenStack installation. Also, you need to make sure that you do not have VirtualBox, Fusion, or similar virtualization technology, running on the system where you will be creating your image. We will be creating a KVM-based, CentOS image.

Getting ready

To begin with, ensure you are logged in to a Linux system that is not your OpenStack environment.

On Ubuntu, install the kvm/qemu and libvirt libraries:

sudo apt-get install qemu-kvm libvirt-bin virt-manager

Start the libvirt-bin service with the following command:

sudo start libvirt-bin

On CentOS or RHEL:

sudo yum groupinstall "Virtualization" "Virtualization Platform"
sudo chkconfig libvirtd on  
sudo service libvirtd start

On Fedora:

sudo yum groupinstall "Virtualization" "Virtualization Platform"
sudo systemctl enable libvirtd  
sudo systemctl start libvirtd

Ideally, you will also need a VNC client, though our example could be done without using one.

How to do it...

Carry out the following steps to create a custom image:

  1. Create a kickstart file called openstack.txt:
    install
    text
    url --url http://mirror.rackspace.com/CentOS/6.6/os/x86_64/
    lang en_US.UTF-8
    keyboard us
    network --onboot yes --bootproto dhcp --noipv6
    timezone --utc America/Chicago
    zerombr
    clearpart --all --initlabel
    bootloader --location=mbr --append="crashkernel=auto rhgb quiet"
    part / --fstype=ext4 --size=1024 --grow
    authconfig --enableshadow --passalgo=sha512
    rootpw openstack
    firewall --disable
    selinux --disabled
    skipx
    shutdown
    %packages
    @core
    openssh-server
    openssh-clients
    wget
    curl
    git
    man
    vim
    ntp
    %end
    %post
    %end 
  2. Execute the following command:
    sudo virt-install --virt-type kvm --name centos-6.6 --ram 1024 --location=http://mirror.rackspace.com/CentOS/6.6/os/x86_64/ --disk path=/tmp/centos-6.6-vm.img,size=5 --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole --os-type=linux --os-variant=rhel6 --initrd-inject=centos-6.6-x86_64-openstack.txt --extra-args="noverifyssl  console=tty0 console=ttyS0,115200 ks=file:/centos-6.6-x86_64-openstack.txt " 
    
  3. You should see something similar to the following output:
    Starting install...
    Retrieving file .treeinfo...                                                                                                                          |  728 B     00:00 ... 
    Retrieving file vmlinuz...                                                                                                                            | 7.9 MB     00:00 ... 
    Retrieving file initrd.img...                                                                                                                         |  66 MB     00:00 ... 
    Creating domain...                                                                                                                                    |    0 B     00:01     
    Domain installation still in progress. You can reconnect to the console to complete the installation process.
    
  4. Verify that the image creation was completed by using VNC. The VNC server will be running by default on host:5900:
    How to do it...

    If you do not have access to a VNC client, or for some reason cannot use it, wait for 10 minutes (a rough estimate) and proceed to the next step. We will assume things worked.

  5. List the running VMs in virsh with the following commands:
    sudo virsh list --all
     Id    Name                           State
    ----------------------------------------------------
     62    centos-6.6                     running
    
  6. Stop (destroy) the VM with the following command:
    sudo virsh destroy centos-6.6
    Domain centos-6.6 destroyed
    
  7. Start the VM with the following commands:
    sudo virsh start centos-6.6
    Domain centos-6.6 started
    
  8. Log in to the VM console as root user with the password openstack. To escape from the console session, press Ctrl + ].
    sudo virsh console centos-6.6
    Connected to domain centos-6.6
    Escape character is ^]
    

    Now, let's exit by clicking Enter:

    CentOS release 6.6 (Final)
    Kernel 2.6.32-504.el6.x86_64 on an x86_64
    
    localhost.localdomain login: root
    Password:
    
  9. In the guest, install the cloud-init package with the following commands:
    sudo yum install http://dl.fedoraproject.org/pub/epel/6Server/x86_64/epel-release-6-8.noarch.rpm
    sudo yum install cloud-init cloud-utils cloud-utils-growpart
    
  10. Change guest's cloud config file /etc/cloud/cloud.cfg with the following commands:
    rm /etc/cloud/cloud.cfg
    vi /etc/cloud/cloud.cfg
    
  11. Paste the following:
    users:
     - default
    
    disable_root: 1
    ssh_pwauth:   0
    
    locale_configfile: /etc/sysconfig/i18n
    mount_default_fields: [~, ~, 'auto', 'defaults,nofail', '0', '2']
    resize_rootfs_tmp: /dev
    ssh_deletekeys:   0
    ssh_genkeytypes:  ~
    syslog_fix_perms: ~
    
    cloud_init_modules:
     - bootcmd
     - write-files
     - resizefs
     - set_hostname
     - update_hostname
     - update_etc_hosts
     - rsyslog
     - users-groups
     - ssh
    
    cloud_config_modules:
     - mounts
     - locale
     - set-passwords
     - timezone
     - puppet
     - chef
     - salt-minion
     - mcollective
     - disable-ec2-metadata
     - runcmd
    
    cloud_final_modules:
     - rightscale_userdata
     - scripts-per-once
     - scripts-per-boot
     - scripts-per-instance
     - scripts-user
     - ssh-authkey-fingerprints
     - keys-to-console
     - phone-home
     - final-message
    
    system_info:
      distro: rhel
      default_user:
        name: centos
        lock_passwd: True
        shell: /bin/bash
        sudo: ["ALL=(ALL) NOPASSWD: ALL"]
      paths:
        cloud_dir: /var/lib/cloud
        templates_dir: /etc/cloud/templates
      ssh_svcname: sshd
  12. Make sure the guest can communicate with the metadata service with the following command:
    sudo echo "NOZEROCONF=yes" >> /etc/sysconfig/network
    
  13. Remove persistent rules with the following command:
    sudo rm -f /etc/udev/rules.d/70-persistent-net.rules
    
  14. Remove machine-specific MAC address and UUID. Edit the /etc/sysconfig/network-scripts/ifcfg-eth0 file and remove lines starting with HWADDR and UUID:
    sudo sed -i '/HWADDR/d' /etc/sysconfig/network-scripts/ifcfg-eth0
    sudo sed -i '/UUID/d' /etc/sysconfig/network-scripts/ifcfg-eth0
    
  15. After making the changes, the /etc/sysconfig/network-scripts/ifcfg-eth0 file should look like this:
    DEVICE="eth0"
    BOOTPROTO="dhcp"
    IPV6INIT="no"
    MTU="1500"
    NM_CONTROLLED="yes"
    ONBOOT="yes"
    TYPE="Ethernet"
  16. Clean up the yum, logs, temporary files, and history with the following commands:
    sudo yum clean all
    sudo rm -rf /var/log/* 
    sudo rm -rf /tmp/*
    sudo history -c
    
  17. Shutdown the guest with the following command:
    sudo shutdown -h now
    
  18. Compress the newly created image with the following command:
    sudo qemu-img convert -c /tmp/centos-6.6-vm.img -O qcow2 /tmp/centos-6.6.img
    
  19. Upload the image to Glance with the following command:
    glance image-create --name centos-6.6 --disk-format=qcow2 --container-format=bare --file /tmp/centos-6.6.img
    

How it works...

The qemu-img convert command-line tool works on multiple formats, including VMDK. Conversion to VMDK or other desired formats would work as well. Since the QCOW format supports image compression, it can be useful to get a smaller image that can grow later.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset