diff options
| author | Your Name <you@example.com> | 2024-12-23 16:06:31 +0800 |
|---|---|---|
| committer | Your Name <you@example.com> | 2024-12-23 16:06:31 +0800 |
| commit | 456f72de0592e801213df04f042f6684ea2cc439 (patch) | |
| tree | 898a537b1390405225a3a50852f581397fa1b997 /others/vm3/compute | |
| parent | f7f159a04671690786de2f84e34046c103521d58 (diff) | |
udpate
Diffstat (limited to 'others/vm3/compute')
| -rwxr-xr-x | others/vm3/compute/create.sh | 43 |
1 files changed, 26 insertions, 17 deletions
diff --git a/others/vm3/compute/create.sh b/others/vm3/compute/create.sh index 0bb2e63..84646d7 100755 --- a/others/vm3/compute/create.sh +++ b/others/vm3/compute/create.sh | |||
| @@ -7,7 +7,8 @@ | |||
| 7 | vcpu=8 | 7 | vcpu=8 |
| 8 | ram_gb=4 | 8 | ram_gb=4 |
| 9 | disk_gb=32 | 9 | disk_gb=32 |
| 10 | os="fedora40.qcow2" | 10 | os="fedora40" |
| 11 | ostype="linux" | ||
| 11 | 12 | ||
| 12 | # parse arguments | 13 | # parse arguments |
| 13 | while [[ $# -gt 0 ]]; do | 14 | while [[ $# -gt 0 ]]; do |
| @@ -41,35 +42,39 @@ while [[ $# -gt 0 ]]; do | |||
| 41 | esac | 42 | esac |
| 42 | done | 43 | done |
| 43 | 44 | ||
| 45 | scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" #&& echo $scriptdir | ||
| 46 | workingdir="/var/lib/libvirt/images/.temp" | ||
| 47 | seed_iso="${workingdir}/seed.iso" | ||
| 48 | xml="${workingdir}/xml" | ||
| 49 | |||
| 50 | image_dir_path="/var/lib/libvirt/images/.image_store" | ||
| 51 | src_file="${image_dir_path}/${os}.qcow2" | ||
| 52 | new_vm_dir="/var/lib/libvirt/images/${vmname}" | ||
| 53 | new_vm="${new_vm_dir}/${vmname}.qcow2" | ||
| 54 | |||
| 44 | # Check mandatory arguments, basicaly checking for initial 1 argument. if it dont exist, vmname will be null | 55 | # Check mandatory arguments, basicaly checking for initial 1 argument. if it dont exist, vmname will be null |
| 45 | # image is os!! | 56 | # image is os!! |
| 46 | if [ -z "$vmname" ] ; then | 57 | if [ -z "$vmname" ] ; then |
| 47 | echo "Usage: $0 <vm-name> [--image <os>.qcow2] [--vcpu N] [--ram N] [--disk-size N]" | 58 | echo "" |
| 59 | echo "Usage: $0 <vm-name> [--image <os>] [--vcpu N] [--ram N] [--disk-size N]" | ||
| 48 | echo "seed.iso and image file have to be present! Default os is fedora" | 60 | echo "seed.iso and image file have to be present! Default os is fedora" |
| 49 | echo "" | 61 | echo "" |
| 50 | echo "Available images:" | 62 | echo "Available images:" |
| 51 | sudo ls /var/lib/libvirt/images/.image_store | 63 | sudo ls -1 /var/lib/libvirt/images/.image_store | sed 's/\.qcow2$//' |
| 64 | echo "" | ||
| 65 | echo "Available images to download:" | ||
| 66 | sudo ls -1 "${scriptdir}/../.config/cloud-init-generator/" | sed 's/\.sh$//' | ||
| 52 | exit 1 | 67 | exit 1 |
| 53 | fi | 68 | fi |
| 54 | 69 | ||
| 55 | scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" #&& echo $scriptdir | 70 | # run the script to make the cloud init files |
| 56 | workingdir="/var/lib/libvirt/images/.temp" | 71 | sudo bash "${scriptdir}/../.config/cloud-init-generator/${os}.sh" "${vmname}" |
| 57 | seed_iso="${workingdir}/seed.iso" | ||
| 58 | xml="${workingdir}/xml" | ||
| 59 | |||
| 60 | image_dir_path="/var/lib/libvirt/images/.image_store" | ||
| 61 | src_file="${image_dir_path}/${os}" | ||
| 62 | new_vm_dir="/var/lib/libvirt/images/${vmname}" | ||
| 63 | new_vm="${new_vm_dir}/${vmname}.qcow2" | ||
| 64 | 72 | ||
| 65 | if [ ! -f "${src_file}" ]; then | 73 | if [ ! -f "${src_file}" ]; then |
| 66 | echo -e "${os} image file is cannot be found. please make it available in ${image_dir_path}" | 74 | echo -e "${os} image file is cannot be found. please make it available in ${image_dir_path}" |
| 67 | exit 1 | 75 | exit 1 |
| 68 | fi | 76 | fi |
| 69 | 77 | ||
| 70 | # run the script to make the cloud init files | ||
| 71 | sudo bash "${scriptdir}/../.config/cloud-init-files/$(basename "$os" .qcow2).sh" "${vmname}" | ||
| 72 | |||
| 73 | sudo mkdir -p $new_vm_dir | 78 | sudo mkdir -p $new_vm_dir |
| 74 | 79 | ||
| 75 | sudo cp "$src_file" "$new_vm" &> /dev/null || { echo "Failed to create a new image."; exit 1; } | 80 | sudo cp "$src_file" "$new_vm" &> /dev/null || { echo "Failed to create a new image."; exit 1; } |
| @@ -79,13 +84,17 @@ if sudo virsh list --all | awk "\$2==\"$vmname\"" | grep -q .; then | |||
| 79 | exit 1 | 84 | exit 1 |
| 80 | fi | 85 | fi |
| 81 | 86 | ||
| 87 | if [[ ${os,,} == *"freebsd"* ]]; then | ||
| 88 | ostype="generic" | ||
| 89 | fi | ||
| 90 | |||
| 82 | sudo virt-install --name $vmname \ | 91 | sudo virt-install --name $vmname \ |
| 83 | --vcpus $vcpu \ | 92 | --vcpus $vcpu \ |
| 84 | --memory "$((ram_gb * 1024))"\ | 93 | --memory "$((ram_gb * 1024))"\ |
| 85 | --disk path=$new_vm,format=qcow2 \ | 94 | --disk path=$new_vm,format=qcow2 \ |
| 86 | --disk path=$seed_iso,device=cdrom \ | 95 | --disk path=$seed_iso,device=cdrom \ |
| 87 | --os-type linux \ | 96 | --os-type $ostype \ |
| 88 | --os-variant $(basename "$os" .qcow2) \ | 97 | --os-variant $os \ |
| 89 | --virt-type kvm \ | 98 | --virt-type kvm \ |
| 90 | --graphics none \ | 99 | --graphics none \ |
| 91 | --network bridge=virbr0,model=virtio \ | 100 | --network bridge=virbr0,model=virtio \ |
