Merge pull request 'develop' (#2) from develop into master

This commit is contained in:
antoine 2020-11-12 13:09:00 +00:00
commit 38c0f0f4ca
34 changed files with 391 additions and 621 deletions

7
.gitignore vendored
View File

@ -6,9 +6,10 @@
.env
*.retry
packer/assets/id_rsa_qemu*
packer/assets/packer_cache/
packer/assets/output_gentoo_qcow2/
image/assets/id_rsa_qemu*
image/cache/
image/assets/output_qcow2/
.idea/
*.iml
/image/assets/test-misc/

View File

@ -1,13 +1,20 @@
.PHONY: up
.PHONY: up-gentoo
include .env
up: packer/assets/id_rsa_qemu
docker-compose up --build -d
imagePackerBuilder:
docker build -t $(REGISTRY_URL)/$(IMAGE_NAME):$(VERSION) ./image
exec:
up-gentoo: imagePackerBuilder image/assets/id_rsa_qemu
docker-compose up --build -d gentoo_packer
exec-gentoo:
docker-compose exec gentoo_packer bash
packer/assets/id_rsa_qemu:
down:
docker-compose down
image/assets/id_rsa_qemu:
@if [ ! -f "$@" ]; then \
ssh-keygen -a 100 -C antoinroux@hotmail.fr -t ed25519 -N "" -b 2048 -q -f $@; \
fi

View File

@ -3,18 +3,19 @@ version: '3.7'
services:
gentoo_packer:
image: ${REGISTRY_URL}/${IMAGE_NAME}:${VERSION}
build:
context: ./packer
args:
PROXY: "${PROXY}"
entrypoint: "bash -c"
command: "'while sleep 3600; do :; done'"
stdin_open: true
tty: true
privileged: true
volumes:
- "./packer/assets:/packer/"
- "./image/assets:/packer"
- "./image/cache:/packer-cache"
network_mode: "host"
environment:
- SOURCE_NAME=qemu.gentoo
- SSH_LIVE_USERNAME=root
- SSH_LIVE_PASSWORD=toortrort@orto0rTempPASSWORD
- ANSIBLE_EXTRA_VARS=generated_user=antoine generated_group=users gentoo_version=20201111T214503Z
- ANSIBLE_PLAYBOOK=/packer/ansible/playbook-gentoo.yml
- ISO_CHECKSUM=file:http://distfiles.gentoo.org/releases/amd64/autobuilds/current-install-amd64-minimal/install-amd64-minimal-20201111T214503Z.iso.DIGESTS
- ISO_URL=http://distfiles.gentoo.org/releases/amd64/autobuilds/current-install-amd64-minimal/install-amd64-minimal-20201111T214503Z.iso
#ports:
# - "5900:5900/udp"
# - "2222:2229"

2
image/.dockerignore Normal file
View File

@ -0,0 +1,2 @@
cache/
assets/output_qcow2/

View File

@ -1,4 +1,4 @@
FROM hashicorp/packer:1.5.4
FROM hashicorp/packer:1.6.3
ARG PROXY=""
ENV http_proxy="${PROXY}"
@ -15,14 +15,23 @@ RUN apk add --update qemu qemu-system-x86_64 qemu-img \
RUN addgroup -g 1000 "${GROUP}" && \
adduser -u 1000 -D -G "${GROUP}" "${USER}" && \
adduser "${USER}" qemu && \
echo "$USER ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/$USER && \
chmod 0440 /etc/sudoers.d/$USER
# setup mitogen to enhance ansible speed
ENV MITOGEN_VERSION="0.2.9"
RUN wget https://networkgenomics.com/try/mitogen-${MITOGEN_VERSION}.tar.gz && \
tar xvf mitogen-${MITOGEN_VERSION}.tar.gz && \
mv mitogen-${MITOGEN_VERSION} /mitogen && \
rm mitogen-${MITOGEN_VERSION}.tar.gz
USER "${USER}"
ENV PACKER_LOG=1
ENV PACKER_CACHE_DIR=/packer-cache
ENV PS1="[\u@\h \W]# "
ENTRYPOINT ["/bin/packer"]
ENTRYPOINT ["/packer/run-packer.sh"]

View File

@ -0,0 +1,5 @@
[defaults]
host_key_checking = false
strategy_plugins = /mitogen/ansible_mitogen/plugins/strategy
strategy = mitogen_linear

View File

@ -1,5 +1,5 @@
---
# file: playbook.yml
# file: playbook-gentoo.yml
- hosts: default
become: true

View File

@ -1,30 +1,29 @@
---
# default variable relative to paritionning
devices_path: /dev/sda
devices_path: /dev/vdb
devices_system:
- name: bootloader
dev: /dev/sda1
dev: "{{ devices_path }}1"
label: gpt
flags: [ bios_grub ]
part_start: 0%
part_end: 2MiB
- name: boot
dev: /dev/sda2
dev: "{{ devices_path }}2"
label: gpt
flags: [ boot, esp ]
part_start: 2MiB
part_end: 128MiB
fstype: "ext2"
- name: root
dev: /dev/sda3
dev: "{{ devices_path }}3"
label: gpt
flags: [ lvm ]
part_start: 128MiB
part_end: 100%
fstype: "lvm"
vg_name: vg_system
lvs:
- name: "swap"
size: "1G"
@ -34,8 +33,13 @@ lvs:
size: "22G"
type: "ext4"
descr: "Root partition"
mount_dir: "{{ mount_base_dir }}"
- name: "home"
size: "7G"
type: "ext4"
descr: "home partition"
mount_dir: "{{ mount_base_dir }}/home"
# default swapiness value
swapiness: "60"
gentoo_version: "lastest"

View File

@ -0,0 +1,69 @@
---
# configure and setup os
- name:
set_fact:
make_conf:
- path: "GENTOO_MIRRORS=\"{{ mirror_source }}\""
regexp: "^GENTOO_MIRRORS=\".*\""
- path: "COMMON_FLAGS=\"{{ gcc_common_args }}\""
regexp: "^COMMON_FLAGS="
- path: "MAKEOPTS=\"{{ make_opts }}\""
regexp: "^MAKEOPTS="
tags:
- configure
- name: "Configure Gentoo_mirrors"
lineinfile:
path: "{{ mount_base_dir }}/etc/portage/make.conf"
regexp: "{{ item.regexp }}"
line: "{{ item.path }}"
state: present
loop: "{{ make_conf }}"
tags:
- configure
- name: "Create portage repo conf dir"
file:
path: "{{ portage_conf_dir_path }}"
state: directory
tags:
- configure
- name: "Copy portage conf file"
copy:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
remote_src: yes
loop:
- src: "{{ mount_base_dir }}/usr/share/portage/config/repos.conf"
dest: "{{ portage_conf_dir_path }}/gentoo.conf"
- src: "/etc/resolv.conf"
dest: "{{ mount_base_dir }}/etc/resolv.conf"
tags:
- configure
#- name: create portage directory
# command: chroot /mnt/gentoo bash -c 'source /etc/profile; mkdir /usr/portage' creates=/mnt/gentoo/usr/portage
# tags:
# - chroot
#
#- name: emerge-webrsync
# command: chroot /mnt/gentoo bash -c 'source /etc/profile; /usr/bin/emerge-webrsync' creates=/mnt/gentoo/usr/portage/xfce-extra
# tags:
# - chroot
#
#- name: set zoneinfo
# command: chroot /mnt/gentoo bash -c 'cp /usr/share/zoneinfo/{{ timezone }} /etc/localtime; echo "{{ timezone }}" > /etc/timezone'
# tags:
# - chroot
#
#- name: Configure Hosts File
# template: src=hosts dest=/mnt/gentoo/etc/hosts
#
#- name: Set Hostname
# template: src=hostname dest=/mnt/gentoo/etc/conf.d/hostname
#
#
#- name: Configure Fstab
# copy: src=fstab dest=/mnt/gentoo/etc/fstab

View File

@ -2,6 +2,9 @@
# setup gentoo
- import_tasks: partition.yml
- import_tasks: mount.yml
- import_tasks: os.yml
- import_tasks: configure.yml
#- import_tasks: users.yml
#- import_tasks: security.yml

View File

@ -0,0 +1,43 @@
---
- name: "Enable swap partition"
command: "swapon /dev/{{ vg_name }}/{{ item.name }}"
loop: "{{ lvs }}"
when: "'swap' in item.type"
tags:
- mount
- name: "Set swappiness value"
sysctl:
name: vm.swappiness
value: "{{ swapiness }}"
state: present
tags:
- mount
- name: "Create mountpoints base dir"
file:
path: "{{ mount_base_dir }}"
state: directory
tags:
- mount
- name: "Create mountpoints"
file:
path: "{{ item.mount_dir }}"
state: directory
loop: "{{ lvs }}"
when: "item.mount_dir is defined"
tags:
- mount
- name: "Mount partition"
mount:
path: "{{ item.mount_dir }}"
src: "/dev/{{ vg_name }}/{{ item.name }}"
fstype: "{{ item.type }}"
state: mounted
loop: "{{ lvs }}"
when: "item.mount_dir is defined"
tags:
- mount

View File

@ -0,0 +1,70 @@
---
# retrieve gentoo os and copy it
- name: "Set time"
command: "ntpd -q -g"
tags:
- os
- name: "Retrieve last version"
shell: >
curl {{ latest_url }} 2>&1 |
awk -F/ '/\/stage3-amd64-[0-9TZ]+.tar.xz/{print $1}'
args:
warn: no
when: "gentoo_version is not defined or gentoo_version == 'lastest'"
register: "stage_path"
tags:
- os
- set_fact:
url_dl: "{{ mirror_stage }}/releases/amd64/autobuilds/{{ stage_path.stdout | default(gentoo_version) }}/stage3-amd64-{{ stage_path.stdout | default(gentoo_version) }}.tar.xz"
tags:
- os
- name: "Download stage3 archive version {{ stage_path.stdout }}"
get_url:
url: "{{ url_dl }}"
dest: "/root/stage3-amd64.tar.xz"
register: "archive_downloaded_result"
tags:
- os
- name: "Extract stage3 archive"
unarchive:
src: "{{ archive_downloaded_result.dest }}"
dest: "{{ mount_base_dir }}"
remote_src: yes
creates: "{{ mount_base_dir }}/bin"
tags:
- os
- name: "Mount /proc"
mount:
src: /proc
path: "{{ mount_base_dir }}/proc"
state: mounted
fstype: proc
tags:
- os
- name: "Mount /dev bind volume"
mount:
src: "/{{ item.path }}"
path: "{{ mount_base_dir }}/{{ item.path }}"
opts: rbind,make-rslave
state: mounted
fstype: none
loop:
- path: dev
- path: sys
tags:
- os
- name: "Cleanup Stage3 Archive"
file:
path: "{{ archive_downloaded_result.dest }}"
state: absent
when: "debug is not defined or debug == false"
tags:
- os

View File

@ -38,6 +38,10 @@
tags:
- storage
- import_tasks: "umount.yml"
tags:
- storage
- name: "Create list of lvs"
lvol:
vg: "{{ vg_name }}"
@ -52,7 +56,6 @@
fstype: "{{ item.type }}"
dev: "/dev/{{ vg_name }}/{{ item.name }}"
force: yes
resizefs: yes
loop: "{{ lvs }}"
tags:
- storage

View File

@ -0,0 +1,16 @@
---
# umount and disabe swap
- name: "disable swap partition"
command: "swapoff -a"
tags:
- umount
- name: "Umount partition"
mount:
path: "{{ item.mount_dir }}"
state: "unmounted"
loop: "{{ lvs | reverse | list }}"
when: "item.mount_dir is defined"
tags:
- umount

View File

@ -0,0 +1,22 @@
---
# non overridable variable
vg_name: vg_system
mount_base_dir: /mnt/gentoo
mirror_stage: "https://gentoo.osuosl.org"
latest_url: "{{ mirror_stage }}/releases/amd64/autobuilds/latest-stage3.txt"
# compilation arguments
mirror_source: >
http://gentoo.mirrors.ovh.net/gentoo-distfiles/
ftp://ftp.free.fr/mirrors/ftp.gentoo.org/
http://ftp.free.fr/mirrors/ftp.gentoo.org/
https://mirrors.soeasyto.com/distfiles.gentoo.org/
http://mirrors.soeasyto.com/distfiles.gentoo.org/
ftp://mirrors.soeasyto.com/distfiles.gentoo.org/
http://gentoo.modulix.net/gentoo/
gcc_common_args: "-march=native -O2 -pipe"
make_opts: "-j2"
# portage variable
portage_conf_dir_path: "{{ mount_base_dir }}/etc/portage/repos.conf/"

View File

@ -0,0 +1,14 @@
# "timestamp" template function replacement
locals { timestamp = regex_replace(timestamp(), "[- TZ:]", "") }
build {
sources = ["source.qemu.gentoo"]
provisioner "ansible" {
ansible_env_vars = ["ANSIBLE_CONFIG=/packer/ansible/ansible.cfg"]
extra_arguments = ["--extra-vars", "${var.ansible_extra_vars}"]
groups = ["default"]
playbook_file = "${var.ansible_provisioning_playbook}"
user = "root"
}
}

View File

@ -0,0 +1,48 @@
source "qemu" "gentoo" {
accelerator = "kvm"
boot_command = [
"gentoo-nofb", "<enter>",
"<wait5>", "<enter>",
"<wait15s>", "passwd ${var.ssh_username}", "<enter>",
"<wait>", "${var.ssh_password}", "<enter>",
"<wait>", "${var.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>"
]
boot_wait = "3s"
cpus = 2
disk_compression = true
disk_detect_zeroes = "unmap"
disk_discard = "unmap"
skip_compaction = false
disk_image = false
use_backing_file = false
disk_interface = "virtio"
disk_size = "10G"
disk_additional_size = ["80G"]
format = "qcow2"
headless = true
http_directory = "/packer/httpdir"
http_port_max = 10082
http_port_min = 10082
host_port_min = 2229
host_port_max = 2229
iso_checksum = "${var.iso_checksum}"
iso_url = "${var.iso_url}"
memory = "1024"
net_device = "virtio-net"
output_directory = "output_qcow2"
qemuargs = [
["-boot", "b"],
["-device", "virtio-rng-pci"]
]
shutdown_command = "shutdown -P now"
ssh_password = "${var.ssh_password}"
ssh_username = "${var.ssh_username}"
ssh_wait_timeout = "900m"
vm_name = "gentoo_packer"
vnc_port_max = "5900"
vnc_port_min = "5900"
}

View File

@ -0,0 +1,29 @@
variable "ansible_extra_vars" {
type = string
default = ""
}
variable "ansible_provisioning_playbook" {
type = string
default = "/packer/ansible/playbook.yml"
}
variable "iso_checksum" {
type = string
default = ""
}
variable "iso_url" {
type = string
default = ""
}
variable "ssh_password" {
type = string
default = ""
}
variable "ssh_username" {
type = string
default = ""
}

19
image/assets/run-packer.sh Executable file
View File

@ -0,0 +1,19 @@
#!/bin/bash
if [ "$SOURCE_NAME" = "qemu.gentoo" ]; then
VERSION=$(curl -s http://distfiles.gentoo.org/releases/amd64/autobuilds/latest-install-amd64-minimal.txt \
| grep "^[^#;]" | awk '{print $1}' | cut -d'/' -f1)
fi
SSH_PUB=$(cat ./id_rsa_qemu.pub)
export PACKER_LOG=1; packer build --only="$SOURCE_NAME" \
-var ssh_username="$SSH_LIVE_USERNAME" \
-var ssh_password="$SSH_LIVE_PASSWORD" \
-var ansible_extra_vars="$ANSIBLE_EXTRA_VARS ssh_pub=$SSH_PUB" \
-var ansible_provisioning_playbook="$ANSIBLE_PLAYBOOK" \
-var iso_checksum="$ISO_CHECKSUM" \
-var iso_url="$ISO_URL" \
-on-error="ask" \
-force \
/packer/hcl/

View File

@ -1,2 +0,0 @@
[defaults]
host_key_checking = false

View File

@ -1,4 +0,0 @@
[default]
100.64.0.24 ansible_ssh_pass=toor ansible_ssh_port=5556 ansible_user=root
#192.168.1.24 ansible_ssh_pass=toor ansible_ssh_port=5556 ansible_user=root

View File

@ -1,32 +0,0 @@
---
- hosts: localhost
vars:
my_loop: >
[
{% for i in range(1, 2) %}
{
'name':'prefix-{{ i }}'
}
{% if not loop.last %},{% endif %}
{% endfor %}
]
ips_str: |
{% for idx in range(0,2) %}
- name: 192.168.100.{{ idx }}
test: {{ idx }}
{% endfor %}
ips: "{{ ips_str | from_yaml }}"
tasks:
- debug:
msg: "{{ my_loop }}"
- debug:
msg: "{{ item.name }}"
loop: "{{ my_loop }}"
- debug:
msg: "{{ ips }}"
- debug:
msg: "{{ item.name }}"
loop: "{{ ips }}"

View File

@ -1,7 +0,0 @@
#!/bin/bash
PLAYBOOK="${1:-playbook.yml}"
DEBUG="-vvv"
# run playbook on aready running qemu vm
ansible-playbook -i inventory_test.ini "$PLAYBOOK" --extra-vars "ssh_pub=$(cat ../id_rsa_qemu) generated_user=antoine generated_group=antoine"

View File

@ -1,100 +0,0 @@
{
"builders":
[
{
"type": "qemu",
"accelerator": "kvm",
"headless": true,
"qemuargs": [
["-boot", "b" ],
["-device", "virtio-rng-pci"]
],
"memory": "1024",
"cpus": "2",
"use_backing_file": "true",
"format": "qcow2",
"net_device": "virtio-net",
"disk_interface": "virtio",
"disk_size": "80g",
"disk_image": "true",
"disk_compression": "true",
"disk_additional_size": [],
"iso_urls": [
"/packer/install-amd64-minimal-20200205T214502Z.iso",
"http://distfiles.gentoo.org/releases/amd64/autobuilds/current-install-amd64-minimal//install-amd64-minimal-{{user `version`}}.iso"
],
"iso_checksum_type": "file",
"iso_checksum_url": "/packer/install-amd64-minimal-20200205T214502Z.iso.DIGESTS",
"vm_name": "gentoo_packer.qcow2",
"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": "ansible",
"playbook_file": "/packer/ansible/playbook.yml",
"extra_arguments": [ "--extra-vars", "ssh_pub={{user `ssh_pub_key`}} generated_user={{user `generated_user`}} generated_group={{user `generated_group`}}" ],
"groups": "default",
"user": "root",
"ansible_env_vars": [ "ANSIBLE_CONFIG=/packer/ansible/ansible.cfg" ]
}
],
"variables": {
"ssh_username": "root",
"ssh_password": "CHANGEME",
"ssh_pub_key": "",
"generated_user": "antoine",
"generated_group": "antoine",
"version": "20200205T214502Z"
}
}

View File

@ -1,142 +0,0 @@
{
"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"
]
},
{
"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"
]
}
]
}

