From 6f49e7a01ed42cb1227e5070f5d1095342e9bc29 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 29 Dec 2024 20:10:29 +0800 Subject: debian12-support --- .../vm3/.config/cloud-init-generator/debian12.sh | 60 ++++++++++----------- others/vm3/.docs.swp | Bin 16384 -> 0 bytes others/vm3/compute/create.sh | 13 +++-- others/vm3/compute/list.sh | 45 ---------------- others/vm3/compute/ls.sh | 45 ++++++++++++++++ others/vm3/docs | 2 + others/vm3/vm.sh | 6 +-- 7 files changed, 90 insertions(+), 81 deletions(-) delete mode 100644 others/vm3/.docs.swp delete mode 100755 others/vm3/compute/list.sh create mode 100755 others/vm3/compute/ls.sh (limited to 'others/vm3') diff --git a/others/vm3/.config/cloud-init-generator/debian12.sh b/others/vm3/.config/cloud-init-generator/debian12.sh index 0bdb418..245f73c 100755 --- a/others/vm3/.config/cloud-init-generator/debian12.sh +++ b/others/vm3/.config/cloud-init-generator/debian12.sh @@ -18,36 +18,36 @@ if [ $# -ne 1 ]; then echo "Usage: $0 " exit 1 fi -sudo mkdir -p "$dir_path" +sudo mkdir -p "$dir_path" # make image store dir sudo mkdir -p "$config_dir" + [ ! -f "$src_file" ] && echo "source image does not exist! downloading..." && sudo wget -O "$src_file" "$image_url" -cat > "$user_data" << EOF -#cloud-config -users: - - name: user - ssh-authorized-keys: -EOF -for key in $sshkeysdir/*.pub; do - echo " - $(cat "$key")" >> $user_data -done -cat >> "$user_data" << 'EOF' - sudo: ['ALL=(ALL) NOPASSWD:ALL'] - groups: sudo - shell: /bin/bash -runcmd: - - sudo growpart /dev/sda 1 - - sudo resize2fs /dev/sda1 - - sudo apt-get update - - sudo apt-get install -y vim git - - cd /home/user - - #git clone https://git.0nom.ch/setup - - #sudo ./setup/setup.sh - - touch /home/user/runcmd_done -EOF -cat > "$meta_data" << EOF -instance-id: vm_id -local-hostname: $1 -EOF -genisoimage -output "$seed_iso" -volid cidata -joliet -rock "$user_data" "$meta_data" &> /dev/null || { echo "Failed to create seed.iso."; exit 1; } -echo "Configuration files generated successfully" + +# First create a command that will generate all the ssh-inject options +SSH_INJECT_OPTS=$(find ${sshkeysdir} -name "*.pub" -exec echo "--ssh-inject user:file:{}" \;) + +flag_file="${dir_path}/debian12_customized" + +# Check if customization has already been done +if [ -f "$flag_file" ]; then + echo "Image already customized, skipping customization..." +else + echo "Customizing image..." + export LIBGUESTFS_BACKEND=direct + sudo -E virt-customize -a /var/lib/libvirt/images/.image_store/debian12.qcow2 \ + --run-command 'useradd -m -s /bin/bash user' \ + $SSH_INJECT_OPTS \ + --update \ + --install openssh-server,git \ + --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" +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' && \ + # Create flag file after successful customization + sudo touch "$flag_file" +fi diff --git a/others/vm3/.docs.swp b/others/vm3/.docs.swp deleted file mode 100644 index 3eb0602..0000000 Binary files a/others/vm3/.docs.swp and /dev/null differ diff --git a/others/vm3/compute/create.sh b/others/vm3/compute/create.sh index 3e6c088..a1d84f6 100755 --- a/others/vm3/compute/create.sh +++ b/others/vm3/compute/create.sh @@ -88,11 +88,18 @@ if [[ ${os,,} == *"freebsd"* ]]; then ostype="generic" fi +# Define the disk options based on OS type +if [ "$os" = "debian12" ]; then + disk_opts="--disk path=$new_vm,format=qcow2" +else + 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 \ - --memory "$((ram_gb * 1024))"\ - --disk path=$new_vm,format=qcow2 \ - --disk path=$seed_iso,device=cdrom \ + --memory "$((ram_gb * 1024))" \ + $disk_opts \ --os-type $ostype \ --os-variant $os \ --virt-type kvm \ diff --git a/others/vm3/compute/list.sh b/others/vm3/compute/list.sh deleted file mode 100755 index b62ef14..0000000 --- a/others/vm3/compute/list.sh +++ /dev/null @@ -1,45 +0,0 @@ - -#!/bin/bash -printf "%-10s %-15s %-8s %-6s %-8s %-12s %-10s\n" "Network" "IP" "State" "vCPUs" "RAM(GB)" "Disk(GB)" "Name" -printf "%-10s %-15s %-8s %-6s %-8s %-12s %-10s\n" "----------" "---------------" "--------" "-----" "-------" "-----------" "----------" - -# Get all VMs -vms=$(sudo virsh list --name --all) - -# Cache the network leases once -default_leases=$(sudo virsh net-dhcp-leases default 2>/dev/null) - -for vm in $vms; do - # Get XML once and use it multiple times - xml=$(sudo virsh dumpxml "$vm" 2>/dev/null) - - # Extract all data from the cached XML - mac=$(echo "$xml" | grep "mac address" | awk -F\' '{ print $2}') - net=$(echo "$xml" | grep "/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.$$ & - - wait - - # Read from temp files - state=$(cat /tmp/state.$$ 2>/dev/null) - vcpus=$(cat /tmp/vcpus.$$ 2>/dev/null) - ram=$(cat /tmp/ram.$$ 2>/dev/null) - disk=$(cat /tmp/disk.$$ 2>/dev/null) - - # Clean up temp files - rm -f /tmp/state.$$ /tmp/vcpus.$$ /tmp/ram.$$ /tmp/disk.$$ - - printf "%-10s %-15s %-8s %-6s %-8s %-12s %-10s\n" \ - "$net" "$ip" "$state" "$vcpus" "$ram" "$disk" "$vm" -done diff --git a/others/vm3/compute/ls.sh b/others/vm3/compute/ls.sh new file mode 100755 index 0000000..b62ef14 --- /dev/null +++ b/others/vm3/compute/ls.sh @@ -0,0 +1,45 @@ + +#!/bin/bash +printf "%-10s %-15s %-8s %-6s %-8s %-12s %-10s\n" "Network" "IP" "State" "vCPUs" "RAM(GB)" "Disk(GB)" "Name" +printf "%-10s %-15s %-8s %-6s %-8s %-12s %-10s\n" "----------" "---------------" "--------" "-----" "-------" "-----------" "----------" + +# Get all VMs +vms=$(sudo virsh list --name --all) + +# Cache the network leases once +default_leases=$(sudo virsh net-dhcp-leases default 2>/dev/null) + +for vm in $vms; do + # Get XML once and use it multiple times + xml=$(sudo virsh dumpxml "$vm" 2>/dev/null) + + # Extract all data from the cached XML + mac=$(echo "$xml" | grep "mac address" | awk -F\' '{ print $2}') + net=$(echo "$xml" | grep "/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.$$ & + + wait + + # Read from temp files + state=$(cat /tmp/state.$$ 2>/dev/null) + vcpus=$(cat /tmp/vcpus.$$ 2>/dev/null) + ram=$(cat /tmp/ram.$$ 2>/dev/null) + disk=$(cat /tmp/disk.$$ 2>/dev/null) + + # Clean up temp files + rm -f /tmp/state.$$ /tmp/vcpus.$$ /tmp/ram.$$ /tmp/disk.$$ + + printf "%-10s %-15s %-8s %-6s %-8s %-12s %-10s\n" \ + "$net" "$ip" "$state" "$vcpus" "$ram" "$disk" "$vm" +done diff --git a/others/vm3/docs b/others/vm3/docs index e350e4e..ca8ec36 100644 --- a/others/vm3/docs +++ b/others/vm3/docs @@ -1,6 +1,8 @@ todo make theseediso cloud init stuff happen inside the vm storage directory so that multiple poeple can use it ast the same time on the same system + fedora debian freebsd opensuse + change deployment for non-fedora to manually make a base image and copy ssh stuff over, then duplicate it however you want diff --git a/others/vm3/vm.sh b/others/vm3/vm.sh index 30168ba..c36b0ae 100755 --- a/others/vm3/vm.sh +++ b/others/vm3/vm.sh @@ -35,8 +35,8 @@ case $category in start) $SCRIPT_DIR/compute/start.sh "$@" ;; - list) - $SCRIPT_DIR/compute/list.sh "$@" + ls) + $SCRIPT_DIR/compute/ls.sh "$@" ;; shutdown) $SCRIPT_DIR/compute/shutdown.sh "$@" @@ -45,7 +45,7 @@ case $category in $SCRIPT_DIR/compute/delete.sh "$@" ;; *) - echo -e "Available compute actions: \ncreate\nstart\nlist\nshutdown\ndelete" + echo -e "Available compute actions: \ncreate\nstart\nls\nshutdown\ndelete" exit 1 ;; esac -- cgit v1.2.3-70-g09d2