add povision with sh script

This commit is contained in:
Antoine 2020-02-10 08:40:08 +01:00 committed by Antoine Roux
parent 7bccaad723
commit d1c7b35411
9 changed files with 559 additions and 4 deletions

3
.gitignore vendored
View File

@ -3,3 +3,6 @@
*.iso
*.tar
*.bz2
packer/assets/id_rsa_qemu*
packer/assets/packer_cache/

View File

@ -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

View File

@ -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"

View File

@ -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"]

101
packer/assets/gentoo.json Normal file
View File

@ -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",
"<enter>",
"<wait5>",
"<enter>",
"<wait15s>",
"passwd {{user `ssh_username`}}",
"<enter>",
"<wait>",
"{{user `ssh_password`}}",
"<enter>",
"<wait>",
"{{user `ssh_password`}}",
"<enter>",
"<wait>",
"sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/' /etc/ssh/sshd_config",
"<enter>",
"<wait>",
"echo 'PermitRootLogin yes' >> /etc/ssh/sshd_config",
"<enter>",
"<wait>",
"/etc/init.d/sshd start",
"<enter>",
"<wait>"
],
"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"
}
}

View File

@ -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":
[
"<up><wait><tab><wait>",
"text ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/provision_gentoo.sh<enter><wait>",
"chmod +x provision_gentoo.sh && ./provision_gentoo.sh<enter>"
],
"shutdown_command": "shutdown -P now"
},
{
"type": "hyperv-iso",
"boot_command": [
"<enter><wait5><enter><wait5><enter><wait5><enter><wait5><enter><wait5><enter><wait5>",
"<enter><wait5><enter><wait5><enter><wait5><enter><wait5><enter><wait5><enter><wait5>",
"<enter><wait5><enter><wait5><enter><wait5><enter><wait5><enter><wait5><enter><wait5>",
"<enter><wait5><enter><wait5><enter><wait5><enter><wait5><enter><wait5><enter><wait5>",
"<enter><wait5><enter><wait5><enter><wait5><enter><wait5><enter><wait5><enter><wait5>",
"wget http://{{ .HTTPIP }}:{{ .HTTPPort }}/provision_gentoo.sh<enter><wait>",
"echo http://{{ .HTTPIP }}:{{ .HTTPPort }} > /root/config_server_uri<enter>",
"chmod +x provision_gentoo.sh && ./provision_gentoo.sh<enter>",
"",
" append console=ttyS0,115200n8 ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/centos6-ks.cfg",
"",
"<up><wait><tab><wait>",
"text ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/c7-kvm-ks.cfg<enter><wait>"
],
"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"
]
}
]
}

View File

