build squashfs in packer; set hostname using mac address

This commit is contained in:
jalr 2018-04-01 02:12:03 +02:00
parent 657336cf7c
commit 2bc4d064b3
6 changed files with 58 additions and 38 deletions

View file

@ -55,6 +55,7 @@ images/debian-stretch.squashfs: images
-v "${PWD}/images:/images" \
-e "user=`id -u`" \
-e "group=`id -g`" \
-e "IMAGES=${PWD}/images" \
-e "ANNOUNCE=$(ANNOUNCE)" \
-e "WEBSEED=$(WEBSEED)" \
-e "COMPRESSION_LEVEL=$(COMPRESSION_LEVEL)" \

View file

@ -1,4 +1,4 @@
FROM docker:dind
FROM docker
RUN apk add --no-cache \
ca-certificates \

View file

@ -37,36 +37,10 @@ make_torrent() {
}
run_packer() {
packer build "$PACKER_JSON"
mkdir -p build/rootfs
tar xf build/rootfs.tar -C build/rootfs
mv build/rootfs/artifacts/initramfs "/images/$INITRAMFS_FILE"
mv build/rootfs/artifacts/linux "/images/$LINUX_FILE"
rm -rf build/rootfs/artifacts
rm -- \
build/rootfs/.dockerenv \
build/rootfs/initrd.* \
build/rootfs/vmlinuz*
if [ -e "/images/$SQUASHFS_FILE" ]; then
rm "/images/$SQUASHFS_FILE"
fi
mksquashfs build/rootfs "/images/$SQUASHFS_FILE" \
-comp lzo \
-Xcompression-level $COMPRESSION_LEVEL
rm -rf build
packer build "$NAME.json"
if [ "$user" != "" ] && [ "$group" != "" ]; then
chown $user:$group \
"/images/$INITRAMFS_FILE" \
"/images/$LINUX_FILE" \
"/images/$SQUASHFS_FILE"
chown $user:$group "/images/${NAME}."*
fi
}
@ -77,10 +51,7 @@ if [ "$NAME" = "" ]; then
exit 1
fi
export PACKER_JSON="$NAME.json"
export INITRAMFS_FILE="$NAME.initramfs"
export LINUX_FILE="$NAME.linux"
export SQUASHFS_FILE="$NAME.squashfs"
export NAME
if [ -z "$TASK" ] || [ "$TASK" == "packer" ]; then
run_packer

View file

@ -1,5 +1,11 @@
{
"variables": {
"name": "{{env `NAME`}}",
"squashfs_file": "{{env `NAME`}}.squashfs",
"initramfs_file": "{{env `NAME`}}.initramfs",
"linux_file": "{{env `NAME`}}.linux",
"compression_level": "{{env `COMPRESSION_LEVEL`}}",
"images": "{{env `IMAGES`}}",
"ci_job_id": "{{env `CI_JOB_ID`}}",
"ci_commit_sha": "{{env `CI_COMMIT_SHA`}}",
"ci_commit_tag": "{{env `CI_COMMIT_TAG`}}",
@ -16,7 +22,15 @@
{
"type": "docker",
"image": "debian:stretch",
"export_path": "build/rootfs.tar"
"discard": true,
"run_command": [
"-d",
"-i",
"-t",
"-v", "{{user `images`}}:/tmp/images",
"{{.Image}}",
"/bin/bash"
]
}
],
"provisioners": [
@ -58,10 +72,9 @@
"echo overlay >> /etc/initramfs-tools/modules",
"echo 'RESUME=none' > /etc/initramfs-tools/conf.d/resume",
"mkdir /usr/local/boot",
"apt-get -f -y install aria2 ca-certificates linux-image-amd64 lvm2",
"mkdir /artifacts",
"cp $(find /boot/ -name 'initrd.img-*' | sort -V | tail -n 1) /artifacts/initramfs",
"cp $(find /boot/ -name 'vmlinuz-*' | sort -V | tail -n 1) /artifacts/linux"
"apt-get -f -y install aria2 linux-image-amd64 lvm2",
"cp $(find /boot/ -name 'initrd.img-*' | sort -V | tail -n 1) '/tmp/images/{{user `initramfs_file`}}'",
"cp $(find /boot/ -name 'vmlinuz-*' | sort -V | tail -n 1) '/tmp/images/{{user `linux_file`}}'"
]
},
{
@ -76,6 +89,31 @@
{
"type": "shell",
"inline": [ "rm /boot && mkdir /boot" ]
},
{
"type": "shell",
"inline": [
"set -x",
"apt-get -y install squashfs-tools",
"dpkg -L squashfs-tools liblzo2-2 | while read f; do [ -f \"$f\" ] && echo \"$f\"; done > /tmp/ignore_files",
"echo '/etc/resolv.conf' >> /tmp/ignore_files",
"echo '/etc/hostname' >> /tmp/ignore_files",
"echo '/etc/hosts' >> /tmp/ignore_files",
"mkdir -p /tmp/extra/tmp /tmp/extra/proc /tmp/extra/sys /tmp/extra/etc"
]
},
{
"type": "file",
"source": "hosts",
"destination": "/tmp/extra/etc/hosts"
},
{
"type": "shell",
"inline": [
"if [ -e '/tmp/images/{{user `squashfs_file`}}' ]; then rm '/tmp/images/{{user `squashfs_file`}}'; fi",
"squashfs_content=\"$(find / -mindepth 1 -maxdepth 1 | grep -vE '^/(proc|sys|tmp|[.]dockerenv|packer-files)$')\"",
"mksquashfs $squashfs_content /tmp/extra/* '/tmp/images/{{user `squashfs_file`}}' -comp lzo -Xcompression-level {{user `compression_level`}} -ef /tmp/ignore_files"
]
}
]
}

6
packer/hosts Normal file
View file

@ -0,0 +1,6 @@
127.0.0.1 localhost
127.0.1.1 @@HOSTNAME@@
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

View file

@ -184,6 +184,10 @@ PARTTABLE
mount -n -o move /overlay ${rootmnt}/overlay
mount -n -o move /rom ${rootmnt}/rom
hostname=$(ip -o link show | sed -ne 's#^.*link/ether \([0-9a-f:]*\).*$#\1#p' | sed -ne '1{s/://g;p}')
echo $hostname > ${rootmnt}/etc/hostname
sed -i "s/@@HOSTNAME@@/$hostname/" ${rootmnt}/etc/hosts
if [ -z "$labsync_localboot" ]; then
labsync_set_environment LABSYNC "$torrent_name"
labsync_set_environment LABSYNC_TORRENT "${labsync_torrent}"