diff --git a/examples/docker-for-mac.yml b/examples/docker-for-mac.yml index ec96589a1..215573f26 100644 --- a/examples/docker-for-mac.yml +++ b/examples/docker-for-mac.yml @@ -26,7 +26,7 @@ onboot: command: ["/usr/bin/mountie", "/var/lib"] # make a swap file on the mounted disk - name: swap - image: linuxkit/swap:v0.7 + image: linuxkit/swap:f8b0434963b4c9114db473aab3b9409bccd4f85b command: ["/swap.sh", "--path", "/var/lib/swap", "--size", "1024M"] # mount-vpnkit mounts the 9p share used by vpnkit to coordinate port forwarding - name: mount-vpnkit diff --git a/examples/swap.yml b/examples/swap.yml index 16a811642..aa9371fa8 100644 --- a/examples/swap.yml +++ b/examples/swap.yml @@ -18,7 +18,7 @@ onboot: image: linuxkit/mount:v0.7 command: ["/usr/bin/mountie", "/var/external"] - name: swap - image: linuxkit/swap:v0.7 + image: linuxkit/swap:f8b0434963b4c9114db473aab3b9409bccd4f85b # to use unencrypted swap, use: # command: ["/swap.sh", "--path", "/var/external/swap", "--size", "1G"] command: ["/swap.sh", "--path", "/var/external/swap", "--size", "1G", "--encrypt"] diff --git a/pkg/swap/swap.sh b/pkg/swap/swap.sh index 7c421c124..fe8718469 100755 --- a/pkg/swap/swap.sh +++ b/pkg/swap/swap.sh @@ -115,23 +115,27 @@ done ### Required? exit 1 ### Else? exit 0 - -## Allocate the file -dd if=/dev/zero of=$path bs=1024 count=$(disksize_to_count 1024 $size) -chmod 0600 $path - -## was it encrypted? use cryptsetup and get the mapped device if [ "$ENCRYPT" == "true" ]; then - # might need - #loop=$(losetup -f) - #losetup ${loop} ${path} - - cryptsetup open --type plain --key-file /dev/urandom --key-size=256 --cipher=aes-cbc-essiv:sha256 --offset=0 ${path} swapfile SWAPDEV=/dev/mapper/swapfile else SWAPDEV=$path fi -## mkswap and swapon the device -/sbin/mkswap $SWAPDEV +if [ ! -f $path ] || ! [ $(stat -c "%s" $path) == $(disksize_to_count 1 $size) ]; then + ## Allocate the file + dd if=/dev/zero of=$path bs=1024 count=$(disksize_to_count 1024 $size) + chmod 0600 $path + + ## was it encrypted? use cryptsetup and get the mapped device + if [ "$ENCRYPT" == "true" ]; then + # might need + #loop=$(losetup -f) + #losetup ${loop} ${path} + + cryptsetup open --type plain --key-file /dev/urandom --key-size=256 --cipher=aes-cbc-essiv:sha256 --offset=0 ${path} swapfile + fi + + /sbin/mkswap $SWAPDEV +fi + /sbin/swapon $SWAPDEV