aboutsummaryrefslogtreecommitdiff
path: root/vms.sh
blob: 9aedd88d863d0704eb9727dc18c9bff5f3e4700d (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/bin/bash

set -e

vms_path=$(realpath ~/vms)

# Default vm configuration
# Declare an associative array
declare -A config
config=(
    [graphic]="yes"
    [audio]="no"
    [boot]="menu=on"
    [ram]="12G"
    [cpu]="host"
    [display]="sdl,grab-mod=rctrl"
    [ports]="10022:22 8080:80"
    [audiodevices]="intel-hda hda-duplex"
    [vgadevices]="VGA,vgamem_mb=64"
    [devices]=""
    [bios_path]="/usr/share/qemu/bios.bin"
    [net]="nic"
)

# Load config from a file
load_config() {
    local file="$1" value
    while read -r value; do
        # Check if the line contains an equals sign
        if [[ $value = *?=* ]]; then
            # skip if it's a comment
            if [[ "$value" == \#* ]]; then
                continue
            fi
            local key=${value%%=*}
            config[$key]=${value#*=}
        fi
    done <"$file"
}

# Save config to a file
save_config() {
    local file="$1" key
    printf "### Default vm configuration\n" >"$file"
    for key in "${!config[@]}"; do
        printf "# %s=%s\n" "$key" "${config[$key]}" >>"$file"
    done
}

# Print the current config 
print_config() {
    local key
    for key in "${!config[@]}"; do
        printf "# %s=%s\n" "$key" "${config[$key]}"
    done
}

# Print error message to stderr
printerr() { printf "%s\n" "$*" >&2; }

# Check if a file exists
file_exists() {
    if [ ! -f "$1" ]; then
        printerr "error: file $1 not exist"
        exit 1
    fi
}

# Check if the number of parameters is correct
check_params() {
    if [ "$1" -lt "$2" ]; then
        printerr "error: wrong parameters"
        exit 1
    fi
}

# Print help information
usage() {
    printf "vms:
  vms path: %s 
  commands:
    $ vms run IMAGE_NAME
      $ vms run arch
    $ vms boot IMAGE_NAME ISO_PATH
      $ vms boot arch /path/to/arch.iso
    $ vms create IMAGE_NAME SIZE_OF_IMAGE
      $ vms create arch 50G
    $ vms list\n" "$vms_path"
    exit 1
}

# Run QEMU 
run_qemu() {

    load_config "$1"

    # Define the QEMU arguments
    local qemu_args=(
        --enable-kvm
        -bios "${config[bios_path]}"
        -boot "${config[boot]}"
        -m "${config[ram]}"
        -cpu "${config[cpu]}"
    )

    if [ -n "${config[net]}" ]; then
        qemu_args+=(-net "${config[net]}")
        local qemu_net_arg="user" ports
        for ports in ${config[ports]}; do
            IFS=':' read -ra p <<<"$ports"
            if [ ${#p[@]} != 2 ]; then
                printerr "error: wrong port: ${p[*]}"
                exit 1
            fi
            qemu_net_arg+=",hostfwd=tcp::${p[0]}-:${p[1]}"
        done

        qemu_args+=(-net "$qemu_net_arg")
    fi

    local device

    if [[ "${config[graphic]}" == "yes" ]]; then
        qemu_args+=(-display "${config[display]}")
        for device in ${config[vgadevices]}; do
            qemu_args+=(-device "${device}")
        done

        if [[ "${config[audio]}" == "yes" ]]; then
            for device in ${config[audiodevices]}; do
                qemu_args+=(-device "${device}")
            done
        fi
    else
        qemu_args+=(-nographic)
    fi

    for device in ${config[devices]}; do
        qemu_args+=(-device "${device}")
    done

    qemu-system-x86_64 "${qemu_args[@]}" "${@:2}"
}

# Create a new vm
create_new_vm() {
    qemu-img create "$vms_path/$1.img" "$2"
}

# Create the vms directory if it doesn't exists
if [ ! -d "$vms_path" ]; then
    printf "Creating vms directory in %s\n" "$vms_path"
    mkdir -p "$vms_path"
fi

# Print usage information if no arguments are provided
if [ -z "$1" ]; then
    usage
fi

# Ensure required commands are available
for cmd in qemu-img qemu-system-x86_64; do
    if ! command -v "$cmd" &>/dev/null; then
        printerr "error: $cmd is not installed"
        exit 1
    fi
done

case "$1" in
"run")
    check_params $# 2

    img_path="$vms_path/$2.img"
    conf_path="$vms_path/$2.conf"

    file_exists "$img_path"
    file_exists "$conf_path"

    img_args=(
        -drive "file=$img_path,format=raw"
    )

    run_qemu "$conf_path" "${img_args[@]}"
    ;;

"boot")
    check_params $# 3

    img_path="$vms_path/$2.img"
    conf_path="$vms_path/$2.conf"
    iso_path=$(realpath "$3")

    file_exists "$img_path"
    file_exists "$iso_path"
    file_exists "$conf_path"

    img_args=(
        -drive "file=$img_path,format=raw"
        -cdrom "$iso_path"
    )

    run_qemu "$conf_path" "${img_args[@]}"
    ;;

"create")
    check_params $# 3

    iso_path="$vms_path/$2.iso"

    create_new_vm "$2" "$3"

    save_config "$vms_path/$2.conf"

    printf "Created %s Successfully!\n" "$2"
    printf "Please modify the config file: %s \n" "$vms_path/$2.conf"
    ;;

"list")
    for img in "$vms_path"/*.img; do
        printf " - %s\n" "$(basename "$img" .img)"
    done
    ;;

"help")
    usage
    ;;

*)
    printerr "error: command $1 not found"
    usage
    ;;
esac