Skip to content

Instantly share code, notes, and snippets.

@hshimamoto
Created March 12, 2021 21:55
Show Gist options
  • Save hshimamoto/02fd1dd8124b101d6b8f3fc959245d56 to your computer and use it in GitHub Desktop.
Save hshimamoto/02fd1dd8124b101d6b8f3fc959245d56 to your computer and use it in GitHub Desktop.
Download cloudimage
focal https://cloud-images.ubuntu.com/focal/current/ focal-server-cloudimg-amd64.img MD5SUMS
bionic https://cloud-images.ubuntu.com/bionic/current/ bionic-server-cloudimg-amd64.img MD5SUMS
xenial https://cloud-images.ubuntu.com/xenial/current/ xenial-server-cloudimg-amd64-disk1.img MD5SUMS
#!/bin/bash
cd $(dirname $0)
if [ $# -lt 1 ]; then
echo "update.sh <conf file>"
exit 1
fi
conf=$1
echo $conf
if [ ! -e $conf ]; then
echo "no $conf"
exit 1
fi
IMGDIR=~/vm/images
check_and_download() {
local suit=$1
local url=$2
local img=$3
local chksum=$4
echo $suit $url $img $chksum
# get chksum
curl -qLO $url/$chksum
if [ $? -ne 0 ]; then
echo "ERROR: $suit: download checksum"
return
fi
if [ -e $chksum.$suit ]; then
prevsum=$(cat $chksum.$suit)
newsum=$(grep $img $chksum)
if [ "$prevsum" == "$newsum" ]; then
return
fi
fi
# try to download the image
curl -qLO $url/$img
if [ $? -ne 0 ]; then
echo "ERROR: $suit: download image"
return
fi
# is there previous image?
if [ -e $IMGDIR/$img ]; then
rm -f $IMGDIR/$img.prev
mv $IMGDIR/$img $IMGDIR/$img.prev
fi
mv $img $IMGDIR/$img
grep $img $chksum > $chksum.$suit
}
while read l; do
case "$l" in
\#*)
continue
esac
a=($l)
check_and_download ${a[0]} ${a[1]} ${a[2]} ${a[3]}
done < $conf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment