Skip to content

Instantly share code, notes, and snippets.

@leachim6
Forked from giovtorres/virt-install-centos
Created March 15, 2017 15:47
Show Gist options
  • Select an option

  • Save leachim6/72d042a5ee0fb3f2582ae7cf4216af72 to your computer and use it in GitHub Desktop.

Select an option

Save leachim6/72d042a5ee0fb3f2582ae7cf4216af72 to your computer and use it in GitHub Desktop.

Revisions

  1. @giovtorres giovtorres revised this gist Mar 13, 2017. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions virt-install-centos
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    #!/bin/bash

    ## **Updates to this file are now at https://github.com/giovtorres/virt-install-centos **

    # Take one argument from the commandline: VM name
    if ! [ $# -eq 1 ]; then
    echo "Usage: $0 <node-name>"
  2. @giovtorres giovtorres revised this gist Jan 21, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion virt-install-centos
    Original file line number Diff line number Diff line change
    @@ -125,6 +125,6 @@ _EOF_
    # Remove the unnecessary cloud init files
    rm $USER_DATA $CI_ISO

    echo "$(date -R) DONE. SSH to $1 using $IP, with username 'centos'."
    echo "$(date -R) DONE. SSH to $1 using $IP with username 'centos'."

    popd > /dev/null
  3. @giovtorres giovtorres revised this gist May 22, 2016. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions virt-install-centos
    Original file line number Diff line number Diff line change
    @@ -10,9 +10,11 @@ fi
    virsh dominfo $1 > /dev/null 2>&1
    if [ "$?" -eq 0 ]; then
    echo -n "[WARNING] $1 already exists. "
    read -p "Do you want to overwrite $1 [y/N]? " -r
    read -p "Do you want to overwrite $1 (y/[N])? " -r
    if [[ $REPLY =~ ^[Yy]$ ]]; then
    echo ""
    virsh destroy $1 > /dev/null
    virsh undefine $1 > /dev/null
    else
    echo -e "\nNot overwriting $1. Exiting..."
    exit 1
    @@ -97,11 +99,11 @@ _EOF_
    echo "[INFO] Installing with the following parameters:"
    echo "virt-install --import --name $1 --ram $MEM --vcpus $CPUS --disk
    $DISK,format=qcow2,bus=virtio --disk $CI_ISO,device=cdrom --network
    bridge=virbr0,model=virtio --os-type=linux --os-variant=rhel6 --noautoconsole"
    bridge=virbr0,model=virtio --os-type=linux --os-variant=rhel7 --noautoconsole"

    virt-install --import --name $1 --ram $MEM --vcpus $CPUS --disk \
    $DISK,format=qcow2,bus=virtio --disk $CI_ISO,device=cdrom --network \
    bridge=virbr0,model=virtio --os-type=linux --os-variant=rhel6 --noautoconsole
    bridge=virbr0,model=virtio --os-type=linux --os-variant=rhel7 --noautoconsole

    MAC=$(virsh dumpxml $1 | awk -F\' '/mac address/ {print $2}')
    while true
  4. @giovtorres giovtorres revised this gist May 14, 2016. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions virt-install-centos
    Original file line number Diff line number Diff line change
    @@ -74,8 +74,9 @@ output:
    all: ">> /var/log/cloud-init.log"
    # configure interaction with ssh server
    #ssh_deletekeys: True
    #ssh_genkeytypes: ['rsa', 'ecdsa']
    ssh_svcname: ssh
    ssh_deletekeys: True
    ssh_genkeytypes: ['rsa', 'ecdsa']
    # Install my public ssh key to the first user-defined user configured
    # in cloud.cfg in the template (which is centos for CentOS cloud images)
  5. @giovtorres giovtorres revised this gist May 14, 2016. 1 changed file with 0 additions and 3 deletions.
    3 changes: 0 additions & 3 deletions virt-install-centos
    Original file line number Diff line number Diff line change
    @@ -74,7 +74,6 @@ output:
    all: ">> /var/log/cloud-init.log"
    # configure interaction with ssh server
    #ssh_svcname: sshd
    #ssh_deletekeys: True
    #ssh_genkeytypes: ['rsa', 'ecdsa']
    @@ -99,8 +98,6 @@ _EOF_
    $DISK,format=qcow2,bus=virtio --disk $CI_ISO,device=cdrom --network
    bridge=virbr0,model=virtio --os-type=linux --os-variant=rhel6 --noautoconsole"

    exit

    virt-install --import --name $1 --ram $MEM --vcpus $CPUS --disk \
    $DISK,format=qcow2,bus=virtio --disk $CI_ISO,device=cdrom --network \
    bridge=virbr0,model=virtio --os-type=linux --os-variant=rhel6 --noautoconsole
  6. @giovtorres giovtorres revised this gist May 14, 2016. No changes.
  7. @giovtorres giovtorres created this gist May 14, 2016.
    130 changes: 130 additions & 0 deletions virt-install-centos
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,130 @@
    #!/bin/bash

    # Take one argument from the commandline: VM name
    if ! [ $# -eq 1 ]; then
    echo "Usage: $0 <node-name>"
    exit 1
    fi

    # Check if domain already exists
    virsh dominfo $1 > /dev/null 2>&1
    if [ "$?" -eq 0 ]; then
    echo -n "[WARNING] $1 already exists. "
    read -p "Do you want to overwrite $1 [y/N]? " -r
    if [[ $REPLY =~ ^[Yy]$ ]]; then
    echo ""
    else
    echo -e "\nNot overwriting $1. Exiting..."
    exit 1
    fi
    fi

    # Directory to store images
    DIR=~/virt/images

    # Location of cloud image
    IMAGE=$DIR/CentOS-7-x86_64-GenericCloud.qcow2

    # Amount of RAM in MB
    MEM=768

    # Number of virtual CPUs
    CPUS=1

    # Cloud init files
    USER_DATA=user-data
    META_DATA=meta-data
    CI_ISO=$1-cidata.iso
    DISK=$1.qcow2

    # Bridge for VMs (default on Fedora is virbr0)
    BRIDGE=virbr0

    # Start clean
    rm -rf $DIR/$1
    mkdir -p $DIR/$1

    pushd $DIR/$1 > /dev/null

    # Create log file
    touch $1.log

    echo "$(date -R) Destroying the $1 domain (if it exists)..."

    # Remove domain with the same name
    virsh destroy $1 >> $1.log 2>&1
    virsh undefine $1 >> $1.log 2>&1

    # cloud-init config: set hostname, remove cloud-init package,
    # and add ssh-key
    cat > $USER_DATA << _EOF_
    #cloud-config
    # Hostname management
    preserve_hostname: False
    hostname: $1
    fqdn: $1.example.local
    # Remove cloud-init when finished with it
    runcmd:
    - [ yum, -y, remove, cloud-init ]
    # Configure where output will go
    output:
    all: ">> /var/log/cloud-init.log"
    # configure interaction with ssh server
    #ssh_svcname: sshd
    #ssh_deletekeys: True
    #ssh_genkeytypes: ['rsa', 'ecdsa']
    # Install my public ssh key to the first user-defined user configured
    # in cloud.cfg in the template (which is centos for CentOS cloud images)
    ssh_authorized_keys:
    - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDBil2QzORhDcnKiVVNpO5daOSYVp8nshcIc7aTEkdlqCRir2Oni8BEStK7x7bvh0jrp9KptlHPeos87fQs//VXEb1FEprL2c6fPWmVdtjmYw3yzSkaFKMksL7FdUoEiwF6t8pQAg2mU0Qj9emSHBKg5ttdGqNoSvXc92k7iOzgauda7jdNak+Dx9dPhR3FJwHMcZSlQHO4cweZcK63bZitxlFkJ/FJdry/TBirDhRcXslOJ3ECU2xiyRXJVPs3VNLjMdOTTAoMmZj+GraUBbQ9VIqe683xe02sM83th5hj2C4gW3qXUoFkNLfKAMRxXLRMEwI3ABFB/AAUhACxyTJp giovanni@throwaway
    _EOF_

    echo "instance-id: $1; local-hostname: $1" > $META_DATA

    echo "$(date -R) Copying template image..."
    cp $IMAGE $DISK

    # Create CD-ROM ISO with cloud-init config
    echo "$(date -R) Generating ISO for cloud-init..."
    genisoimage -output $CI_ISO -volid cidata -joliet -r $USER_DATA $META_DATA &>> $1.log

    echo "$(date -R) Installing the domain and adjusting the configuration..."
    echo "[INFO] Installing with the following parameters:"
    echo "virt-install --import --name $1 --ram $MEM --vcpus $CPUS --disk
    $DISK,format=qcow2,bus=virtio --disk $CI_ISO,device=cdrom --network
    bridge=virbr0,model=virtio --os-type=linux --os-variant=rhel6 --noautoconsole"

    exit

    virt-install --import --name $1 --ram $MEM --vcpus $CPUS --disk \
    $DISK,format=qcow2,bus=virtio --disk $CI_ISO,device=cdrom --network \
    bridge=virbr0,model=virtio --os-type=linux --os-variant=rhel6 --noautoconsole

    MAC=$(virsh dumpxml $1 | awk -F\' '/mac address/ {print $2}')
    while true
    do
    IP=$(grep -B1 $MAC /var/lib/libvirt/dnsmasq/$BRIDGE.status | head \
    -n 1 | awk '{print $2}' | sed -e s/\"//g -e s/,//)
    if [ "$IP" = "" ]
    then
    sleep 1
    else
    break
    fi
    done

    # Eject cdrom
    echo "$(date -R) Cleaning up cloud-init..."
    virsh change-media $1 hda --eject --config >> $1.log

    # Remove the unnecessary cloud init files
    rm $USER_DATA $CI_ISO

    echo "$(date -R) DONE. SSH to $1 using $IP, with username 'centos'."

    popd > /dev/null