summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYour Name <you@example.com>2024-12-23 16:06:31 +0800
committerYour Name <you@example.com>2024-12-23 16:06:31 +0800
commit456f72de0592e801213df04f042f6684ea2cc439 (patch)
tree898a537b1390405225a3a50852f581397fa1b997
parentf7f159a04671690786de2f84e34046c103521d58 (diff)
udpate
-rwxr-xr-xothers/vm3/.config/cloud-init-generator/debian12.sh53
-rwxr-xr-xothers/vm3/.config/cloud-init-generator/fedora40.sh (renamed from others/vm3/.config/cloud-init-files/fedora40.sh)2
-rwxr-xr-xothers/vm3/.config/cloud-init-generator/freebsd14.0.sh75
-rwxr-xr-xothers/vm3/.config/cloud-init-generator/freebsd14.0.shbackup59
-rwxr-xr-xothers/vm3/.config/cloud-init-generator/ubuntu24.04.sh62
-rw-r--r--others/vm3/.docs.swpbin12288 -> 16384 bytes
-rwxr-xr-xothers/vm3/compute/create.sh43
-rw-r--r--others/vm3/docs21
-rwxr-xr-xothers/vm3/test.sh49
9 files changed, 297 insertions, 67 deletions
diff --git a/others/vm3/.config/cloud-init-generator/debian12.sh b/others/vm3/.config/cloud-init-generator/debian12.sh
new file mode 100755
index 0000000..0bdb418
--- /dev/null
+++ b/others/vm3/.config/cloud-init-generator/debian12.sh
@@ -0,0 +1,53 @@
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
7image_url="https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-nocloud-amd64.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}/debian12.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: sudo
36 shell: /bin/bash
37runcmd:
38 - sudo growpart /dev/sda 1
39 - sudo resize2fs /dev/sda1
40 - sudo apt-get update
41 - sudo apt-get install -y vim git
42 - cd /home/user
43 - #git clone https://git.0nom.ch/setup
44 - #sudo ./setup/setup.sh
45 - touch /home/user/runcmd_done
46EOF
47cat > "$meta_data" << EOF
48instance-id: vm_id
49local-hostname: $1
50EOF
51genisoimage -output "$seed_iso" -volid cidata -joliet -rock "$user_data" "$meta_data" &> /dev/null || { echo "Failed to create seed.iso."; exit 1; }
52echo "Configuration files generated successfully"
53
diff --git a/others/vm3/.config/cloud-init-files/fedora40.sh b/others/vm3/.config/cloud-init-generator/fedora40.sh
index cd05e10..f32327d 100755
--- a/others/vm3/.config/cloud-init-files/fedora40.sh
+++ b/others/vm3/.config/cloud-init-generator/fedora40.sh
@@ -44,7 +44,6 @@ cat >> "$user_data" << 'EOF'
44 sudo: ['ALL=(ALL) NOPASSWD:ALL'] 44 sudo: ['ALL=(ALL) NOPASSWD:ALL']
45 groups: wheel 45 groups: wheel
46 shell: /bin/bash 46 shell: /bin/bash
47 password: 123
48runcmd: 47runcmd:
49 - sudo growpart /dev/sda 1 48 - sudo growpart /dev/sda 1
50 - sudo xfs_growfs / 49 - sudo xfs_growfs /
@@ -63,3 +62,4 @@ EOF
63genisoimage -output "$seed_iso" -volid cidata -joliet -rock "$user_data" "$meta_data" &> /dev/null || { echo "Failed to create seed.iso."; exit 1; } 62genisoimage -output "$seed_iso" -volid cidata -joliet -rock "$user_data" "$meta_data" &> /dev/null || { echo "Failed to create seed.iso."; exit 1; }
64 63
65echo "Configuration files generated successfully" 64echo "Configuration files generated successfully"
65
diff --git a/others/vm3/.config/cloud-init-generator/freebsd14.0.sh b/others/vm3/.config/cloud-init-generator/freebsd14.0.sh
new file mode 100755
index 0000000..c498efa
--- /dev/null
+++ b/others/vm3/.config/cloud-init-generator/freebsd14.0.sh
@@ -0,0 +1,75 @@
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
new file mode 100755
index 0000000..81fcc72
--- /dev/null
+++ b/others/vm3/.config/cloud-init-generator/freebsd14.0.shbackup
@@ -0,0 +1,59 @@
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/ubuntu24.04.sh b/others/vm3/.config/cloud-init-generator/ubuntu24.04.sh
new file mode 100755
index 0000000..11e0a98
--- /dev/null
+++ b/others/vm3/.config/cloud-init-generator/ubuntu24.04.sh
@@ -0,0 +1,62 @@
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/.docs.swp b/others/vm3/.docs.swp
index 849e1f3..304e100 100644
--- a/others/vm3/.docs.swp
+++ b/others/vm3/.docs.swp
Binary files differ
diff --git a/others/vm3/compute/create.sh b/others/vm3/compute/create.sh
index 0bb2e63..84646d7 100755
--- a/others/vm3/compute/create.sh
+++ b/others/vm3/compute/create.sh
@@ -7,7 +7,8 @@
7vcpu=8 7vcpu=8
8ram_gb=4 8ram_gb=4
9disk_gb=32 9disk_gb=32
10os="fedora40.qcow2" 10os="fedora40"
11ostype="linux"
11 12
12# parse arguments 13# parse arguments
13while [[ $# -gt 0 ]]; do 14while [[ $# -gt 0 ]]; do
@@ -41,35 +42,39 @@ while [[ $# -gt 0 ]]; do
41 esac 42 esac
42done 43done
43 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_dir="/var/lib/libvirt/images/${vmname}"
53new_vm="${new_vm_dir}/${vmname}.qcow2"
54
44# Check mandatory arguments, basicaly checking for initial 1 argument. if it dont exist, vmname will be null 55# Check mandatory arguments, basicaly checking for initial 1 argument. if it dont exist, vmname will be null
45# image is os!! 56# image is os!!
46if [ -z "$vmname" ] ; then 57if [ -z "$vmname" ] ; then
47 echo "Usage: $0 <vm-name> [--image <os>.qcow2] [--vcpu N] [--ram N] [--disk-size N]" 58 echo ""
59 echo "Usage: $0 <vm-name> [--image <os>] [--vcpu N] [--ram N] [--disk-size N]"
48 echo "seed.iso and image file have to be present! Default os is fedora" 60 echo "seed.iso and image file have to be present! Default os is fedora"
49 echo "" 61 echo ""
50 echo "Available images:" 62 echo "Available images:"
51 sudo ls /var/lib/libvirt/images/.image_store 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$//'
52 exit 1 67 exit 1
53fi 68fi
54 69
55scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" #&& echo $scriptdir 70# run the script to make the cloud init files
56workingdir="/var/lib/libvirt/images/.temp" 71sudo bash "${scriptdir}/../.config/cloud-init-generator/${os}.sh" "${vmname}"
57seed_iso="${workingdir}/seed.iso"
58xml="${workingdir}/xml"
59
60image_dir_path="/var/lib/libvirt/images/.image_store"
61src_file="${image_dir_path}/${os}"
62new_vm_dir="/var/lib/libvirt/images/${vmname}"
63new_vm="${new_vm_dir}/${vmname}.qcow2"
64 72
65if [ ! -f "${src_file}" ]; then 73if [ ! -f "${src_file}" ]; then
66 echo -e "${os} image file is cannot be found. please make it available in ${image_dir_path}" 74 echo -e "${os} image file is cannot be found. please make it available in ${image_dir_path}"
67 exit 1 75 exit 1
68fi 76fi
69 77
70# run the script to make the cloud init files
71sudo bash "${scriptdir}/../.config/cloud-init-files/$(basename "$os" .qcow2).sh" "${vmname}"
72
73sudo mkdir -p $new_vm_dir 78sudo mkdir -p $new_vm_dir
74 79
75sudo cp "$src_file" "$new_vm" &> /dev/null || { echo "Failed to create a new image."; exit 1; } 80sudo cp "$src_file" "$new_vm" &> /dev/null || { echo "Failed to create a new image."; exit 1; }
@@ -79,13 +84,17 @@ if sudo virsh list --all | awk "\$2==\"$vmname\"" | grep -q .; then
79 exit 1 84 exit 1
80fi 85fi
81 86
87if [[ ${os,,} == *"freebsd"* ]]; then
88 ostype="generic"
89fi
90
82sudo virt-install --name $vmname \ 91sudo virt-install --name $vmname \
83 --vcpus $vcpu \ 92 --vcpus $vcpu \
84 --memory "$((ram_gb * 1024))"\ 93 --memory "$((ram_gb * 1024))"\
85 --disk path=$new_vm,format=qcow2 \ 94 --disk path=$new_vm,format=qcow2 \
86 --disk path=$seed_iso,device=cdrom \ 95 --disk path=$seed_iso,device=cdrom \
87 --os-type linux \ 96 --os-type $ostype \
88 --os-variant $(basename "$os" .qcow2) \ 97 --os-variant $os \
89 --virt-type kvm \ 98 --virt-type kvm \
90 --graphics none \ 99 --graphics none \
91 --network bridge=virbr0,model=virtio \ 100 --network bridge=virbr0,model=virtio \
diff --git a/others/vm3/docs b/others/vm3/docs
index c274dd3..dc5a3e6 100644
--- a/others/vm3/docs
+++ b/others/vm3/docs
@@ -1,4 +1,9 @@
1 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
5
6
2vm 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 7vm 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
3 # fedora not available. available os: listed down 8 # fedora not available. available os: listed down
4 # storage not available. please create it 9 # storage not available. please create it
@@ -73,3 +78,19 @@ Usage: ./vm compute create <name> <os> --vcpu 4 --ram 8 --disk 40 --storage-pool
73 78
74# Then use directly 79# Then use directly
75./vm compute create myvm fedora 2 4 20 prod-net prod-store 80./vm compute create myvm fedora 2 4 20 prod-net prod-store
81
82
83
84
85for debian12,
86sudo dnf install libguestfs libguestfs-tools libvirt virt-install virt-manager guestfs-tools
87sudo virt-customize -a /var/lib/libvirt/images/.image_store/debian12.qcow2 --root-password password:yourpassword
88sudo apt update && sudo apt install -y ssh cloud-init
89
90console works but no ssh, and the generic cloud ssh fails after editing the password(not sure if this is the reason)
91try to find out how to execute commands in sudo virsh console t1 in a bash script
92not sure if cloud init can work after this but
93
94currently only fedra works fully, debian can work and login after editing the root password
95freebsd have not tried editing root password, boots on generic cloud image, no login yet
96ubuntu no boot no login yet.
diff --git a/others/vm3/test.sh b/others/vm3/test.sh
deleted file mode 100755
index 0dafc23..0000000
--- a/others/vm3/test.sh
+++ /dev/null
@@ -1,49 +0,0 @@
1#!/bin/bash
2
3# Default values
4vcpu=2
5ram_gb=4
6disk_gb=20
7
8# Parse named arguments
9while [[ $# -gt 0 ]]; do
10 case $1 in
11 --vcpu)
12 vcpu="$2"
13 shift 2
14 ;;
15 --ram)
16 ram_gb="$2"
17 shift 2
18 ;;
19 --disk-size)
20 disk_gb="$2"
21 shift 2
22 ;;
23 *)
24 # Handle positional arguments (vmname and os)
25 if [ -z "$vmname" ]; then
26 vmname="$1"
27 elif [ -z "$os" ]; then
28 os="$1"
29 else
30 echo "Unknown argument: $1"
31 exit 1
32 fi
33 shift
34 ;;
35 esac
36done
37
38# Check mandatory arguments
39if [ -z "$vmname" ] || [ -z "$os" ]; then
40 echo "Usage: $0 <vm-name> <os> [--vcpu N] [--ram N] [--disk-size N]"
41 exit 1
42fi
43
44# Now you can use the variables
45echo "VM Name: $vmname"
46echo "OS: $os"
47echo "VCPU: $vcpu"
48echo "RAM: $ram_gb GB"
49echo "Disk: $disk_gb GB"