diff --git a/packer/debian-stretch.json b/packer/debian-stretch.json index 392c70a..298fcff 100644 --- a/packer/debian-stretch.json +++ b/packer/debian-stretch.json @@ -29,6 +29,12 @@ "source": "initramfs/labsync", "destination": "/etc/initramfs-tools/scripts/" }, + { + "type": "shell", + "inline": [ + "sed -i 's/@@CI_REPOSITORY_URL@@/{{user `ci_repository_url`}}/' /etc/initramfs-tools/scripts/labsync" + ] + }, { "type": "file", "source": "initramfs/labsync-prereqs", @@ -44,7 +50,7 @@ "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 openssh-server python", + "apt-get -y install aria2 initramfs-tools linux-image-amd64 lvm2 openssh-server python", "echo 'RESUME=none' > /etc/initramfs-tools/conf.d/resume", "echo squashfs >> /etc/initramfs-tools/modules", "echo overlay >> /etc/initramfs-tools/modules", diff --git a/packer/initramfs/labsync b/packer/initramfs/labsync index bad7984..6c1787e 100755 --- a/packer/initramfs/labsync +++ b/packer/initramfs/labsync @@ -1,152 +1,153 @@ #!/bin/sh -labsync_mount_root() { - ## - # FUNCTION DEFINITIONS - ## +labsync_create_or_resize_lv() { + vg="$1" + lv="$2" + new_size_bytes="$3" + minimum_size_bytes="$4" + current_lv_size_bytes="$(lvm lvs "$vg/$lv" -o LV_SIZE --noheadings --units b --nosuffix 2>/dev/null | sed 's/\s//g')" - print_waiting() { - echo -en "\033[36m${1}... \033[0m" - } - - print_warning() { - echo -e "\n\033[31mWARNING: ${1}\033[0m" - } - - print_done() { - echo -e "\033[32mdone.\033[0m" - } - - error_fatal() { - print_warning "$1; interrupting startup" - while sleep 3600;do sleep 3600;done - } - - ## - # SHOW BRANDING - ## - - echo -e ' - Starting - ""# # - # mmm #mmm mmm m m m mm mmm - # " # #" "# # " "m m" #" # #" " - # m"""# # # """m #m# # # # - "mm "mm"# ##m#" "mmm" "# # # "#mm" - m" - "" - https://gitlab.jalr.de/fablab/initramfs - ' - - # Check if mandatory parameters are missing - - if [ -z "$disk" ];then - error_fatal "The disk parameter is missing" + if [ -z "$current_lv_size_bytes" ]; then + # no size returned, it's likely that lv doesn't exist + lvm lvcreate -L ${new_size_bytes}b -n "$lv" "$vg" + elif [ "$minimum_size_bytes" != "" ] && [ $minimum_size_bytes -gt 0 ]; then + # lv already exists + if [ $current_lv_size_bytes -lt $minimum_size_bytes ]; then + lvm lvextend -L ${new_size_bytes}b "$vg/$lv" + fi fi - if [ -z "$partsize" ];then - error_fatal "The partsize parameter is missing" - fi - if [ -z "$torrent" ];then - error_fatal "The torrent parameter is missing" - fi - if [ -z "$torrent_file" ];then - error_fatal "The torrent_file parameter is missing" - fi - print_done +} - ## - # PARTITIONING OF DISK - ## - - size_part_1=$(($partsize * 1024 * 1024 / 512)) - - parttable="label: dos - label-id: 0xdeadbeef - device: $disk - unit: sectors - - $root : start= 2048, size= $size_part_1, type=83" - - readtable="$(sfdisk -d $disk)" # wtf? but it is needed - - print_waiting "Checking partition tables" - if [ "$parttable" != "$readtable" ];then - print_done - print_waiting "Partition tables do not match; partitioning disk" - rewrite_rootfs=true - sfdisk -q $disk << PARTTABLE - $parttable -PARTTABLE - print_done - else - print_done - fi - - ## - # TEMPORARY NETWORK CONFIG (only for qemu) - ## - - configure_networking - - #if ! [ -z "$qemu" ];then - # print_waiting "Setting up temporary network config" - # ip addr add 10.2.2.2/24 dev ens3 - # ip link set ens3 up - # print_done - #else - # print_waiting "Setting up interfaces with DHCP" - # ipconfig all - # print_done - #fi - - ## - # DOWNLOAD SQUASHFS - ## - - print_waiting "Downloading root filesystem image via $torrent" - # --allow-overwrite: do not rename the torrent file - # --check-integrity: verify downloaded file and do not complain about missing controll file - # --seed-time=0: do not wait until ratio 1, we seed when we’re booted - # --summary-interval=0: do not show summary while downloading - # --console-log-level=error: Only show errors - # --download-result=hide: Do not show download results - ip a - sleep 1 - - #wget -O /tmp/torrent "$torrent" - - (cd /tmp - ln -s "${disk}1" "$torrent_file" - aria2c \ - --allow-overwrite \ - --check-integrity \ - --seed-time=0 \ - --summary-interval=10 \ - --file-allocation=none \ - --enable-dht=false \ - "$torrent" - ) - print_done - - modprobe overlay - mkdir -p /root - mkdir /ro_root /rw_root - mount -t squashfs /dev/sda1 /ro_root - mke2fs -q -F -t ext4 /dev/sdb1 - mount -t ext4 /dev/sdb1 /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 - sleep 10 +labsync_info() { + echo -e "\n\033[36m${1}... \033[0m" +} +labsync_warn() { + echo -e "\n\033[31mWARNING: ${1}\033[0m" +} +labsync_err() { + labsync_warn "$1; interrupting startup" + read x } 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" + fi + + cat << EOM +Starting + ""# # + # mmm #mmm mmm m m m mm mmm + # " # #" "# # " "m m" #" # #" " + # m"""# # # """m #m# # # # + "mm "mm"# ##m#" "mmm" "# # # "#mm" + m" + "" +@@CI_REPOSITORY_URL@@ + +EOM } labsync_premount() { - : + labsync_info "Configuring networking" + configure_networking +} + + +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 -T /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)) + + sfdisk -q ${labsync_disk} << PARTTABLE +label: dos +label-id: 0xdeadbeef +device: ${labsync_disk} +unit: sectors + +${labsync_disk}1 : start= 2048, size= $size_part_1, type=83 +${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" + + 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="$(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 + + case "$labsync_wait" in + pause) + read x + ;; + *) + sleep $labsync_wait + ;; + esac } labsync_bottom() {