summaryrefslogtreecommitdiff
path: root/others/vm3/compute/create.sh
blob: 649cd86a3250a8ceff1de4fef4cea5af5ecbecda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/bin/bash

# takes in vm name, os type,  vcpu, ram, disk as argument
# takes in already generated seed iso and downloaded vm.iso file
vmname=$1
os=$2
vcpu=$3
ram_gb=$4
disk_gb=$5

#echo "$vmname $os $vcpu $ram_gb $disk_gb"


if [ $# -ne 5 ]; then
  echo "Usage: $0 <vm-name> <os> <vcpu> <ram(gb)> <disk(gb)>"
  echo "seed.iso and image file have to be present!"
  echo ""
  echo "Available images"
  sudo ls /var/lib/libvirt/images/.image_store
  exit 1
fi

scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" #&& echo $scriptdir
workingdir="/var/lib/libvirt/images/.temp"
seed_iso="${workingdir}/seed.iso"
xml="${workingdir}/xml"

image_dir_path="/var/lib/libvirt/images/.image_store"
src_file="${image_dir_path}/${os}.qcow2"
new_vm="/var/lib/libvirt/images/${vmname}.qcow2"

if [ ! -f "${src_file}" ]; then
  echo -e "${os} image file is cannot be found. please make it available in ${image_dir_path}"
  exit 1
fi

sudo cp "$src_file" "$new_vm" &> /dev/null || { echo "Failed to create a new image."; exit 1; }

if sudo virsh list --all | awk "\$2==\"$vmname\"" | grep -q .; then
  echo -e "\n$vmname already exist. Delete it before using the same name."
  exit 1
fi

sudo virt-install --name $vmname \
  --vcpus $vcpu \
  --memory "$((ram_gb * 1024))"\
  --disk path=$new_vm,format=qcow2 \
  --disk path=$seed_iso,device=cdrom \
  --os-type linux \
  --os-variant $os \
  --virt-type kvm \
  --graphics none \
  --network bridge=virbr0,model=virtio \
  --print-xml > $xml #|| { echo "Failed to print XML."; exit 1; }

sudo virsh define $xml #&> /dev/null || { echo "Failed to define the new VM."; exit 1; }

sudo qemu-img resize $new_vm +$disk_gb"G" #&> /dev/null

sudo virsh start $vmname

#sudo rm "${workingdir}"/*