0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

KVMでVMをpythonとshellから操作する

Last updated at Posted at 2023-05-26

裏ではvirshコマンドをたたくshをpythonから呼び出す構成となっている。これは、後にdiscord.pyと連携するためである。

作成

dep.sh

#!/bin/bash
cd `dirname $0`


virt-clone --original ubuntu1 --name ubuntu-$1 --file /var/lib/libvirt/images/ubuntu-$1.qcow2



virt-edit -d ubuntu-$1 /etc/netplan/00-installer-config.yaml -e "s/172.24.20.12/172.24.20.$1/"


virt-edit -d ubuntu-$1 /etc/hostname -e "s/ubuntu1/ubuntu-$1/"
virsh start ubuntu-$1

vm.py

import os,sys,time

args = sys.argv

ip1 = args[1]
ip = int(ip1)

num1 = args[2]
num = int(num1)

while num > 0:

    com = "/home/yamato/image/dep.sh " + str(ip)
    os.system(com)
    ip = ip + 1
    num = num - 1
    time.sleep(5)

print("VM作成完了")

削除

delvm.sh

#!/bin/bash

COU=1
IP=$1
VMNUM=` expr $2 + 1`

while [ "$COU" -lt "$VMNUM" ]
do
virsh destroy ubuntu-$IP
virsh autostart --disable ubuntu-$IP
virsh undefine ubuntu-$IP
virsh vol-delete --pool default ubuntu-$IP.qcow2

virsh destroy  ubuntu-k8s-master-$IP
virsh autostart --disable ubuntu-k8s-master-$IP
virsh undefine ubuntu-k8s-master-$IP
virsh vol-delete --pool default ubuntu-k8s-master-$IP.qcow2

virsh destroy ubuntu-k8s-worker-$IP
virsh autostart --disable ubuntu-k8s-worker-$IP
virsh undefine ubuntu-k8s-worker-$IP
virsh vol-delete --pool default ubuntu-k8s-worker-$IP.qcow2


rm /etc/libvirt/qemu/ubuntu-$IP.qcow2
rm /etc/libvirt/qemu/ubuntu-k8s-worker-$IP.qcow2
rm /etc/libvirt/qemu/ubuntu-k8s-master-$IP.qcow2



let IP++
let COU++
sleep 1
done

echo "VM削除完了"
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?