diff options
Diffstat (limited to 'others')
27 files changed, 0 insertions, 1599 deletions
diff --git a/others/.vm.sh.swp b/others/.vm.sh.swp Binary files differdeleted file mode 100644 index 962a1ee..0000000 --- a/others/.vm.sh.swp +++ /dev/null diff --git a/others/init.sh b/others/init.sh deleted file mode 100755 index 30b47bb..0000000 --- a/others/init.sh +++ /dev/null @@ -1 +0,0 @@ -/usr/bin/autossh -fN -M 0 -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" -i /home/x/m -R localhost:8888:localhost:8888 -R 24:localhost:22 -R localhost:3389:localhost:3389 -o StrictHostKeyChecking=no root@sg1.0nom.ch diff --git a/others/meta-data b/others/meta-data deleted file mode 100644 index ce225e1..0000000 --- a/others/meta-data +++ /dev/null @@ -1,2 +0,0 @@ -instance-id: iid-fedora-vm -local-hostname: fedora-cloudimg diff --git a/others/openstack_config.txt b/others/openstack_config.txt deleted file mode 100644 index 5e991a6..0000000 --- a/others/openstack_config.txt +++ /dev/null @@ -1,14 +0,0 @@ -export OS_AUTH_URL=https://api.pub1.infomaniak.cloud/identity/v3 -export OS_PROJECT_NAME=PCP-UV9N94K -export OS_PROJECT_DOMAIN_NAME=default -export OS_USERNAME=PCU-UV9N94K -export OS_USER_DOMAIN_NAME=default -export OS_PROJECT_ID=4b23183ad37e4271900622f738cf3a17 -export OS_IDENTITY_API_VERSION=3 -export OS_INTERFACE=public -export OS_REGION_NAME=dc3-a -# To avoid being prompted for your password each time, -# write your password below and uncomment the line -#OS_PASSWORD='your password goes here' -[ -z "$OS_PASSWORD" ] && read -e -p "Please enter your OpenStack Password for project $OS_PROJECT_NAME as user $OS_USERNAME: " OS_PASSWORD -export OS_PASSWORD diff --git a/others/reset-ssh-connections.sh b/others/reset-ssh-connections.sh deleted file mode 100755 index f19300d..0000000 --- a/others/reset-ssh-connections.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash -ss -tnp | grep ':22' -ps -ef | grep sshd | grep -v grep | awk '{if($3!=1) print $2}' | xargs kill -#sudo systemctl restart sshd diff --git a/others/setup_2_openstackenv.sh b/others/setup_2_openstackenv.sh deleted file mode 100644 index e56b66f..0000000 --- a/others/setup_2_openstackenv.sh +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/bash - -# directories -mkdir ~/work -mkdir ~/work/mounts -mkdir ~/work/mounts/private -mkdir ~/work/mounts/public -mkdir ~/work/mounts/.localtmp - -touch ~/work/mounts/.passwd-s3fs -chmod 600 ~/work/mounts/.passwd-s3fs -echo "id:pass" > ~/work/mounts/.passwd-s3fs -sudo s3fs s3ch1 ~/work/mounts/private -o passwd_file=~/work/mounts/.passwd-s3fs -o url=https://s3.pub1.infomaniak.cloud -o allow_other -o use_cache=~/work/mounts/.localtmp/ -o umask=000 -o use_path_request_style -sudo s3fs s3ch1-public ~/work/mounts/public -o passwd_file=~/work/mounts/.passwd-s3fs -o url=https://s3.pub1.infomaniak.cloud -o allow_other -o use_cache=~/work/mounts/.localtmp/ -o umask=000 -o use_path_request_style - diff --git a/others/user-data b/others/user-data deleted file mode 100644 index 795fb19..0000000 --- a/others/user-data +++ /dev/null @@ -1,16 +0,0 @@ -#cloud-config -users: - - name: fedora - ssh-authorized-keys: - - - sudo: ['ALL=(ALL) NOPASSWD:ALL'] - groups: wheel - shell: /bin/bash -runcmd: - - 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/vm.sh b/others/vm.sh deleted file mode 100755 index ceafde4..0000000 --- a/others/vm.sh +++ /dev/null @@ -1,316 +0,0 @@ -#!/bin/bash - -help(){ - echo -e "\n$0 - info - create\n -vcpu\n -ram \n -disk \n -ssh-keys-dir \n -add-disk-block\n -public-ssh-port - manage\n -start\n -shutdown\n -reboot\n -kill\n -erase - disk\n -attach\n -detach\n -resize\n" - exit 1 -} - -case "$1" in - "create") - if [[ $# -lt 2 ]]; then - echo "" - echo -e "$0 $1 [VM_NAME]" - echo -e "default: -vcpu 8 -ram 8 -disk 60 --ssh-keys-dir keys/" - echo -e "args[GB]:\n -vcpu\n -ram \n -disk \n -ssh-keys-dir \n -add-disk-block\n -public-ssh-port)" - echo "" - exit 1 - fi - - VM_NAME=$2 - VCPU=8 - RAM_GB=8000 #memory is noted as mb in the virt-install program - DISK_GB=64 - SSH_KEYS_DIR="keys/" - DISK_BLOCK_GB=0 - PSP=${PSP:-0} - - while [[ $# -gt 0 ]]; do - case "$1" in - create) - shift 2 - ;; - -vcpu) - VCPU=$2 - shift 2 - ;; - -ram) - RAM_GB=$(($2*1000)) - shift 2 - ;; - -disk) - DISK_GB=$2 - shift 2 - ;; - -ssh-keys-dir) - SSH_KEYS_DIR=$2 - shift 2 - ;; - -add-disk-block) - DISK_BLOCK_GB=$2 - shift 2 - ;; - -public-ssh-port) - PSP=$2 - shift 2 - ;; - *) - echo "Unknown argument: $1" - exit 1 - ;; - esac - done - download_url="https://download.rockylinux.org/pub/rocky/9.3/images/x86_64/Rocky-9-GenericCloud-LVM-9.3-20231113.0.x86_64.qcow2" - dir_path="/var/lib/libvirt/images/" - src_file="/var/lib/libvirt/images/Rocky9.3.qcow2" - seed_iso="/var/lib/libvirt/images/seed.iso" - packages=("nc" "htop" "wireguard-tools" "bind-utils" "tmux" "net-tools" "curl" "mlocate" "dnsmasq" "qemu-kvm" "libvirt" "libvirt-client" "bridge-utils" "virt-install" "virt-manager" "genisoimage") - NEW_IMG_PATH="/var/lib/libvirt/images/${VM_NAME}.qcow2" - XML_PATH="/tmp/${VM_NAME}.xml" - DISK_BLOCK_GB_BASE_PATH="/var/lib/libvirt/images/${VM_NAME}-vm-disks/" - DISK_BLOCK_GB_PATH="${DISK_BLOCK_GB_BASE_PATH}${VM_NAME}-vda-${DISK_BLOCK_GB}G.qcow2" - ssh_private=$(cat /home/s22/man1) - - function check_package_installed() { - local package_name=$1 - rpm -q "$package_name" &> /dev/null - } - - for package in "${packages[@]}"; do - if ! check_package_installed "$package"; then - #echo "Downloading packages..." - sudo dnf install -y "$package" &> /dev/null - fi - done - - [ ! -d $dir_path ] && mkdir -p $dir_path &> /dev/null - [ ! -f "$src_file" ] && (wget -O "$src_file" "$download_url" &> /dev/null || { echo "Failed to download Rocky"; exit 1; }) - - echo -e "#cloud-config\nusers:\n - name: root\n ssh-authorized-keys:" > user-data - - for key in "$SSH_KEYS_DIR"/*.pub; do - echo " - $(cat "$key")" >> user-data - done - - echo " sudo: ['ALL=(ALL) NOPASSWD:ALL'] - groups: sudo - shell: /bin/bash" >> user-data - - if [ "$PSP" -ne 0 ]; then - echo "write_files:" >> user-data - echo " - path: /root/man1" >> user-data - echo " content: |" >> user-data - while read -r line; do - echo " $line" >> user-data - done <<< "${ssh_private}" - fi - - echo 'runcmd:' >> user-data - #echo ' - [ /usr/bin/wget, "http://example.com/file", -O, /tmp/examplefile ]' >> user-data - #echo ' - touch /root/test1.txt' >> user-data - if [ "$DISK_BLOCK_GB" -ne 0 ]; then - echo "echo to disksetup in vm" - echo "vm name 1 ${VM_NAME}" - echo ' - echo "#!/bin/bash" > /root/disk-setup.sh' >> user-data - # echo ' - echo "sleep 40;" >> /root/disk-setup.sh' >> user-data - echo " - echo \"DISK='/dev/vda'; MOUNT_PATH='/home/${VM_NAME}-vda-${DISK_BLOCK_GB}G';\" >> /root/disk-setup.sh" >> user-data - echo " - echo '[ ! -d \$MOUNT_PATH ] && mkdir -p \$MOUNT_PATH;' >> /root/disk-setup.sh" >> user-data - echo " - echo 'blkid | grep -q \$DISK || mkfs.ext4 \$DISK;' >> /root/disk-setup.sh" >> user-data - echo " - echo 'grep -q \$DISK /etc/fstab || echo \"\$DISK \$MOUNT_PATH ext4 defaults,nofail 0 0\" >> /etc/fstab;' >> /root/disk-setup.sh" >> user-data - echo ' - echo "mount -a;" >> /root/disk-setup.sh' >> user-data - echo ' - echo "systemctl daemon-reload" >> /root/disk-setup.sh' >> user-data - echo ' - chmod +x /root/disk-setup.sh' >> user-data - echo ' - /root/disk-setup.sh' >> user-data - echo ' - rm -f /root/disk-setup.sh' >> user-data - fi - - if [ "$PSP" -ne 0 ]; then - echo ' - chmod 600 /root/man1' >> user-data - #kill ssh and reconnect every 4 hours - ##echo " - (echo \"0 */4 * * * PIDS=\\\$(pgrep -f \\\"ssh.*root@64.176.179.97\\\"); if [ -n \\\"\\\${PIDS}\\\" ]; then kill \\\${PIDS}; fi; /usr/bin/ssh -fN -i /root/man1 -R ${PSP}:localhost:22 -o StrictHostKeyChecking=no root@64.176.179.97\") | crontab -" >> user-data - echo " - (echo \"* * * * * /root/initial.sh\") | crontab -" >> user-data - echo " - echo 'PIDS=\$(pgrep -f \"ssh.*root@64.176.179.97\"); if [ -z \"\${PIDS}\" ]; then /usr/bin/ssh -fN -i /root/man1 -R ${PSP}:localhost:22 -o StrictHostKeyChecking=no root@64.176.179.97; fi' > /root/initial.sh" >> user-data - echo " - chmod +x /root/initial.sh" >> user-data - echo " - /root/initial.sh" >> user-data - fi - - #nofail is present in the fstab which means that boot will continue even if it fails to mount - echo ' - growpart /dev/sda 4 ' >> user-data #do note that restart is required for the system to register the increased disk size - echo ' - sudo lvresize -l +100%FREE /dev/rocky/lvroot' >> user-data - echo ' - sudo dnf install -y epel-release dnf-utils' >> user-data - echo ' - sudo dnf install -y nc xclip tmux htop tar tree wget curl mlocate nano vim unzip net-tools git python3 python3-pip make'>> user-data - echo ' - touch /root/runcmd_done' >> user-data - - echo -e "instance-id: iid-ihatecs\nlocal-hostname: cloudimg" > meta-data - - genisoimage -output "$seed_iso" -volid cidata -joliet -rock user-data meta-data &> /dev/null || { echo "Failed to create seed.iso."; exit 1; } - - cp $src_file $NEW_IMG_PATH &> /dev/null || { echo "Failed to create a new image."; exit 1; } - - if virsh list --all | grep -q "$VM_NAME"; then - echo -e "\n$VM_NAME already exist. Delete it using \n$0 manage -erase $VM_NAME\nExiting..." - exit 1 - fi - - sudo virt-install --name $VM_NAME \ - --vcpus $VCPU \ - --ram $RAM_GB \ - --disk path=$NEW_IMG_PATH,size=$DISK_GB,format=qcow2 \ - --disk path=$seed_iso,device=cdrom \ - --os-type linux \ - --os-variant rhl9 \ - --virt-type kvm \ - --graphics none \ - --network bridge=virbr0,model=virtio \ - --print-xml > $XML_PATH || { echo "Failed to print XML."; exit 1; } - - sudo virsh define $XML_PATH &> /dev/null || { echo "Failed to define the new VM."; exit 1; } - - sudo qemu-img resize $NEW_IMG_PATH +$DISK_GB"G" #&> /dev/null - - virsh start $VM_NAME - echo "" - - if [ $DISK_BLOCK_GB -ne 0 ]; then - mkdir -p $DISK_BLOCK_GB_BASE_PATH - qemu-img create -f qcow2 $DISK_BLOCK_GB_PATH "${DISK_BLOCK_GB}G" - virsh attach-disk $VM_NAME $DISK_BLOCK_GB_PATH vda --cache none --subdriver qcow2 - fi - - message="waiting 29s to begin finding ip address..." - duration=29 - for ((i=$duration; i>=1; i--)); do - printf "\r%s%2ds" "$message" $i - sleep 1 - done - echo - - while true; do - OUTPUT=$($0 info | grep "$VM_NAME") - if [[ "$OUTPUT" != "" ]]; then - IP_ADDRESS=$(echo "$OUTPUT" | grep -oP '\d+\.\d+\.\d+\.\d+') - # If IP address is found and is not empty - if [[ ! -z "$IP_ADDRESS" ]]; then - echo -e "\nIP address of $VM_NAME is:\n$IP_ADDRESS" - break - else - echo "IP address for $VM_NAME not found. Retrying in 1 second..." - sleep 1 - fi - else - echo "$VM_NAME not found. Exiting..." - exit 1 - fi - done - - #do take note that df -h will not reflect until reboot - - ;; - "manage") - if [[ $# -lt 2 ]]; then - echo "" - echo -e "$0 $1 arg [VM_1] [VM_2] [VM_3] \n$0 $1 arg --all" - echo -e "args:\n -start\n -shutdown\n -kill\n -erase " - echo "" - exit 1 - fi - - arg=$2 - shift 2 - - if [[ "$1" == "--all" ]]; then - vms=$(virsh list --all --name) # List all running VMs by name - set -- $vms # Set the positional parameters to the VM names - fi - - case $arg in - "-shutdown") - for vm in "$@"; do - virsh shutdown "$vm" > /dev/null 2>&1 - printf "%-50s%10s\n" "Shutting down $vm..." $([[ $? -eq 0 ]] && echo "Successful" || echo "Failed") - done - ;; - "-kill") - for vm in "$@"; do - virsh destroy "$vm" > /dev/null 2>&1 - printf "%-50s%10s\n" "Destroying $vm..." $([[ $? -eq 0 ]] && echo "Successful" || echo "Failed") - done - ;; - "-erase") - echo -e "\nWARNING: THIS IS IRREVERSIBLE. Sleeping for 10 seconds. Ctrl-C to stop. WARNING.\n" - sleep 10 - echo -e "\nErasing image(s) from /var/lib/libvirt/images/\n" - for vm in "$@"; do - virsh destroy "$vm" > /dev/null 2>&1 - virsh undefine "$vm" --remove-all-storage > /dev/null 2>&1 - printf "%-50s%10s\n" "Removing $vm..." $([[ $? -eq 0 ]] && echo "Successful" || echo "Failed") - done - ;; - "-start") - for vm in "$@"; do - virsh start "$vm" > /dev/null 2>&1 - printf "%-50s%10s\n" "Starting $vm..." $([[ $? -eq 0 ]] && echo "Successful" || echo "Failed") - done - ;; - "-reboot") - for vm in "$@"; do - virsh shutdown "$vm" > /dev/null 2>&1 - virsh start "$vm" > /dev/null 2>&1 - printf "%-50s%10s\n" "Rebooting $vm..." $([[ $? -eq 0 ]] && echo "Successful" || echo "Failed") - done - ;; - *) - echo "Unknown argument: $arg" - exit 1 - ;; - esac - ;; - "info") - printf "%-4s %-10s %-15s %-10s %-10s %-6s %-8s %-12s\n" "Id" "Name" "IP" "State" "Network" "vCPUs" "RAM(GB)" "Disk(GB)" - printf "%-4s %-10s %-15s %-10s %-10s %-6s %-8s %-12s\n" "----" "----------" "---------------" "----------" "----------" "-----" "-------" "-----------" - - vms=$(virsh list --name --all) - - id=1 - for vm in $vms; do - # Get the MAC address of the VM - mac=$(virsh dumpxml $vm | grep "mac address" | awk -F\' '{ print $2}') - - # Get the network name - net=$(virsh dumpxml $vm | grep "<source network" | awk -F\' '{print $2}') - - # Assign "default" if no network name is found - if [ -z "$net" ] - then - net="default" - fi - - # Get the IP address of the VM - ip=$(virsh net-dhcp-leases $net | grep $mac | awk '{print $5}' | cut -f1 -d'/') - - # Get the state of the VM - state=$(virsh domstate $vm) - - # Get the vCPUs, RAM and Disk details of the VM - vcpus=$(virsh dominfo $vm | grep "CPU(s)" | awk '{print $2}') - - ram=$(bc <<< "scale=2; $(virsh dominfo $vm | grep "Max memory" | awk '{print $3}')/1000000") - - # Use du to get the size of the disk file in GB - disk=$(du -sk /var/lib/libvirt/images/${vm}.qcow2 | awk '{ printf "%.2f", $1/1024/1024 }' ) - - printf "%-4s %-10s %-15s %-10s %-10s %-6s %-8s %-12s\n" "$id" "$vm" "$ip" "$state" "$net" "$vcpus" "$ram" "$disk" - id=$((id+1)) - done - exit 1 - ;; - *) - help - exit 1 - ;; -esac - - diff --git a/others/vm.sh2 b/others/vm.sh2 deleted file mode 100755 index fe3eec0..0000000 --- a/others/vm.sh2 +++ /dev/null @@ -1,291 +0,0 @@ -#!/bin/bash - -help(){ - echo -e "\n$0 - info - create\n -vcpu\n -ram \n -disk \n -ssh-keys-dir \n -add-disk-block\n -public-ssh-port - manage\n -start\n -shutdown\n -reboot\n -kill\n -erase - disk\n -attach\n -detach\n -resize\n" - exit 1 -} - -case "$1" in - "create") - if [[ $# -lt 2 ]]; then - echo "" - echo -e "$0 $1 [VM_NAME]" - echo -e "default: -vcpu 8 -ram 8 -disk 60 --ssh-keys-dir keys/" - echo -e "args[GB]:\n -vcpu\n -ram \n -disk \n -ssh-keys-dir \n -add-disk-block\n -public-ssh-port)" - echo "" - exit 1 - fi - - VM_NAME=$2 - VCPU=8 - RAM_GB=8000 #memory is noted as mb in the virt-install program - DISK_GB=64 - SSH_KEYS_DIR="k/" - DISK_BLOCK_GB=0 - PSP=${PSP:-0} - - while [[ $# -gt 0 ]]; do - case "$1" in - create) - shift 2 - ;; - -vcpu) - VCPU=$2 - shift 2 - ;; - -ram) - RAM_GB=$(($2*1000)) - shift 2 - ;; - -disk) - DISK_GB=$2 - shift 2 - ;; - -ssh-keys-dir) - SSH_KEYS_DIR=$2 - shift 2 - ;; - -add-disk-block) - DISK_BLOCK_GB=$2 - shift 2 - ;; - -public-ssh-port) - PSP=$2 - shift 2 - ;; - *) - echo "Unknown argument: $1" - exit 1 - ;; - esac - done - download_url="https://download.nus.edu.sg/mirror/fedora/linux/releases/40/Cloud/x86_64/images/Fedora-Cloud-Base-Generic.x86_64-40-1.14.qcow2" - dir_path="/var/lib/libvirt/images/" - src_file="/var/lib/libvirt/images/fedora40.qcow2" - seed_iso="/var/lib/libvirt/images/seed.iso" - packages=("nc" "htop" "wireguard-tools" "bind-utils" "tmux" "net-tools" "curl" "mlocate" "dnsmasq" "qemu-kvm" "libvirt" "libvirt-daemon-kvm" "virt-install" "virt-manager" "genisoimage") - NEW_IMG_PATH="/var/lib/libvirt/images/${VM_NAME}.qcow2" - XML_PATH="/tmp/${VM_NAME}.xml" - DISK_BLOCK_GB_BASE_PATH="/var/lib/libvirt/images/${VM_NAME}-vm-disks/" - DISK_BLOCK_GB_PATH="${DISK_BLOCK_GB_BASE_PATH}${VM_NAME}-vda-${DISK_BLOCK_GB}G.qcow2" - ssh_private=$(cat /home/x/m) - - function check_package_installed() { - local package_name=$1 - rpm -q "$package_name" &> /dev/null - } - - for package in "${packages[@]}"; do - if ! check_package_installed "$package"; then - sudo dnf install -y "$package" &> /dev/null - fi - done - - [ ! -d $dir_path ] && sudo mkdir -p $dir_path &> /dev/null - [ ! -f "$src_file" ] && (sudo wget -O "$src_file" "$download_url" &> /dev/null || { echo "Failed to download Fedora image"; exit 1; }) - - echo -e "#cloud-config\nusers:\n - name: fedora\n ssh-authorized-keys:" > user-data - - for key in "$SSH_KEYS_DIR"*.pub; do - echo " - $(cat "$key")" >> user-data - done - - echo " sudo: ['ALL=(ALL) NOPASSWD:ALL'] - groups: wheel - shell: /bin/bash" >> user-data - - if [ "$PSP" -ne 0 ]; then - echo "write_files:" >> user-data - echo " - path: /home/fedora/m" >> user-data - echo " content: |" >> user-data - while read -r line; do - echo " $line" >> user-data - done <<< "${ssh_private}" - fi - - echo 'runcmd:' >> user-data - if [ "$DISK_BLOCK_GB" -ne 0 ]; then - echo ' - echo "#!/bin/bash" > /home/fedora/disk-setup.sh' >> user-data - echo " - echo \"DISK='/dev/vda'; MOUNT_PATH='/home/${VM_NAME}-vda-${DISK_BLOCK_GB}G';\" >> /home/fedora/disk-setup.sh" >> user-data - echo " - echo '[ ! -d \$MOUNT_PATH ] && sudo mkdir -p \$MOUNT_PATH;' >> /home/fedora/disk-setup.sh" >> user-data - echo " - echo 'sudo blkid | grep -q \$DISK || sudo mkfs.ext4 \$DISK;' >> /home/fedora/disk-setup.sh" >> user-data - echo " - 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" >> user-data - echo ' - echo "sudo mount -a;" >> /home/fedora/disk-setup.sh' >> user-data - echo ' - echo "sudo systemctl daemon-reload" >> /home/fedora/disk-setup.sh' >> user-data - echo ' - sudo chmod +x /home/fedora/disk-setup.sh' >> user-data - echo ' - sudo /home/fedora/disk-setup.sh' >> user-data - echo ' - sudo rm -f /home/fedora/disk-setup.sh' >> user-data - fi - - if [ "$PSP" -ne 0 ]; then - echo ' - sudo chmod 600 /home/fedora/man1' >> user-data - echo " - (echo \"* * * * * /home/fedora/initial.sh\") | crontab -" >> user-data - echo " - echo 'PIDS=\$(pgrep -f \"ssh.*fedora@64.176.179.97\"); if [ -z \"\${PIDS}\" ]; then /usr/bin/ssh -fN -i /home/fedora/man1 -R ${PSP}:localhost:22 -o StrictHostKeyChecking=no fedora@64.176.179.97; fi' > /home/fedora/initial.sh" >> user-data - echo " - chmod +x /home/fedora/initial.sh" >> user-data - echo " - /home/fedora/initial.sh" >> user-data - fi - - echo ' - sudo growpart /dev/sda 1' >> user-data - echo ' - sudo xfs_growfs /' >> user-data - echo ' - sudo dnf install -y vim git' >> user-data - echo ' - cd /home/fedora' >> user-data - echo ' - git clone https://git.0nom.ch/setup' >> user-data - echo ' - sudo ./setup/setup.sh' >> user-data - echo ' - touch /home/fedora/runcmd_done' >> user-data - - echo -e "instance-id: iid-fedora-vm\nlocal-hostname: fedora-cloudimg" > meta-data - - genisoimage -output "$seed_iso" -volid cidata -joliet -rock user-data meta-data &> /dev/null || { echo "Failed to create seed.iso."; exit 1; } - - sudo cp $src_file $NEW_IMG_PATH &> /dev/null || { echo "Failed to create a new image."; exit 1; } - - if sudo virsh list --all | awk "\$2==\"$VM_NAME\"" | grep -q .; then - # if sudo virsh list --all | grep -q "$VM_NAME"; then - echo -e "\n$VM_NAME already exist. Delete it using \n$0 manage -erase $VM_NAME\nExiting..." - exit 1 - fi - - sudo virt-install --name $VM_NAME \ - --vcpus $VCPU \ - --ram $RAM_GB \ - --disk path=$NEW_IMG_PATH,size=$DISK_GB,format=qcow2 \ - --disk path=$seed_iso,device=cdrom \ - --os-type linux \ - --os-variant fedora38 \ - --virt-type kvm \ - --graphics none \ - --network bridge=virbr0,model=virtio \ - --print-xml > $XML_PATH || { echo "Failed to print XML."; exit 1; } - - sudo virsh define $XML_PATH &> /dev/null || { echo "Failed to define the new VM."; exit 1; } - - sudo qemu-img resize $NEW_IMG_PATH +$DISK_GB"G" #&> /dev/null - - sudo virsh start $VM_NAME - echo "" - - if [ $DISK_BLOCK_GB -ne 0 ]; then - sudo mkdir -p $DISK_BLOCK_GB_BASE_PATH - sudo qemu-img create -f qcow2 $DISK_BLOCK_GB_PATH "${DISK_BLOCK_GB}G" - sudo virsh attach-disk $VM_NAME $DISK_BLOCK_GB_PATH vda --cache none --subdriver qcow2 - fi - - message="waiting 29s to begin finding ip address..." - duration=29 - for ((i=$duration; i>=1; i--)); do - printf "\r%s%2ds" "$message" $i - sleep 1 - done - echo - - while true; do - OUTPUT=$($0 info | grep "$VM_NAME") - if [[ "$OUTPUT" != "" ]]; then - IP_ADDRESS=$(echo "$OUTPUT" | grep -oP '\d+\.\d+\.\d+\.\d+') - if [[ ! -z "$IP_ADDRESS" ]]; then - echo -e "\nIP address of $VM_NAME is:\n$IP_ADDRESS" - break - else - echo "IP address for $VM_NAME not found. Retrying in 1 second..." - sleep 1 - fi - else - echo "$VM_NAME not found. Exiting..." - exit 1 - fi - done - - ;; - "manage") - if [[ $# -lt 2 ]]; then - echo "" - echo -e "$0 $1 arg [VM_1] [VM_2] [VM_3] \n$0 $1 arg --all" - echo -e "args:\n -start\n -shutdown\n -kill\n -erase " - echo "" - exit 1 - fi - - arg=$2 - shift 2 - - if [[ "$1" == "--all" ]]; then - vms=$(sudo virsh list --all --name) - set -- $vms - fi - - case $arg in - "-shutdown") - for vm in "$@"; do - sudo virsh shutdown "$vm" > /dev/null 2>&1 - printf "%-50s%10s\n" "Shutting down $vm..." $([[ $? -eq 0 ]] && echo "Successful" || echo "Failed") - done - ;; - "-kill") - for vm in "$@"; do - sudo virsh destroy "$vm" > /dev/null 2>&1 - printf "%-50s%10s\n" "Destroying $vm..." $([[ $? -eq 0 ]] && echo "Successful" || echo "Failed") - done - ;; - "-erase") - echo -e "\nWARNING: THIS IS IRREVERSIBLE. Sleeping for 10 seconds. Ctrl-C to stop. WARNING.\n" - sleep 10 - echo -e "\nErasing image(s) from /var/lib/libvirt/images/\n" - for vm in "$@"; do - sudo virsh destroy "$vm" > /dev/null 2>&1 - sudo virsh undefine "$vm" --remove-all-storage > /dev/null 2>&1 - printf "%-50s%10s\n" "Removing $vm..." $([[ $? -eq 0 ]] && echo "Successful" || echo "Failed") - done - ;; - "-start") - for vm in "$@"; do - sudo virsh start "$vm" > /dev/null 2>&1 - printf "%-50s%10s\n" "Starting $vm..." $([[ $? -eq 0 ]] && echo "Successful" || echo "Failed") - done - ;; - "-reboot") - for vm in "$@"; do - sudo virsh shutdown "$vm" > /dev/null 2>&1 - sudo virsh start "$vm" > /dev/null 2>&1 - printf "%-50s%10s\n" "Rebooting $vm..." $([[ $? -eq 0 ]] && echo "Successful" || echo "Failed") - done - ;; - *) - echo "Unknown argument: $arg" - exit 1 - ;; - esac - ;; - "info") - printf "%-4s %-10s %-15s %-10s %-10s %-6s %-8s %-12s\n" "Id" "Name" "IP" "State" "Network" "vCPUs" "RAM(GB)" "Disk(GB)" - printf "%-4s %-10s %-15s %-10s %-10s %-6s %-8s %-12s\n" "----" "----------" "---------------" "----------" "----------" "-----" "-------" "-----------" - - vms=$(sudo virsh list --name --all) - - id=1 - for vm in $vms; do - mac=$(sudo virsh dumpxml $vm | grep "mac address" | awk -F\' '{ print $2}') - net=$(sudo virsh dumpxml $vm | grep "<source network" | awk -F\' '{print $2}') - if [ -z "$net" ] - then - net="default" - fi - ip=$(sudo virsh net-dhcp-leases $net | grep $mac | awk '{print $5}' | cut -f1 -d'/') - state=$(sudo virsh domstate $vm) - vcpus=$(sudo virsh dominfo $vm | grep "CPU(s)" | awk '{print $2}') - ram=$(bc <<< "scale=2; $(sudo virsh dominfo $vm | grep "Max memory" | awk '{print $3}')/1000000") - disk=$(sudo du -sk /var/lib/libvirt/images/${vm}.qcow2 | awk '{ printf "%.2f", $1/1024/1024 }') - - printf "%-4s %-10s %-15s %-10s %-10s %-6s %-8s %-12s\n" "$id" "$vm" "$ip" "$state" "$net" "$vcpus" "$ram" "$disk" - id=$((id+1)) - done - exit 0 - ;; - *) - help - exit 1 - ;; -esac diff --git a/others/vm3/.config/cloud-init-generator/debian12.sh b/others/vm3/.config/cloud-init-generator/debian12.sh deleted file mode 100755 index 6bfa54b..0000000 --- a/others/vm3/.config/cloud-init-generator/debian12.sh +++ /dev/null @@ -1,67 +0,0 @@ -#!/bin/bash -# generates user data, meta data, and seed.iso for cloud init. for debian -# requires the name of the vm as an argument -# -# osinfo-query os to list all available vm types to deploy. debian is debian12 -# use no cloud and some other tools because cloud init does not work -# -#qemu-img resize debian-11-generic-amd64.qcow2 +10G # try this if you want to install something big like kde -# sudo apt update -# sudo apt install kde-plasma-desktop - -# -image_url="https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-nocloud-amd64.qcow2" -sshkeysdir="/root/k" -scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" #&& echo $scriptdir -dir_path="/var/lib/libvirt/images/.image_store" -src_file="${dir_path}/debian12.qcow2" -config_dir="/var/lib/libvirt/images/.temp" -user_data="${config_dir}/user-data" -meta_data="${config_dir}/meta-data" -seed_iso="${config_dir}/seed.iso" -# note that cloud init specifically looks for "user-data" and "meta-data" in the seed.iso disk. do not deviate from the naming convention. -if [ $# -ne 1 ]; then - echo "Usage: $0 <vm-name>" - exit 1 -fi -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" - -# generate all the lines from the file -SSH_INJECT_OPTS=$(find ${sshkeysdir} -name "*.pub" -exec echo "--ssh-inject user:file:{}" \;) - -flag_file="${dir_path}/debian12_customized" - -# NOTE to delete customfile if debqcow2 is deleted. otherwise it will be ambiguous -# this to check if image is already customised -if [ -f "$flag_file" ]; then - echo "Image already customized, skipping customization..." -else - echo "Customizing image..." - # to install virt customise tool - sudo dnf install -y libguestfs libguestfs-tools libvirt virt-install virt-manager guestfs-tools - 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,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/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' \ - --run-command 'tee -a /home/user/.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' \ - --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/.config/cloud-init-generator/fedora40.sh b/others/vm3/.config/cloud-init-generator/fedora40.sh deleted file mode 100755 index bd3e0c3..0000000 --- a/others/vm3/.config/cloud-init-generator/fedora40.sh +++ /dev/null @@ -1,105 +0,0 @@ -#!/bin/bash - -# generates user data, meta data, and seed.iso for cloud init. for fedora -# requires the name of the vm as an argument -# -# osinfo-query os to list all available vm types to deploy. fedora is fedora 40 - - -image_url="https://download.fedoraproject.org/pub/fedora/linux/releases/41/Cloud/x86_64/images/Fedora-Cloud-Base-Generic-41-1.4.x86_64.qcow2" - -sshkeysdir="/root/k" - -scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" #&& echo $scriptdir -dir_path="/var/lib/libvirt/images/.image_store" -src_file="${dir_path}/fedora40.qcow2" -config_dir="/var/lib/libvirt/images/.temp" -user_data="${config_dir}/user-data" -meta_data="${config_dir}/meta-data" -seed_iso="${config_dir}/seed.iso" -# note that cloud init specifically looks for "user-data" and "meta-data" in the seed.iso disk. do not deviate from the naming convention. - -if [ $# -ne 1 ]; then - echo "Usage: $0 <vm-name>" - exit 1 -fi - -sudo mkdir -p "$dir_path" -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: wheel - shell: /bin/bash -runcmd: - - sudo growpart /dev/sda 1 - - sudo xfs_growfs / - - | - cat > /etc/yum.repos.d/local-fedora.repo << 'FOE' - [local-fedora] - name=Local Fedora Mirror - baseurl=http://192.168.124.227/fedora-base - enabled=1 - gpgcheck=0 - priority=1 - [local-updates] - name=Local Fedora Updates - baseurl=http://192.168.124.227/fedora-updates - enabled=1 - gpgcheck=0 - priority=1 - [local-updates-testing] - name=Local Fedora Updates Testing - baseurl=http://192.168.124.227/fedora-updates-testing - enabled=1 - gpgcheck=0 - priority=1 - FOE - - sudo sed -i 's/enabled=1/enabled=0/' /etc/yum.repos.d/fedora.repo - - sudo sed -i 's/enabled=1/enabled=0/' /etc/yum.repos.d/fedora-updates.repo - - sudo sed -i 's/enabled=1/enabled=0/' /etc/yum.repos.d/fedora-updates-testing.repo - - sudo dnf 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" - diff --git a/others/vm3/.config/cloud-init-generator/freebsd14.0.sh b/others/vm3/.config/cloud-init-generator/freebsd14.0.sh deleted file mode 100755 index c498efa..0000000 --- a/others/vm3/.config/cloud-init-generator/freebsd14.0.sh +++ /dev/null @@ -1,75 +0,0 @@ -#!/bin/bash -# generates configuration for FreeBSD VM deployment -# requires the name of the vm as an argument -image_url="https://download.freebsd.org/releases/VM-IMAGES/14.2-RELEASE/amd64/Latest/FreeBSD-14.2-RELEASE-amd64-BASIC-CLOUDINIT.zfs.qcow2.xz" -sshkeysdir="/root/k" -scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -dir_path="/var/lib/libvirt/images/.image_store" -src_file="${dir_path}/freebsd14.0.qcow2" -config_dir="/var/lib/libvirt/images/.temp" -user_data="${config_dir}/user-data" -meta_data="${config_dir}/meta-data" -seed_iso="${config_dir}/seed.iso" - -if [ $# -ne 1 ]; then - echo "Usage: $0 <vm-name>" - exit 1 -fi - -sudo mkdir -p "$dir_path" -sudo mkdir -p "$config_dir" - -# Download and extract FreeBSD image if it doesn't exist -if [ ! -f "$src_file" ]; then - echo "source image does not exist! downloading..." - sudo wget -O "${src_file}.xz" "$image_url" - sudo xz -d "${src_file}.xz" -fi - -cat > "$user_data" << EOF -#cloud-config -users: - - name: user - passwd: 'yourpassword' - lock_passwd: false - ssh-authorized-keys: -EOF - -for key in $sshkeysdir/*.pub; do - echo " - $(cat "$key")" >> $user_data -done - -cat >> "$user_data" << 'EOF' - groups: wheel - shell: /bin/tcsh - -# FreeBSD specific configuration -package_update: true -package_upgrade: true - -packages: - - vim-console - - git - - doas - -write_files: - - path: /usr/local/etc/doas.conf - content: | - permit nopass :wheel - permissions: '0600' - -runcmd: - - pw usermod user -s /bin/tcsh - - env ASSUME_ALWAYS_YES=YES pkg update - - env ASSUME_ALWAYS_YES=YES pkg upgrade - - env ASSUME_ALWAYS_YES=YES pkg install vim-console git doas - - 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" diff --git a/others/vm3/.config/cloud-init-generator/freebsd14.0.shbackup b/others/vm3/.config/cloud-init-generator/freebsd14.0.shbackup deleted file mode 100755 index 81fcc72..0000000 --- a/others/vm3/.config/cloud-init-generator/freebsd14.0.shbackup +++ /dev/null @@ -1,59 +0,0 @@ -#!/bin/bash -# generates configuration for FreeBSD VM deployment -# requires the name of the vm as an argument - -image_url="https://download.freebsd.org/releases/VM-IMAGES/14.2-RELEASE/amd64/Latest/FreeBSD-14.2-RELEASE-amd64-BASIC-CLOUDINIT.zfs.qcow2.xz" -sshkeysdir="/root/k" -scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -dir_path="/var/lib/libvirt/images/.image_store" -src_file="${dir_path}/freebsd14.0.qcow2" -config_dir="/var/lib/libvirt/images/.temp" -user_data="${config_dir}/user-data" -meta_data="${config_dir}/meta-data" -seed_iso="${config_dir}/seed.iso" - -if [ $# -ne 1 ]; then - echo "Usage: $0 <vm-name>" - exit 1 -fi - -sudo mkdir -p "$dir_path" -sudo mkdir -p "$config_dir" - -# Download and extract FreeBSD image if it doesn't exist -if [ ! -f "$src_file" ]; then - echo "source image does not exist! downloading..." - sudo wget -O "${src_file}.xz" "$image_url" - sudo xz -d "${src_file}.xz" -fi - -# Note: FreeBSD uses a different user setup mechanism -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: wheel - shell: /bin/sh -runcmd: - - pw usermod user -s /bin/sh - - pkg update -f - - pkg install -y vim git sudo - - 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" diff --git a/others/vm3/.config/cloud-init-generator/rocky9.0.sh b/others/vm3/.config/cloud-init-generator/rocky9.0.sh deleted file mode 100644 index c880206..0000000 --- a/others/vm3/.config/cloud-init-generator/rocky9.0.sh +++ /dev/null @@ -1,53 +0,0 @@ - -#!/bin/bash -# generates user data, meta data, and seed.iso for cloud init. for rocky -# requires the name of the vm as an argument -# -# osinfo-query os to list all available vm types to deploy. rocky is rocky9.0 or similar -image_url="https://dl.rockylinux.org/pub/rocky/9/images/x86_64/Rocky-9-GenericCloud-Base.latest.x86_64.qcow2" -sshkeysdir="/root/k" -scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" #&& echo $scriptdir -dir_path="/var/lib/libvirt/images/.image_store" -src_file="${dir_path}/rocky9.0.qcow2" -config_dir="/var/lib/libvirt/images/.temp" -user_data="${config_dir}/user-data" -meta_data="${config_dir}/meta-data" -seed_iso="${config_dir}/seed.iso" -# note that cloud init specifically looks for "user-data" and "meta-data" in the seed.iso disk. do not deviate from the naming convention. -if [ $# -ne 1 ]; then - echo "Usage: $0 <vm-name>" - exit 1 -fi -sudo mkdir -p "$dir_path" -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: wheel - shell: /bin/bash -runcmd: - - sudo systemctl enable --now serial-getty@ttyS0.service - - sudo growpart /dev/sda 1 - - sudo xfs_growfs / - - sudo dnf install -y vim git - - cd /home/user - - git clone https://git.0nom.ch/setup - - 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" - - diff --git a/others/vm3/.config/cloud-init-generator/ubuntu24.04.sh b/others/vm3/.config/cloud-init-generator/ubuntu24.04.sh deleted file mode 100755 index 11e0a98..0000000 --- a/others/vm3/.config/cloud-init-generator/ubuntu24.04.sh +++ /dev/null @@ -1,62 +0,0 @@ -#!/bin/bash -# generates user data, meta data, and seed.iso for cloud init. for Ubuntu -# requires the name of the vm as an argument - -image_url="https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img" -sshkeysdir="/root/k" -scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -dir_path="/var/lib/libvirt/images/.image_store" -src_file="${dir_path}/ubuntu24.04.qcow2" -config_dir="/var/lib/libvirt/images/.temp" -user_data="${config_dir}/user-data" -meta_data="${config_dir}/meta-data" -seed_iso="${config_dir}/seed.iso" -temp_img="${dir_path}/ubuntu_temp.img" - -if [ $# -ne 1 ]; then - echo "Usage: $0 <vm-name>" - exit 1 -fi - -sudo mkdir -p "$dir_path" -sudo mkdir -p "$config_dir" - -if [ ! -f "$src_file" ]; then - echo "source image does not exist! downloading..." - sudo wget -O "$temp_img" "$image_url" - echo "Converting to qcow2 format..." - sudo qemu-img convert -f raw -O qcow2 "$temp_img" "$src_file" - sudo rm "$temp_img" -fi - -cat > "$user_data" << EOF -#cloud-config -users: - - name: ubuntu - 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/ubuntu - - touch /home/ubuntu/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" diff --git a/others/vm3/.config/init/install_packages.sh b/others/vm3/.config/init/install_packages.sh deleted file mode 100755 index d5ca81f..0000000 --- a/others/vm3/.config/init/install_packages.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash - -#install packages -packages=("nc" "htop" "wireguard-tools" "bind-utils" "tmux" "net-tools" "curl" "mlocate" "dnsmasq" "qemu-kvm" "libvirt" "libvirt-daemon-kvm" "virt-install" "virt-manager" "genisoimage" "bc") - -for package in "${packages[@]}"; do - if ! rpm -q "$package" &> /dev/null; then - sudo dnf install -y "$package" - fi -done - -sudo systemctl enable --now libvirtd diff --git a/others/vm3/.config/init/make_rootkey.sh b/others/vm3/.config/init/make_rootkey.sh deleted file mode 100755 index f725985..0000000 --- a/others/vm3/.config/init/make_rootkey.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash - -sudo mkdir -p /root/k -ssh-keygen -t ed25519 -f /root/k/k1 -N "" diff --git a/others/vm3/compute/create.sh b/others/vm3/compute/create.sh deleted file mode 100755 index ab481cd..0000000 --- a/others/vm3/compute/create.sh +++ /dev/null @@ -1,123 +0,0 @@ -#!/bin/bash - -# takes in vm name, os type, vcpu, ram, disk as argument -# takes in already generated seed iso and downloaded vm.iso file - -# default values -vcpu=8 -ram_gb=8 -disk_gb=64 -os="fedora40" -ostype="linux" - -# parse arguments -while [[ $# -gt 0 ]]; do - case $1 in - --vcpu) - vcpu="$2" - shift 2 - ;; - --ram) - ram_gb="$2" - shift 2 - ;; - --disk-size) - disk_gb="$2" - shift 2 - ;; - --image) - os="$2" - shift 2 - ;; - *) - # Handle positional arguments (vmname and os) - if [ -z "$vmname" ]; then - vmname="$1" - else - echo "Unknown argument: $1" - exit 1 - fi - shift - ;; - esac -done - -scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" #&& echo $scriptdir -workingdir="/var/lib/libvirt/images/.temp" -seed_iso="${workingdir}/seed.iso" -xml="${workingdir}/xml" - -image_dir_path="/var/lib/libvirt/images/.image_store" -src_file="${image_dir_path}/${os}.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!! -if [ -z "$vmname" ] ; then - echo "" - echo "Usage: $0 <vm-name> [--image <os>] [--vcpu N] [--ram N] [--disk-size N]" - echo "seed.iso and image file have to be present! Default os is fedora" - echo "" - echo "Available images:" - sudo ls -1 /var/lib/libvirt/images/.image_store | sed 's/\.qcow2$//' - echo "" - echo "Available images to download:" - sudo ls -1 "${scriptdir}/../.config/cloud-init-generator/" | sed 's/\.sh$//' - exit 1 -fi - -# run the script to make the cloud init files -sudo bash "${scriptdir}/../.config/cloud-init-generator/${os}.sh" "${vmname}" - -if [ ! -f "${src_file}" ]; then - echo -e "${os} image file is cannot be found. please make it available in ${image_dir_path}" - exit 1 -fi - -sudo mkdir -p $new_vm_config_dir - -sudo cp "$src_file" "$new_vm" &> /dev/null || { echo "Failed to create a new image."; exit 1; } - -if sudo virsh list --all | awk "\$2==\"$vmname\"" | grep -q .; then - echo -e "\n$vmname already exist. Delete it before using the same name." - exit 1 -fi - -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" - virt-customize -a ${new_vm} --run-command "rm -f /etc/machine-id && dbus-uuidgen --ensure=/etc/machine-id && echo ${vmname} > /etc/hostname" -else - disk_opts="--disk path=${new_vm},format=qcow2 --disk path=$seed_iso,device=cdrom" -fi - -generate_mac() { - printf "52:54:00:%02x:%02x:%02x\n" $((RANDOM%256)) $((RANDOM%256)) $((RANDOM%256)) -} - -# Use the conditional disk options in virt-install -sudo virt-install --name $vmname \ - --vcpus $vcpu \ - --memory "$((ram_gb * 1024))" \ - $disk_opts \ - --os-type $ostype \ - --os-variant $os \ - --virt-type kvm \ - --graphics none \ - --network bridge=virbr0,model=virtio,mac=$(generate_mac) \ - --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; } - -sudo qemu-img resize $new_vm +$disk_gb"G" #&> /dev/null - -sudo virsh start $vmname - -sudo rm "${workingdir}"/* - diff --git a/others/vm3/compute/ls.sh b/others/vm3/compute/ls.sh deleted file mode 100755 index b821fd1..0000000 --- a/others/vm3/compute/ls.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 "<source network" | awk -F\' '{print $2}') - if [ -z "$net" ]; then - net="default" - fi - - # Use cached leases - ip=$(echo "$default_leases" | grep "$mac" | awk '{print $5}' | cut -f1 -d'/') - - # Run commands in background and save to temp files - sudo virsh domstate "$vm" 2>/dev/null > /tmp/state.$$ & - echo "$xml" | grep "<vcpu" | awk -F'[<>]' '{print $3}' > /tmp/vcpus.$$ & - echo "$xml" | grep "<memory" | awk -F'[<>]' '{print $3}' | awk '{ printf "%.2f", $1/1048576 }' > /tmp/ram.$$ & - sudo du -sk "/var/lib/libvirt/images/${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/rm.sh b/others/vm3/compute/rm.sh deleted file mode 100755 index f77831c..0000000 --- a/others/vm3/compute/rm.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/shutdown.sh b/others/vm3/compute/shutdown.sh deleted file mode 100755 index 081499b..0000000 --- a/others/vm3/compute/shutdown.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash - -for vm in "$@"; do - sudo virsh shutdown "$vm" > /dev/null 2>&1 - printf "%-50s%10s\n" "Shutting down $vm..." $([[ $? -eq 0 ]] && echo "Successful" || echo "Failed") -done diff --git a/others/vm3/compute/start.sh b/others/vm3/compute/start.sh deleted file mode 100755 index d64dbb1..0000000 --- a/others/vm3/compute/start.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/bash - -# starts vm -# remove seed.iso first if vm have the seed iso - -vmname=$1 - -# Get the target device for seed.iso -target_dev=$(sudo virsh domblklist $vmname | grep 'seed.iso' | awk '{print $1}') - -# If seed.iso is found, detach it, -# --config means it is persistent but do not affect the state now. -# --config --live means online remove -# no option means that the changes are not persistent -# -if [ ! -z "$target_dev" ]; then - sudo virsh detach-disk $vmname $target_dev --config -fi - -sudo virsh start $vmname diff --git a/others/vm3/default.xml b/others/vm3/default.xml deleted file mode 100644 index bf71f7c..0000000 --- a/others/vm3/default.xml +++ /dev/null @@ -1,13 +0,0 @@ -<network> - <name>default</name> - <uuid>3d6341fb-fad2-49c1-b54a-41d2027f56cc</uuid> - <forward mode='nat'/> - <bridge name='virbr0' stp='on' delay='0'/> - <mac address='52:54:00:73:4c:22'/> - <ip address='192.168.124.1' netmask='255.255.255.0'> - <dhcp> - <range start='192.168.124.2' end='192.168.124.254'/> - <option opt='61' value='ignore'/> - </dhcp> - </ip> -</network> diff --git a/others/vm3/docs b/others/vm3/docs deleted file mode 100644 index eea91d1..0000000 --- a/others/vm3/docs +++ /dev/null @@ -1,121 +0,0 @@ - -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 - - - -vm compute create name1 -os fedora -vcpu 1 -ram 1 -disk 10 -s.pool storage1 -add-disk 200 storage2 -add-network nw3 # default fedora 4,4,20 - # fedora not available. available os: listed down - # storage not available. please create it - # nw3 not available. create y/n? -vm network create <name> 192.168.100.1 /32 -vm network create <name> (auto) -vm storage create <name> /dev/ -vm storage create <name2> /dir/ - - -vm/ - ./vm.sh # Single entry point. one vm at a time(except for compute delete-all) - .config/ # all config files - init/ # run this everytime. try to think of a way to set a flag that says this device has been initialised - install_packages.sh - make_rootkey.sh # in /root/k/k1. can also add keys in here - /var/lib/libvirt/images/.image_store/ # stores the images - /var/lib/libvirt/images/.temp/ # for user-data, meta-data, and seed.iso(has ud and md inside) need to delete after initialising a new vm - image/ # vm cloud init templates to make user-data, meta-data, and seed.iso - fedora.sh <vm name> # images in /var/lib/libvirt/images/.image_store/ and in images/.temp - opensuse.sh - debian.sh - freebsd.sh - list-all-images.sh - compute/ - create.sh - # default vcpu ram disk storage pool network - # allow specification of storage pool and network. - # auto list all available images, storage pools, networks. on no argument. just call stroage/network list.sh - delete.sh and all associated virtual machines and block devices on all storage device - start.sh before starting the vm, make sure the cloud init seed.iso is not attached anymore. - shutdown.sh - list.sh # list by (sorted by) network group, ip address, then cpu ram disk, total disk(incl all attachedvols), name - network/ - attach.sh # one click, so if vm is running, ask fro permission to shutdown. or if can, live attach - list.sh - detach.sh - create.sh # allow specifying the ip address range. auto make uuid - delete.sh - disk/ - attach.sh - detach.sh - create.sh - list all available storage hardware devices on no argument. just call list.sh - make directory linking from /var/lib/libvirt/images/${VM_NAME}-vm-disks/ to the actual storage device - delete.sh - delete all blocks. all the linking if can. may not be able to link like that idk. list all mounts and delete the originla and the links - list.sh #list storage pools and include --all to view all blocks and their sizes - device/ - usb/ - attach.sh - detach.sh - view.sh # usbs of all the vms - -# No arguments shows available options -./vm compute create -Available networks: - prod-net 192.168.1.0/24 - dev-net 192.168.2.0/24 - test-net 192.168.3.0/24 - -Available storage pools: - prod-store 2TB free - dev-store 500GB free - backup 1TB free - -Available images: - fedora40(41) - - -Usage: ./vm compute create <name> <os> --vcpu 4 --ram 8 --disk 40 --storage-pool prod-store --network prod-net - -# Then use directly -./vm compute create myvm fedora40 prod-net prod-store - - - -os - debian functionally works, and issues are primarily quality of life issues - Debian primarily uses /etc/bash.bashrc, move the colour to this instead of etc bashrc on fedora - change the localhost to name of the vm in the config app - - - freebsd have not tried editing root password, boots on generic cloud image, no login yet - ubuntu no boot no login yet. - -others - ssh - public ssh port like online vps - this forwards 2222 on the host to port 22 - # 1. First, check your VM's IP address (from inside the VM) - ip addr show - # 2. Set up port forwarding in libvirt - virsh net-list # List networks - virsh net-edit default # Edit default network config - # Add this inside the <network> section: - <forward mode='nat'> - <nat> - <port start='2222' end='2222'> - <protocol>tcp</protocol> - <forward address='192.168.122.x' port='22'/> # Replace x with your VM's last octet - </port> - </nat> - </forward> - # 3. Restart the network - virsh net-destroy default - virsh net-start default - # 4. Configure firewall on Fedora host - sudo firewall-cmd --add-port=2222/tcp --permanent - sudo firewall-cmd --reload - seed.iso - move seed.iso to images/vmname/seed.iso - allows concurrent usage of vm.sh as there will be no conflicts diff --git a/others/vm3/meta-data b/others/vm3/meta-data deleted file mode 100644 index ce225e1..0000000 --- a/others/vm3/meta-data +++ /dev/null @@ -1,2 +0,0 @@ -instance-id: iid-fedora-vm -local-hostname: fedora-cloudimg diff --git a/others/vm3/user-data b/others/vm3/user-data deleted file mode 100644 index 58817da..0000000 --- a/others/vm3/user-data +++ /dev/null @@ -1,27 +0,0 @@ -#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 deleted file mode 100755 index a043f89..0000000 --- a/others/vm3/vm.sh +++ /dev/null @@ -1,119 +0,0 @@ -#!/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" - exit 1 -fi - -# Get script directory for relative paths -SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" - -# First argument is the category (compute, network, storage, etc) -category=$1 -shift - -# Exit if no category specified -if [ -z "$category" ]; then - echo -e "Usage: ./vm <category> <action> [args...]\n" - echo -e "Categories: \ncompute\nnetwork\nstorage\ndevice\n" - echo "Run ./vm <category> for available subactions or tree for all available actions." - exit 1 -fi - -# Second argument is the action -action=$1 -shift - -# Handle each category -case $category in - compute) - case $action in - create) - $SCRIPT_DIR/compute/create.sh "$@" - ;; - start) - $SCRIPT_DIR/compute/start.sh "$@" - ;; - ls) - $SCRIPT_DIR/compute/ls.sh "$@" - ;; - shutdown) - $SCRIPT_DIR/compute/shutdown.sh "$@" - ;; - rm) - $SCRIPT_DIR/compute/rm.sh "$@" - ;; - *) - echo -e "Available compute actions: \ncreate\nstart\nls\nshutdown\ndelete" - exit 1 - ;; - esac - ;; - - network) - case $action in - attach) - $SCRIPT_DIR/network/attach.sh "$@" - ;; - detach) - $SCRIPT_DIR/network/detach.sh "$@" - ;; - list) - $SCRIPT_DIR/network/list.sh "$@" - ;; - create) - $SCRIPT_DIR/network/create.sh "$@" - ;; - delete) - $SCRIPT_DIR/network/delete.sh "$@" - ;; - *) - echo "Available network actions: \ncreate\nattach\ndetach\nlist\ndelete" - exit 1 - ;; - esac - ;; - - disk) - case $action in - attach) - $SCRIPT_DIR/disk/attach.sh "$@" - ;; - list) - $SCRIPT_DIR/disk/list.sh "$@" - ;; - *) - echo "Available disk actions: \ncreate\nattach\ndetach\nlist\ndelete" - exit 1 - ;; - esac - ;; - - storage-pool) - case $action in - create) # using a directory as a storage pool - $SCRIPT_DIR/storage-pool/create.sh "$@" - ;; - list) - $SCRIPT_DIR/storage-pool/list.sh "$@" - ;; - create-from-device) # initialise and use a devcie as storage pool - $SCRIPT_DIR/storage-pool/create-from-device.sh "$@" - ;; - - *) - echo "Available disk actions: \ncreate\nlist\ncreate-from-device\ndelete" - exit 1 - ;; - esac - ;; - - - *) - echo "Unknown category: $category" - echo "Available categories: compute, network, storage" - exit 1 - ;; -esac |
