OpenStack Growth Tour-3 image creation and batch creation scripts

Source: Internet
Author: User

This article is the last of the virtual explanation, the content is not very deep, only for the back of the OpenStack to do bedding, please do not forget to spit groove ha.

Then we'll start.

Make a system-Imagebroadly divided into two ways,Virt-managerand theVirsh. Virt-manageris required to be used by the desktop, andVirshYou can complete the operation directly at the command line, forLinuxoperation and maintenance, the creation of graphical interface is difficult in the implementation of automated operation and maintenance, so here is the main explanation underVirshthe way to create a virtual machine, and self-feeling as if the graphical way of creating really nothing to say, haha.

  use vrish KVM

openstack growth tour   - &NBSP, style= font-family": ' Times New Roman '; font-size:14px; " > "  template.xml because some of the parameters in the configuration file are not explicitly defined, you need to modify it.

# Modify the UUID in the configuration file

uuid= ' Uuidgen '

s ed-i "s,%uuid%, $UUID, g" Centos-7.xml         

# Set up a virtual hard disk

Qemu-img create-f Raw Centos-7.raw 20G

Sed-i "S,%image_path%,/root/image/centos-7.raw,g" Centos-7.xml

# Modify The path of the ISO disc image

Sed-i "S,%iso_path%,/root/image/centos-7-x86_64-dvd-1511.iso,g" Centos-7.xml

# Set up a virtual network card

Mac= "fa:95:$ (dd if=/dev/urandom count=1 2>/dev/null |md5sum|sed ' s/^\ (... \)\(.. \)\(.. \)\(.. \). *$/\1:\2:\3:\4/') "

Sed-i "s,%mac%, $MAC, G" centos-7.xml

Mac2= "52:54:$ (dd if=/dev/urandom count=1 2>/dev/null |md5sum|sed ' s/^\ (... \)\(.. \)\(.. \)\(.. \). *$/\1:\2:\3:\4/') "

Sed-i "s,%mac2%, $MAC 2,g" Centos-7.xml

# Start the virtual machine

Virsh Define Centos-7.xml

# View all virtual machines

Virsh List--all

# Turn on the virtual machine

Virsh Start Centos-7-test

# VNC View virtual machine Port

Vrish Vncdisplay Centos-7-test

Virtual machine configuration file configuration, then want to bulk create virtual machine, you need to use script to implement, I have a well-written shell script, can refer to, according to their own environment to use, directly on the script.

