summaryrefslogtreecommitdiff
path: root/others/vm3/.config/cloud-init-generator/debian12.sh
blob: 0bdb418f873da6335af847899dff5de73b51f01a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/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
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"
sudo mkdir -p "$config_dir"
[ ! -f "$src_file" ] && echo "source image does not exist! downloading..." && sudo wget -O "$src_file" "$image_url" 
cat > "$user_data" << EOF
#cloud-config
users:
  - name: user
    ssh-authorized-keys:
EOF
for key in $sshkeysdir/*.pub; do
    echo "      - $(cat "$key")" >> $user_data
done
cat >> "$user_data" << 'EOF'
    sudo: ['ALL=(ALL) NOPASSWD:ALL']
    groups: sudo
    shell: /bin/bash
runcmd:
  - sudo growpart /dev/sda 1
  - sudo resize2fs /dev/sda1
  - sudo apt-get update
  - sudo apt-get install -y vim git
  - cd /home/user
  - #git clone https://git.0nom.ch/setup
  - #sudo ./setup/setup.sh
  - touch /home/user/runcmd_done
EOF
cat > "$meta_data" << EOF
instance-id: vm_id
local-hostname: $1
EOF
genisoimage -output "$seed_iso" -volid cidata -joliet -rock "$user_data" "$meta_data" &> /dev/null || { echo "Failed to create seed.iso."; exit 1; }
echo "Configuration files generated successfully"