From c69c9654a60cc31aec2f38265462e5251359ef9b Mon Sep 17 00:00:00 2001 From: jalr Date: Sat, 17 Mar 2018 21:22:19 +0100 Subject: [PATCH] Keep initramfs in squashfs image; Implement localboot in labsync script --- packer/debian-stretch.json | 19 +++-- packer/initramfs/labsync | 151 +++++++++++++++++++++---------------- 2 files changed, 100 insertions(+), 70 deletions(-) diff --git a/packer/debian-stretch.json b/packer/debian-stretch.json index c5de2c7..297b144 100644 --- a/packer/debian-stretch.json +++ b/packer/debian-stretch.json @@ -43,6 +43,7 @@ { "type": "shell", "inline": [ + "set -x", "echo \"job: {{user `ci_job_id`}}\" > /.build-info", "echo \"commit: {{user `ci_commit_sha`}}\" >> /.build-info", "echo \"commit tag: {{user `ci_commit_tag`}}\" >> /.build-info", @@ -50,18 +51,22 @@ "echo \"commit ref slug: {{user `ci_commit_ref_slug`}}\" >> /.build-info", "apt-get update", "apt-get -y dist-upgrade", - "apt-get -y install aria2 initramfs-tools linux-image-amd64 lvm2 openssh-server python", - "echo 'RESUME=none' > /etc/initramfs-tools/conf.d/resume", + "rmdir /boot && ln -s /usr/local/boot /boot", + "apt-get -y install initramfs-tools || true", "echo squashfs >> /etc/initramfs-tools/modules", "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 linux-image-amd64 lvm2", "mkdir /artifacts", - "kernel_release=$(find /boot -name 'vmlinuz-*' -printf '%f\\n' | sed 's/^vmlinuz-//')", - "mkinitramfs -o /boot/initrd.img-${kernel_release} artifacts/initramfs $kernel_release", - "cp /boot/initrd.img-${kernel_release} /artifacts/initramfs", - "cp $(find /boot -name 'vmlinuz-*' | sort -V | tail -n 1) /artifacts/linux", - "mv /boot /usr/local/boot" + "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" ] }, + { + "type": "shell", + "inline": [ "apt-get -y install openssh-server python" ] + }, { "type": "ansible", "playbook_file": "ansible/playbook.yml", diff --git a/packer/initramfs/labsync b/packer/initramfs/labsync index 2884331..ca8599d 100755 --- a/packer/initramfs/labsync +++ b/packer/initramfs/labsync @@ -1,5 +1,9 @@ #!/bin/sh +labsync_lv_path() { + lvm lvs "$1/$2" -o LV_PATH --noheadings | sed 's/\s//g' +} + labsync_create_or_resize_lv() { vg="$1" lv="$2" @@ -32,15 +36,17 @@ labsync_err() { } labsync_top() { - # Check if mandatory parameters are missing - if [ -z "${labsync_disk}" ]; then - labsync_err "The disk parameter is missing" - fi - if [ -z "${labsync_partsize_boot}" ]; then - labsync_err "The partsize_boot parameter is missing" - fi - if [ -z "${labsync_torrent}" ]; then - labsync_err "The torrent parameter is missing" + if [ -z ${labsync_localboot} ]; then + # Check if mandatory parameters are missing + if [ -z "${labsync_disk}" ]; then + labsync_err "The disk parameter is missing" + fi + if [ -z "${labsync_partsize_boot}" ]; then + labsync_err "The partsize_boot parameter is missing" + fi + if [ -z "${labsync_torrent}" ]; then + labsync_err "The torrent parameter is missing" + fi fi cat << EOM @@ -58,26 +64,32 @@ EOM } labsync_premount() { - labsync_info "Configuring networking" - configure_networking + if [ -z "$labsync_localboot" ]; then + labsync_info "Configuring networking" + configure_networking + fi } labsync_mount_root() { - labsync_info "Downloading torrent file from ${labsync_torrent}" - while ! wget -T 10 -O /tmp/torrent "${labsync_torrent}"; do sleep 1; done - aria2c -S /tmp/torrent > /tmp/torrent_info - squashfs_file="$(sed -n '/idx|path\/length/,${s/\s*1|\(.*\)$/\1/p}' /tmp/torrent_info)" - image_size_bytes="$(sed -n 's/Total Length: .*(\([0-9,]*\)).*$/\1/p' /tmp/torrent_info | tr -d ',')" - torrent_name="$(sed -n 's/^Name: \(.*\)$/\1/p' /tmp/torrent_info)" + if [ -z "$labsync_localboot" ]; then + labsync_info "Downloading torrent file from ${labsync_torrent}" + while ! wget -T 10 -O /tmp/torrent "${labsync_torrent}"; do sleep 1; done + aria2c -S /tmp/torrent > /tmp/torrent_info + squashfs_file="$(sed -n '/idx|path\/length/,${s/\s*1|\(.*\)$/\1/p}' /tmp/torrent_info)" + image_size_bytes="$(sed -n 's/Total Length: .*(\([0-9,]*\)).*$/\1/p' /tmp/torrent_info | tr -d ',')" + torrent_name="$(sed -n 's/^Name: \(.*\)$/\1/p' /tmp/torrent_info)" - labsync_info "Updating partitions" - size_disk=$(blockdev --getsz ${labsync_disk}) - size_part_1=$((labsync_partsize_boot * 1024 * 1024 / 512)) - size_part_2=$(((size_disk - labsync_partsize_boot) * 1024 * 1024 / 512)) - start_part_2=$((size_part_1 + 2048)) + lv_name_sqashfs="sqfs-$torrent_name" + lv_name_overlay="ovly-$torrent_name" - sfdisk -q ${labsync_disk} << PARTTABLE + labsync_info "Updating partitions" + size_disk=$(blockdev --getsz ${labsync_disk}) + size_part_1=$((labsync_partsize_boot * 1024 * 1024 / 512)) + size_part_2=$(((size_disk - labsync_partsize_boot) * 1024 * 1024 / 512)) + start_part_2=$((size_part_1 + 2048)) + + sfdisk -q ${labsync_disk} << PARTTABLE label: dos label-id: 0xdeadbeef device: ${labsync_disk} @@ -88,59 +100,72 @@ ${labsync_disk}2 : start=$start_part_2, size= $size_part_2, type=8e PARTTABLE - labsync_info "Doing lvm stuff" - pv_device="${labsync_disk}2" - vg_name="vglab" - lv_name_sqashfs="sqfs-$torrent_name" - lv_name_overlay="ovly-$torrent_name" + labsync_info "Doing lvm stuff" + pv_device="${labsync_disk}2" + vg_name="vglab" - lvm pvcreate -t "$pv_device" - pvstatus=$? - if [ $pvstatus -eq 0 ]; then - # is not a physical volume - lvm pvcreate "$pv_device" + lvm pvcreate -t "$pv_device" + pvstatus=$? + if [ $pvstatus -eq 0 ]; then + # is not a physical volume + lvm pvcreate "$pv_device" + fi + + lvm vgcreate -t "$vg_name" "$pv_device" + vgstatus=$? + if [ $? -eq 0 ]; then + lvm vgcreate "$vg_name" "$pv_device" + fi + + labsync_create_or_resize_lv "$vg_name" "$lv_name_sqashfs" $((image_size_bytes * 2)) $image_size_bytes + labsync_create_or_resize_lv "$vg_name" "$lv_name_overlay" $((500 * 1024 * 1024)) + + #lvm vgchange -ay "$vg_name" + #lvm lvscan -a --ignorelockingfailure + #lvm lvchange -aly --ignorelockingfailure "$vg_name/$lv_name_sqashfs" + lv_path_squashfs="$(labsync_lv_path "$vg_name" "$lv_name_sqashfs")" + lv_path_overlay="$(labsync_lv_path "$vg_name" "$lv_name_overlay")" + + mke2fs -q -F -t ext4 "$lv_path_overlay" + + labsync_info "Downloading squashfs image $squashfs_file to $lv_path_squashfs" + (cd /tmp + ln -s "$(realpath "$lv_path_squashfs")" "$squashfs_file" + aria2c \ + --console-log-level=warn \ + --allow-overwrite \ + --check-integrity \ + --seed-time=0 \ + --summary-interval=0 \ + --file-allocation=none \ + --enable-dht=false \ + /tmp/torrent + ) + else + # local boot + lv_name_sqashfs="sqfs-$labsync_localboot" + lv_name_overlay="ovly-$labsync_localboot" + lv_path_squashfs="$(labsync_lv_path "$vg_name" "$lv_name_sqashfs")" + lv_path_overlay="$(labsync_lv_path "$vg_name" "$lv_name_overlay")" fi - lvm vgcreate -t "$vg_name" "$pv_device" - vgstatus=$? - if [ $? -eq 0 ]; then - lvm vgcreate "$vg_name" "$pv_device" - fi - - labsync_create_or_resize_lv "$vg_name" "$lv_name_sqashfs" $((image_size_bytes * 2)) $image_size_bytes - labsync_create_or_resize_lv "$vg_name" "$lv_name_overlay" $((500 * 1024 * 1024)) - - #lvm vgchange -ay "$vg_name" - #lvm lvscan -a --ignorelockingfailure - #lvm lvchange -aly --ignorelockingfailure "$vg_name/$lv_name_sqashfs" - lv_path_squashfs="$(lvm lvs "$vg_name/$lv_name_sqashfs" -o LV_PATH --noheadings --units b --nosuffix | sed 's/\s//g')" - lv_path_overlay="$(lvm lvs "$vg_name/$lv_name_overlay" -o LV_PATH --noheadings --units b --nosuffix | sed 's/\s//g')" - - labsync_info "Downloading squashfs image $squashfs_file to $lv_path_squashfs" - (cd /tmp - ln -s "$(realpath "$lv_path_squashfs")" "$squashfs_file" - aria2c \ - --console-log-level=warn \ - --allow-overwrite \ - --check-integrity \ - --seed-time=0 \ - --summary-interval=0 \ - --file-allocation=none \ - --enable-dht=false \ - /tmp/torrent - ) - labsync_info "Mounting squashfs and overlay" modprobe overlay mkdir -p /root mkdir /ro_root /rw_root mount -t squashfs "$(realpath "$lv_path_squashfs")" /ro_root - mke2fs -q -F -t ext4 "$lv_path_overlay" mount -t ext4 "$lv_path_overlay" /rw_root mkdir -p /rw_root/upper /rw_root/work mount -t overlay overlay -o lowerdir=/ro_root,upperdir=/rw_root/upper,workdir=/rw_root/work /root + if grep -q '^LABSYNC=' /root/etc/environment; then + sed -i "s/^\(LABSYNC\)=.*$/\1='$torrent_name'/" /root/etc/environment + else + echo "LABSYNC='$torrent_name'" >> /root/etc/environment + fi case "$labsync_wait" in + "") + ;; pause) read x ;;