Browse Source

how to create a vm

Signed-off-by: Achilleas Tzenetopoulos <atzenetopoulos@gmail.com>
Achilleas Tzenetopoulos 4 years ago
parent
commit
55eb5af82c
5 changed files with 131 additions and 0 deletions
  1. 22 0
      createVM/README.md
  2. 2 0
      createVM/meta-data
  3. 56 0
      createVM/script.sh
  4. 38 0
      createVM/user-data
  5. 13 0
      createVM/vars

+ 22 - 0
createVM/README.md

@@ -0,0 +1,22 @@
+# Create Virtual Machines
+
+## First some checks
+```sh
+$ lscpu
+## or ##
+$ kvm-ok
+```
+
+## Packages Installation (for ubuntu 20)
+```sh
+$ sudo apt install qemu-kvm libvirt-daemon-system libvirt-clients virtinst cpu-checker libguestfs-tools libosinfo-bin
+```
+## Configure bridge networking
+
+
+## Create the virtual machine
+
+1. Configure the `vars` file
+2. Configure the `user-data` and `meta-data` files
+3. Run the `script.sh`
+4. Map an IP to the newly added MAC address

+ 2 - 0
createVM/meta-data

@@ -0,0 +1,2 @@
+instance-id: centos8.2-k3s-03
+local-hostname: centos8.2-k3s-03

+ 56 - 0
createVM/script.sh

@@ -0,0 +1,56 @@
+#!/bin/bash
+
+SCRIPT_PATH=/home/tzenet/createVM
+
+source $(pwd)/vars
+
+# Get the cloud image
+cd /var/lib/libvirt/boot
+wget $CLOUD_IMAGE_URL
+
+mkdir -pv $DIR/$VM_NAME
+cd $DIR/$VM_NAME
+
+# Create meta-data file
+
+# Create user-data file
+# For this step you need to have an ssh key-pair
+cp ${SCRIPT_PATH}/user-data $DIR/$VM_NAME/
+cp ${SCRIPT_PATH}/meta-data $DIR/$VM_NAME/
+
+# Validate user-data for syntax errors if any
+cloud-init devel schema --config-file user-data
+
+# Copy cloud image
+cp -v /var/lib/libvirt/boot/$CLOUD_IMAGE $VM_NAME.qcow2
+
+#Create disk
+cd $DIR/$VM_NAME
+qemu-img create -f qcow2 -o preallocation=metadata $VM_NAME.new.image ${DISK_SIZE}G
+virt-resize --quiet --expand /dev/sda1 $VM_NAME.qcow2 $VM_NAME.new.image
+mv -f $VM_NAME.new.image $VM_NAME.qcow2
+
+# Create a cloud-init iso
+mkisofs -o $VM_NAME-cidata.iso -V cidata -J -r user-data meta-data
+
+# Create a pool
+virsh pool-create-as --name $VM_NAME --type dir --target $DIR/$VM_NAME
+
+# Create the VM
+cd $DIR/$VM_NAME
+virt-install --import --name $VM_NAME \
+	--ram $VM_MEMORY --vcpus $VM_VCPUS \
+	--os-type=Linux --os-variant=centos8 \
+	--disk $VM_NAME.qcow2,format=qcow2,bus=virtio \
+       	--disk $VM_NAME-cidata.iso,device=cdrom \
+	--network=bridge:br0,model=virtio --graphics none \
+	--mac $VM_MAC
+
+# Remove unwanted files
+
+cd $DIR/$VM_NAME
+virsh change-media $VM_NAME sda --eject --config
+rm -v meta-data user-data $VM_NAME-cidata.iso
+
+echo "This is the end"
+

File diff suppressed because it is too large
+ 38 - 0
createVM/user-data


+ 13 - 0
createVM/vars

@@ -0,0 +1,13 @@
+#!/bin/sh
+
+export CLOUD_IMAGE_URL=https://cloud.centos.org/centos/8/x86_64/images/CentOS-8-GenericCloud-8.2.2004-20200611.2.x86_64.qcow2
+export CLOUD_IMAGE=CentOS-8-GenericCloud-8.2.2004-20200611.2.x86_64.qcow2
+
+export VM_NAME=k3s-03
+export DIR=/var/lib/libvirt/images
+
+export DISK_SIZE=16
+export VM_MEMORY=1024
+export VM_VCPUS=2
+export OS_VARIANT=centos8
+export VM_MAC=42:42:42:42:42:03