@ -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 <<EOT
n
p
1
+256M
n
p
2
+4G
n
p
3
t
2
82
w
EOT
# Create some filesystems and enable swap (which we'll want for the build, particularly when hv_balloon misbehaves)
echo "Creating filesystems"
mkfs.ext4 /dev/sda1
mkswap /dev/sda2
mkfs.ext4 /dev/sda3
swapon /dev/sda2
# Pull the latest stage3 and unpack into the new filesystem
echo "Unpacking stage 3"
mount /dev/sda3 /mnt/gentoo
mkdir -p /mnt/gentoo/boot
mount /dev/sda1 /mnt/gentoo/boot
reboot
while [ "a" != "b" ]
do
DECOMPRESS_FLAG=$(curl -w "%{redirect_url}" -o /dev/null -s "http://vagrant.widgit.com/gentoo.php?file=stage3" | egrep 'bz2$' > /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 <<EOT
MAKEOPTS="-j5"
EMERGE_DEFAULT_OPTS="--quiet-build --jobs=4 --load-average=4 --autounmask-continue"
USE="-doc"
EOT
# use systemd
sed -i 's/USE="/USE="systemd /' /mnt/gentoo/etc/portage/make.conf
sed -i 's/CFLAGS="-O2/CFLAGS="-s -Os/' /mnt/gentoo/etc/portage/make.conf
echo 'LDFLAGS="-s"' >> /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 <<EOT
en_GB ISO-8859-1
en_GB.UTF-8 UTF-8
EOT
# Create an fstab
cat > /mnt/gentoo/etc/fstab <<EOT
/dev/sda1 /boot ext4 noauto,noatime 1 2
/dev/sda2 none swap sw 0 0
/dev/sda3 / ext4 noauto,noatime 0 1
EOT
# kernel config & friends
mkdir -p /mnt/gentoo/etc/{kernels,default}
wget ${CONFIG_SERVER_URI}/gentoo/genkernel.conf -O /mnt/gentoo/etc/genkernel.conf
wget ${CONFIG_SERVER_URI}/gentoo/kernel_config -O /mnt/gentoo/etc/kernels/kernel_config
wget ${CONFIG_SERVER_URI}/gentoo/default_grub -O /mnt/gentoo/etc/default/grub
mkdir -p /mnt/gentoo/usr/lib/systemd/system
wget ${CONFIG_SERVER_URI}/gentoo/hv_fcopy_daemon.service -O /mnt/gentoo/usr/lib/systemd/system/hv_fcopy_daemon.service
wget ${CONFIG_SERVER_URI}/gentoo/hv_vss_daemon.service -O /mnt/gentoo/usr/lib/systemd/system/hv_vss_daemon.service
wget ${CONFIG_SERVER_URI}/gentoo/hv_kvp_daemon.service -O /mnt/gentoo/usr/lib/systemd/system/hv_kvp_daemon.service
mkdir -p /mnt/gentoo/etc/portage/sets
wget ${CONFIG_SERVER_URI}/gentoo/tools -O /mnt/gentoo/etc/portage/sets/tools
mkdir -p /mnt/gentoo/etc/portage/patches/app-emulation/virtualbox-modules-5.1.30
wget ${CONFIG_SERVER_URI}/gentoo/virtualbox-modules-5.1.30-udp.patch -O /mnt/gentoo/etc/portage/patches/app-emulation/virtualbox-modules-5.1.30/udp.patch
mkdir -p /mnt/gentoo/etc/portage/patches/app-emulation/virtualbox-5.2.4
wget ${CONFIG_SERVER_URI}/gentoo/virtualbox-5.2.4-x11.patch -O /mnt/gentoo/etc/portage/patches/app-emulation/virtualbox-5.2.4/x11.patch
# enter the chroot and run the in-chroot script
echo "Entering chroot"
mount -t proc /proc /mnt/gentoo/proc
mount --rbind /sys /mnt/gentoo/sys
mount --make-rslave /mnt/gentoo/sys
mount --rbind /dev /mnt/gentoo/dev
mount --make-rslave /mnt/gentoo/dev
cp /etc/resolv.conf /mnt/gentoo/etc/resolv.conf
wget ${CONFIG_SERVER_URI}/scripts/provision_gentoo_chroot.sh -O /mnt/gentoo/root/provision_gentoo_chroot.sh
chmod +x /mnt/gentoo/root/provision_gentoo_chroot.sh
chroot /mnt/gentoo /root/provision_gentoo_chroot.sh
# and get ready to reboot
echo "Chroot finished, ready to restart"
umount -l /mnt/gentoo/{proc,sys,dev,boot,}
# hail mary!
reboot

View File

@ -0,0 +1,136 @@
#!/bin/bash
set -e
set -x
# Grab the latest portage
echo "Syncing Portage"
emerge-webrsync && emerge --sync --quiet
# Set the portage profile
eselect profile set default/linux/amd64/17.0/systemd
. /etc/profile
# Install updates
echo "Updating system"
emerge -uDN @world
# Set the system locale
echo "Setting locale"
locale-gen
eselect locale set "en_GB.utf8"
. /etc/profile
# Grab the kernel sources
echo "Installing kernel source"
emerge sys-kernel/gentoo-sources
# Install kernel build tools and configure
echo "Preparing to build kernel"
emerge sys-kernel/genkernel-next sys-boot/grub sys-fs/fuse sys-apps/dmidecode
if [ "$(dmidecode -s system-manufacturer)" == "Microsoft Corporation" ]; then
# Ensure hyperv modules are loaded at boot, and included in the initramfs
echo 'MODULES_HYPERV="hv_vmbus hv_storvsc hv_balloon hv_netvsc hv_utils"' >> /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 <<EOT
[Match]
Name=eth0
[Network]
DHCP=yes
[DHCP]
ClientIdentifier=mac
EOT
systemctl enable systemd-networkd.service
# ssh
systemctl enable sshd.service
echo "UseDNS no" >> /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

11
packer/assets/run-packer.sh Executable file
View File

@ -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