Skip to content

Commit

Permalink
Add variable/option to control if zeros are written to the disk
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ckotte authored and elasticdog committed Aug 13, 2020
1 parent b8ea7c0 commit c1125dc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
3 changes: 2 additions & 1 deletion arch-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down Expand Up @@ -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"
}
],
Expand Down
12 changes: 7 additions & 5 deletions scripts/cleanup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 18 additions & 1 deletion wrapacker
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -165,6 +168,7 @@ country=''
provider=''
dry_run=false
timeout=''
skip_write_zeros=false
on_error=''
force=false
headless=false
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit c1125dc

Please sign in to comment.