#!/bin/bash#  Two set is a set -e          #  for debugging scripts When the script goes wrong, it stops immediately and does not follow the subsequent command set -o xtrace   #  the execution of the script, which facilitates debugging #  setting some global variables Temp_ Dir= ' mktemp '; rm -rf  $temp _fir; mkdir -p  $temp _dir  #  Create a folder to hold temporary files topdir=$ (cd $ (dirname  ")  && pwd)   #保存脚本所在路径 #  First configure the next Mac, We use configuration files to implement the settings Function create_mac_file () {    #  This function as long as it is to create a Mac configuration template      mac_temp= $TOPDIR/mac_temp    cat << eof >  $mac _temp     subsysten== "NET",  action== "Add",  drivers== "? *",  attr{address}== "%MAC%",  attr{type}== "1",  kernel= "eth*",  name= "eth0"     subsysten== "NET",  action== "Add",  drivers== "? *",  attr{address}== "%mac2%",  attr{type}== "1",  kernel= "eth*",  name= "Eth1"    &nbsp eof     #在这里是用了两个网卡, if as long as a network card, then the eth1 comment off the good, note: Different system card name,  is the same, custom Kazakhstan}#  IP configuration Template function create_net_file () {    net_temp= $TOPDIR/net_temp     cat << EOF > $net _temp    auto lo     iface lo inet loopback    auto eth0    iface  eth0 inet dhcp     auto eth1    iface  eth1 inet static   #  Here network information such as, IP address, mask, etc., should be awakened according to the actual situation set      Address 192.168.1.%i p%    netmask 255.255.255.0    gateway 192.168.1.1     broadcast 192.168.1.1    EOF    #  Here the virtual machine eth0 is based on the implementation of the Network bridge, the use of DHCP automatic acquisition of IP, do not need to configure the}#  mount/unload Qcow2 file #  Mount Function mount_qcow2_disk () {    #挂在qcow2文件的话, the function requires a parameter: QOCW2 file name &NBSP;&NBSP;&NBSP;&NBSP;MODPROBE&NBSP;NBD&NBSP;MAX_PART=63&NBSP;&NBSP;&NBSP;&NBSP;QEMU-NBD  -c /dev/nbd0  $TOPDIR/ubuntu-$1.qcow2    sleep 1     kpartx -a /dev/nbd0    sleep 1    mount /dev /mapper/nbd0p1  $temp _dir   #在这里/dev/mapper/nbd0p1 is}#  uninstall Function umount_ based on Qcow2 file Qcow2_disk () {   #卸载qcow2文件, function does not require parameters     umount  $temp _dir     qemu-nbd -d /dev/nbd0    for n in  ' Ls /dev/mapper &NBSP;|GREP&NBSP;-V&NBSP;CONTROL|GREP&NBSP;-V&NBSP;NBD ';d o         dmsetup clear /dev/mapper/$n   #清除挂载过的nbd设备          dmsetup remove /dev/mapper/$n      done    #  Note that there are two for loops that cannot be used together, write    &nbs separatelyp;for n in  ' Ls /dev/mapper |grep -v control|sort -r ';d o         dmsetup clear /dev/mapper/$n   #清除挂载过的nbd设备          dmsetup remove /dev/mapper/$n      done     rmmod nbd}#  Create and modify virtual Disks function create_image () { #  need to receive three parameters here, are: IP address, MAC address of Eth0 and MAC address of eth1     ip=$1;mac=$2;mac2=$3; Host_name=ubuntu-${ip}    qemu-img create -f qcow2 -o cluster_ size=2m,backing_file= $TOPDIR/ubuntu-16.04.raw ubuntu-$ip. Qcow2 20g    mount_ qcow2_disk  $ip      #这里提供这挂载qcow2文件的函数     mac_file= $temp _dir/etc/ udev/rules.d/70-persistent-net.rules    net_file= $temp _dir/etc/network/interfaces     cp -rf  $TOPDIR/mac_temp  $mac _file     #利用mac地址模板替换qcow2磁盘中的文件     cp -rf  $TOPDIR/net_temp $ net_file     #利用IP信息模板替换qcow2磁盘中的文件     sed -i  "s,%mac%, $mac, G "  $mac _file    #替换相应的mac地址信息     sed -i " s,%mac2%, $mac 2,g "   $mac _file    sed -i  "s,%ip%, $ip, g"   $net _file   #替换相应的IP信息     sed -i  "s,127.0.0.1 *,127.0.0.1  $HOST _name,g"   $temp _dir/etc/ hosts   #替换主机     echo  "$HOST _name"  >  $temp _dir/etc/hostname     umount_qcow2_disk     #调用函数, uninstalling Qcow2 disk} #创建及修改xml文件function   Create_xml () {    ip=$1    xml_file=ubuntu-${ip}.xml   # To facilitate automation, XML files need to be dynamically named     cp -rf  $TOPDIR/template.xml  $xml _file     #把xml的模板文件复制为本虚拟机的配置Files     sed -i  "s,%vm_name%,ubuntu-${i},g"   $xml _file   # Modify virtual machine name     uuid= ' Uuidgen '     sed -i  ' s,%uuid%, $UUID, G '   $xml _file    #修改虚拟机UUID     mac= "fa:95:$ (dd if=/dev/urandom count=1  2>/dev/null |md5sum|sed  ' s/^\ (.. \)\(.. \)\(.. \)\(.. \). *$/\1:\2:\3:\4/') "    sed -i " s,%mac%, $MAC, g "  $xml _file     mac2= "52:54:$ (dd if=/dev/urandom count=1 2>/dev/null |md5sum|sed  ' s/^\ (.. \)\(.. \)\(.. \)\(.. \). *$/\1:\2:\3:\4/') "    sed -i " s,%mac2%, $MAC 2,g "  $xml _file     create_image  $ip   $MAC   $MAC 2  #  Create and modify network information in the Qcow2 file      image_path= "${topdir}/ubuntu-${ip}.qcow2"     sed -i  "s,%image_path%, $IMAGE _path,g "  $xml _file}#  Here's how to create a virtual machine. Create_mac_ile    #创建MAC信息模板create_net_file     #创建IP信息模板for  i in 233  235 190;do      #IP地址的最后一位, define it yourself     create_xml  $i     #创建虚拟机配置文件     virsh define ubuntu-${i}.xml   #定义虚拟机     virsh start ubuntu-${i}    #启动虚拟机doneset  +o xtrace   #关闭命令执行跟踪, paired with Set -o xtrace at the beginning of the script

If you find this script has a problem, please leave a message to inform, lest astray, thank you


The introduction of virtualization is here, because the main thing is to explain OpenStack because there is not too much in-depth explanation of virtualization, then the introduction and installation of OpenStack components in detail, for example: Keystone Security certification services, Swift storage services, Glance mirror service and so on, haha, OK, wait what? , pay attention to me, the back will be more exciting.



This article is from the "11283981" blog, please be sure to keep this source http://11293981.blog.51cto.com/11283981/1792809

OpenStack Growth Tour-3 image creation and batch creation scripts

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.