In the meantime, I have been able to achieve what I want with a fairly brittle set of scripts:
Code:
#!/bin/bash
# Sample command:
#
# prlctl list --info --json ubuntu22_default_1670598894122_54405 \
# | jq '.[0]["Home path"]' \
# | xargs -n1 sed -i -e 's#<ReclaimDiskSpace>0</ReclaimDiskSpace>#<ReclaimDiskSpace>1</ReclaimDiskSpace>#p'
prlctl list --info --json "$1" \
| jq '.[0]["Home path"]' \
| xargs -n1 sed -i -e 's#<ReclaimDiskSpace>0</ReclaimDiskSpace>#<ReclaimDiskSpace>1</ReclaimDiskSpace>#p'
(Note you need to install jq, e.g. "brew install jq")
I trigger this for all vagrant VMs I create via ~/.vagrant.d/Vagrantfile (which is applied to all vagrant commands, irrespective of which directory you run them in):
Code:
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Install Parallels provider.
# # Cf. https://kb.parallels.com/en/122843
# if Vagrant.plugins_enabled?
# unless Vagrant.has_plugin?("vagrant-parallels")
# system("vagrant plugin install vagrant-parallels") || exit!
# exit system('vagrant', *ARGV)
# end
# end
Vagrant.configure("2") do |config|
# Newer way to install Parallels provider
config.vagrant.plugins = "vagrant-parallels"
ENV["VAGRANT_DEFAULT_PROVIDER"] = "parallels"
# Cf. https://parallels.github.io/vagrant-parallels/docs/configuration.html
config.vm.provider "parallels" do |prl, override|
#prl.memory = 1024
#prl.customize ["set", :id, "--nested-virt", "on"]
# Record which Vagrantfile created the VM
prl.customize ["set", :id, "--description", __FILE__]
# Set Parallels to automatically reclaim disk space after shutdown. Sample command:
# prlctl list --info --json ubuntu22_default_1670598894122_54405 \
# | jq '.[0]["Home path"]' \
# | xargs -n1 sed -i -e 's#<ReclaimDiskSpace>0</ReclaimDiskSpace>#<ReclaimDiskSpace>1</ReclaimDiskSpace>#p'
override.trigger.after [:up] do |trigger|
trigger.info = "Configuring Parallels to automatically reclaim disk space on shutdown"
trigger.ruby do |env,machine|
system("~/.vagrant.d/prl_conf_reclaim_disk_space.sh #{machine.id}")
end
end
# Switch first disk to SCSI, so it supports TRIM/DISCARD
#prl.customize ["set", :id,
# "--device-set", "hdd0",
# "--type", "expand",
# "--iface", "scsi",
# "--subtype", "lsi-spi",
# "--online-compact", "on",
# ]
#prl.name = "hostname"
prl.cpus = 8
end
config.vm.provision "Set timezone",
type: "shell",
inline: <<-SHELL
PROFILE=/etc/profile.d/set_timezone.sh
if [[ ! -e "$PROFILE" ]]
then
echo "export TZ=Europe/London" > "$PROFILE"
chmod 0644 "$PROFILE"
source "$PROFILE"
fi
SHELL
config.vm.provision "Configure disk trimming",
type: "shell",
inline: <<-SHELL
# Add 'discard' option to swap definition, and reactivate swap
sed -i -e 's/\\(\\sswap\\s\\+sw\\)\\(\\s\\)/\\1,discard\\2/' /etc/fstab
swapoff --all
swapon --all
# Silently abort if OS does not have support
which fstrim >/dev/null 2>&1 || exit 0
which systemctl >/dev/null 2>&1 || exit 0
sed -e 's/^\\s\\+//' > /sbin/shrink_disks <<' EOF'
#!/bin/sh
set -x
# Free blocks no longer used by file-systems
(
sync
fstrim --verbose --all # Mounted file-systems
fstrim --verbose --fstab # Defined in /etc/fstab
sync
) >/dev/tty0 2>&1
EOF
chmod 0755 /sbin/shrink_disks
sed -e 's/^\\s\\+//' > /etc/systemd/system/shrink_disks.service <<' EOF'
[Unit]
Description=Discard unused storage blocks during shutdown
[Service]
Type=oneshot
RemainAfterExit=true
ExecStop=/sbin/shrink_disks
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now --quiet shrink_disks.service
SHELL
end