From c33f0979f0312e7a6d43645aa0a5d5b1481751f5 Mon Sep 17 00:00:00 2001 From: hc Date: Thu, 2 Jan 2025 09:26:32 +0800 Subject: udpated --- others/.vm.sh2.swp | Bin 16384 -> 0 bytes .../vm3/.config/cloud-init-generator/debian12.sh | 8 +++--- others/vm3/compute/create.sh | 14 ++++++----- others/vm3/compute/delete.sh | 27 --------------------- others/vm3/compute/ls.sh | 2 +- others/vm3/compute/rm.sh | 27 +++++++++++++++++++++ others/vm3/meta-data | 2 ++ others/vm3/user-data | 27 +++++++++++++++++++++ others/vm3/vm.sh | 5 ++-- 9 files changed, 73 insertions(+), 39 deletions(-) delete mode 100644 others/.vm.sh2.swp delete mode 100755 others/vm3/compute/delete.sh create mode 100755 others/vm3/compute/rm.sh create mode 100644 others/vm3/meta-data create mode 100644 others/vm3/user-data diff --git a/others/.vm.sh2.swp b/others/.vm.sh2.swp deleted file mode 100644 index bfa52a2..0000000 Binary files a/others/.vm.sh2.swp and /dev/null differ diff --git a/others/vm3/.config/cloud-init-generator/debian12.sh b/others/vm3/.config/cloud-init-generator/debian12.sh index 5e4d7fb..de0751f 100755 --- a/others/vm3/.config/cloud-init-generator/debian12.sh +++ b/others/vm3/.config/cloud-init-generator/debian12.sh @@ -40,15 +40,17 @@ else --run-command 'useradd -m -s /bin/bash user' \ $SSH_INJECT_OPTS \ --update \ - --install openssh-server,git \ + --install openssh-server,git,cloud-guest-utils,tmux \ --run-command 'echo "user ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers.d/user' \ --run-command 'sed -i "s/PermitRootLogin yes/PermitRootLogin no/" /etc/ssh/sshd_config' \ --run-command 'sed -i "s/#PasswordAuthentication yes/PasswordAuthentication no/" /etc/ssh/sshd_config' \ --run-command 'cd /home/user && git clone https://git.0nom.ch/keys && chown -R user:user keys && sudo -u user ./keys/add-ssh-keys.sh' \ - --run-command 'tee -a /etc/bashrc > /dev/null << "EOF" + --run-command 'tee -a /etc/bash.bashrc > /dev/null << "EOF" LS_COLORS=$LS_COLORS:'\''di=38;5;135:ex=00;32:'\'' ; export LS_COLORS PS1='\''[\[\033[01;32m\]\u\[\033[00m\]@\h \[\033[38;5;135m\]\W\[\033[00m\]]\$ '\'' -EOF' && \ +EOF' \ + --run-command 'echo "sudo growpart /dev/vda 1 && sudo resize2fs /dev/vda1 && source /etc/bash.bashrc" > /home/user/start.sh && chmod +x /home/user/start.sh' && \ + # Create flag file after successful customization sudo touch "$flag_file" fi diff --git a/others/vm3/compute/create.sh b/others/vm3/compute/create.sh index a1d84f6..1a0411b 100755 --- a/others/vm3/compute/create.sh +++ b/others/vm3/compute/create.sh @@ -49,8 +49,8 @@ xml="${workingdir}/xml" image_dir_path="/var/lib/libvirt/images/.image_store" src_file="${image_dir_path}/${os}.qcow2" -new_vm_dir="/var/lib/libvirt/images/${vmname}" -new_vm="${new_vm_dir}/${vmname}.qcow2" +new_vm_config_dir="/var/lib/libvirt/images/${vmname}.config" +new_vm="/var/lib/libvirt/images/${vmname}.qcow2" # Check mandatory arguments, basicaly checking for initial 1 argument. if it dont exist, vmname will be null # image is os!! @@ -75,7 +75,7 @@ if [ ! -f "${src_file}" ]; then exit 1 fi -sudo mkdir -p $new_vm_dir +sudo mkdir -p $new_vm_config_dir sudo cp "$src_file" "$new_vm" &> /dev/null || { echo "Failed to create a new image."; exit 1; } @@ -90,11 +90,12 @@ fi # Define the disk options based on OS type if [ "$os" = "debian12" ]; then - disk_opts="--disk path=$new_vm,format=qcow2" + disk_opts="--disk path=${new_vm},format=qcow2" else - disk_opts="--disk path=$new_vm,format=qcow2 --disk path=$seed_iso,device=cdrom" + disk_opts="--disk path=${new_vm},format=qcow2 --disk path=$seed_iso,device=cdrom" fi + # Use the conditional disk options in virt-install sudo virt-install --name $vmname \ --vcpus $vcpu \ @@ -105,7 +106,8 @@ sudo virt-install --name $vmname \ --virt-type kvm \ --graphics none \ --network bridge=virbr0,model=virtio \ - --print-xml > $xml || { sudo rm -rf $new_vm_dir; exit 1; } + --print-xml > $xml || { sudo rm -rf $new_vm; exit 1; } +# if you want this in a new storage pool, move it to a new storage pool after initialisation sudo virsh define $xml #&> /dev/null || { echo "Failed to define the new VM."; exit 1; } diff --git a/others/vm3/compute/delete.sh b/others/vm3/compute/delete.sh deleted file mode 100755 index dd65379..0000000 --- a/others/vm3/compute/delete.sh +++ /dev/null @@ -1,27 +0,0 @@ -#!/bin/bash - -# Check if VMs are provided -if [ $# -eq 0 ]; then - echo "Error: No VM names provided. Usage: $0 vm1 vm2 vm3" - exit 1 -fi - -# List VMs to be removed -echo "The following VMs will be permanently destroyed:" -for vm in "$@"; do - echo "- $vm" -done - -# Confirmation prompt -read -p "Are you sure you want to remove these VMs? (y/N): " confirmation -if [[ ! "$confirmation" =~ ^[Yy]$ ]]; then - echo "Operation cancelled." - exit 1 -fi - -for vm in "$@"; do - sudo virsh destroy "$vm" &>/dev/null - sudo virsh undefine "$vm" --remove-all-storage &>/dev/null - sudo rm -rf "/var/lib/libvirt/images/${vm}" &>/dev/null - printf "%-50s%10s\n" "Removing $vm..." $([[ $? -eq 0 ]] && echo "Successful" || echo "Failed") -done diff --git a/others/vm3/compute/ls.sh b/others/vm3/compute/ls.sh index b62ef14..b821fd1 100755 --- a/others/vm3/compute/ls.sh +++ b/others/vm3/compute/ls.sh @@ -27,7 +27,7 @@ for vm in $vms; do sudo virsh domstate "$vm" 2>/dev/null > /tmp/state.$$ & echo "$xml" | grep "]' '{print $3}' > /tmp/vcpus.$$ & echo "$xml" | grep "]' '{print $3}' | awk '{ printf "%.2f", $1/1048576 }' > /tmp/ram.$$ & - sudo du -sk "/var/lib/libvirt/images/${vm}/${vm}.qcow2" 2>/dev/null | awk '{ printf "%.2f", $1/1024/1024 }' > /tmp/disk.$$ & + sudo du -sk "/var/lib/libvirt/images/${vm}.qcow2" 2>/dev/null | awk '{ printf "%.2f", $1/1024/1024 }' > /tmp/disk.$$ & wait diff --git a/others/vm3/compute/rm.sh b/others/vm3/compute/rm.sh new file mode 100755 index 0000000..f77831c --- /dev/null +++ b/others/vm3/compute/rm.sh @@ -0,0 +1,27 @@ +#!/bin/bash + +# Check if VMs are provided +if [ $# -eq 0 ]; then + echo "Error: No VM names provided. Usage: $0 vm1 vm2 vm3" + exit 1 +fi + +# List VMs to be removed +echo "The following VMs will be permanently destroyed:" +for vm in "$@"; do + echo "- $vm" +done + +# Confirmation prompt +read -p "Are you sure you want to remove these VMs? (y/N): " confirmation +if [[ ! "$confirmation" =~ ^[Yy]$ ]]; then + echo "Operation cancelled." + exit 1 +fi + +for vm in "$@"; do + sudo virsh destroy "$vm" &>/dev/null + sudo virsh undefine "$vm" --remove-all-storage &>/dev/null + #sudo rm -rf "/var/lib/libvirt/images/${vm}" &>/dev/null + printf "%-50s%10s\n" "Removing $vm..." $([[ $? -eq 0 ]] && echo "Successful" || echo "Failed") +done diff --git a/others/vm3/meta-data b/others/vm3/meta-data new file mode 100644 index 0000000..ce225e1 --- /dev/null +++ b/others/vm3/meta-data @@ -0,0 +1,2 @@ +instance-id: iid-fedora-vm +local-hostname: fedora-cloudimg diff --git a/others/vm3/user-data b/others/vm3/user-data new file mode 100644 index 0000000..58817da --- /dev/null +++ b/others/vm3/user-data @@ -0,0 +1,27 @@ +#cloud-config +users: + - name: fedora + ssh-authorized-keys: + - sk-ssh-ed25519@openssh.com AAAAGnNrLXNzaC1lZDI1NTE5QG9wZW5zc2guY29tAAAAIGoBf5Lj8oDjXaXNrXtO8YVdOLgV2FN/bwiX93VxDmZ0AAAAEnNzaDptYWNtMS1yZXNpZGVudA== macm1-resident + - ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHPOwh0+Bpkhg2dNKgUWsh0FKeCeI/awegY3sMb/ZAMb x@fedora + sudo: ['ALL=(ALL) NOPASSWD:ALL'] + groups: wheel + shell: /bin/bash +runcmd: + - echo "#!/bin/bash" > /home/fedora/disk-setup.sh + - echo "DISK='/dev/vda'; MOUNT_PATH='/home/ceph5-vda-200G';" >> /home/fedora/disk-setup.sh + - echo '[ ! -d $MOUNT_PATH ] && sudo mkdir -p $MOUNT_PATH;' >> /home/fedora/disk-setup.sh + - echo 'sudo blkid | grep -q $DISK || sudo mkfs.ext4 $DISK;' >> /home/fedora/disk-setup.sh + - echo 'grep -q $DISK /etc/fstab || echo "$DISK $MOUNT_PATH ext4 defaults,nofail 0 0" | sudo tee -a /etc/fstab;' >> /home/fedora/disk-setup.sh + - echo "sudo mount -a;" >> /home/fedora/disk-setup.sh + - echo "sudo systemctl daemon-reload" >> /home/fedora/disk-setup.sh + - sudo chmod +x /home/fedora/disk-setup.sh + - sudo /home/fedora/disk-setup.sh + - sudo rm -f /home/fedora/disk-setup.sh + - sudo growpart /dev/sda 1 + - sudo xfs_growfs / + - sudo dnf install -y vim git + - cd /home/fedora + - git clone https://git.0nom.ch/setup + - sudo ./setup/setup.sh + - touch /home/fedora/runcmd_done diff --git a/others/vm3/vm.sh b/others/vm3/vm.sh index c36b0ae..a043f89 100755 --- a/others/vm3/vm.sh +++ b/others/vm3/vm.sh @@ -1,5 +1,6 @@ #!/bin/bash # Main entry point for VM management +# also negotiator for executing scripts, so for storage if [ "$(id -u)" != "0" ]; then echo "This script must be run as root" @@ -41,8 +42,8 @@ case $category in shutdown) $SCRIPT_DIR/compute/shutdown.sh "$@" ;; - delete) - $SCRIPT_DIR/compute/delete.sh "$@" + rm) + $SCRIPT_DIR/compute/rm.sh "$@" ;; *) echo -e "Available compute actions: \ncreate\nstart\nls\nshutdown\ndelete" -- cgit v1.2.3-70-g09d2