From bf00263bc585780a1bd7494ce9d56c3cc57f34eb Mon Sep 17 00:00:00 2001 From: GnomeZworc Date: Thu, 15 May 2025 21:20:28 +0200 Subject: [PATCH] add metadata files Signed-off-by: GnomeZworc --- README.md | 62 ++++++++++++++++++++++++++++++++++++++++ agent.sh | 5 ++-- files/dnsmasq.service | 11 ++++++++ files/dnsmasq.sh | 21 ++++++++++++++ files/metadata.service | 10 +++++++ files/metadata.sh | 17 +++++++++++ lib/metadata.sh | 64 ++++++++++++++++++++++++++++++++++++++++++ lib/numbers.sh | 7 ++++- lib/qemu.sh | 14 +++++++-- 9 files changed, 206 insertions(+), 5 deletions(-) create mode 100644 files/dnsmasq.service create mode 100644 files/dnsmasq.sh create mode 100644 files/metadata.service create mode 100644 files/metadata.sh create mode 100644 lib/metadata.sh diff --git a/README.md b/README.md index 2242bc4..a8208f5 100644 --- a/README.md +++ b/README.md @@ -68,4 +68,66 @@ exec ip netns exec "$NETNS" \ --no-resolv \ --log-facility="/var/log/dnsmasq-$arg.log" \ --no-daemon -p0 +``` + +``` +vm-1-toto:~# cat /etc/cloud/cloud.cfg.d/20_user.cfg +system_info: + default_user: + name: syonad +vm-1-toto:~# cat /etc/cloud/cloud.cfg.d/99_metadata.cfg +datasource_list: [ NoCloud ] +datasource: + NoCloud: + seedfrom: 'http://169.254.169.254:80' + timeout: 5 + max_wait: 10 +``` + + +``` +qemu-system-x86_64 -enable-kvm -cpu host -m 512 \ + -smp 1 -serial unix:/tmp/i-0343234.sock,server,nowait \ + -monitor unix:/tmp/i-0343234.mon-sock,server,nowait \ + -qmp unix:/tmp/i-0343234.qmp-sock,server,nowait \ + -drive file=/disk/vm-1.qcow2,if=virtio \ + -netdev tap,id=net0,ifname=tap9837026863,script=no,downscript=no -device virtio-net-pci,netdev=net0,mac=00:22:33:00:00:0A \ + -display none -daemonize +``` + + +``` +qemu-system-x86_64 -enable-kvm -cpu host -m 512 \ + -smp 1 -serial unix:/tmp/i-0343234.sock,server,nowait \ + -monitor unix:/tmp/i-0343234.mon-sock,server,nowait \ + -qmp unix:/tmp/i-0343234.qmp-sock,server,nowait \ + -drive file=/disk/vm-2.qcow2,if=virtio \ + -drive file=/disk/tmp.qcow2,if=virtio \ + -drive file=/disk/root.qcow2,if=virtio \ + -netdev tap,id=net0,ifname=tap9102959250,script=no,downscript=no -device virtio-net-pci,netdev=net0,mac=00:22:33:00:00:0A \ + -display none -daemonize \ + -drive file=./seed/seed.iso,media=cdrom,if=ide +``` + +add public ip: +``` +brctl addbr br-public +brctl stp "br-public" off +ip link add "veth-public-ext" type veth peer name "veth-public-int" netns "vpc-00003" +ip netns exec "vpc-00003" brctl addbr "br-public" +ip netns exec "vpc-00003" brctl stp "br-public" off + +brctl addif "br-public" "veth-public-ext" +ip netns exec "vpc-00003" brctl addif "br-public" "veth-public-int" + + +ip link set up dev "veth-public-ext" +ip link set up dev "br-public" +ip -n "vpc-00003" link set up dev "veth-public-int" +ip -n "vpc-00003" link set up dev "br-public" + +ip link add link eno1 name macvlan0 type macvlan mode bridge + +-A PREROUTING -d 192.168.15.2/32 -j DNAT --to-destination 192.168.20.10 +-A POSTROUTING -s 192.168.20.10/32 -o br-public -j MASQUERADE ``` \ No newline at end of file diff --git a/agent.sh b/agent.sh index c847469..955b150 100644 --- a/agent.sh +++ b/agent.sh @@ -26,6 +26,7 @@ function start_vm { DEFINE_string 'vm_name' '-' 'VM NAME' 'm' DEFINE_string 'vm_ip' '-' 'VM CIDR' 'p' DEFINE_string 'volume_id' '-' 'Volume backend file' 'o' + DEFINE_string 'ssh_key' '-' 'pub ssh Key' 'k' DEFINE_boolean 'dryrun' false 'Enable dry-run mode' 'd' FLAGS "$@" || exit $? @@ -52,14 +53,14 @@ function start_vm { print_in_color "${COLOR_SYAN}" " name: ${FLAGS_vm_name}" print_in_color "${COLOR_SYAN}" " ip: ${vm_ip}" print_in_color "${COLOR_SYAN}" " volume backing file: ${FLAGS_volume_id}" - print_in_color "${COLOR_SYAN}" " tap: to generate" + print_in_color "${COLOR_SYAN}" " sshkey: ${FLAGS_ssh_key}" print_in_color "${COLOR_SYAN}" "#############################################" echo echo create_vpc "${FLAGS_vpc_name}" create_subnet "${FLAGS_vpc_name}" "${FLAGS_subnet_name}" "${FLAGS_vxlan_id}" "${local_ip}" "${gateway_ip}" "${subnet}" - qemu_start_vm "${FLAGS_vm_ip}" "${FLAGS_subnet_name}" "${FLAGS_vpc_name}" "${FLAGS_vm_name}" "${FLAGS_volume_id}" + qemu_start_vm "${FLAGS_vm_ip}" "${FLAGS_subnet_name}" "${FLAGS_vpc_name}" "${FLAGS_vm_name}" "${FLAGS_volume_id}" "${FLAGS_ssh_key}" "${FLAGS_gateway_ip}" } function stop_vm { diff --git a/files/dnsmasq.service b/files/dnsmasq.service new file mode 100644 index 0000000..d8ee419 --- /dev/null +++ b/files/dnsmasq.service @@ -0,0 +1,11 @@ +[Unit] +Description=dnsmasq in netns %i +After=network.target + +[Service] +Type=simple +ExecStart=/usr/local/bin/run-dnsmasq-in-netns.sh %i +ExecStopPost=/bin/rm -f /run/dnsmasq-%i.pid + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/files/dnsmasq.sh b/files/dnsmasq.sh new file mode 100644 index 0000000..f506ef8 --- /dev/null +++ b/files/dnsmasq.sh @@ -0,0 +1,21 @@ +#!/bin/bash +set -e + +# Expects one argument: netns_bridge (e.g. vpc-00003_br-00002 or vpc1_br0) +arg="$1" +NETNS="${arg%%_*}" +BRIDGE="${arg#*_}" + +echo "start dnsmasq ${NETNS} ${BRIDGE}" + +exec ip netns exec "${NETNS}" \ + dnsmasq \ + --no-daemon \ + --interface="${BRIDGE}" \ + --bind-interfaces \ + --pid-file="/run/dnsmasq-$arg.pid" \ + --conf-file="/etc/dnsmasq.d/$arg.conf" \ + --no-hosts \ + --no-resolv \ + --log-facility="/var/log/dnsmasq-$arg.log" \ + --no-daemon -p0 \ No newline at end of file diff --git a/files/metadata.service b/files/metadata.service new file mode 100644 index 0000000..54cf08a --- /dev/null +++ b/files/metadata.service @@ -0,0 +1,10 @@ +[Unit] +Description=metadata in netns %i +After=network.target + +[Service] +Type=simple +ExecStart=/usr/local/bin/run-metadata-in-netns.sh %i + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/files/metadata.sh b/files/metadata.sh new file mode 100644 index 0000000..94d7057 --- /dev/null +++ b/files/metadata.sh @@ -0,0 +1,17 @@ +#!/bin/bash +set -e + +# Expects one argument: netns_bridge (e.g. vpc-00003_br-00002 or vpc1_br0) +arg="$1" +NETNS="${arg%%_*}" +ip_port="${arg#*_}" +IP="${ip_port%%-*}" +PORT="${ip_port#*-}" + +echo "start metadata ${NETNS} " + +exec ip netns exec "${NETNS}" \ + /usr/bin/metadata \ + -file "/opt/metadata/${arg}.json" \ + -interface "${IP}" \ + -port "${PORT}" \ No newline at end of file diff --git a/lib/metadata.sh b/lib/metadata.sh new file mode 100644 index 0000000..d4d793f --- /dev/null +++ b/lib/metadata.sh @@ -0,0 +1,64 @@ +#!/bin/bash + +function metadata_service() { + cp ./files/metadata.service /etc/systemd/system/metadata@.service + cp ./files/metadata.sh /usr/local/bin/run-metadata-in-netns.sh + chmod +x /usr/local/bin/run-metadata-in-netns.sh +} + +function metadata_config() { + vm_name="${1}" + meta_def="${2}" + ssh_key="${3}" + + PASSWORD="\$6\$LD5z9v/s2b170Twj\$pl1B/Mz0w8PCr7JhFcclQpBSxUQTDOiuPko8yf4LntEo8FrIo2yKVeHR6fb5VQpxX9NP2k9yhXdsp671Sc56u1" + + VENDOR_DATA=$( + cat < "/opt/metadata/${meta_def}.conf" +{ + "meta-data": "instance-id: ${vm_name}\nlocal-hostname: ${vm_name}\n", + "user-data": "#!/bin/sh\npasswd -d root\n", + "network-config": "version: 2\nethernets:\n eth0:\n dhcp4: true\n", + "vendor-data": "${VENDOR_DATA}" +} +END + + +} + +function metadata_start() { + vpc="${1}" + ip="${2}" + port="${3}" + vm_name="${4}" + ssh_key="${5}" + + + metadata_config "${vm_name}" "${vpc}_${ip}-${port}" "${ssh_key}" + systemctl start "metadata@${vpc}_${ip}-${port}.service" +} + +function metadata_stop() { + vpc="${1}" + ip="${2}" + port="${3}" + + systemctl stop "metadata@${vpc}_${ip}-${port}.service" + rm -f "/opt/metadata/${vpc}_${ip}-${port}.conf" +} \ No newline at end of file diff --git a/lib/numbers.sh b/lib/numbers.sh index db4c4dc..f50ced7 100644 --- a/lib/numbers.sh +++ b/lib/numbers.sh @@ -1,10 +1,15 @@ #!/bin/bash -function generate_random_number { +function generate_random_id { local digits="$1" tr -dc '0-9'