diff --git a/.gitignore b/.gitignore index 47514ca..2f41577 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,6 @@ *.iso *.tar *.bz2 + +packer/assets/id_rsa_qemu* +packer/assets/packer_cache/ diff --git a/Makefile b/Makefile index f862202..09f8d3e 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,13 @@ .PHONY: up -up: + +up: ssh docker-compose up --build -d + +exec: + docker-compose exec gentoo_packer bash + +ssh: packer/assets/id_rsa_qemu packer/assets/id_rsa_qemu.pub + @if [ ! -f $< ]; then \ + ssh-keygen -a 100 -C antoinroux@hotmail.fr -t ed25519 -N "" -b 2048 -q -f $<; \ + fi diff --git a/docker-compose.yml b/docker-compose.yml index 05ddebe..cc57c27 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,5 +9,12 @@ services: command: "'while sleep 3600; do :; done'" stdin_open: true tty: true + privileged: true volumes: - - "./assets:/packer/" + - "./packer/assets:/packer/" + network_mode: "host" + #ports: + # - "5959:5959" + # - "2222:2229" + # - "8888:10082" + diff --git a/packer/Dockerfile b/packer/Dockerfile index 9852fa4..4e40b27 100644 --- a/packer/Dockerfile +++ b/packer/Dockerfile @@ -1,9 +1,22 @@ FROM hashicorp/packer:1.5.1 -RUN apk add --update qemu qemu-system-x86_64 qemu-img -RUN find / -iname '*qemu*' +ARG USER=packer +ARG GROUP=packer + +WORKDIR /packer + +RUN apk add --update qemu qemu-system-x86_64 qemu-img \ + vim htop util-linux gzip sysfsutils openssh-client openssh-keygen sudo + +RUN addgroup -g 1000 "${GROUP}" && \ + adduser -u 1000 -D -G "${GROUP}" "${USER}" && \ + echo "$USER ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/$USER && \ + chmod 0440 /etc/sudoers.d/$USER + +USER "${USER}" ENV PACKER_LOG=1 +ENV PS1="[\u@\h \W]# " ENTRYPOINT ["/bin/packer"] diff --git a/packer/assets/gentoo.json b/packer/assets/gentoo.json new file mode 100644 index 0000000..1c60b6b --- /dev/null +++ b/packer/assets/gentoo.json @@ -0,0 +1,101 @@ +{ + "builders": + [ + { + "type": "qemu", + "accelerator": "kvm", + "headless": true, + "qemuargs": [ + ["-boot", "b" ], + ["-device", "virtio-rng-pci"] + ], + "memory": "1024", + "cpus": "2", + "disk_interface": "virtio", + "disk_size": "5000M", + "disk_image": "true", + "use_backing_file": "true", + "format": "qcow2", + "net_device": "virtio-net", + + "iso_urls": [ + "/packer/install-amd64-minimal-{{user `version`}}.iso", + "http://distfiles.gentoo.org/releases/amd64/autobuilds/{{user `version`}}/install-amd64-minimal-{{user `version`}}.iso" + ], + "iso_checksum_url": "http://distfiles.gentoo.org/releases/amd64/autobuilds/{{user `version`}}/install-amd64-minimal-{{user `version`}}.iso.DIGESTS.asc", + "iso_checksum_type": "file", + + "vm_name": "gentoo_packer", + "output_directory": "output_gentoo_qcow2", + + "http_directory": "/packer/httpdir", + "http_port_min": 10082, + "http_port_max": 10082, + + "ssh_host_port_min": 2229, + "ssh_host_port_max": 2229, + + "ssh_host": "127.0.0.1", + "ssh_port": "22", + "ssh_username": "{{user `ssh_username`}}", + "ssh_password": "{{user `ssh_password`}}", + + "ssh_wait_timeout": "900m", + + "vnc_port_min": "5900", + "vnc_port_max": "5900", + + "boot_wait": "3s", + "boot_command": + [ + "gentoo-nofb", + "", + "", + "", + "", + "passwd {{user `ssh_username`}}", + "", + "", + "{{user `ssh_password`}}", + "", + "", + "{{user `ssh_password`}}", + "", + "", + "sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/' /etc/ssh/sshd_config", + "", + "", + "echo 'PermitRootLogin yes' >> /etc/ssh/sshd_config", + "", + "", + "/etc/init.d/sshd start", + "", + "" + ], + + "shutdown_command": "shutdown -P now" + } + ], + "provisioners": + [ + { + "type": "file", + "source": "/packer/id_rsa_qemu.pub", + "destination": "/root/.ssh/authorized_keys" + }, + { + "type": "shell", + "inline": [ + "chown root:root /root/.ssh/authorized_keys", + "chmod 600 /root/.ssh/authorized_keys", + "sed -i 's/^SELINUX=.*/SELINUX=permissive/g' /etc/selinux/config", + "echo package_upgrade: true >>/etc/cloud/cloud.cfg" + ] + } + ], + "variables": { + "ssh_username": "root", + "ssh_password": "CHANGEME", + "version": "20200205T214502Z" + } +} diff --git a/packer/assets/gentoo_save.json b/packer/assets/gentoo_save.json new file mode 100644 index 0000000..87b783e --- /dev/null +++ b/packer/assets/gentoo_save.json @@ -0,0 +1,128 @@ +{ + "builders": + [ + { + "type": "qemu", + "accelerator": "kvm", + "headless": true, + "qemuargs": [ + ["-serial", "file:serial.out" ], + [ "-netdev", "user,hostfwd=tcp::{{ .SSHHostPort }}-:22,,hostfwd=tcp::5959-:{{ . }},id=forward"] + ], + "memory": "1g", + "cpus": "2", + "disk_interface": "virtio", + "disk_size": "5000M", + "disk_image": "true", + "use_backing_file": "true", + "format": "qcow2", + "net_device": "virtio-net", + + "iso_url": "http://ftp.uni-stuttgart.de/centos/6.5/isos/x86_64/CentOS-6.5-x86_64-minimal.iso", + "iso_checksum": "9bea61ff620257c3343eb3c588c555800488ace01602b7a0e4f89d09a3f976851a81b6501d79cb3696696675b4724c9da82913e4a036f7874a55bfe2b91ee994", + "iso_checksum_type": "sha512", + + "vm_name": "gentoo_packer", + "output_directory": "output_gentoo_qcow2", + + "http_directory": "/packer/httpdir", + "http_port_min": 10082, + "http_port_max": 10089, + + "ssh_host_port_min": 2222, + "ssh_host_port_max": 2229, + + "ssh_username": "root", + "ssh_password": "MySecretPassword", + + "ssh_port": 22, + "ssh_wait_timeout": "900m", + + "boot_wait": "5s", + "boot_command": + [ + "", + "text ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/provision_gentoo.sh", + "chmod +x provision_gentoo.sh && ./provision_gentoo.sh" + ], + + "shutdown_command": "shutdown -P now" + }, + { + "type": "hyperv-iso", + "boot_command": [ + "", + "", + "", + "", + "", + "wget http://{{ .HTTPIP }}:{{ .HTTPPort }}/provision_gentoo.sh", + "echo http://{{ .HTTPIP }}:{{ .HTTPPort }} > /root/config_server_uri", + "chmod +x provision_gentoo.sh && ./provision_gentoo.sh", + "", + " append console=ttyS0,115200n8 ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/centos6-ks.cfg", + "", + "", + "text ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/c7-kvm-ks.cfg" + ], + "boot_wait": "10s", + "disk_size": 32768, + "http_directory": ".", + "iso_url": "http://vagrant.widgit.com/gentoo.php", + "iso_checksum_type": "none", + "communicator": "ssh", + "ssh_username": "vagrant", + "ssh_password": "vagrant", + "ssh_port": 22, + "ssh_timeout": "4h", + "shutdown_command": "sudo systemctl poweroff", + "vm_name": "gentoo-minimal", + "generation": 1, + "ram_size": "4096", + "enable_dynamic_memory": false, + "cpu": 4 + } + ], + "provisioners": + [ + { + "type": "shell", + "inline": [ + "sleep 3", + "rpm -ivh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm", + "yum -y update", + "yum -y install cloud-init cloud-utils-growpart dracut-modules-growroot", + "echo \"NOZEROCONF=yes\" >> /etc/sysconfig/network", + "adduser build-user", + "echo 'build-user:MySecretPassword' |chpasswd", + "mkdir /home/build-user/.ssh", + "chown build-user:build-user /home/build-user/.ssh", + "chmod 700 /home/build-user/.ssh", + "echo \"build-user ALL=(ALL) ALL\" >> /etc/sudoers", + "yum clean cache", + "yum clean all" + ] + }, + { + "type": "file", + "source": "centos.json", + "destination": "/root/centos.json", + "source": "httpdir/centos6-ks.cfg", + "destination": "/root/centos6-ks.cfg" + }, + { + "type": "file", + "source": "id_rsa_cloud-user.pub", + "destination": "/home/build-user/.ssh/authorized_keys" + }, + { + "type": "shell", + "inline": [ + "chmod 600 /home/build-user/.ssh/authorized_keys", + "chown build-user:build-user /home/build-user/.ssh/authorized_keys", + "sed -i 's/^SELINUX=.*/SELINUX=permissive/g' /etc/selinux/config", + "echo package_upgrade: true >>/etc/cloud/cloud.cfg" + ] + } + ] +} diff --git a/packer/assets/httpdir/provision_gentoo.sh b/packer/assets/httpdir/provision_gentoo.sh new file mode 100644 index 0000000..2681af7 --- /dev/null +++ b/packer/assets/httpdir/provision_gentoo.sh @@ -0,0 +1,147 @@ +#!/bin/bash + +set -e +set -x + +# disable blanking so we can look for problems on the VM console +setterm -blank 0 -powersave off + +# This will have been written out by the typed boot command +export CONFIG_SERVER_URI=`cat /root/config_server_uri` + +# Pipe some commands into fdisk to partition +# Works better than sfdisk as the size of the final partition is flexible +echo "Partitioning SDA" + +fdisk /dev/sda < /dev/null && echo "j" || echo "-J") + if [ "x$DECOMPRESS_FLAG" == "x-J" ]; then + DECOMPRESS_FLAG=' -J' + fi + + curl -SsLl "http://vagrant.widgit.com/gentoo.php?file=stage3" | tar xp$DECOMPRESS_FLAG -C /mnt/gentoo --xattrs --numeric-owner && break + sleep 30 +done + +# modify the chroot with some custom settings +echo "Setting up chroot configuration" + +# configure portage +cat >> /mnt/gentoo/etc/portage/make.conf <> /mnt/gentoo/etc/portage/make.conf + +# package-specific configuration and unmasks +mkdir -p /mnt/gentoo/etc/portage/package.accept_keywords +mkdir -p /mnt/gentoo/etc/portage/package.use +touch /mnt/gentoo/etc/portage/package.accept_keywords/zzz-autounmask +touch /mnt/gentoo/etc/portage/package.use/zzz-autounmask + +echo "sys-kernel/gentoo-sources" > /mnt/gentoo/etc/portage/package.accept_keywords/kernel + +echo "sys-kernel/gentoo-sources symlink experimental" > /mnt/gentoo/etc/portage/package.use/kernel +echo "sys-boot/grub efiemu -fonts -nls -themes" > /mnt/gentoo/etc/portage/package.use/grub +echo "sys-apps/systemd nat" > /mnt/gentoo/etc/portage/package.use/systemd + +# Locale and time +echo "Etc/UTC" > /mnt/gentoo/etc/timezone +cat > /mnt/gentoo/etc/locale.gen < /mnt/gentoo/etc/fstab <> /usr/share/genkernel/arch/x86_64/modules_load + echo 'modules="hv_storvsc hv_netvsc hv_vmbus hv_utils hv_balloon"' >> /etc/conf.d/modules + sed -ri "s/(HWOPTS='.*)'/\1 hyperv'/" /usr/share/genkernel/defaults/initrd.defaults +fi + +# Build the kernel with genkernel +echo "Building the kernel" + +genkernel --kernel-config=/etc/kernels/kernel_config --makeopts=-j5 all + +# Build & install the VM tools + +# If we're running on hyper-v, enable the tools +if [ "$(dmidecode -s system-manufacturer)" == "Microsoft Corporation" ]; then + # kernel modules are already built in the kernel + cd /usr/src/linux/tools/hv + make + cp hv_fcopy_daemon hv_vss_daemon hv_kvp_daemon /usr/sbin + + systemctl enable hv_fcopy_daemon.service + systemctl enable hv_vss_daemon.service + systemctl enable hv_kvp_daemon.service +elif [ "$(dmidecode -s system-product-name)" == "VirtualBox" ]; then + # Install VirtualBox from portage + echo "app-emulation/virtualbox-guest-additions ~amd64" > /etc/portage/package.accept_keywords/virtualbox + emerge app-emulation/virtualbox-guest-additions + + systemctl enable virtualbox-guest-additions.service +elif [ "$(dmidecode -s system-product-name)" == "VMware Virtual Platform" ]; then + echo "app-emulation/open-vm-tools ~amd64" > /etc/portage/package.accept_keywords/vmware + emerge app-emulation/open-vm-tools + + systemctl enable vmtoolsd +else + echo "Unknown hypervisor! :(" 1>&2 + exit 1 +fi + +# Set up the things we need for a base system +echo "Configuring up the base system" + +# sudo and cron +echo "app-admin/sudo -sendmail" > /etc/portage/package.use/sudo +emerge sys-process/cronie app-admin/sudo + +# systemd setup and hostname +systemd-machine-id-setup --commit # remember to remove this before packaging the box +echo "gentoo-minimal" > /etc/hostname +echo "127.0.1.1 gentoo-minimal.local gentoo-minimal" >> /etc/hosts + +# networking +cat > /etc/systemd/network/50-dhcp.network <> /etc/ssh/sshd_config + +yes YES | etc-update --automode -9 + +# Create the vagrant user with the vagrant public key +echo "Creating Vagrant user" + +date > /etc/vagrant_box_build_time + +useradd -s /bin/bash -m vagrant +echo -e "vagrant\nvagrant" | passwd vagrant + +mkdir -pm 700 /home/vagrant/.ssh +wget -O /home/vagrant/.ssh/authorized_keys \ + 'https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub' +chmod 0600 /home/vagrant/.ssh/authorized_keys +chown -R vagrant:vagrant /home/vagrant/.ssh + +echo 'vagrant ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers + +# Install grub and hope everything is ready! +echo "Installing bootloader" + +grub-install /dev/sda +grub-mkconfig -o /boot/grub/grub.cfg + +echo "Installing additional tools" +emerge @tools + +echo "Updating resolv.conf" + +rm /etc/resolv.conf +ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf +systemctl enable systemd-resolved.service + +echo "Removing provision script" +rm /root/provision_gentoo_chroot.sh diff --git a/packer/assets/run-packer.sh b/packer/assets/run-packer.sh new file mode 100755 index 0000000..9a9fa4a --- /dev/null +++ b/packer/assets/run-packer.sh @@ -0,0 +1,11 @@ +#!/bin/bash + + +#packer build --only=qemu --var outfile=boxen/gentoo-minimal-hyperv.box --var headless=true --var hyperv_switchname=Ethernet gentoo-minimal.json + + +export PACKER_LOG=1; packer build --only=qemu \ + -var version="20200205T214502Z" \ + -var ssh_username="root" \ + -var ssh_password="toor" \ + /packer/gentoo.json