View File

@ -1,147 +0,0 @@
#!/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

@ -1,136 +0,0 @@
#!/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

View File

@ -1,2 +0,0 @@
9bea61ff620257c3343eb3c588c555800488ace01602b7a0e4f89d09a3f976851a81b6501d79cb3696696675b4724c9da82913e4a036f7874a55bfe2b91ee994 install-amd64-minimal-20200205T214502Z.iso

View File

@ -1,13 +0,0 @@
#!/bin/bash
latest=$(curl -s http://distfiles.gentoo.org/releases/amd64/autobuilds/latest-install-amd64-minimal.txt \
| grep "^[^#;]" | awk '{print $1}' | cut -d'/' -f1)
SSH_PUB=$(cat ./id_rsa_qemu.pub)
export PACKER_LOG=1; packer build --only=qemu \
-var version="$latest" \
-var ssh_username="root" \
-var ssh_password="toor" \
-var ssh_pub_key="$SSH_PUB" \
-force \
/packer/gentoo.json

10
test.sh
View File

@ -1,10 +0,0 @@
#!/bin/bash
qemu-system-x86_64 \
-cdrom ./packer/assets/install-amd64-minimal-20200205T214502Z.iso \
-boot order=d -m 1024 \
-smp cpus=1,sockets=2,maxcpus=2 \
-drive "file=../../VirtualBox VMs/gentoo_snapshot.cow,format=qcow2,index=1" \
-device virtio-net,netdev=user.0 \
-netdev user,id=user.0,hostfwd=tcp::5556-:22