uncloud/uncloud/hack/vm.py

75 lines
2.6 KiB
Python
Executable File

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# 2020 Nico Schottelius (nico.schottelius at ungleich.ch)
#
# This file is part of uncloud.
#
# uncloud 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
# (at your option) any later version.
#
# uncloud 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 uncloud. If not, see <http://www.gnu.org/licenses/>.
#
#
import subprocess
import uuid
import os
from uncloud.hack.db import DB
from uncloud.hack.mac import MAC
class VM(object):
def __init__(self, config):
self.config = config
self.db = DB(config, prefix="/vm")
self.hackprefix="/home/nico/vcs/uncloud/uncloud/hack/hackcloud"
self.qemu="/usr/bin/qemu-system-x86_64"
self.accel="kvm"
self.vm = {}
self.owner="nico"
self.bridge="br100"
self.ifup = os.path.join(self.hackprefix, "ifup.sh")
self.ifdown = os.path.join(self.hackprefix, "ifdown.sh")
def create(self):
self.uuid = uuid.uuid4()
self.vm['uuid'] = str(self.uuid)
self.vm['memory'] = 1024
self.vm['cores'] = 2
self.vm['os_image'] = os.path.join(self.hackprefix, "alpine-virt-3.11.2-x86_64.iso")
self.mac=MAC().next()
self.vm['commandline' ] = [ "sudo",
"{}".format(self.qemu),
"-name", "uncloud-{}".format(self.vm['uuid']),
"-machine", "pc,accel={}".format(self.accel),
"-m", "{}".format(self.vm['memory']),
"-smp", "{}".format(self.vm['cores']),
"-uuid", "{}".format(self.vm['uuid']),
"-drive", "file={},media=cdrom".format(self.vm['os_image']),
"-netdev", "tap,id=netmain,script={},downscript={}".format(self.ifup, self.ifdown),
"-device", "virtio-net-pci,netdev=netmain,id=net0,mac={}".format(self.mac)
]
self.db.set(str(self.vm['uuid']),
self.vm,
as_json=True)
print(" ".join(self.vm['commandline']))
subprocess.run(self.vm['commandline'])