From 79bfd839d2eb21276539476b544d680be9cfade7 Mon Sep 17 00:00:00 2001 From: fanta Date: Sun, 20 Aug 2023 13:22:25 +0200 Subject: [PATCH] cambios en el readme y script python --- README.md | 58 ++----------------------------------- q.py | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 55 deletions(-) create mode 100644 q.py diff --git a/README.md b/README.md index 85a9d19..fe23122 100644 --- a/README.md +++ b/README.md @@ -6,63 +6,11 @@
 $ git clone http://git.56k.es/fanta/q ; cd q
-$ chmod +x q
-# cp -pRv q /bin/q
+$ chmod +x q.py
+# cp -pRv q.py /bin/q
 
**Dependencies:** - qemu-system-x86_64 -- qemu-system-i386 -- qemu-system-sparc -- wget - - -**Run first time:** - -The first time we run "q" it will download the isos from a large list of different GNU+Linux distributions. -It may take a long time to download the files. Please be patient. -Note: Right now u can choice the iso to download. - -run script first time - - -**Example: Install and run a Rocky Linux 8.6 distro** - -Install virtual machine: -
-$ cd ~/vms/rocky-8.6-x86_64
-$ bash install.sh
-
- -Start virtual machine: -
-$ bash start.sh
-
- -Actual distro list: -
-     1	openSUSELeap 15.4
-     2	RHEL 5.11
-     3	RHEL 6.10
-     4	RHEL 7.9
-     5	RHEL 8.6
-     6	Rocky 8.5
-     7	Rocky 8.6
-     8	Debian 9.13.0
-     9	Debian 10.12.0
-    10	Debian 11.4.0
-    11	RouterOS 7.3.1
-    12	AlmaLinux 8.6
-    13	AlmaLinux 9.0
-    14	Centos 5.11
-    15	Centos 6.10
-    16	Centos 7.9.2009
-    17	Ubuntu 20.04.4
-    18	Ubuntu 22.04.1
-    19	Mint 21
-
- -Enjoy ! - -Kind regards. +- libguestfs-tools diff --git a/q.py b/q.py new file mode 100644 index 0000000..fe54412 --- /dev/null +++ b/q.py @@ -0,0 +1,87 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# Debian Lab - Máquinas virtuales con Debian levantadas rápidamente + +import os +import requests +import shutil + +dirVms=os.path.expanduser('~')+"/vms" +dirVmsTemp=dirVms+"/templates" +dirVmsImgs=dirVms+"/images" +cloudImgs=["https://cloud.debian.org/images/cloud/buster/latest/debian-10-nocloud-amd64.qcow2", "https://cloud.debian.org/images/cloud/bullseye/latest/debian-11-nocloud-amd64.qcow2", "https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-nocloud-amd64.qcow2"] +vmMemory=4096 # En Megas + +def main(): + createDirs() # Comprobamos si existen los directorios y si no es así se crean. + downloadTemplates() # Comprobamos si ya están bajadas las imagenes cloud y si no es así se bajan. + choice() # Elegir entre Crear una nueva máquina virtual, ejecutar una ya existente o borrarla. + +def createDirs(): + if not os.path.exists(dirVms): + os.makedirs(dirVms) + os.makedirs(dirVmsTemp) + os.makedirs(dirVmsImgs) + +def downloadTemplates(): + qcowTemplates = os.listdir(dirVmsTemp) + if len(qcowTemplates) == 0: + for i in range(len(cloudImgs)): + localFilename = cloudImgs[i].split('/')[-1] + session = requests.Session() + response = session.get(cloudImgs[i],allow_redirects=True,stream=True) + if response.status_code == 200: + print("[+] Downloading \t"+cloudImgs[i]+"\t[200]") + downloadQcow = session.get(cloudImgs[i], allow_redirects=True) + open(dirVmsTemp+"/"+localFilename, 'wb').write(downloadQcow.content) + +def choice(): + print("[1] New VM\n[2] Run VM\n[3] Del VM\n[4] Exit") + option=input("\nOption: ") + if option == "1": createVms() + if option == "2": runVms() + if option == "3": delVms() + if option == "4": return 0 + + +def createVms(): + qcowFiles = os.listdir(dirVmsTemp) + qcowFiles.sort() + vmName=input("\nVM Name: ") + print("\nSeleccione una versión de Debian\n") + for i in range(len(qcowFiles)): + print("["+str(i)+"] - Debian "+qcowFiles[i].split('-')[-3]+" ("+qcowFiles[i].split('-')[-1]+")") + vmVersion=input("\nVersion: ") + shutil.copyfile(dirVmsTemp+"/"+qcowFiles[int(vmVersion)], dirVmsImgs+"/"+vmName+".qcow2") + print("\nTamaño del disco en Gigas") + vmSize=input("\nSize: ") + os.system("qemu-img resize "+dirVmsImgs+"/"+vmName+".qcow2 "+vmSize+"G") + print("\nPassword de root") + vmPasswd=input("\nPassword: ") + os.system("virt-customize -a "+dirVmsImgs+"/"+vmName+".qcow2 --root-password password:"+vmPasswd) + os.system("virt-customize -a "+dirVmsImgs+"/"+vmName+".qcow2 --hostname "+vmName) + choice() + +def runVms(): + qcowFiles = os.listdir(dirVmsImgs) + qcowFiles.sort() + if len(qcowFiles) != 0: + print("\nSeleccione una máquina para ejecutarla\n") + for i in range(len(qcowFiles)): + print("["+str(i)+"] - "+qcowFiles[i]) + vmRun=input("\nMachine: ") + os.system("qemu-system-x86_64 -machine type=q35,accel=kvm -enable-kvm -smp $(nproc) --boot c -nographic -monitor telnet::45454,server,nowait -serial mon:stdio -m "+str(vmMemory)+" -hda "+dirVmsImgs+"/"+qcowFiles[int(vmRun)]) + choice() + +def delVms(): + qcowFiles = os.listdir(dirVmsImgs) + qcowFiles.sort() + if len(qcowFiles) != 0: + print("\nSeleccione una máquina para borrarla\n") + for i in range(len(qcowFiles)): + print("["+str(i)+"] - "+qcowFiles[i]) + vmDel=input("\nMachine: ") + os.system("rm -rf "+dirVmsImgs+"/"+qcowFiles[int(vmDel)]) + choice() + +main()