|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Environment variables for the CentOS cloud image |
| 4 | +ARCH="x86_64" |
| 5 | +OS_VER="7" |
| 6 | +ROOTFS_VER="2003" |
| 7 | +ROOTFS_FN="CentOS-${OS_VER}-${ARCH}-GenericCloud-${ROOTFS_VER}.qcow2" |
| 8 | +ROOTFS_URL="http://cloud.centos.org/centos/${OS_VER}/images/${ROOTFS_FN}" |
| 9 | + |
| 10 | +# Environment variables for Yuk7's wsldl |
| 11 | +LNCR_BLD="20040300" |
| 12 | +LNCR_ZIP="icons.zip" |
| 13 | +LNCR_NAME="CentOS" |
| 14 | +LNCR_FN=${LNCR_NAME}.exe |
| 15 | +LNCR_ZIPFN=${LNCR_NAME}${OS_VER}.exe |
| 16 | +LNCR_URL="https://github.com/yuk7/wsldl/releases/download/${LNCR_BLD}/${LNCR_ZIP}" |
| 17 | + |
| 18 | +# Waits until a file appears or disappears |
| 19 | +# - $1 File path to wait for its existence |
| 20 | +# - [$2] The string 'a' to wait until the file appears, or 'd' to wait until the file disappears |
| 21 | +# - [$3] Timeout in seconds |
| 22 | +waitFile() { |
| 23 | + local START=$(cut -d '.' -f 1 /proc/uptime) |
| 24 | + local MODE=${2:-"a"} |
| 25 | + until [[ "${MODE}" = "a" && -e "$1" ]] || [[ "${MODE}" = "d" && ( ! -e "$1" ) ]]; do |
| 26 | + sleep 1s |
| 27 | + if [ -n "$3" ]; then |
| 28 | + local NOW=$(cut -d '.' -f 1 /proc/uptime) |
| 29 | + local ELAPSED=$(( NOW - START )) |
| 30 | + if [ $ELAPSED -ge "$3" ]; then break; fi |
| 31 | + fi |
| 32 | + done |
| 33 | +} |
| 34 | + |
| 35 | +# Create a work dir |
| 36 | +mkdir wsl |
| 37 | +cd wsl |
| 38 | + |
| 39 | +# Download the CentOS cloud image and Yuk7's WSLDL |
| 40 | +wget --no-verbose ${ROOTFS_URL} -O ${ROOTFS_FN} |
| 41 | +wget --no-verbose ${LNCR_URL} -O ${LNCR_ZIP} |
| 42 | + |
| 43 | +# Extract the CentOS WSL launcher |
| 44 | +unzip ${LNCR_ZIP} ${LNCR_FN} |
| 45 | + |
| 46 | +# Clean up |
| 47 | +rm ${LNCR_ZIP} |
| 48 | + |
| 49 | +# Mount the qcow2 image |
| 50 | +sudo mkdir mntfs |
| 51 | +sudo modprobe nbd |
| 52 | +sudo qemu-nbd -c /dev/nbd0 --read-only ./${ROOTFS_FN} |
| 53 | +waitFile /dev/nbd0p1 "a" 30 |
| 54 | +sudo mount -o ro /dev/nbd0p1 mntfs |
| 55 | + |
| 56 | +# Clone the qcow2 image contents to a writable directory |
| 57 | +sudo cp -a mntfs rootfs |
| 58 | + |
| 59 | +# Unmount the qcow2 image |
| 60 | +sudo umount mntfs |
| 61 | +sudo qemu-nbd -d /dev/nbd0 |
| 62 | +waitFile /dev/nbd0p1 "d" 30 |
| 63 | +sudo rmmod nbd |
| 64 | +sudo rmdir mntfs |
| 65 | + |
| 66 | +# Clean up |
| 67 | +rm ${ROOTFS_FN} |
| 68 | + |
| 69 | +# Create a tar.gz of the rootfs |
| 70 | +sudo tar -zcpf rootfs.tar.gz -C ./rootfs . |
| 71 | +sudo chown "$(id -un)" rootfs.tar.gz |
| 72 | + |
| 73 | +# Clean up |
| 74 | +sudo rm -rf rootfs |
| 75 | + |
| 76 | +# Create the distribution zip of WSL CentOS |
| 77 | +mkdir out |
| 78 | +mkdir dist |
| 79 | +mv -f ${LNCR_FN} ./out/${LNCR_ZIPFN} |
| 80 | +mv -f rootfs.tar.gz ./out/ |
| 81 | +pushd out |
| 82 | +zip -r ../dist/CentOS${OS_VER}.zip ./* |
| 83 | +popd |
| 84 | + |
| 85 | +# Clean up |
| 86 | +rm -rf out |
0 commit comments