Skip to content

Instantly share code, notes, and snippets.

@electrocucaracha
Last active August 7, 2018 13:57
Show Gist options
  • Save electrocucaracha/4fc5d4b7a31db969f45cef70e31fdf2c to your computer and use it in GitHub Desktop.
Save electrocucaracha/4fc5d4b7a31db969f45cef70e31fdf2c to your computer and use it in GitHub Desktop.
Bash script that pull latest ClearLinux image and starts a VM thru Libvirt client
#!/bin/bash
# SPDX-license-identifier: Apache-2.0
##############################################################################
# Copyright (c) 2018
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
set -o errexit
set -o nounset
set -o pipefail
create_bash=create_box.sh
clr_version=$(curl -s https://download.clearlinux.org/latest)
IMAGE=clear-${clr_version}-kvm
clr_box="$(pwd)/clr_box"
if [[ ! -f create_box.sh ]]; then
wget https://raw.githubusercontent.com/vagrant-libvirt/vagrant-libvirt/master/tools/$create_bash
chmod +x $create_bash
fi
qemu-img convert -f raw -O qcow2 $clr_box/$IMAGE.img $clr_box/$IMAGE.qcow2
rm clearlinux.box
./$create_bash $clr_box/$IMAGE.qcow2 clearlinux.box
vagrant destroy -f
vagrant box remove clearlinux
vagrant box add clearlinux --name clearlinux
vagrant init clearlinux
vagrant up
#!/bin/bash
# SPDX-license-identifier: Apache-2.0
##############################################################################
# Copyright (c) 2018
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
set -o errexit
set -o nounset
set -o pipefail
clr_version=$(curl -s https://download.clearlinux.org/latest)
IMAGE=clear-${clr_version}-kvm.img
UEFI=OVMF.fd
filename=$IMAGE.xz
domain_name=clearlinux
network_name=clr-net
workdir=$(pwd)
clr_box="$workdir/clr_box"
mkdir -p $clr_box
pushd $clr_box
if [[ ! -f "./${IMAGE}" ]]; then
curl -O https://download.clearlinux.org/image/$filename
unxz -v $filename
fi
if [[ ! -f "./${UEFI}" ]]; then
curl -O https://download.clearlinux.org/image/$UEFI
fi
cat << DOMAIN > $workdir/$domain_name.xml
<domain type='kvm'>
<name>$domain_name</name>
<memory unit='KiB'>1048576</memory>
<vcpu placement='static'>1</vcpu>
<os>
<type arch='x86_64' machine='pc-i440fx-artful'>hvm</type>
<loader type='rom'>$clr_box/$UEFI</loader>
</os>
<features>
<acpi/>
</features>
<cpu mode='host-passthrough' check='none'/>
<clock offset='utc'/>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>destroy</on_crash>
<devices>
<emulator>/usr/bin/kvm-spice</emulator>
<disk type='file' device='disk'>
<driver name='qemu' type='raw' io='threads'/>
<source file='$clr_box/$IMAGE'/>
<target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
</disk>
<controller type='usb' index='0' model='piix3-uhci'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
</controller>
<controller type='pci' index='0' model='pci-root'/>
<input type='mouse' bus='ps2'/>
<input type='keyboard' bus='ps2'/>
<memballoon model='none'/>
<serial type='pty'>
<source path='/dev/pts/2'/>
<target port='0'/>
<alias name='serial0'/>
</serial>
<console type='pty' tty='/dev/pts/2'>
<source path='/dev/pts/2'/>
<target type='serial' port='0'/>
<alias name='serial0'/>
</console>
<interface type='network'>
<source network='${network_name}'/>
<model type='virtio'/>
</interface>
</devices>
</domain>
DOMAIN
if ! virsh net-list --name --all | grep -q ${network_name}; then
cat << NET > $workdir/${network_name}.xml
<network>
<name>${network_name}</name>
<forward mode='nat'>
<nat>
<port start='1024' end='65535'/>
</nat>
</forward>
<bridge name='clr_br0' std='off' delay='0'/>
<ip address='192.168.130.1' netmask='255.255.255.0'>
<dhcp>
<range start='192.168.130.2' end='192.168.130.254'/>
</dhcp>
</ip>
</network>
NET
virsh net-define $workdir/${network_name}.xml
virsh net-autostart ${network_name}
virsh net-start ${network_name}
fi
virsh net-list --autostart | grep -q ${network_name} || virsh net-autostart ${network_name}
virsh net-list --inactive | grep -q ${network_name} && virsh net-start ${network_name}
popd
if [[ $(virsh list --all | grep $domain_name ) ]]; then
virsh destroy $domain_name
virsh undefine $domain_name
fi
virsh define $workdir/$domain_name.xml
virsh start $domain_name
#!/bin/bash
# SPDX-license-identifier: Apache-2.0
##############################################################################
# Copyright (c) 2018
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
set -o errexit
set -o nounset
set -o pipefail
vagrant_home_dir=/home/vagrant
useradd -m -d $vagrant_home_dir -U vagrant
mkdir -p $vagrant_home_dir/.ssh/
wget -O $vagrant_home_dir/.ssh/authorized_keys https://raw.githubusercontent.com/hashicorp/vagrant/master/keys/vagrant.pub
chown -R vagrant:vagrant $vagrant_home_dir/.ssh/
chmod 0700 $vagrant_home_dir/.ssh/
chmod 600 $vagrant_home_dir/.ssh/authorized_keys
mkdir -p /etc/sudoers.d
echo "vagrant ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/vagrant
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment