From 52fb7aebaf2ee9572aac2176eda5d89f2c7fa727 Mon Sep 17 00:00:00 2001 From: kencx Date: Sun, 24 Mar 2024 02:27:48 +0800 Subject: [PATCH] Remove packer vagrant --- packer/vagrant/Makefile | 12 ---- packer/vagrant/README.md | 65 --------------------- packer/vagrant/bin/minimize.sh | 36 ------------ packer/vagrant/bin/vagrant.sh | 21 ------- packer/vagrant/http/preseed.pkrtpl | 74 ------------------------ packer/vagrant/qemu-vagrant.pkr.hcl | 90 ----------------------------- packer/vagrant/tests/Vagrantfile | 11 ---- packer/vagrant/variables.pkr.hcl | 47 --------------- 8 files changed, 356 deletions(-) delete mode 100644 packer/vagrant/Makefile delete mode 100644 packer/vagrant/README.md delete mode 100644 packer/vagrant/bin/minimize.sh delete mode 100644 packer/vagrant/bin/vagrant.sh delete mode 100644 packer/vagrant/http/preseed.pkrtpl delete mode 100644 packer/vagrant/qemu-vagrant.pkr.hcl delete mode 100644 packer/vagrant/tests/Vagrantfile delete mode 100644 packer/vagrant/variables.pkr.hcl diff --git a/packer/vagrant/Makefile b/packer/vagrant/Makefile deleted file mode 100644 index d3b2103..0000000 --- a/packer/vagrant/Makefile +++ /dev/null @@ -1,12 +0,0 @@ -.PHONY: build add up up.d -build: - packer build -var-file="auto.pkrvars.hcl" . - -add: - vagrant box add builds/base.libvirt.box --provider libvirt --name kenc/debian-11.5 - -up: - cd tests && vagrant up --provider=libvirt - -up.d: - cd tests && vagrant up --provider=libvirt --debug diff --git a/packer/vagrant/README.md b/packer/vagrant/README.md deleted file mode 100644 index 8aa082d..0000000 --- a/packer/vagrant/README.md +++ /dev/null @@ -1,65 +0,0 @@ -## Installation - -1. Install Vagrant -2. Install libvirt - -```bash -$ sudo apt install qemu libvirt-daemon-system libvirt-dev ebtables \ - libguestfs-tools ruby-libvirt libvirt-clients bridge-utils -$ sudo adduser [user] kvm -$ sudo adduser [user] libvirt -$ virsh list --all -``` - -3. Install Vagrant plugins - -```bash -$ vagrant plugin install vagrant-libvirt -$ vagrant plugin install vagrant-mutate -``` - -4. Build Vagrant box - -```bash -$ packer build . -$ vagrant box add builds/base.libvirt.box --provider libvirt --name test/debian-11.5 -``` - -5. Start VM with libvirt provider - -```bash -$ vagrant init test/debian-11.5 -$ vagrant up --provider=libvirt -$ vagrant ssh -``` - -## Building - -We build the Vagrant box with the qemu builder and Vagrant post-processor. Several -scripts are **required** for the Vagrant box to function properly with Vagrant: - -- Ensure `UseDNS no` is added to `sshd_config` -- Use `eth0` interface (for libvirt) -- For qemu builds, minimize the image with `minimize.sh` - -## Notes - -Specify your custom SSH key pair with `ssh_private_key_file` and `ssh_public_key_file`. -The SSH public key will be added to the user's `.ssh/authorized_keys` file. - -The default root password is `vagrant`. Although root login is disabled, it is -recommended to change this for non-development systems: - -```hcl -# auto.pkrvars.hcl -root_password = changeme -``` - -or you can choose to change the root password on startup with - -```bash -$ sudo passwd root -``` - -It is also recommmended to disable password-less sudo, which has been enabled for -easy provisioning. diff --git a/packer/vagrant/bin/minimize.sh b/packer/vagrant/bin/minimize.sh deleted file mode 100644 index 8384b60..0000000 --- a/packer/vagrant/bin/minimize.sh +++ /dev/null @@ -1,36 +0,0 @@ -#!/bin/sh -eux - -case "$PACKER_BUILDER_TYPE" in - qemu) exit 0 ;; -esac - -# Whiteout root -count=$(df --sync -kP / | tail -n1 | awk -F ' ' '{print $4}') -count=$((count-1)) -dd if=/dev/zero of=/tmp/whitespace bs=1M count=$count || echo "dd exit code $? is suppressed"; -rm /tmp/whitespace - -# Whiteout /boot -count=$(df --sync -kP /boot | tail -n1 | awk -F ' ' '{print $4}') -count=$((count-1)) -dd if=/dev/zero of=/boot/whitespace bs=1M count=$count || echo "dd exit code $? is suppressed"; -rm /boot/whitespace - -set +e -swapuuid="$(/sbin/blkid -o value -l -s UUID -t TYPE=swap)"; -case "$?" in - 2|0) ;; - *) exit 1 ;; -esac -set -e - -if [ "x${swapuuid}" != "x" ]; then - # Whiteout the swap partition to reduce box size - # Swap is disabled till reboot - swappart="$(readlink -f /dev/disk/by-uuid/$swapuuid)"; - /sbin/swapoff "$swappart" || true; - dd if=/dev/zero of="$swappart" bs=1M || echo "dd exit code $? is suppressed"; - /sbin/mkswap -U "$swapuuid" "$swappart"; -fi - -sync; diff --git a/packer/vagrant/bin/vagrant.sh b/packer/vagrant/bin/vagrant.sh deleted file mode 100644 index f9c27ba..0000000 --- a/packer/vagrant/bin/vagrant.sh +++ /dev/null @@ -1,21 +0,0 @@ -#!/bin/sh -eux - -# turn off reverse dns lookup when ssh-ing -SSHD_CONFIG="/etc/ssh/sshd_config" -# ensure that there is a trailing newline before attempting to concatenate -sed -i -e '$a\' "$SSHD_CONFIG" - -USEDNS="UseDNS no" -if grep -q -E "^[[:space:]]*UseDNS" "$SSHD_CONFIG"; then - sed -i "s/^\s*UseDNS.*/${USEDNS}/" "$SSHD_CONFIG" -else - echo "$USEDNS" >>"$SSHD_CONFIG" -fi - -# disable predictable network interface names and use eth0 -sed -i 's/en[[:alnum:]]*/eth0/g' /etc/network/interfaces; -sed -i 's/GRUB_CMDLINE_LINUX="\(.*\)"/GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0 \1"/g' /etc/default/grub; -update-grub; - -# Adding a 2 sec delay to the interface up, to make the dhclient happy -echo "pre-up sleep 2" >> /etc/network/interfaces diff --git a/packer/vagrant/http/preseed.pkrtpl b/packer/vagrant/http/preseed.pkrtpl deleted file mode 100644 index 213f7aa..0000000 --- a/packer/vagrant/http/preseed.pkrtpl +++ /dev/null @@ -1,74 +0,0 @@ -choose-mirror-bin mirror/http/proxy string -d-i apt-setup/use_mirror boolean true -d-i base-installer/kernel/override-image string linux-server -d-i grub-installer/only_debian boolean true -d-i grub-installer/with_other_os boolean true -# d-i grub-installer/bootdev string default -# d-i hw-detect/load_firmware boolean false -# d-i hw-detect/load_media boolean false - -# Clock Setup -d-i clock-setup/utc boolean true -d-i clock-setup/utc-auto boolean true -d-i time/zone string UTC -# d-i time/zone string Asia/Singapore - -# Locale Setup -# d-i debian-installer/language string en -# d-i debian-installer/country string sg - -# # Keyboard Setup -d-i keymap select us - -# mirror -apt-mirror-setup apt-setup/use_mirror boolean true -d-i mirror/country string manual -d-i mirror/http/directory string /debian -d-i mirror/http/hostname string httpredir.debian.org -d-i mirror/http/proxy string - -# Drive Setup -d-i partman-auto/method string lvm -d-i partman-auto-lvm/guided_size string max -d-i partman-auto/choose_recipe select atomic -d-i partman-lvm/confirm boolean true -d-i partman-lvm/confirm_nooverwrite boolean true -d-i partman-lvm/device_remove_lvm boolean true -d-i partman-md/device_remove_md boolean true -d-i partman/choose_partition select finish -d-i partman/confirm boolean true -d-i partman/confirm_nooverwrite boolean true -d-i partman/confirm_write_new_label boolean true - -# User Setup -d-i passwd/root-login boolean false -d-i passwd/root-password password ${root_password} -d-i passwd/root-password-again password ${root_password} -d-i passwd/user-fullname string ${username} -d-i passwd/username string ${username} -d-i passwd/user-uid string 1000 -d-i passwd/user-password password ${password} -d-i passwd/user-password-again password ${password} -d-i passwd/user-default-groups sudo -d-i user-setup/allow-password-weak boolean true -d-i user-setup/encrypt-home boolean false - -# Package Setup -# d-i apt-setup/contrib boolean true -# d-i apt-setup/non-free boolean true -tasksel tasksel/first multiselect standard, ssh-server -d-i pkgsel/install-language-support boolean false -d-i pkgsel/update-policy select none -d-i pkgsel/upgrade select full-upgrade -d-i pkgsel/include string sudo wget curl fuse make -# d-i pkgsel/update-policy select unattended-upgrades -# d-i pkgsel/include string sudo unattended-upgrades -# d-i pkgsel/include string sudo cloud-init qemu-guest-agent -popularity-contest popularity-contest/participate boolean false - -# Final Setup -d-i finish-install/reboot_in_progress note - -d-i preseed/late_command string sed -i '/^deb cdrom:/s/^/#/' /target/etc/apt/sources.list -# https://unix.stackexchange.com/questions/409212/preseed-directive-to-skip-another-cd-dvd-scanning -apt-cdrom-setup apt-setup/cdrom/set-first boolean false diff --git a/packer/vagrant/qemu-vagrant.pkr.hcl b/packer/vagrant/qemu-vagrant.pkr.hcl deleted file mode 100644 index 93de42e..0000000 --- a/packer/vagrant/qemu-vagrant.pkr.hcl +++ /dev/null @@ -1,90 +0,0 @@ -locals { - preseed = { - username = var.ssh_username - password = var.ssh_password - root_password = var.root_password - } - ssh_public_key = file(var.ssh_public_key_path) -} - -source "qemu" "base" { - vm_name = var.vm_name - headless = true - shutdown_command = "echo '${var.ssh_password}' | sudo -S /sbin/shutdown -hP now" - - iso_url = var.iso_url - iso_checksum = var.iso_checksum - - cpus = 2 - disk_size = "65536" - memory = 1024 - qemuargs = [ - ["-m", "1024M"], - ["-bios", "bios-256k.bin"], - ["-display", "none"] - ] - - ssh_username = var.ssh_username - ssh_password = var.ssh_password - ssh_private_key_file = var.ssh_private_key_path - ssh_port = 22 - ssh_wait_timeout = "3600s" - - http_content = { - "/preseed.cfg" = templatefile("${path.root}/http/preseed.pkrtpl", local.preseed) - } - boot_wait = "5s" - boot_command = ["install preseed/url=http://{{ .HTTPIP }}:{{ .HTTPPort }}/preseed.cfg debian-installer=en_US.UTF-8 auto locale=en_US.UTF-8 kbd-chooser/method=us keyboard-configuration/xkb-keymap=us netcfg/get_hostname={{ .Name }} netcfg/get_domain=vagrantup.com fb=false debconf/frontend=noninteractive console-setup/ask_detect=false console-keymaps-at/keymap=us grub-installer/bootdev=default "] -} - -build { - sources = ["source.qemu.base"] - - # Make user ssh-ready for Ansible - provisioner "shell" { - execute_command = "echo '${var.ssh_password}' | {{ .Vars }} sudo -S -E sh -eux '{{ .Path }}'" - inline = [ - "HOME_DIR=/home/${var.ssh_username}/.ssh", - "mkdir -m 0700 -p $HOME_DIR", - "echo '${local.ssh_public_key}' >> $HOME_DIR/authorized_keys", - "chown -R ${var.ssh_username}:${var.ssh_username} $HOME_DIR", - "chmod 0600 $HOME_DIR/authorized_keys", - "SUDOERS_FILE=/etc/sudoers.d/${var.ssh_username}", - "echo '${var.ssh_username} ALL=(ALL) NOPASSWD: ALL' > $SUDOERS_FILE", - "chmod 0440 $SUDOERS_FILE", - ] - expect_disconnect = true - } - - provisioner "ansible" { - playbook_file = "../../ansible/playbooks/common.yml" - extra_arguments = [ - "-e", - "user=${var.ssh_username}", - "-e", - "ansible_become_password=${var.ssh_password}", - ] - galaxy_file = "../../requirements.yml" - user = var.ssh_username - ansible_env_vars = [ - "ANSIBLE_STDOUT_CALLBACK=yaml", - "ANSIBLE_HOST_KEY_CHECKING=False", - ] - } - - # vagrant-specific setup - provisioner "shell" { - execute_command = "echo '${var.ssh_password}' | {{ .Vars }} sudo -S -E sh -eux '{{ .Path }}'" - scripts = [ - "./bin/vagrant.sh", - "./bin/minimize.sh" - ] - expect_disconnect = true - } - - post-processors { - post-processor "vagrant" { - output = "./builds/{{ .BuildName }}.{{ .Provider }}.${formatdate("YYYY-MM-DD", timestamp())}.box" - } - } -} diff --git a/packer/vagrant/tests/Vagrantfile b/packer/vagrant/tests/Vagrantfile deleted file mode 100644 index 79a316f..0000000 --- a/packer/vagrant/tests/Vagrantfile +++ /dev/null @@ -1,11 +0,0 @@ -# -*- mode: ruby -*- -# vi: set ft=ruby : - -Vagrant.configure("2") do |config| - - config.vm.box = "kenc/debian-11.5" - config.vm.box_url = "file://../builds/base.libvirt.box" - - config.nfs.verify_installed = false - config.vm.synced_folder '.', '/vagrant', disabled: true -end diff --git a/packer/vagrant/variables.pkr.hcl b/packer/vagrant/variables.pkr.hcl deleted file mode 100644 index e12c4cb..0000000 --- a/packer/vagrant/variables.pkr.hcl +++ /dev/null @@ -1,47 +0,0 @@ -variable "iso_url" { - type = string - description = "ISO file URL" - default = "https://cdimage.debian.org/debian-cd/11.5.0/amd64/iso-cd/debian-11.5.0-amd64-netinst.iso" -} - -variable "iso_checksum" { - type = string - description = "ISO file checksum" - default = "file:https://cdimage.debian.org/debian-cd/11.5.0/amd64/iso-cd/SHA256SUMS" -} - -variable "vm_name" { - type = string - description = "VM name" - default = "base" -} - -variable "root_password" { - type = string - description = "Root password" - default = "vagrant" -} - -variable "ssh_username" { - type = string - description = "SSH username" - default = "debian" -} - -variable "ssh_password" { - type = string - description = "SSH password" - default = "vagrant" -} - -variable "ssh_public_key_path" { - type = string - description = "SSH Public Key Path" - default = "~/.ssh/vagrant.pub" -} - -variable "ssh_private_key_path" { - type = string - description = "SSH Private Key Path" - default = "~/.ssh/vagrant" -}