From 30c76bf9c5b171dea4b07a349914e441e649d44b Mon Sep 17 00:00:00 2001 From: Matthieu Bessat Date: Fri, 21 Jun 2024 11:28:42 +0200 Subject: [PATCH] feat: add cloud-init and virt-customize method --- cloud-init_method/.gitignore | 1 + cloud-init_method/README.md | 7 ++++++ .../cloud-init-config/.gitignore | 1 + .../cloud-init-config/meta-data.yaml | 2 ++ .../cloud-init-config/user-data.yaml | 22 +++++++++++++++++++ .../cloud-init-config/vendor-data.yaml | 0 cloud-init_method/generate-config.sh | 10 +++++++++ cloud-init_method/init.sh | 12 ++++++++++ cloud-init_method/run.sh | 13 +++++++++++ sandbox | 7 ++++++ sandbox.pub | 1 + virt-customize_method/.gitignore | 2 ++ virt-customize_method/.ssh/authorized_keys | 1 + virt-customize_method/README.md | 7 ++++++ virt-customize_method/init.sh | 15 +++++++++++++ virt-customize_method/netplan/50-dhcp.yaml | 9 ++++++++ virt-customize_method/run.sh | 9 ++++++++ 17 files changed, 119 insertions(+) create mode 100644 cloud-init_method/.gitignore create mode 100644 cloud-init_method/README.md create mode 100644 cloud-init_method/cloud-init-config/.gitignore create mode 100644 cloud-init_method/cloud-init-config/meta-data.yaml create mode 100644 cloud-init_method/cloud-init-config/user-data.yaml create mode 100644 cloud-init_method/cloud-init-config/vendor-data.yaml create mode 100755 cloud-init_method/generate-config.sh create mode 100755 cloud-init_method/init.sh create mode 100755 cloud-init_method/run.sh create mode 100644 sandbox create mode 100644 sandbox.pub create mode 100644 virt-customize_method/.gitignore create mode 100644 virt-customize_method/.ssh/authorized_keys create mode 100644 virt-customize_method/README.md create mode 100755 virt-customize_method/init.sh create mode 100644 virt-customize_method/netplan/50-dhcp.yaml create mode 100755 virt-customize_method/run.sh diff --git a/cloud-init_method/.gitignore b/cloud-init_method/.gitignore new file mode 100644 index 0000000..b511ae1 --- /dev/null +++ b/cloud-init_method/.gitignore @@ -0,0 +1 @@ +*.qcow2 diff --git a/cloud-init_method/README.md b/cloud-init_method/README.md new file mode 100644 index 0000000..ca57e53 --- /dev/null +++ b/cloud-init_method/README.md @@ -0,0 +1,7 @@ +# Auto setup of debian vm with cloud image + cloudinit method + +First download cloud generic image + + wget https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-generic-amd64.qcow2 + +To run the VM diff --git a/cloud-init_method/cloud-init-config/.gitignore b/cloud-init_method/cloud-init-config/.gitignore new file mode 100644 index 0000000..1fcb152 --- /dev/null +++ b/cloud-init_method/cloud-init-config/.gitignore @@ -0,0 +1 @@ +out diff --git a/cloud-init_method/cloud-init-config/meta-data.yaml b/cloud-init_method/cloud-init-config/meta-data.yaml new file mode 100644 index 0000000..0e25ffd --- /dev/null +++ b/cloud-init_method/cloud-init-config/meta-data.yaml @@ -0,0 +1,2 @@ +instance-id: mbessinfra/sandbox03 +local-hostname: sandbox03 diff --git a/cloud-init_method/cloud-init-config/user-data.yaml b/cloud-init_method/cloud-init-config/user-data.yaml new file mode 100644 index 0000000..c20cfab --- /dev/null +++ b/cloud-init_method/cloud-init-config/user-data.yaml @@ -0,0 +1,22 @@ +#cloud-config +hostname: jpplandhost +groups: + - jppland +users: + - default + - calyjohn + - name: mbess + passwd: "$6$QQuYYEMEG0bJf7iH$mhFeXl10jSQhFO8ltMAi3CtoGLOz5tPdnEBTcp.4JzS5mnBbpANvm9s.VQFMvWq8nfzgzdDZMGr1hnZLGVd2V." #root + sudo: ALL=(ALL) NOPASSWD:ALL + groups: sudo + shell: /bin/bash + lock_passwd: false + ssh_authorized_keys: + - "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIBeNeE0gk6mO0Bz9dDvKQrhA2Oo7x05FicCQJivxqIyi sandbox" + +write_files: + - encoding: gzip + content: !!binary | + H4sIAIDb/U8C/1NW1E/KzNMvzuBKTc7IV8hIzcnJVyjPL8pJ4QIA6N+MVxsAAAA= + path: /usr/bin/hello + permissions: '0755' diff --git a/cloud-init_method/cloud-init-config/vendor-data.yaml b/cloud-init_method/cloud-init-config/vendor-data.yaml new file mode 100644 index 0000000..e69de29 diff --git a/cloud-init_method/generate-config.sh b/cloud-init_method/generate-config.sh new file mode 100755 index 0000000..9f0f05b --- /dev/null +++ b/cloud-init_method/generate-config.sh @@ -0,0 +1,10 @@ +#!/usr/bin/sh +cd cloud-init-config +rm -rf out +mkdir -p out +cp meta-data.yaml out/meta-data +cp user-data.yaml out/user-data +cd out +genisoimage -output seed.iso -volid cidata -joliet -rock user-data meta-data + + diff --git a/cloud-init_method/init.sh b/cloud-init_method/init.sh new file mode 100755 index 0000000..5e78add --- /dev/null +++ b/cloud-init_method/init.sh @@ -0,0 +1,12 @@ +#!/usr/bin/sh + +wget -N https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-generic-amd64.qcow2 + +# virt-install \ +# --name=sandbox03 \ +# --ram=512 --vcpus=1 \ +# --import --disk path=debian-12-generic-amd64.qcow2,format=qcow2 \ +# --disk path=cloud-init-config/seed.iso,device=cdrom \ +# --os-variant=debian12 \ +# --network bridge=virbr0,model=virtio + diff --git a/cloud-init_method/run.sh b/cloud-init_method/run.sh new file mode 100755 index 0000000..ba0ccb8 --- /dev/null +++ b/cloud-init_method/run.sh @@ -0,0 +1,13 @@ +#!/usr/bin/sh + +qemu-system-x86_64 \ + -machine accel=kvm \ + -cpu host \ + -m 512 \ + -nographic \ + -hda ./debian-12-generic-amd64.qcow2 \ + -drive driver=raw,file=./cloud-init-config/out/seed.iso,if=virtio \ + -net nic \ + -net user,hostfwd=tcp::2222-:22 + + #-smbios type=1,serial=ds='nocloud;s=http://10.0.2.2:8000/' diff --git a/sandbox b/sandbox new file mode 100644 index 0000000..de35991 --- /dev/null +++ b/sandbox @@ -0,0 +1,7 @@ +-----BEGIN OPENSSH PRIVATE KEY----- +b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW +QyNTUxOQAAACAXjXhNIJOpjtAc/XQ7ykK4QNjqO8dORYnAkCYr8aiMogAAAJBTmHyjU5h8 +owAAAAtzc2gtZWQyNTUxOQAAACAXjXhNIJOpjtAc/XQ7ykK4QNjqO8dORYnAkCYr8aiMog +AAAEAmrXAipMd8QVHaDDofYK9OvJ1NXzcz7wwCeC/zCb4gOxeNeE0gk6mO0Bz9dDvKQrhA +2Oo7x05FicCQJivxqIyiAAAAB3NhbmRib3gBAgMEBQY= +-----END OPENSSH PRIVATE KEY----- diff --git a/sandbox.pub b/sandbox.pub new file mode 100644 index 0000000..5213627 --- /dev/null +++ b/sandbox.pub @@ -0,0 +1 @@ +ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIBeNeE0gk6mO0Bz9dDvKQrhA2Oo7x05FicCQJivxqIyi sandbox diff --git a/virt-customize_method/.gitignore b/virt-customize_method/.gitignore new file mode 100644 index 0000000..e178f45 --- /dev/null +++ b/virt-customize_method/.gitignore @@ -0,0 +1,2 @@ +*.qcow2 +vm diff --git a/virt-customize_method/.ssh/authorized_keys b/virt-customize_method/.ssh/authorized_keys new file mode 100644 index 0000000..5213627 --- /dev/null +++ b/virt-customize_method/.ssh/authorized_keys @@ -0,0 +1 @@ +ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIBeNeE0gk6mO0Bz9dDvKQrhA2Oo7x05FicCQJivxqIyi sandbox diff --git a/virt-customize_method/README.md b/virt-customize_method/README.md new file mode 100644 index 0000000..fdb607d --- /dev/null +++ b/virt-customize_method/README.md @@ -0,0 +1,7 @@ +# Initial setup of debian VM with virt-customize + netplan + +This method consist of using the debian generic cloud image and just customizing it before first boot with the [virt-customize](https://man.archlinux.org/man/virt-customize.1.en) util. + +Then for the network config, we will be using [netplan](https://netplan.readthedocs.io/en/stable/). + + diff --git a/virt-customize_method/init.sh b/virt-customize_method/init.sh new file mode 100755 index 0000000..bca7b2d --- /dev/null +++ b/virt-customize_method/init.sh @@ -0,0 +1,15 @@ +#!/usr/bin/sh +base="$(pwd)" +mkdir -p vm +cd vm +wget -N https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-generic-amd64.qcow2 + +virt-customize \ + --add ./debian-12-generic-amd64.qcow2 \ + --root-password password:root \ + --hostname "sandbox05" \ + --firstboot-install "procps,psmisc,vim,net-tools,curl,dnsutils,file,tmux" \ + --copy-in "$base/netplan:/etc" \ + --copy-in "$base/.ssh:/root" \ + --firstboot-command 'netplan apply && ssh-keygen -A && systemctl restart sshd' + diff --git a/virt-customize_method/netplan/50-dhcp.yaml b/virt-customize_method/netplan/50-dhcp.yaml new file mode 100644 index 0000000..cd6b2ca --- /dev/null +++ b/virt-customize_method/netplan/50-dhcp.yaml @@ -0,0 +1,9 @@ +network: + version: 2 + ethernets: + all-en: + match: + name: "en*" + dhcp4: true + dhcp6: true + diff --git a/virt-customize_method/run.sh b/virt-customize_method/run.sh new file mode 100755 index 0000000..5f66a94 --- /dev/null +++ b/virt-customize_method/run.sh @@ -0,0 +1,9 @@ +cd vm +qemu-system-x86_64 \ + -net nic \ + -net user \ + -machine accel=kvm \ + -cpu host \ + -m 512 \ + -nographic \ + -hda ./debian-12-generic-amd64.qcow2