aboutsummaryrefslogtreecommitdiff
path: root/kvmx
blob: 1605bd38f24a84204eaf0604439f713ddc666ed5 (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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#!/usr/bin/env bash
#
# kvmx virtual machine manager
#
# Copyright (C) 2017 Silvio Rhatto - rhatto at riseup.net
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License,
# or any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

# Basic parameters
BASENAME="`basename $0`"
DIRNAME="`dirname $0`"
ACTION="$1"
VM="$2"
GLOBAL_USER_CONFIG_FOLDER="$HOME/.config/kvmx"

# Alias to be used in config files
KVMX_BASE="$DIRNAME"

# Run spice client
function kvmx_spice {
  if ! kvmx_running; then
    echo "$BASENAME: guest $VM is not running"
    exit 1
  fi

  # Ensure we have the right port configuration: we can also be
  # running directly from command line.
  PORT="`cat $PORTFILE`"

  if [ -z "$PORT" ]; then
    echo "$BASENAME: cannot get spice port for $VM."
    exit 1
  fi

  # https://lists.freedesktop.org/archives/spice-devel/2013-September/014643.html
  SPICE_NOGRAB=1 spicec --host localhost --port $PORT &> $SPICEFILE &
  #spicy -h localhost -p $PORT
  #remote-viewer spice://localhost:$PORT

  # Give time to boot
  sleep 5

  # Fix window titles
  if which /usr/bin/xdotool &> /dev/null; then
    xdotool search --name "SPICEc:0" set_window --name $VM
  fi
}

# Bring virtual machine up
function kvmx_up {
  if kvmx_running; then
    echo "$BASENAME: guest $VM is already running"
    exit 1
  fi

  if [ ! -z "$shared_folder" ]; then
    local shared="-fsdev local,id=shared,path=$shared_folder,security_model=none -device virtio-9p-pci,fsdev=shared,mount_tag=shared"
  fi

  if [ ! -z "$port_mapping" ]; then
    local hostfwd=",$port_mapping"
  fi

  # Check if image exists, create otherwise
  if [ ! -e "$image" ]; then
    if [ ! -z "$basebox" ]; then
      if [ -e "$GLOBAL_USER_CONFIG_FOLDER/$basebox" ]; then
        baseimage="`kvmx list_image $basebox`"

        if [ ! -e "$baseimage" ]; then
          echo "$BASENAME: could not find basebox $baseimage. Please create it first."
          exit 1
        fi

        echo "Copying base image $baseimage to $image"
        cp $baseimage $image
      fi
    else
      kvmx-create $GLOBAL_USER_CONFIG_FOLDER/$VM
    fi
  fi

  # Run virtual machine
  # See https://en.wikipedia.org/wiki/Nohup#Overcoming_hanging
  nohup kvm -m 2048 -name $VM -drive file=$image,if=virtio -vga qxl $shared \
      -spice port=$PORT,addr=127.0.0.1,disable-ticketing,streaming-video=off,jpeg-wan-compression=never,playback-compression=off,zlib-glz-wan-compression=never,image-compression=off \
      -device virtio-serial-pci \
      -device virtserialport,chardev=spicechannel0,name=com.redhat.spice.0 \
      -chardev spicevmc,id=spicechannel0,name=vdagent \
      -smp 2 -soundhw ac97 -cpu host -balloon virtio  \
      -net nic,model=virtio \
      -net user,hostfwd=tcp:127.0.0.1:$SSH-:22$hostfwd &> $LOGFILE < /dev/null &

  PID="$!"

  # Save state
  echo $PID  > $PIDFILE
  echo $PORT > $PORTFILE
  echo $SSH  > $SSHFILE

  if [ "$run_spice_client" == "1" ]; then
    kvmx_spice
  fi
}

# Display usage
function kvmx_usage {
  echo "usage: $BASENAME <action> [options]"
  echo "examples:"
  echo ""
  echo "$BASENAME list"
  echo "$BASENAME init  <machine>    [folder]"
  echo "$BASENAME clone <orig-guest> <dest-folder>"

  exit 1
}

# Log into the guest using SSH
function kvmx_ssh {
  if ! kvmx_running; then
    echo "$BASENAME: guest $VM is not running"
    exit 1
  fi

  shift 2
  SSH="`cat $SSHFILE`"
  $SSH_COMMAND -p $SSH $SSH_LOGIN@127.0.0.1 $*
}

# Suspend the virtual machine
function kvmx_suspend {
  if ! kvmx_running; then
    echo "$BASENAME: guest $VM is not running"
    exit 1
  fi

  PID="`cat $PIDFILE`"
  kill -STOP $PID
}

# Check if a guest is running
function kvmx_running {
  if [ ! -e "$PIDFILE" ]; then
    return 1
  fi

  PID="`cat $PIDFILE`"

  if [ -z "$PID" ]; then
    return 1
  fi

  ps $PID &> /dev/null

  return $?
}

# Resume the guest
function kvmx_resume {
  if ! kvmx_running; then
    echo "$BASENAME: guest $VM is not running"
    exit 1
  fi

  PID="`cat $PIDFILE`"
  kill -CONT $PID
}

# Poweroff the guest
function kvmx_poweroff {
  if ! kvmx_running; then
    echo "$BASENAME: guest $VM is not running"
    exit 1
  fi

  echo /usr/bin/sudo poweroff | kvmx_ssh &> /dev/null
  sleep 3
  kvmx_status
}

# Reboot the guest
function kvmx_reboot {
  if ! kvmx_running; then
    echo "$BASENAME: guest $VM is not running"
    exit 1
  fi

  echo /usr/bin/sudo reboot | kvmx_ssh &> /dev/null
  sleep 3
  kvmx_status
}

# Rsync files to the guest
function kvmx_rsync {
  if ! kvmx_running; then
    echo "$BASENAME: guest $VM is not running"
    exit 1
  fi

  ORIG="$3"
  DEST="$4"
  SSH="`cat $SSHFILE`"
  rsync -av -e "$SSH_COMMAND -o Port=$SSH" --rsync-path "sudo rsync" $ORIG/ $SSH_LOGIN@127.0.0.1:$DEST/
}

# List guests
function kvmx_list {
  ls $GLOBAL_USER_CONFIG_FOLDER
}

# Alias to list command
function kvmx_ls {
  kvmx_list
}

# Upgrade guest
function kvmx_upgrade {
  echo "sudo apt-get update && sudo apt-get dist-upgrade -y && sudo apt-get autoremove -y" | kvmx_ssh
}

# Initialize
function kvmx_initialize {
  if [ "$ACTION" == "init" ] || [ "$ACTION" == "list" ]; then
    return
  fi

  if [ -z "$VM" ]; then
    VM="$(basename `pwd`)"
  fi

  # Default parameters
  PORT="$(($RANDOM + 1024))"
  SSH="$(($PORT + 22))"
  SSH_COMMAND="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o LogLevel=FATAL -i $DIRNAME/ssh/insecure_private_key"
  SSH_LOGIN="user"

  # Initalize
  mkdir -p $GLOBAL_USER_CONFIG_FOLDER

  # Load and check guest config
  if [ "$ACTION" != "init" ] && [ "$ACTION" != "list" ] && [ "$ACTION" != "edit" ]; then
    if [ ! -e "$GLOBAL_USER_CONFIG_FOLDER/$VM" ]; then
      echo "$BASENAME: config not found: $GLOBAL_USER_CONFIG_FOLDER/$VM"
      exit 1
    else
      source $GLOBAL_USER_CONFIG_FOLDER/$VM
    fi

    if [ -z "$image" ]; then
      image="/var/cache/qemu/$VM/box.img"
    fi

    if [ -z "$KVMXFILE" ]; then
      KVMXFILE="/var/cache/qemu/$VM/kvmxfile"
    fi

    # Box and folder config
    STORAGE="`dirname $image`"
    STATE_DIR="$STORAGE/state/$VM"
    PIDFILE="$STATE_DIR/pid"
    PORTFILE="$STATE_DIR/port"
    SSHFILE="$STATE_DIR/ssh"
    LOGFILE="$STATE_DIR/log"
    SPICEFILE="$STATE_DIR/spice"
    mkdir -p $STATE_DIR

    if [ ! -e "$image" ] && [ "$ACTION" != "up" ] && [ "$ACTION" != "purge" ] && [ "$ACTION" != "destroy" ]; then
      echo "$BASENAME: file not found: $image"
      exit 1
    fi
  fi
}

# Initializes a new guest
function kvmx_init {
  FOLDER="$3"

  if [ -z "$FOLDER" ]; then
    if [ -z "$VM" ]; then
      VM="$(basename `pwd`)"
      FOLDER="$(dirname `pwd`)/$VM"
    else
      FOLDER="$(pwd)/$VM"
    fi
  fi

  if [ -e "$GLOBAL_USER_CONFIG_FOLDER/$VM" ]; then
    echo "$BASENAME: guest $VM already exists"
    exit 1
  fi

  if [ ! -d "$FOLDER" ]; then
    mkdir -p $FOLDER
  fi

  # Ensure we have an absolute folder name
  FOLDER="`cd $FOLDER &> /dev/null && pwd`"

  # Copy config from template
  if [ ! -e "$FOLDER/kvmxfile" ]; then
    cp $DIRNAME/kvmxfile $FOLDER/
    sed -i -e "s|hostname=\"machine\"|hostname=\"$VM\"|g" $FOLDER/kvmxfile
  fi

  # Create config entry
  ( cd $GLOBAL_USER_CONFIG_FOLDER && ln -s $FOLDER/kvmxfile $VM )
}

# Clone a guest
function kvmx_clone {
  if kvmx_running; then
    echo "$BASENAME: orig $VM is running, cannot clone."
    exit 1
  fi

  FOLDER="$3"
  DEST="`basename $FOLDER`"

  if [ -z "$FOLDER" ]; then
    kvmx_usage
  fi

  # Check if dest machine exists
  if [ -e "$GLOBAL_USER_CONFIG_FOLDER/$DEST" ]; then
    echo "$BASENAME: destination guest $DEST already exists."
    exit 1
  fi

  if [ -d "$FOLDER" ]; then
    echo "$BASENAME: destination $FOLDER already exists."
    exit 1
  fi

  # Ensure we have an absolute folder name
  mkdir -p $FOLDER
  FOLDER="`cd $FOLDER &> /dev/null && pwd`"
  rmdir $FOLDER

  # Copy image and configuration
  cp -r `dirname $image` $FOLDER/
  ( cd $GLOBAL_USER_CONFIG_FOLDER && ln -s $FOLDER/kvmxfile $DEST )

  # Update config file
  new_image="$FOLDER/`basename $image`"
  sed -i -e "s|image=\"$image\"|image=\"$new_image\"|g" $GLOBAL_USER_CONFIG_FOLDER/$DEST
  sed -i -e "s|hostname=\"$VM\"|hostname=\"$DEST\"|g"   $GLOBAL_USER_CONFIG_FOLDER/$DEST
}

# Edit guest config
function kvmx_edit {
  if [ -z "$EDITOR" ]; then
    EDITOR="vi"
  fi

  if [ -e "$GLOBAL_USER_CONFIG_FOLDER/$VM" ]; then
    $EDITOR $GLOBAL_USER_CONFIG_FOLDER/$VM
  else
    echo "$BASENAME: $GLOBAL_USER_CONFIG_FOLDER/$VM: file not found."
  fi
}

# Stop a guest
function kvmx_stop {
  if kvmx_running; then
    PID="`cat $PIDFILE`"
    kill $PID
  fi
}

# Destroy a guest
function kvmx_destroy {
  kvmx_stop

  rm -f  $image
  rm -rf $STATE_DIR

  echo "$BASENAME: removed image and state files, but not the whole`dirname $image` folder."
}

# Purge a guest and all its configuration
function kvmx_purge {
  kvmx_destroy
  rm -f $GLOBAL_USER_CONFIG_FOLDER/$VM
  echo "$BASENAME: removed $GLOBAL_USER_CONFIG_FOLDER/$VM config."
}

# Provision a machine
function kvmx_provision {
  if ! kvmx_running; then
    echo "$BASENAME: guest $VM is not running"
    exit 1
  fi

  if [ -z "$provision_command" ]; then
    echo "$BASENAME: error: parameter provision_command is not configured for $VM."
    exit 1
  fi

  if [ ! -z "$provision_rsync" ]; then
    SSH="`cat $SSHFILE`"
    ORIG="`echo $provision_rsync | cut -d ' ' -f 1`"
    DEST="`echo $provision_rsync | cut -d ' ' -f 2`"

    echo "sudo mkdir -p `dirname $DEST`" | kvmx_ssh
    rsync -av -e "$SSH_COMMAND -o Port=$SSH" --rsync-path "sudo rsync" $ORIG/ $SSH_LOGIN@127.0.0.1:$DEST/
  fi

  echo "$provision_command $hostname $domain $mirror" | kvmx_ssh
}

# Print guest image file name
function kvmx_list_image {
  echo $image
}

# Print guest status
function kvmx_status {
  if kvmx_running; then
    echo "$BASENAME: $VM guest is running"
    PID="`cat $PIDFILE`"
    ps $PID
  else
    echo "$BASENAME: $VM guest is stopped"
  fi
}

# Print guest log
function kvmx_log {
  local blank_line=""

  if [ -s "$LOGFILE" ]; then
    blank_line="1"
    echo "In $LOGFILE:"
    echo ""
    cat $LOGFILE
  fi

  if [ -s "$SPICEFILE" ]; then
    if [ ! -z "$blank_line" ]; then
      echo ""
    fi

    echo "In $SPICEFILE:"
    echo ""
    cat $SPICEFILE
  fi
}

# Dispatch
if type kvmx_$ACTION 2> /dev/null | grep -q 'function'; then
  kvmx_initialize
  kvmx_$ACTION $*
else
  kvmx_usage
fi