Add variable/option to control if zeros are written to the disk

It takes some time to write zeros to the disk and it aborts later if
less than 20GB are available.

Add variable to the template, cleanup script and add a new option to
wrapacker.
This commit is contained in:
Christian Kotte 2020-07-13 13:03:01 +02:00
parent 5d9fdde3dc
commit 31965a8808
No known key found for this signature in database
GPG key ID: 6E24DDCC81A95734
3 changed files with 27 additions and 7 deletions

View file

@ -4,6 +4,7 @@
"iso_checksum_url": "https://mirrors.kernel.org/archlinux/iso/{{isotime \"2006.01\"}}.01/sha1sums.txt",
"ssh_timeout": "20m",
"country": "US",
"write_zeros": "true",
"headless": "false"
},
"builders": [
@ -124,7 +125,7 @@
},
{
"type": "shell",
"execute_command": "{{ .Vars }} sudo -E -S bash '{{ .Path }}'",
"execute_command": "{{ .Vars }} WRITE_ZEROS={{ user `write_zeros` }} sudo -E -S bash '{{ .Path }}'",
"script": "scripts/cleanup.sh"
}
],

View file

@ -5,8 +5,10 @@ echo ">>>> cleanup.sh: Cleaning pacman cache.."
/usr/bin/pacman -Scc --noconfirm
# Write zeros to improve virtual disk compaction.
echo ">>>> cleanup.sh: Writing zeros to improve virtual disk compaction.."
zerofile=$(/usr/bin/mktemp /zerofile.XXXXX)
/usr/bin/dd if=/dev/zero of="$zerofile" bs=1M
/usr/bin/rm -f "$zerofile"
/usr/bin/sync
if [[ $WRITE_ZEROS == "true" ]]; then
echo ">>>> cleanup.sh: Writing zeros to improve virtual disk compaction.."
zerofile=$(/usr/bin/mktemp /zerofile.XXXXX)
/usr/bin/dd if=/dev/zero of="$zerofile" bs=1M
/usr/bin/rm -f "$zerofile"
/usr/bin/sync
fi

View file

@ -64,7 +64,7 @@ validate_country() {
# print this script's usage message to stderr
usage() {
cat <<-EOF >&2
usage: wrapacker [-c COUNTRY] [-p PROVIDER] [-t TIMEOUT] [-o ON-ERROR ACTION] [-f] [-d] [-h]
usage: wrapacker [-c COUNTRY] [-p PROVIDER] [-t TIMEOUT] [-w] [-o ON-ERROR ACTION] [-f] [-d] [-h]
EOF
}
@ -129,6 +129,9 @@ help() {
sets the amount of time packer will wait for trying ssh login;
defaults to 20m
-w, --skip-write-zeros
don't inflate the disk to improve virtual disk compaction
-o, --on-error=ACTION
error handling if the build fails;
defaults to cleanup
@ -165,6 +168,7 @@ country=''
provider=''
dry_run=false
timeout=''
skip_write_zeros=false
on_error=''
force=false
headless=false
@ -193,6 +197,9 @@ while [[ $1 != '' ]]; do
--timeout=*)
timeout=${1#*=}
;;
-w | --skip-write-zeros)
skip_write_zeros=true
;;
-o | --on-error)
on_error=$2
shift
@ -288,6 +295,12 @@ else
SSH_TIMEOUT='20m'
fi
if [[ $skip_write_zeros = true ]]; then
WRITE_ZEROS="false"
else
WRITE_ZEROS="true"
fi
if [[ $on_error ]]; then
case $(echo "$on_error" | tr '[:upper:]' '[:lower:]') in
cleanup)
@ -320,6 +333,7 @@ cat <<-EOF
-var "ssh_timeout=$SSH_TIMEOUT" \\
-var "country=$country" \\
-var "headless=$headless" \\
-var "write_zeros=$WRITE_ZEROS" \\
-on-error=$ON_ERROR \\
-force \\
arch-template.json
@ -333,6 +347,7 @@ cat <<-EOF
-var "ssh_timeout=$SSH_TIMEOUT" \\
-var "country=$country" \\
-var "headless=$headless" \\
-var "write_zeros=$WRITE_ZEROS" \\
-on-error=$ON_ERROR \\
arch-template.json
EOF
@ -346,6 +361,7 @@ else
-var "ssh_timeout=$SSH_TIMEOUT" \
-var "country=$country" \
-var "headless=$headless" \
-var "write_zeros=$WRITE_ZEROS" \
-on-error=$ON_ERROR \
-force \
arch-template.json
@ -357,6 +373,7 @@ else
-var "ssh_timeout=$SSH_TIMEOUT" \
-var "country=$country" \
-var "headless=$headless" \
-var "write_zeros=$WRITE_ZEROS" \
-on-error=$ON_ERROR \
arch-template.json
fi