@@ -36,8 +36,8 @@ ssh_pwauth: True
36
36
37
37
function download_cloud_img_if_not_exist () {
38
38
local cloudimg=" $1 "
39
- [[ " $1 " != " focal-server-cloudimg-amd64.img" ]] && echo " ERROR: cloudimg not supported"
40
- [[ -f " base/$cloudimg " ]] || aria2c -o " base/$cloudimg " https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img || ! echo " Failed to download ubuntu cloudimg" || return $?
39
+ [[ " $1 " != " focal-server-cloudimg-amd64.img" ]] && echo2 " ERROR: cloudimg not supported"
40
+ [[ -f " base/$cloudimg " ]] || aria2c -o " base/$cloudimg " https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img || ! echo2 " Failed to download ubuntu cloudimg" || return $?
41
41
}
42
42
43
43
function create_vm_if_not_exist () {
@@ -65,7 +65,8 @@ function create_vm_if_not_exist () {
65
65
66
66
function start_vm_if_not_running () {
67
67
local name=$1
68
- local options=" $2 "
68
+ local options_txt=" $2 "
69
+ read -a options <<< " $options_txt"
69
70
70
71
# For tracking started instance
71
72
local uuid=` uuidgen --namespace @oid --name " qemu.$name " --sha1`
@@ -74,15 +75,18 @@ function start_vm_if_not_running () {
74
75
ps aux | grep -F " --uuid $uuid " | grep qemu && return 0
75
76
76
77
# start it
78
+ [[ ! -f " vm/$name /disk.img" ]] && echo2 " In start_vm, disk image vm/$name /disk.img doesn't exist. Did init_vm fail?" && return 1
77
79
nohup qemu-system-x86_64 --uuid " $uuid " -drive file=" vm/$name /disk.img" ,if=virtio -cdrom " vm/$name /initimg.iso" -cpu host --enable-kvm -bios /usr/share/edk2-ovmf/x64/OVMF.fd -net nic,model=rtl8139 " ${options[@]} " & disown
78
80
}
79
81
80
- # [[ $2 = "" ]] && echo "Temp script to create VM. Usage: $0 MY_GOOD_VM11 :11" && exit 1
81
- # create_vm_from "$1" 2 4G 50G __hardcoded__ r 1 "$2" __hardcoded__ || exit $?
82
- # echo "DEBUG: sshpass -p 1 ssh -p 30472 r@localhost"
83
-
84
82
function do_init () {
85
83
while IFS= read -r line; do
84
+ # Ignore lines starting with #
85
+ if [[ " $line " =~ ^\# ]]; then
86
+ continue
87
+ fi
88
+ # Trim leading and trailing whitespaces
89
+ line=$( echo " $line " | sed -e ' s/^[[:space:]]*//' -e ' s/[[:space:]]*$//' )
86
90
# Check if the line is non-empty
87
91
if [ -n " $line " ]; then
88
92
# Parse the line as "name;cloudimg;disk;username;password", trim space
@@ -104,27 +108,23 @@ function do_start () {
104
108
if [[ " $line " =~ ^\# ]]; then
105
109
continue
106
110
fi
107
-
108
111
# Trim leading and trailing whitespaces
109
112
line=$( echo " $line " | sed -e ' s/^[[:space:]]*//' -e ' s/[[:space:]]*$//' )
110
-
111
113
# Check if the line is non-empty
112
114
if [ -n " $line " ]; then
113
115
# Parse the line as "name;options", only trim space in name, options can contain ;
114
116
name=$( echo " $line " | sed -e ' s/[[:space:]]*;.*$//' -e ' s/^[[:space:]]*//' )
115
- options=$( echo " $line " | sed ' s/^[^:]*: //' )
117
+ options=$( echo " $line " | sed ' s/^[^;]*; //' )
116
118
117
119
# Check if the name is empty
118
120
if [ -n " $name " ]; then
119
- # Print the parsed values
120
- echo " Name: $name |"
121
- echo " Options: $options |"
122
- echo " ---------------------"
121
+ start_vm_if_not_running " $name " " $options " || echo2 " Failed to start_vm_if_not_running. $? "
123
122
else
124
- # Print an error message for empty name
125
- echo " Error: Name is empty. Skipping the line."
123
+ echo2 " Error: Bad configuration line: $line "
126
124
fi
127
125
fi
128
126
done < ../runtime.settings
129
127
}
130
128
129
+ do_init
130
+ do_start
0 commit comments