summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorroot <root@sg1.noml.ch>2025-05-22 18:36:01 +0800
committerroot <root@sg1.noml.ch>2025-05-22 18:36:01 +0800
commit3a52e92fbbbab4df949740f89a389d68281a9428 (patch)
tree5cb3b864eb0e3f3b90a1efd9cbdee1fb9f158f4a
parent8cb260002db7c46ea5fe2c3f589b485e5f5d970d (diff)
-rwxr-xr-xcore/install-packages.sh4
-rwxr-xr-xcore/ssh.sh3
-rwxr-xr-xcore/vim.sh10
-rw-r--r--others/.vm.sh.swpbin16384 -> 0 bytes
-rwxr-xr-xothers/init.sh1
-rw-r--r--others/meta-data2
-rw-r--r--others/openstack_config.txt14
-rw-r--r--others/setup_2_openstackenv.sh15
-rw-r--r--others/user-data16
-rwxr-xr-xothers/vm.sh316
-rwxr-xr-xothers/vm.sh2291
-rwxr-xr-xothers/vm3/.config/cloud-init-generator/debian12.sh67
-rwxr-xr-xothers/vm3/.config/cloud-init-generator/fedora40.sh105
-rwxr-xr-xothers/vm3/.config/cloud-init-generator/freebsd14.0.sh75
-rwxr-xr-xothers/vm3/.config/cloud-init-generator/freebsd14.0.shbackup59
-rw-r--r--others/vm3/.config/cloud-init-generator/rocky9.0.sh53
-rwxr-xr-xothers/vm3/.config/cloud-init-generator/ubuntu24.04.sh62
-rwxr-xr-xothers/vm3/.config/init/install_packages.sh12
-rwxr-xr-xothers/vm3/.config/init/make_rootkey.sh4
-rwxr-xr-xothers/vm3/compute/create.sh123
-rwxr-xr-xothers/vm3/compute/ls.sh45
-rwxr-xr-xothers/vm3/compute/rm.sh27
-rwxr-xr-xothers/vm3/compute/shutdown.sh6
-rwxr-xr-xothers/vm3/compute/start.sh20
-rw-r--r--others/vm3/default.xml13
-rw-r--r--others/vm3/docs121
-rw-r--r--others/vm3/meta-data2
-rw-r--r--others/vm3/user-data27
-rwxr-xr-xothers/vm3/vm.sh119
-rwxr-xr-xport-forward/rm_all_ssh_connections.sh (renamed from others/reset-ssh-connections.sh)0
-rwxr-xr-xport-forward/ssh-port-forward.sh2
31 files changed, 12 insertions, 1602 deletions
diff --git a/core/install-packages.sh b/core/install-packages.sh
index 9a6aea3..75426a7 100755
--- a/core/install-packages.sh
+++ b/core/install-packages.sh
@@ -2,8 +2,8 @@
2 2
3 3
4#install packages 4#install packages
5#sudo dnf install -y epel-release 5sudo dnf install -y epel-release
6sudo dnf group install -y "Development Tools" 6#sudo dnf group install -y "Development Tools"
7sudo dnf install -y nc openssl bat autossh tmux htop tar bmon gzip tree wget curl plocate nano vim unzip net-tools git python3 python3-pip make wireguard-tools usbutils yum 7sudo dnf install -y nc openssl bat autossh tmux htop tar bmon gzip tree wget curl plocate nano vim unzip net-tools git python3 python3-pip make wireguard-tools usbutils yum
8sudo dnf install -y xclip 8sudo dnf install -y xclip
9 9
diff --git a/core/ssh.sh b/core/ssh.sh
index 3cf2583..c8fb49d 100755
--- a/core/ssh.sh
+++ b/core/ssh.sh
@@ -3,6 +3,7 @@
3echo "PasswordAuthentication no" | sudo tee -a /etc/ssh/sshd_config 3echo "PasswordAuthentication no" | sudo tee -a /etc/ssh/sshd_config
4echo "AllowTcpForwarding yes" | sudo tee -a /etc/ssh/sshd_config 4echo "AllowTcpForwarding yes" | sudo tee -a /etc/ssh/sshd_config
5echo "GatewayPorts yes" | sudo tee -a /etc/ssh/sshd_config 5echo "GatewayPorts yes" | sudo tee -a /etc/ssh/sshd_config
6echo "AllowAgentForwarding yes" | sudo tee -a /etc/ssh/sshd_config
6 7
7git clone https://git.0nom.ch/keys ~/keys 8git clone https://git.noml.ch/keys ~/keys
8~/keys/add-ssh-keys.sh 9~/keys/add-ssh-keys.sh
diff --git a/core/vim.sh b/core/vim.sh
index e82414a..883a75e 100755
--- a/core/vim.sh
+++ b/core/vim.sh
@@ -5,7 +5,7 @@ cd "$(dirname "${BASH_SOURCE[0]}")" || exit
5cat << EOF >> /etc/vimrc 5cat << EOF >> /etc/vimrc
6augroup netcat_clipboard 6augroup netcat_clipboard
7 au! 7 au!
8 au TextYankPost * call system("openssl enc -aes-256-cbc -pbkdf2 -pass pass:YourPassword | nc -w 1 p.0nom.ch 5023", @") 8 au TextYankPost * call system("openssl enc -aes-256-cbc -pbkdf2 -pass pass:YourPassword | nc -w 1 p.noml.ch 5023", @")
9augroup END 9augroup END
10 10
11" Enable line wrapping 11" Enable line wrapping
@@ -18,9 +18,9 @@ set breakindentopt=shift:1
18syntax on 18syntax on
19set mouse=a 19set mouse=a
20set expandtab 20set expandtab
21set shiftwidth=2 21set shiftwidth=4
22set softtabstop=2 22set softtabstop=4
23set tabstop=2 23set tabstop=4
24set noautoindent 24set noautoindent
25set nosmartindent 25set nosmartindent
26filetype plugin indent on 26filetype plugin indent on
@@ -50,6 +50,8 @@ nnoremap <C-e> $
50set number 50set number
51setlocal regexpengine=2 51setlocal regexpengine=2
52set paste 52set paste
53set foldcolumn=12
54
53 55
54EOF 56EOF
55 57
diff --git a/others/.vm.sh.swp b/others/.vm.sh.swp
deleted file mode 100644
index 962a1ee..0000000
--- a/others/.vm.sh.swp
+++ /dev/null
Binary files differ
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 @@
1/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 @@
1instance-id: iid-fedora-vm
2local-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 @@
1export OS_AUTH_URL=https://api.pub1.infomaniak.cloud/identity/v3
2export OS_PROJECT_NAME=PCP-UV9N94K
3export OS_PROJECT_DOMAIN_NAME=default
4export OS_USERNAME=PCU-UV9N94K
5export OS_USER_DOMAIN_NAME=default
6export OS_PROJECT_ID=4b23183ad37e4271900622f738cf3a17
7export OS_IDENTITY_API_VERSION=3
8export OS_INTERFACE=public
9export OS_REGION_NAME=dc3-a
10# To avoid being prompted for your password each time,
11# write your password below and uncomment the line
12#OS_PASSWORD='your password goes here'
13[ -z "$OS_PASSWORD" ] && read -e -p "Please enter your OpenStack Password for project $OS_PROJECT_NAME as user $OS_USERNAME: " OS_PASSWORD
14export OS_PASSWORD
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 @@
1#!/bin/bash
2
3# directories
4mkdir ~/work
5mkdir ~/work/mounts
6mkdir ~/work/mounts/private
7mkdir ~/work/mounts/public
8mkdir ~/work/mounts/.localtmp
9
10touch ~/work/mounts/.passwd-s3fs
11chmod 600 ~/work/mounts/.passwd-s3fs
12echo "id:pass" > ~/work/mounts/.passwd-s3fs
13sudo 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
14sudo 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
15
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 @@
1#cloud-config
2users:
3 - name: fedora
4 ssh-authorized-keys:
5 -
6 sudo: ['ALL=(ALL) NOPASSWD:ALL']
7 groups: wheel
8 shell: /bin/bash
9runcmd:
10 - sudo growpart /dev/sda 1
11 - sudo xfs_growfs /
12 - sudo dnf install -y vim git
13 - cd /home/fedora
14 - git clone https://git.0nom.ch/setup
15 - sudo ./setup/setup.sh
16 - 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 @@
1#!/bin/bash
2
3help(){
4 echo -e "\n$0
5 info
6 create\n -vcpu\n -ram \n -disk \n -ssh-keys-dir \n -add-disk-block\n -public-ssh-port
7 manage\n -start\n -shutdown\n -reboot\n -kill\n -erase
8 disk\n -attach\n -detach\n -resize\n"
9 exit 1
10}
11
12case "$1" in
13 "create")
14 if [[ $# -lt 2 ]]; then
15 echo ""
16 echo -e "$0 $1 [VM_NAME]"
17 echo -e "default: -vcpu 8 -ram 8 -disk 60 --ssh-keys-dir keys/"
18 echo -e "args[GB]:\n -vcpu\n -ram \n -disk \n -ssh-keys-dir \n -add-disk-block\n -public-ssh-port)"
19 echo ""
20 exit 1
21 fi
22
23 VM_NAME=$2
24 VCPU=8
25 RAM_GB=8000 #memory is noted as mb in the virt-install program
26 DISK_GB=64
27 SSH_KEYS_DIR="keys/"
28 DISK_BLOCK_GB=0
29 PSP=${PSP:-0}
30
31 while [[ $# -gt 0 ]]; do
32 case "$1" in
33 create)
34 shift 2
35 ;;
36 -vcpu)
37 VCPU=$2
38 shift 2
39 ;;
40 -ram)
41 RAM_GB=$(($2*1000))
42 shift 2
43 ;;
44 -disk)
45 DISK_GB=$2
46 shift 2
47 ;;
48 -ssh-keys-dir)
49 SSH_KEYS_DIR=$2
50 shift 2
51 ;;
52 -add-disk-block)
53 DISK_BLOCK_GB=$2
54 shift 2
55 ;;
56 -public-ssh-port)
57 PSP=$2
58 shift 2
59 ;;
60 *)
61 echo "Unknown argument: $1"
62 exit 1
63 ;;
64 esac
65 done
66 download_url="https://download.rockylinux.org/pub/rocky/9.3/images/x86_64/Rocky-9-GenericCloud-LVM-9.3-20231113.0.x86_64.qcow2"
67 dir_path="/var/lib/libvirt/images/"
68 src_file="/var/lib/libvirt/images/Rocky9.3.qcow2"
69 seed_iso="/var/lib/libvirt/images/seed.iso"
70 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")
71 NEW_IMG_PATH="/var/lib/libvirt/images/${VM_NAME}.qcow2"
72 XML_PATH="/tmp/${VM_NAME}.xml"
73 DISK_BLOCK_GB_BASE_PATH="/var/lib/libvirt/images/${VM_NAME}-vm-disks/"
74 DISK_BLOCK_GB_PATH="${DISK_BLOCK_GB_BASE_PATH}${VM_NAME}-vda-${DISK_BLOCK_GB}G.qcow2"
75 ssh_private=$(cat /home/s22/man1)
76
77 function check_package_installed() {
78 local package_name=$1
79 rpm -q "$package_name" &> /dev/null
80 }
81
82 for package in "${packages[@]}"; do
83 if ! check_package_installed "$package"; then
84 #echo "Downloading packages..."
85 sudo dnf install -y "$package" &> /dev/null
86 fi
87 done
88
89 [ ! -d $dir_path ] && mkdir -p $dir_path &> /dev/null
90 [ ! -f "$src_file" ] && (wget -O "$src_file" "$download_url" &> /dev/null || { echo "Failed to download Rocky"; exit 1; })
91
92 echo -e "#cloud-config\nusers:\n - name: root\n ssh-authorized-keys:" > user-data
93
94 for key in "$SSH_KEYS_DIR"/*.pub; do
95 echo " - $(cat "$key")" >> user-data
96 done
97
98 echo " sudo: ['ALL=(ALL) NOPASSWD:ALL']
99 groups: sudo
100 shell: /bin/bash" >> user-data
101
102 if [ "$PSP" -ne 0 ]; then
103 echo "write_files:" >> user-data
104 echo " - path: /root/man1" >> user-data
105 echo " content: |" >> user-data
106 while read -r line; do
107 echo " $line" >> user-data
108 done <<< "${ssh_private}"
109 fi
110
111 echo 'runcmd:' >> user-data
112 #echo ' - [ /usr/bin/wget, "http://example.com/file", -O, /tmp/examplefile ]' >> user-data
113 #echo ' - touch /root/test1.txt' >> user-data
114 if [ "$DISK_BLOCK_GB" -ne 0 ]; then
115 echo "echo to disksetup in vm"
116 echo "vm name 1 ${VM_NAME}"
117 echo ' - echo "#!/bin/bash" > /root/disk-setup.sh' >> user-data
118 # echo ' - echo "sleep 40;" >> /root/disk-setup.sh' >> user-data
119 echo " - echo \"DISK='/dev/vda'; MOUNT_PATH='/home/${VM_NAME}-vda-${DISK_BLOCK_GB}G';\" >> /root/disk-setup.sh" >> user-data
120 echo " - echo '[ ! -d \$MOUNT_PATH ] && mkdir -p \$MOUNT_PATH;' >> /root/disk-setup.sh" >> user-data
121 echo " - echo 'blkid | grep -q \$DISK || mkfs.ext4 \$DISK;' >> /root/disk-setup.sh" >> user-data
122 echo " - echo 'grep -q \$DISK /etc/fstab || echo \"\$DISK \$MOUNT_PATH ext4 defaults,nofail 0 0\" >> /etc/fstab;' >> /root/disk-setup.sh" >> user-data
123 echo ' - echo "mount -a;" >> /root/disk-setup.sh' >> user-data
124 echo ' - echo "systemctl daemon-reload" >> /root/disk-setup.sh' >> user-data
125 echo ' - chmod +x /root/disk-setup.sh' >> user-data
126 echo ' - /root/disk-setup.sh' >> user-data
127 echo ' - rm -f /root/disk-setup.sh' >> user-data
128 fi
129
130 if [ "$PSP" -ne 0 ]; then
131 echo ' - chmod 600 /root/man1' >> user-data
132 #kill ssh and reconnect every 4 hours
133 ##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
134 echo " - (echo \"* * * * * /root/initial.sh\") | crontab -" >> user-data
135 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
136 echo " - chmod +x /root/initial.sh" >> user-data
137 echo " - /root/initial.sh" >> user-data
138 fi
139
140 #nofail is present in the fstab which means that boot will continue even if it fails to mount
141 echo ' - growpart /dev/sda 4 ' >> user-data #do note that restart is required for the system to register the increased disk size
142 echo ' - sudo lvresize -l +100%FREE /dev/rocky/lvroot' >> user-data
143 echo ' - sudo dnf install -y epel-release dnf-utils' >> user-data
144 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
145 echo ' - touch /root/runcmd_done' >> user-data
146
147 echo -e "instance-id: iid-ihatecs\nlocal-hostname: cloudimg" > meta-data
148
149 genisoimage -output "$seed_iso" -volid cidata -joliet -rock user-data meta-data &> /dev/null || { echo "Failed to create seed.iso."; exit 1; }
150
151 cp $src_file $NEW_IMG_PATH &> /dev/null || { echo "Failed to create a new image."; exit 1; }
152
153 if virsh list --all | grep -q "$VM_NAME"; then
154 echo -e "\n$VM_NAME already exist. Delete it using \n$0 manage -erase $VM_NAME\nExiting..."
155 exit 1
156 fi
157
158 sudo virt-install --name $VM_NAME \
159 --vcpus $VCPU \
160 --ram $RAM_GB \
161 --disk path=$NEW_IMG_PATH,size=$DISK_GB,format=qcow2 \
162 --disk path=$seed_iso,device=cdrom \
163 --os-type linux \
164 --os-variant rhl9 \
165 --virt-type kvm \
166 --graphics none \
167 --network bridge=virbr0,model=virtio \
168 --print-xml > $XML_PATH || { echo "Failed to print XML."; exit 1; }
169
170 sudo virsh define $XML_PATH &> /dev/null || { echo "Failed to define the new VM."; exit 1; }
171
172 sudo qemu-img resize $NEW_IMG_PATH +$DISK_GB"G" #&> /dev/null
173
174 virsh start $VM_NAME
175 echo ""
176
177 if [ $DISK_BLOCK_GB -ne 0 ]; then
178 mkdir -p $DISK_BLOCK_GB_BASE_PATH
179 qemu-img create -f qcow2 $DISK_BLOCK_GB_PATH "${DISK_BLOCK_GB}G"
180 virsh attach-disk $VM_NAME $DISK_BLOCK_GB_PATH vda --cache none --subdriver qcow2
181 fi
182
183 message="waiting 29s to begin finding ip address..."
184 duration=29
185 for ((i=$duration; i>=1; i--)); do
186 printf "\r%s%2ds" "$message" $i
187 sleep 1
188 done
189 echo
190
191 while true; do
192 OUTPUT=$($0 info | grep "$VM_NAME")
193 if [[ "$OUTPUT" != "" ]]; then
194 IP_ADDRESS=$(echo "$OUTPUT" | grep -oP '\d+\.\d+\.\d+\.\d+')
195 # If IP address is found and is not empty
196 if [[ ! -z "$IP_ADDRESS" ]]; then
197 echo -e "\nIP address of $VM_NAME is:\n$IP_ADDRESS"
198 break
199 else
200 echo "IP address for $VM_NAME not found. Retrying in 1 second..."
201 sleep 1
202 fi
203 else
204 echo "$VM_NAME not found. Exiting..."
205 exit 1
206 fi
207 done
208
209 #do take note that df -h will not reflect until reboot
210
211 ;;
212 "manage")
213 if [[ $# -lt 2 ]]; then
214 echo ""
215 echo -e "$0 $1 arg [VM_1] [VM_2] [VM_3] \n$0 $1 arg --all"
216 echo -e "args:\n -start\n -shutdown\n -kill\n -erase "
217 echo ""
218 exit 1
219 fi
220
221 arg=$2
222 shift 2
223
224 if [[ "$1" == "--all" ]]; then
225 vms=$(virsh list --all --name) # List all running VMs by name
226 set -- $vms # Set the positional parameters to the VM names
227 fi
228
229 case $arg in
230 "-shutdown")
231 for vm in "$@"; do
232 virsh shutdown "$vm" > /dev/null 2>&1
233 printf "%-50s%10s\n" "Shutting down $vm..." $([[ $? -eq 0 ]] && echo "Successful" || echo "Failed")
234 done
235 ;;
236 "-kill")
237 for vm in "$@"; do
238 virsh destroy "$vm" > /dev/null 2>&1
239 printf "%-50s%10s\n" "Destroying $vm..." $([[ $? -eq 0 ]] && echo "Successful" || echo "Failed")
240 done
241 ;;
242 "-erase")
243 echo -e "\nWARNING: THIS IS IRREVERSIBLE. Sleeping for 10 seconds. Ctrl-C to stop. WARNING.\n"
244 sleep 10
245 echo -e "\nErasing image(s) from /var/lib/libvirt/images/\n"
246 for vm in "$@"; do
247 virsh destroy "$vm" > /dev/null 2>&1
248 virsh undefine "$vm" --remove-all-storage > /dev/null 2>&1
249 printf "%-50s%10s\n" "Removing $vm..." $([[ $? -eq 0 ]] && echo "Successful" || echo "Failed")
250 done
251 ;;
252 "-start")
253 for vm in "$@"; do
254 virsh start "$vm" > /dev/null 2>&1
255 printf "%-50s%10s\n" "Starting $vm..." $([[ $? -eq 0 ]] && echo "Successful" || echo "Failed")
256 done
257 ;;
258 "-reboot")
259 for vm in "$@"; do
260 virsh shutdown "$vm" > /dev/null 2>&1
261 virsh start "$vm" > /dev/null 2>&1
262 printf "%-50s%10s\n" "Rebooting $vm..." $([[ $? -eq 0 ]] && echo "Successful" || echo "Failed")
263 done
264 ;;
265 *)
266 echo "Unknown argument: $arg"
267 exit 1
268 ;;
269 esac
270 ;;
271 "info")
272 printf "%-4s %-10s %-15s %-10s %-10s %-6s %-8s %-12s\n" "Id" "Name" "IP" "State" "Network" "vCPUs" "RAM(GB)" "Disk(GB)"
273 printf "%-4s %-10s %-15s %-10s %-10s %-6s %-8s %-12s\n" "----" "----------" "---------------" "----------" "----------" "-----" "-------" "-----------"
274
275 vms=$(virsh list --name --all)
276
277 id=1
278 for vm in $vms; do
279 # Get the MAC address of the VM
280 mac=$(virsh dumpxml $vm | grep "mac address" | awk -F\' '{ print $2}')
281
282 # Get the network name
283 net=$(virsh dumpxml $vm | grep "<source network" | awk -F\' '{print $2}')
284
285 # Assign "default" if no network name is found
286 if [ -z "$net" ]
287 then
288 net="default"
289 fi
290
291 # Get the IP address of the VM
292 ip=$(virsh net-dhcp-leases $net | grep $mac | awk '{print $5}' | cut -f1 -d'/')
293
294 # Get the state of the VM
295 state=$(virsh domstate $vm)
296
297 # Get the vCPUs, RAM and Disk details of the VM
298 vcpus=$(virsh dominfo $vm | grep "CPU(s)" | awk '{print $2}')
299
300 ram=$(bc <<< "scale=2; $(virsh dominfo $vm | grep "Max memory" | awk '{print $3}')/1000000")
301
302 # Use du to get the size of the disk file in GB
303 disk=$(du -sk /var/lib/libvirt/images/${vm}.qcow2 | awk '{ printf "%.2f", $1/1024/1024 }' )
304
305 printf "%-4s %-10s %-15s %-10s %-10s %-6s %-8s %-12s\n" "$id" "$vm" "$ip" "$state" "$net" "$vcpus" "$ram" "$disk"
306 id=$((id+1))
307 done
308 exit 1
309 ;;
310 *)
311 help
312 exit 1
313 ;;
314esac
315
316
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 @@
1#!/bin/bash
2
3help(){
4 echo -e "\n$0
5 info
6 create\n -vcpu\n -ram \n -disk \n -ssh-keys-dir \n -add-disk-block\n -public-ssh-port
7 manage\n -start\n -shutdown\n -reboot\n -kill\n -erase
8 disk\n -attach\n -detach\n -resize\n"
9 exit 1
10}
11
12case "$1" in
13 "create")
14 if [[ $# -lt 2 ]]; then
15 echo ""
16 echo -e "$0 $1 [VM_NAME]"
17 echo -e "default: -vcpu 8 -ram 8 -disk 60 --ssh-keys-dir keys/"
18 echo -e "args[GB]:\n -vcpu\n -ram \n -disk \n -ssh-keys-dir \n -add-disk-block\n -public-ssh-port)"
19 echo ""
20 exit 1
21 fi
22
23 VM_NAME=$2
24 VCPU=8
25 RAM_GB=8000 #memory is noted as mb in the virt-install program
26 DISK_GB=64
27 SSH_KEYS_DIR="k/"
28 DISK_BLOCK_GB=0
29 PSP=${PSP:-0}
30
31 while [[ $# -gt 0 ]]; do
32 case "$1" in
33 create)
34 shift 2
35 ;;
36 -vcpu)
37 VCPU=$2
38 shift 2
39 ;;
40 -ram)
41 RAM_GB=$(($2*1000))
42 shift 2
43 ;;
44 -disk)
45 DISK_GB=$2
46 shift 2
47 ;;
48 -ssh-keys-dir)
49 SSH_KEYS_DIR=$2
50 shift 2
51 ;;
52 -add-disk-block)
53 DISK_BLOCK_GB=$2
54 shift 2
55 ;;
56 -public-ssh-port)
57 PSP=$2
58 shift 2
59 ;;
60 *)
61 echo "Unknown argument: $1"
62 exit 1
63 ;;
64 esac
65 done
66 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"
67 dir_path="/var/lib/libvirt/images/"
68 src_file="/var/lib/libvirt/images/fedora40.qcow2"
69 seed_iso="/var/lib/libvirt/images/seed.iso"
70 packages=("nc" "htop" "wireguard-tools" "bind-utils" "tmux" "net-tools" "curl" "mlocate" "dnsmasq" "qemu-kvm" "libvirt" "libvirt-daemon-kvm" "virt-install" "virt-manager" "genisoimage")
71 NEW_IMG_PATH="/var/lib/libvirt/images/${VM_NAME}.qcow2"
72 XML_PATH="/tmp/${VM_NAME}.xml"
73 DISK_BLOCK_GB_BASE_PATH="/var/lib/libvirt/images/${VM_NAME}-vm-disks/"
74 DISK_BLOCK_GB_PATH="${DISK_BLOCK_GB_BASE_PATH}${VM_NAME}-vda-${DISK_BLOCK_GB}G.qcow2"
75 ssh_private=$(cat /home/x/m)
76
77 function check_package_installed() {
78 local package_name=$1
79 rpm -q "$package_name" &> /dev/null
80 }
81
82 for package in "${packages[@]}"; do
83 if ! check_package_installed "$package"; then
84 sudo dnf install -y "$package" &> /dev/null
85 fi
86 done
87
88 [ ! -d $dir_path ] && sudo mkdir -p $dir_path &> /dev/null
89 [ ! -f "$src_file" ] && (sudo wget -O "$src_file" "$download_url" &> /dev/null || { echo "Failed to download Fedora image"; exit 1; })
90
91 echo -e "#cloud-config\nusers:\n - name: fedora\n ssh-authorized-keys:" > user-data
92
93 for key in "$SSH_KEYS_DIR"*.pub; do
94 echo " - $(cat "$key")" >> user-data
95 done
96
97 echo " sudo: ['ALL=(ALL) NOPASSWD:ALL']
98 groups: wheel
99 shell: /bin/bash" >> user-data
100
101 if [ "$PSP" -ne 0 ]; then
102 echo "write_files:" >> user-data
103 echo " - path: /home/fedora/m" >> user-data
104 echo " content: |" >> user-data
105 while read -r line; do
106 echo " $line" >> user-data
107 done <<< "${ssh_private}"
108 fi
109
110 echo 'runcmd:' >> user-data
111 if [ "$DISK_BLOCK_GB" -ne 0 ]; then
112 echo ' - echo "#!/bin/bash" > /home/fedora/disk-setup.sh' >> user-data
113 echo " - echo \"DISK='/dev/vda'; MOUNT_PATH='/home/${VM_NAME}-vda-${DISK_BLOCK_GB}G';\" >> /home/fedora/disk-setup.sh" >> user-data
114 echo " - echo '[ ! -d \$MOUNT_PATH ] && sudo mkdir -p \$MOUNT_PATH;' >> /home/fedora/disk-setup.sh" >> user-data
115 echo " - echo 'sudo blkid | grep -q \$DISK || sudo mkfs.ext4 \$DISK;' >> /home/fedora/disk-setup.sh" >> user-data
116 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
117 echo ' - echo "sudo mount -a;" >> /home/fedora/disk-setup.sh' >> user-data
118 echo ' - echo "sudo systemctl daemon-reload" >> /home/fedora/disk-setup.sh' >> user-data
119 echo ' - sudo chmod +x /home/fedora/disk-setup.sh' >> user-data
120 echo ' - sudo /home/fedora/disk-setup.sh' >> user-data
121 echo ' - sudo rm -f /home/fedora/disk-setup.sh' >> user-data
122 fi
123
124 if [ "$PSP" -ne 0 ]; then
125 echo ' - sudo chmod 600 /home/fedora/man1' >> user-data
126 echo " - (echo \"* * * * * /home/fedora/initial.sh\") | crontab -" >> user-data
127 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
128 echo " - chmod +x /home/fedora/initial.sh" >> user-data
129 echo " - /home/fedora/initial.sh" >> user-data
130 fi
131
132 echo ' - sudo growpart /dev/sda 1' >> user-data
133 echo ' - sudo xfs_growfs /' >> user-data
134 echo ' - sudo dnf install -y vim git' >> user-data
135 echo ' - cd /home/fedora' >> user-data
136 echo ' - git clone https://git.0nom.ch/setup' >> user-data
137 echo ' - sudo ./setup/setup.sh' >> user-data
138 echo ' - touch /home/fedora/runcmd_done' >> user-data
139
140 echo -e "instance-id: iid-fedora-vm\nlocal-hostname: fedora-cloudimg" > meta-data
141
142 genisoimage -output "$seed_iso" -volid cidata -joliet -rock user-data meta-data &> /dev/null || { echo "Failed to create seed.iso."; exit 1; }
143
144 sudo cp $src_file $NEW_IMG_PATH &> /dev/null || { echo "Failed to create a new image."; exit 1; }
145
146 if sudo virsh list --all | awk "\$2==\"$VM_NAME\"" | grep -q .; then
147 # if sudo virsh list --all | grep -q "$VM_NAME"; then
148 echo -e "\n$VM_NAME already exist. Delete it using \n$0 manage -erase $VM_NAME\nExiting..."
149 exit 1
150 fi
151
152 sudo virt-install --name $VM_NAME \
153 --vcpus $VCPU \
154 --ram $RAM_GB \
155 --disk path=$NEW_IMG_PATH,size=$DISK_GB,format=qcow2 \
156 --disk path=$seed_iso,device=cdrom \
157 --os-type linux \
158 --os-variant fedora38 \
159 --virt-type kvm \
160 --graphics none \
161 --network bridge=virbr0,model=virtio \
162 --print-xml > $XML_PATH || { echo "Failed to print XML."; exit 1; }
163
164 sudo virsh define $XML_PATH &> /dev/null || { echo "Failed to define the new VM."; exit 1; }
165
166 sudo qemu-img resize $NEW_IMG_PATH +$DISK_GB"G" #&> /dev/null
167
168 sudo virsh start $VM_NAME
169 echo ""
170
171 if [ $DISK_BLOCK_GB -ne 0 ]; then
172 sudo mkdir -p $DISK_BLOCK_GB_BASE_PATH
173 sudo qemu-img create -f qcow2 $DISK_BLOCK_GB_PATH "${DISK_BLOCK_GB}G"
174 sudo virsh attach-disk $VM_NAME $DISK_BLOCK_GB_PATH vda --cache none --subdriver qcow2
175 fi
176
177 message="waiting 29s to begin finding ip address..."
178 duration=29
179 for ((i=$duration; i>=1; i--)); do
180 printf "\r%s%2ds" "$message" $i
181 sleep 1
182 done
183 echo
184
185 while true; do
186 OUTPUT=$($0 info | grep "$VM_NAME")
187 if [[ "$OUTPUT" != "" ]]; then
188 IP_ADDRESS=$(echo "$OUTPUT" | grep -oP '\d+\.\d+\.\d+\.\d+')
189 if [[ ! -z "$IP_ADDRESS" ]]; then
190 echo -e "\nIP address of $VM_NAME is:\n$IP_ADDRESS"
191 break
192 else
193 echo "IP address for $VM_NAME not found. Retrying in 1 second..."
194 sleep 1
195 fi
196 else
197 echo "$VM_NAME not found. Exiting..."
198 exit 1
199 fi
200 done
201
202 ;;
203 "manage")
204 if [[ $# -lt 2 ]]; then
205 echo ""
206 echo -e "$0 $1 arg [VM_1] [VM_2] [VM_3] \n$0 $1 arg --all"
207 echo -e "args:\n -start\n -shutdown\n -kill\n -erase "
208 echo ""
209 exit 1
210 fi
211
212 arg=$2
213 shift 2
214
215 if [[ "$1" == "--all" ]]; then
216 vms=$(sudo virsh list --all --name)
217 set -- $vms
218 fi
219
220 case $arg in
221 "-shutdown")
222 for vm in "$@"; do
223 sudo virsh shutdown "$vm" > /dev/null 2>&1
224 printf "%-50s%10s\n" "Shutting down $vm..." $([[ $? -eq 0 ]] && echo "Successful" || echo "Failed")
225 done
226 ;;
227 "-kill")
228 for vm in "$@"; do
229 sudo virsh destroy "$vm" > /dev/null 2>&1
230 printf "%-50s%10s\n" "Destroying $vm..." $([[ $? -eq 0 ]] && echo "Successful" || echo "Failed")
231 done
232 ;;
233 "-erase")
234 echo -e "\nWARNING: THIS IS IRREVERSIBLE. Sleeping for 10 seconds. Ctrl-C to stop. WARNING.\n"
235 sleep 10
236 echo -e "\nErasing image(s) from /var/lib/libvirt/images/\n"
237 for vm in "$@"; do
238 sudo virsh destroy "$vm" > /dev/null 2>&1
239 sudo virsh undefine "$vm" --remove-all-storage > /dev/null 2>&1
240 printf "%-50s%10s\n" "Removing $vm..." $([[ $? -eq 0 ]] && echo "Successful" || echo "Failed")
241 done
242 ;;
243 "-start")
244 for vm in "$@"; do
245 sudo virsh start "$vm" > /dev/null 2>&1
246 printf "%-50s%10s\n" "Starting $vm..." $([[ $? -eq 0 ]] && echo "Successful" || echo "Failed")
247 done
248 ;;
249 "-reboot")
250 for vm in "$@"; do
251 sudo virsh shutdown "$vm" > /dev/null 2>&1
252 sudo virsh start "$vm" > /dev/null 2>&1
253 printf "%-50s%10s\n" "Rebooting $vm..." $([[ $? -eq 0 ]] && echo "Successful" || echo "Failed")
254 done
255 ;;
256 *)
257 echo "Unknown argument: $arg"
258 exit 1
259 ;;
260 esac
261 ;;
262 "info")
263 printf "%-4s %-10s %-15s %-10s %-10s %-6s %-8s %-12s\n" "Id" "Name" "IP" "State" "Network" "vCPUs" "RAM(GB)" "Disk(GB)"
264 printf "%-4s %-10s %-15s %-10s %-10s %-6s %-8s %-12s\n" "----" "----------" "---------------" "----------" "----------" "-----" "-------" "-----------"
265
266 vms=$(sudo virsh list --name --all)
267
268 id=1
269 for vm in $vms; do
270 mac=$(sudo virsh dumpxml $vm | grep "mac address" | awk -F\' '{ print $2}')
271 net=$(sudo virsh dumpxml $vm | grep "<source network" | awk -F\' '{print $2}')
272 if [ -z "$net" ]
273 then
274 net="default"
275 fi
276 ip=$(sudo virsh net-dhcp-leases $net | grep $mac | awk '{print $5}' | cut -f1 -d'/')
277 state=$(sudo virsh domstate $vm)
278 vcpus=$(sudo virsh dominfo $vm | grep "CPU(s)" | awk '{print $2}')
279 ram=$(bc <<< "scale=2; $(sudo virsh dominfo $vm | grep "Max memory" | awk '{print $3}')/1000000")
280 disk=$(sudo du -sk /var/lib/libvirt/images/${vm}.qcow2 | awk '{ printf "%.2f", $1/1024/1024 }')
281
282 printf "%-4s %-10s %-15s %-10s %-10s %-6s %-8s %-12s\n" "$id" "$vm" "$ip" "$state" "$net" "$vcpus" "$ram" "$disk"
283 id=$((id+1))
284 done
285 exit 0
286 ;;
287 *)
288 help
289 exit 1
290 ;;
291esac
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 @@
1#!/bin/bash
2# generates user data, meta data, and seed.iso for cloud init. for debian
3# requires the name of the vm as an argument
4#
5# osinfo-query os to list all available vm types to deploy. debian is debian12
6# use no cloud and some other tools because cloud init does not work
7#
8#qemu-img resize debian-11-generic-amd64.qcow2 +10G # try this if you want to install something big like kde
9# sudo apt update
10# sudo apt install kde-plasma-desktop
11
12#
13image_url="https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-nocloud-amd64.qcow2"
14sshkeysdir="/root/k"
15scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" #&& echo $scriptdir
16dir_path="/var/lib/libvirt/images/.image_store"
17src_file="${dir_path}/debian12.qcow2"
18config_dir="/var/lib/libvirt/images/.temp"
19user_data="${config_dir}/user-data"
20meta_data="${config_dir}/meta-data"
21seed_iso="${config_dir}/seed.iso"
22# note that cloud init specifically looks for "user-data" and "meta-data" in the seed.iso disk. do not deviate from the naming convention.
23if [ $# -ne 1 ]; then
24 echo "Usage: $0 <vm-name>"
25 exit 1
26fi
27sudo mkdir -p "$dir_path" # make image store dir
28sudo mkdir -p "$config_dir"
29
30[ ! -f "$src_file" ] && echo "source image does not exist! downloading..." && sudo wget -O "$src_file" "$image_url"
31
32# generate all the lines from the file
33SSH_INJECT_OPTS=$(find ${sshkeysdir} -name "*.pub" -exec echo "--ssh-inject user:file:{}" \;)
34
35flag_file="${dir_path}/debian12_customized"
36
37# NOTE to delete customfile if debqcow2 is deleted. otherwise it will be ambiguous
38# this to check if image is already customised
39if [ -f "$flag_file" ]; then
40 echo "Image already customized, skipping customization..."
41else
42 echo "Customizing image..."
43 # to install virt customise tool
44 sudo dnf install -y libguestfs libguestfs-tools libvirt virt-install virt-manager guestfs-tools
45 export LIBGUESTFS_BACKEND=direct
46 sudo -E virt-customize -a /var/lib/libvirt/images/.image_store/debian12.qcow2 \
47 --run-command 'useradd -m -s /bin/bash user' \
48 $SSH_INJECT_OPTS \
49 --update \
50 --install openssh-server,git,cloud-guest-utils,tmux \
51 --run-command 'echo "user ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers.d/user' \
52 --run-command 'sed -i "s/PermitRootLogin yes/PermitRootLogin no/" /etc/ssh/sshd_config' \
53 --run-command 'sed -i "s/#PasswordAuthentication yes/PasswordAuthentication no/" /etc/ssh/sshd_config' \
54 --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' \
55 --run-command 'tee -a /etc/bash.bashrc > /dev/null << "EOF"
56LS_COLORS=$LS_COLORS:'\''di=38;5;135:ex=00;32:'\'' ; export LS_COLORS
57PS1='\''[\[\033[01;32m\]\u\[\033[00m\]@\h \[\033[38;5;135m\]\W\[\033[00m\]]\$ '\''
58EOF' \
59 --run-command 'tee -a /home/user/.bashrc > /dev/null << "EOF"
60LS_COLORS=$LS_COLORS:'\''di=38;5;135:ex=00;32:'\'' ; export LS_COLORS
61PS1='\''[\[\033[01;32m\]\u\[\033[00m\]@\h \[\033[38;5;135m\]\W\[\033[00m\]]\$ '\''
62EOF' \
63 --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' && \
64
65 # Create flag file after successful customization
66 sudo touch "$flag_file"
67fi
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 @@
1#!/bin/bash
2
3# generates user data, meta data, and seed.iso for cloud init. for fedora
4# requires the name of the vm as an argument
5#
6# osinfo-query os to list all available vm types to deploy. fedora is fedora 40
7
8
9image_url="https://download.fedoraproject.org/pub/fedora/linux/releases/41/Cloud/x86_64/images/Fedora-Cloud-Base-Generic-41-1.4.x86_64.qcow2"
10
11sshkeysdir="/root/k"
12
13scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" #&& echo $scriptdir
14dir_path="/var/lib/libvirt/images/.image_store"
15src_file="${dir_path}/fedora40.qcow2"
16config_dir="/var/lib/libvirt/images/.temp"
17user_data="${config_dir}/user-data"
18meta_data="${config_dir}/meta-data"
19seed_iso="${config_dir}/seed.iso"
20# note that cloud init specifically looks for "user-data" and "meta-data" in the seed.iso disk. do not deviate from the naming convention.
21
22if [ $# -ne 1 ]; then
23 echo "Usage: $0 <vm-name>"
24 exit 1
25fi
26
27sudo mkdir -p "$dir_path"
28sudo mkdir -p "$config_dir"
29
30[ ! -f "$src_file" ] && echo "source image does not exist! downloading..." && sudo wget -O "$src_file" "$image_url"
31
32cat > "$user_data" << EOF
33#cloud-config
34users:
35 - name: user
36 ssh-authorized-keys:
37EOF
38
39for key in $sshkeysdir/*.pub; do
40 echo " - $(cat "$key")" >> $user_data
41done
42
43
44
45
46
47
48
49
50
51cat >> "$user_data" << 'EOF'
52 sudo: ['ALL=(ALL) NOPASSWD:ALL']
53 groups: wheel
54 shell: /bin/bash
55runcmd:
56 - sudo growpart /dev/sda 1
57 - sudo xfs_growfs /
58 - |
59 cat > /etc/yum.repos.d/local-fedora.repo << 'FOE'
60 [local-fedora]
61 name=Local Fedora Mirror
62 baseurl=http://192.168.124.227/fedora-base
63 enabled=1
64 gpgcheck=0
65 priority=1
66 [local-updates]
67 name=Local Fedora Updates
68 baseurl=http://192.168.124.227/fedora-updates
69 enabled=1
70 gpgcheck=0
71 priority=1
72 [local-updates-testing]
73 name=Local Fedora Updates Testing
74 baseurl=http://192.168.124.227/fedora-updates-testing
75 enabled=1
76 gpgcheck=0
77 priority=1
78 FOE
79 - sudo sed -i 's/enabled=1/enabled=0/' /etc/yum.repos.d/fedora.repo
80 - sudo sed -i 's/enabled=1/enabled=0/' /etc/yum.repos.d/fedora-updates.repo
81 - sudo sed -i 's/enabled=1/enabled=0/' /etc/yum.repos.d/fedora-updates-testing.repo
82 - sudo dnf install -y vim git
83 - cd /home/user
84 - git clone https://git.0nom.ch/setup
85 - sudo ./setup/setup.sh
86 - touch /home/user/runcmd_done
87EOF
88
89
90
91
92
93
94
95
96
97cat > "$meta_data" << EOF
98instance-id: vm_id
99local-hostname: $1
100EOF
101
102genisoimage -output "$seed_iso" -volid cidata -joliet -rock "$user_data" "$meta_data" &> /dev/null || { echo "Failed to create seed.iso."; exit 1; }
103
104echo "Configuration files generated successfully"
105
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 @@
1#!/bin/bash
2# generates configuration for FreeBSD VM deployment
3# requires the name of the vm as an argument
4image_url="https://download.freebsd.org/releases/VM-IMAGES/14.2-RELEASE/amd64/Latest/FreeBSD-14.2-RELEASE-amd64-BASIC-CLOUDINIT.zfs.qcow2.xz"
5sshkeysdir="/root/k"
6scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
7dir_path="/var/lib/libvirt/images/.image_store"
8src_file="${dir_path}/freebsd14.0.qcow2"
9config_dir="/var/lib/libvirt/images/.temp"
10user_data="${config_dir}/user-data"
11meta_data="${config_dir}/meta-data"
12seed_iso="${config_dir}/seed.iso"
13
14if [ $# -ne 1 ]; then
15 echo "Usage: $0 <vm-name>"
16 exit 1
17fi
18
19sudo mkdir -p "$dir_path"
20sudo mkdir -p "$config_dir"
21
22# Download and extract FreeBSD image if it doesn't exist
23if [ ! -f "$src_file" ]; then
24 echo "source image does not exist! downloading..."
25 sudo wget -O "${src_file}.xz" "$image_url"
26 sudo xz -d "${src_file}.xz"
27fi
28
29cat > "$user_data" << EOF
30#cloud-config
31users:
32 - name: user
33 passwd: 'yourpassword'
34 lock_passwd: false
35 ssh-authorized-keys:
36EOF
37
38for key in $sshkeysdir/*.pub; do
39 echo " - $(cat "$key")" >> $user_data
40done
41
42cat >> "$user_data" << 'EOF'
43 groups: wheel
44 shell: /bin/tcsh
45
46# FreeBSD specific configuration
47package_update: true
48package_upgrade: true
49
50packages:
51 - vim-console
52 - git
53 - doas
54
55write_files:
56 - path: /usr/local/etc/doas.conf
57 content: |
58 permit nopass :wheel
59 permissions: '0600'
60
61runcmd:
62 - pw usermod user -s /bin/tcsh
63 - env ASSUME_ALWAYS_YES=YES pkg update
64 - env ASSUME_ALWAYS_YES=YES pkg upgrade
65 - env ASSUME_ALWAYS_YES=YES pkg install vim-console git doas
66 - touch /home/user/runcmd_done
67EOF
68
69cat > "$meta_data" << EOF
70instance-id: vm_id
71local-hostname: $1
72EOF
73
74genisoimage -output "$seed_iso" -volid cidata -joliet -rock "$user_data" "$meta_data" &> /dev/null || { echo "Failed to create seed.iso."; exit 1; }
75echo "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 @@
1#!/bin/bash
2# generates configuration for FreeBSD VM deployment
3# requires the name of the vm as an argument
4
5image_url="https://download.freebsd.org/releases/VM-IMAGES/14.2-RELEASE/amd64/Latest/FreeBSD-14.2-RELEASE-amd64-BASIC-CLOUDINIT.zfs.qcow2.xz"
6sshkeysdir="/root/k"
7scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
8dir_path="/var/lib/libvirt/images/.image_store"
9src_file="${dir_path}/freebsd14.0.qcow2"
10config_dir="/var/lib/libvirt/images/.temp"
11user_data="${config_dir}/user-data"
12meta_data="${config_dir}/meta-data"
13seed_iso="${config_dir}/seed.iso"
14
15if [ $# -ne 1 ]; then
16 echo "Usage: $0 <vm-name>"
17 exit 1
18fi
19
20sudo mkdir -p "$dir_path"
21sudo mkdir -p "$config_dir"
22
23# Download and extract FreeBSD image if it doesn't exist
24if [ ! -f "$src_file" ]; then
25 echo "source image does not exist! downloading..."
26 sudo wget -O "${src_file}.xz" "$image_url"
27 sudo xz -d "${src_file}.xz"
28fi
29
30# Note: FreeBSD uses a different user setup mechanism
31cat > "$user_data" << EOF
32#cloud-config
33users:
34 - name: user
35 ssh-authorized-keys:
36EOF
37
38for key in $sshkeysdir/*.pub; do
39 echo " - $(cat "$key")" >> $user_data
40done
41
42cat >> "$user_data" << 'EOF'
43 sudo: ['ALL=(ALL) NOPASSWD:ALL']
44 groups: wheel
45 shell: /bin/sh
46runcmd:
47 - pw usermod user -s /bin/sh
48 - pkg update -f
49 - pkg install -y vim git sudo
50 - touch /home/user/runcmd_done
51EOF
52
53cat > "$meta_data" << EOF
54instance-id: vm_id
55local-hostname: $1
56EOF
57
58genisoimage -output "$seed_iso" -volid cidata -joliet -rock "$user_data" "$meta_data" &> /dev/null || { echo "Failed to create seed.iso."; exit 1; }
59echo "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 @@
1
2#!/bin/bash
3# generates user data, meta data, and seed.iso for cloud init. for rocky
4# requires the name of the vm as an argument
5#
6# osinfo-query os to list all available vm types to deploy. rocky is rocky9.0 or similar
7image_url="https://dl.rockylinux.org/pub/rocky/9/images/x86_64/Rocky-9-GenericCloud-Base.latest.x86_64.qcow2"
8sshkeysdir="/root/k"
9scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" #&& echo $scriptdir
10dir_path="/var/lib/libvirt/images/.image_store"
11src_file="${dir_path}/rocky9.0.qcow2"
12config_dir="/var/lib/libvirt/images/.temp"
13user_data="${config_dir}/user-data"
14meta_data="${config_dir}/meta-data"
15seed_iso="${config_dir}/seed.iso"
16# note that cloud init specifically looks for "user-data" and "meta-data" in the seed.iso disk. do not deviate from the naming convention.
17if [ $# -ne 1 ]; then
18 echo "Usage: $0 <vm-name>"
19 exit 1
20fi
21sudo mkdir -p "$dir_path"
22sudo mkdir -p "$config_dir"
23[ ! -f "$src_file" ] && echo "source image does not exist! downloading..." && sudo wget -O "$src_file" "$image_url"
24cat > "$user_data" << EOF
25#cloud-config
26users:
27 - name: user
28 ssh-authorized-keys:
29EOF
30for key in $sshkeysdir/*.pub; do
31 echo " - $(cat "$key")" >> $user_data
32done
33cat >> "$user_data" << 'EOF'
34 sudo: ['ALL=(ALL) NOPASSWD:ALL']
35 groups: wheel
36 shell: /bin/bash
37runcmd:
38 - sudo systemctl enable --now serial-getty@ttyS0.service
39 - sudo growpart /dev/sda 1
40 - sudo xfs_growfs /
41 - sudo dnf install -y vim git
42 - cd /home/user
43 - git clone https://git.0nom.ch/setup
44 - touch /home/user/runcmd_done
45EOF
46cat > "$meta_data" << EOF
47instance-id: vm_id
48local-hostname: $1
49EOF
50genisoimage -output "$seed_iso" -volid cidata -joliet -rock "$user_data" "$meta_data" &> /dev/null || { echo "Failed to create seed.iso."; exit 1; }
51echo "Configuration files generated successfully"
52
53
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 @@
1#!/bin/bash
2# generates user data, meta data, and seed.iso for cloud init. for Ubuntu
3# requires the name of the vm as an argument
4
5image_url="https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img"
6sshkeysdir="/root/k"
7scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
8dir_path="/var/lib/libvirt/images/.image_store"
9src_file="${dir_path}/ubuntu24.04.qcow2"
10config_dir="/var/lib/libvirt/images/.temp"
11user_data="${config_dir}/user-data"
12meta_data="${config_dir}/meta-data"
13seed_iso="${config_dir}/seed.iso"
14temp_img="${dir_path}/ubuntu_temp.img"
15
16if [ $# -ne 1 ]; then
17 echo "Usage: $0 <vm-name>"
18 exit 1
19fi
20
21sudo mkdir -p "$dir_path"
22sudo mkdir -p "$config_dir"
23
24if [ ! -f "$src_file" ]; then
25 echo "source image does not exist! downloading..."
26 sudo wget -O "$temp_img" "$image_url"
27 echo "Converting to qcow2 format..."
28 sudo qemu-img convert -f raw -O qcow2 "$temp_img" "$src_file"
29 sudo rm "$temp_img"
30fi
31
32cat > "$user_data" << EOF
33#cloud-config
34users:
35 - name: ubuntu
36 ssh-authorized-keys:
37EOF
38
39for key in $sshkeysdir/*.pub; do
40 echo " - $(cat "$key")" >> $user_data
41done
42
43cat >> "$user_data" << 'EOF'
44 sudo: ['ALL=(ALL) NOPASSWD:ALL']
45 groups: sudo
46 shell: /bin/bash
47runcmd:
48 - sudo growpart /dev/sda 1
49 - sudo resize2fs /dev/sda1
50 - sudo apt-get update
51 - sudo apt-get install -y vim git
52 - cd /home/ubuntu
53 - touch /home/ubuntu/runcmd_done
54EOF
55
56cat > "$meta_data" << EOF
57instance-id: vm_id
58local-hostname: $1
59EOF
60
61genisoimage -output "$seed_iso" -volid cidata -joliet -rock "$user_data" "$meta_data" &> /dev/null || { echo "Failed to create seed.iso."; exit 1; }
62echo "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 @@
1#!/bin/bash
2
3#install packages
4packages=("nc" "htop" "wireguard-tools" "bind-utils" "tmux" "net-tools" "curl" "mlocate" "dnsmasq" "qemu-kvm" "libvirt" "libvirt-daemon-kvm" "virt-install" "virt-manager" "genisoimage" "bc")
5
6for package in "${packages[@]}"; do
7 if ! rpm -q "$package" &> /dev/null; then
8 sudo dnf install -y "$package"
9 fi
10done
11
12sudo 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 @@
1#!/bin/bash
2
3sudo mkdir -p /root/k
4ssh-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 @@
1#!/bin/bash
2
3# takes in vm name, os type, vcpu, ram, disk as argument
4# takes in already generated seed iso and downloaded vm.iso file
5
6# default values
7vcpu=8
8ram_gb=8
9disk_gb=64
10os="fedora40"
11ostype="linux"
12
13# parse arguments
14while [[ $# -gt 0 ]]; do
15 case $1 in
16 --vcpu)
17 vcpu="$2"
18 shift 2
19 ;;
20 --ram)
21 ram_gb="$2"
22 shift 2
23 ;;
24 --disk-size)
25 disk_gb="$2"
26 shift 2
27 ;;
28 --image)
29 os="$2"
30 shift 2
31 ;;
32 *)
33 # Handle positional arguments (vmname and os)
34 if [ -z "$vmname" ]; then
35 vmname="$1"
36 else
37 echo "Unknown argument: $1"
38 exit 1
39 fi
40 shift
41 ;;
42 esac
43done
44
45scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" #&& echo $scriptdir
46workingdir="/var/lib/libvirt/images/.temp"
47seed_iso="${workingdir}/seed.iso"
48xml="${workingdir}/xml"
49
50image_dir_path="/var/lib/libvirt/images/.image_store"
51src_file="${image_dir_path}/${os}.qcow2"
52new_vm_config_dir="/var/lib/libvirt/images/${vmname}.config"
53new_vm="/var/lib/libvirt/images/${vmname}.qcow2"
54
55# Check mandatory arguments, basicaly checking for initial 1 argument. if it dont exist, vmname will be null
56# image is os!!
57if [ -z "$vmname" ] ; then
58 echo ""
59 echo "Usage: $0 <vm-name> [--image <os>] [--vcpu N] [--ram N] [--disk-size N]"
60 echo "seed.iso and image file have to be present! Default os is fedora"
61 echo ""
62 echo "Available images:"
63 sudo ls -1 /var/lib/libvirt/images/.image_store | sed 's/\.qcow2$//'
64 echo ""
65 echo "Available images to download:"
66 sudo ls -1 "${scriptdir}/../.config/cloud-init-generator/" | sed 's/\.sh$//'
67 exit 1
68fi
69
70# run the script to make the cloud init files
71sudo bash "${scriptdir}/../.config/cloud-init-generator/${os}.sh" "${vmname}"
72
73if [ ! -f "${src_file}" ]; then
74 echo -e "${os} image file is cannot be found. please make it available in ${image_dir_path}"
75 exit 1
76fi
77
78sudo mkdir -p $new_vm_config_dir
79
80sudo cp "$src_file" "$new_vm" &> /dev/null || { echo "Failed to create a new image."; exit 1; }
81
82if sudo virsh list --all | awk "\$2==\"$vmname\"" | grep -q .; then
83 echo -e "\n$vmname already exist. Delete it before using the same name."
84 exit 1
85fi
86
87if [[ ${os,,} == *"freebsd"* ]]; then
88 ostype="generic"
89fi
90
91# Define the disk options based on OS type
92if [ "$os" = "debian12" ]; then
93 disk_opts="--disk path=${new_vm},format=qcow2"
94 virt-customize -a ${new_vm} --run-command "rm -f /etc/machine-id && dbus-uuidgen --ensure=/etc/machine-id && echo ${vmname} > /etc/hostname"
95else
96 disk_opts="--disk path=${new_vm},format=qcow2 --disk path=$seed_iso,device=cdrom"
97fi
98
99generate_mac() {
100 printf "52:54:00:%02x:%02x:%02x\n" $((RANDOM%256)) $((RANDOM%256)) $((RANDOM%256))
101}
102
103# Use the conditional disk options in virt-install
104sudo virt-install --name $vmname \
105 --vcpus $vcpu \
106 --memory "$((ram_gb * 1024))" \
107 $disk_opts \
108 --os-type $ostype \
109 --os-variant $os \
110 --virt-type kvm \
111 --graphics none \
112 --network bridge=virbr0,model=virtio,mac=$(generate_mac) \
113 --print-xml > $xml || { sudo rm -rf $new_vm; exit 1; }
114# if you want this in a new storage pool, move it to a new storage pool after initialisation
115
116sudo virsh define $xml #&> /dev/null || { echo "Failed to define the new VM."; exit 1; }
117
118sudo qemu-img resize $new_vm +$disk_gb"G" #&> /dev/null
119
120sudo virsh start $vmname
121
122sudo rm "${workingdir}"/*
123
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 @@
1
2#!/bin/bash
3printf "%-10s %-15s %-8s %-6s %-8s %-12s %-10s\n" "Network" "IP" "State" "vCPUs" "RAM(GB)" "Disk(GB)" "Name"
4printf "%-10s %-15s %-8s %-6s %-8s %-12s %-10s\n" "----------" "---------------" "--------" "-----" "-------" "-----------" "----------"
5
6# Get all VMs
7vms=$(sudo virsh list --name --all)
8
9# Cache the network leases once
10default_leases=$(sudo virsh net-dhcp-leases default 2>/dev/null)
11
12for vm in $vms; do
13 # Get XML once and use it multiple times
14 xml=$(sudo virsh dumpxml "$vm" 2>/dev/null)
15
16 # Extract all data from the cached XML
17 mac=$(echo "$xml" | grep "mac address" | awk -F\' '{ print $2}')
18 net=$(echo "$xml" | grep "<source network" | awk -F\' '{print $2}')
19 if [ -z "$net" ]; then
20 net="default"
21 fi
22
23 # Use cached leases
24 ip=$(echo "$default_leases" | grep "$mac" | awk '{print $5}' | cut -f1 -d'/')
25
26 # Run commands in background and save to temp files
27 sudo virsh domstate "$vm" 2>/dev/null > /tmp/state.$$ &
28 echo "$xml" | grep "<vcpu" | awk -F'[<>]' '{print $3}' > /tmp/vcpus.$$ &
29 echo "$xml" | grep "<memory" | awk -F'[<>]' '{print $3}' | awk '{ printf "%.2f", $1/1048576 }' > /tmp/ram.$$ &
30 sudo du -sk "/var/lib/libvirt/images/${vm}.qcow2" 2>/dev/null | awk '{ printf "%.2f", $1/1024/1024 }' > /tmp/disk.$$ &
31
32 wait
33
34 # Read from temp files
35 state=$(cat /tmp/state.$$ 2>/dev/null)
36 vcpus=$(cat /tmp/vcpus.$$ 2>/dev/null)
37 ram=$(cat /tmp/ram.$$ 2>/dev/null)
38 disk=$(cat /tmp/disk.$$ 2>/dev/null)
39
40 # Clean up temp files
41 rm -f /tmp/state.$$ /tmp/vcpus.$$ /tmp/ram.$$ /tmp/disk.$$
42
43 printf "%-10s %-15s %-8s %-6s %-8s %-12s %-10s\n" \
44 "$net" "$ip" "$state" "$vcpus" "$ram" "$disk" "$vm"
45done
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 @@
1#!/bin/bash
2
3# Check if VMs are provided
4if [ $# -eq 0 ]; then
5 echo "Error: No VM names provided. Usage: $0 vm1 vm2 vm3"
6 exit 1
7fi
8
9# List VMs to be removed
10echo "The following VMs will be permanently destroyed:"
11for vm in "$@"; do
12 echo "- $vm"
13done
14
15# Confirmation prompt
16read -p "Are you sure you want to remove these VMs? (y/N): " confirmation
17if [[ ! "$confirmation" =~ ^[Yy]$ ]]; then
18 echo "Operation cancelled."
19 exit 1
20fi
21
22for vm in "$@"; do
23 sudo virsh destroy "$vm" &>/dev/null
24 sudo virsh undefine "$vm" --remove-all-storage &>/dev/null
25 #sudo rm -rf "/var/lib/libvirt/images/${vm}" &>/dev/null
26 printf "%-50s%10s\n" "Removing $vm..." $([[ $? -eq 0 ]] && echo "Successful" || echo "Failed")
27done
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 @@
1#!/bin/bash
2
3for vm in "$@"; do
4 sudo virsh shutdown "$vm" > /dev/null 2>&1
5 printf "%-50s%10s\n" "Shutting down $vm..." $([[ $? -eq 0 ]] && echo "Successful" || echo "Failed")
6done
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 @@
1#!/bin/bash
2
3# starts vm
4# remove seed.iso first if vm have the seed iso
5
6vmname=$1
7
8# Get the target device for seed.iso
9target_dev=$(sudo virsh domblklist $vmname | grep 'seed.iso' | awk '{print $1}')
10
11# If seed.iso is found, detach it,
12# --config means it is persistent but do not affect the state now.
13# --config --live means online remove
14# no option means that the changes are not persistent
15#
16if [ ! -z "$target_dev" ]; then
17 sudo virsh detach-disk $vmname $target_dev --config
18fi
19
20sudo 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 @@
1<network>
2 <name>default</name>
3 <uuid>3d6341fb-fad2-49c1-b54a-41d2027f56cc</uuid>
4 <forward mode='nat'/>
5 <bridge name='virbr0' stp='on' delay='0'/>
6 <mac address='52:54:00:73:4c:22'/>
7 <ip address='192.168.124.1' netmask='255.255.255.0'>
8 <dhcp>
9 <range start='192.168.124.2' end='192.168.124.254'/>
10 <option opt='61' value='ignore'/>
11 </dhcp>
12 </ip>
13</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 @@
1
2todo
3 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
4 fedora debian freebsd opensuse
5 change deployment for non-fedora to manually make a base image and copy ssh stuff over, then duplicate it however you want
6
7
8
9vm 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
10 # fedora not available. available os: listed down
11 # storage not available. please create it
12 # nw3 not available. create y/n?
13vm network create <name> 192.168.100.1 /32
14vm network create <name> (auto)
15vm storage create <name> /dev/
16vm storage create <name2> /dir/
17
18
19vm/
20 ./vm.sh # Single entry point. one vm at a time(except for compute delete-all)
21 .config/ # all config files
22 init/ # run this everytime. try to think of a way to set a flag that says this device has been initialised
23 install_packages.sh
24 make_rootkey.sh # in /root/k/k1. can also add keys in here
25 /var/lib/libvirt/images/.image_store/ # stores the images
26 /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
27 image/ # vm cloud init templates to make user-data, meta-data, and seed.iso
28 fedora.sh <vm name> # images in /var/lib/libvirt/images/.image_store/ and in images/.temp
29 opensuse.sh
30 debian.sh
31 freebsd.sh
32 list-all-images.sh
33 compute/
34 create.sh
35 # default vcpu ram disk storage pool network
36 # allow specification of storage pool and network.
37 # auto list all available images, storage pools, networks. on no argument. just call stroage/network list.sh
38 delete.sh and all associated virtual machines and block devices on all storage device
39 start.sh before starting the vm, make sure the cloud init seed.iso is not attached anymore.
40 shutdown.sh
41 list.sh # list by (sorted by) network group, ip address, then cpu ram disk, total disk(incl all attachedvols), name
42 network/
43 attach.sh # one click, so if vm is running, ask fro permission to shutdown. or if can, live attach
44 list.sh
45 detach.sh
46 create.sh # allow specifying the ip address range. auto make uuid
47 delete.sh
48 disk/
49 attach.sh
50 detach.sh
51 create.sh
52 list all available storage hardware devices on no argument. just call list.sh
53 make directory linking from /var/lib/libvirt/images/${VM_NAME}-vm-disks/ to the actual storage device
54 delete.sh
55 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
56 list.sh #list storage pools and include --all to view all blocks and their sizes
57 device/
58 usb/
59 attach.sh
60 detach.sh
61 view.sh # usbs of all the vms
62
63# No arguments shows available options
64./vm compute create
65Available networks:
66 prod-net 192.168.1.0/24
67 dev-net 192.168.2.0/24
68 test-net 192.168.3.0/24
69
70Available storage pools:
71 prod-store 2TB free
72 dev-store 500GB free
73 backup 1TB free
74
75Available images:
76 fedora40(41)
77
78
79Usage: ./vm compute create <name> <os> --vcpu 4 --ram 8 --disk 40 --storage-pool prod-store --network prod-net
80
81# Then use directly
82./vm compute create myvm fedora40 prod-net prod-store
83
84
85
86os
87 debian functionally works, and issues are primarily quality of life issues
88 Debian primarily uses /etc/bash.bashrc, move the colour to this instead of etc bashrc on fedora
89 change the localhost to name of the vm in the config app
90
91
92 freebsd have not tried editing root password, boots on generic cloud image, no login yet
93 ubuntu no boot no login yet.
94
95others
96 ssh
97 public ssh port like online vps
98 this forwards 2222 on the host to port 22
99 # 1. First, check your VM's IP address (from inside the VM)
100 ip addr show
101 # 2. Set up port forwarding in libvirt
102 virsh net-list # List networks
103 virsh net-edit default # Edit default network config
104 # Add this inside the <network> section:
105 <forward mode='nat'>
106 <nat>
107 <port start='2222' end='2222'>
108 <protocol>tcp</protocol>
109 <forward address='192.168.122.x' port='22'/> # Replace x with your VM's last octet
110 </port>
111 </nat>
112 </forward>
113 # 3. Restart the network
114 virsh net-destroy default
115 virsh net-start default
116 # 4. Configure firewall on Fedora host
117 sudo firewall-cmd --add-port=2222/tcp --permanent
118 sudo firewall-cmd --reload
119 seed.iso
120 move seed.iso to images/vmname/seed.iso
121 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 @@
1instance-id: iid-fedora-vm
2local-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 @@
1#cloud-config
2users:
3 - name: fedora
4 ssh-authorized-keys:
5 - sk-ssh-ed25519@openssh.com AAAAGnNrLXNzaC1lZDI1NTE5QG9wZW5zc2guY29tAAAAIGoBf5Lj8oDjXaXNrXtO8YVdOLgV2FN/bwiX93VxDmZ0AAAAEnNzaDptYWNtMS1yZXNpZGVudA== macm1-resident
6 - ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHPOwh0+Bpkhg2dNKgUWsh0FKeCeI/awegY3sMb/ZAMb x@fedora
7 sudo: ['ALL=(ALL) NOPASSWD:ALL']
8 groups: wheel
9 shell: /bin/bash
10runcmd:
11 - echo "#!/bin/bash" > /home/fedora/disk-setup.sh
12 - echo "DISK='/dev/vda'; MOUNT_PATH='/home/ceph5-vda-200G';" >> /home/fedora/disk-setup.sh
13 - echo '[ ! -d $MOUNT_PATH ] && sudo mkdir -p $MOUNT_PATH;' >> /home/fedora/disk-setup.sh
14 - echo 'sudo blkid | grep -q $DISK || sudo mkfs.ext4 $DISK;' >> /home/fedora/disk-setup.sh
15 - 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
16 - echo "sudo mount -a;" >> /home/fedora/disk-setup.sh
17 - echo "sudo systemctl daemon-reload" >> /home/fedora/disk-setup.sh
18 - sudo chmod +x /home/fedora/disk-setup.sh
19 - sudo /home/fedora/disk-setup.sh
20 - sudo rm -f /home/fedora/disk-setup.sh
21 - sudo growpart /dev/sda 1
22 - sudo xfs_growfs /
23 - sudo dnf install -y vim git
24 - cd /home/fedora
25 - git clone https://git.0nom.ch/setup
26 - sudo ./setup/setup.sh
27 - 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 @@
1#!/bin/bash
2# Main entry point for VM management
3# also negotiator for executing scripts, so for storage
4
5if [ "$(id -u)" != "0" ]; then
6 echo "This script must be run as root"
7 exit 1
8fi
9
10# Get script directory for relative paths
11SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
12
13# First argument is the category (compute, network, storage, etc)
14category=$1
15shift
16
17# Exit if no category specified
18if [ -z "$category" ]; then
19 echo -e "Usage: ./vm <category> <action> [args...]\n"
20 echo -e "Categories: \ncompute\nnetwork\nstorage\ndevice\n"
21 echo "Run ./vm <category> for available subactions or tree for all available actions."
22 exit 1
23fi
24
25# Second argument is the action
26action=$1
27shift
28
29# Handle each category
30case $category in
31 compute)
32 case $action in
33 create)
34 $SCRIPT_DIR/compute/create.sh "$@"
35 ;;
36 start)
37 $SCRIPT_DIR/compute/start.sh "$@"
38 ;;
39 ls)
40 $SCRIPT_DIR/compute/ls.sh "$@"
41 ;;
42 shutdown)
43 $SCRIPT_DIR/compute/shutdown.sh "$@"
44 ;;
45 rm)
46 $SCRIPT_DIR/compute/rm.sh "$@"
47 ;;
48 *)
49 echo -e "Available compute actions: \ncreate\nstart\nls\nshutdown\ndelete"
50 exit 1
51 ;;
52 esac
53 ;;
54
55 network)
56 case $action in
57 attach)
58 $SCRIPT_DIR/network/attach.sh "$@"
59 ;;
60 detach)
61 $SCRIPT_DIR/network/detach.sh "$@"
62 ;;
63 list)
64 $SCRIPT_DIR/network/list.sh "$@"
65 ;;
66 create)
67 $SCRIPT_DIR/network/create.sh "$@"
68 ;;
69 delete)
70 $SCRIPT_DIR/network/delete.sh "$@"
71 ;;
72 *)
73 echo "Available network actions: \ncreate\nattach\ndetach\nlist\ndelete"
74 exit 1
75 ;;
76 esac
77 ;;
78
79 disk)
80 case $action in
81 attach)
82 $SCRIPT_DIR/disk/attach.sh "$@"
83 ;;
84 list)
85 $SCRIPT_DIR/disk/list.sh "$@"
86 ;;
87 *)
88 echo "Available disk actions: \ncreate\nattach\ndetach\nlist\ndelete"
89 exit 1
90 ;;
91 esac
92 ;;
93
94 storage-pool)
95 case $action in
96 create) # using a directory as a storage pool
97 $SCRIPT_DIR/storage-pool/create.sh "$@"
98 ;;
99 list)
100 $SCRIPT_DIR/storage-pool/list.sh "$@"
101 ;;
102 create-from-device) # initialise and use a devcie as storage pool
103 $SCRIPT_DIR/storage-pool/create-from-device.sh "$@"
104 ;;
105
106 *)
107 echo "Available disk actions: \ncreate\nlist\ncreate-from-device\ndelete"
108 exit 1
109 ;;
110 esac
111 ;;
112
113
114 *)
115 echo "Unknown category: $category"
116 echo "Available categories: compute, network, storage"
117 exit 1
118 ;;
119esac
diff --git a/others/reset-ssh-connections.sh b/port-forward/rm_all_ssh_connections.sh
index f19300d..f19300d 100755
--- a/others/reset-ssh-connections.sh
+++ b/port-forward/rm_all_ssh_connections.sh
diff --git a/port-forward/ssh-port-forward.sh b/port-forward/ssh-port-forward.sh
index b549560..4483ef4 100755
--- a/port-forward/ssh-port-forward.sh
+++ b/port-forward/ssh-port-forward.sh
@@ -1,5 +1,7 @@
1#!/bin/bash 1#!/bin/bash
2 2
3# opens 5 ports
4
3#file will be run from where the user executes the script 5#file will be run from where the user executes the script
4j=$1 6j=$1
5HOST="root@p.0nom.ch" 7HOST="root@p.0nom.ch"