"Reclaim disk space on shutdown" default to true, or means to set it via command line (e.g. prlctl)?

meermanr

Bit poster
When I create a VM (via vagrant), I want the new VM's "Reclaim disk space on shutdown" preference to default to "Yes", so that disk space on my macOS host is not wasted.

I use vagrant to spin up headless GNU/Linux distributions to develop software, or compile software projects. For speed, and cleanliness, I tend to do the heavy lifting in the VM's own disk (rather than in the /vagrant prl_fs mount). For example, compilers, docker images, golang repos are all downloaded to the VM's disk. After a few round of building and cleaning (as in "make clean") my software the VM has consumed 10s of GiB that were subsequently discarded.

I spent this afternoon playing with ~/.vagrant.d/Vagrantfile to ensure that "fstrim -va" is called from within the guest when it is shutdown. However, until I right-click the VM in Parallels GUI and choose to reclaim disk space I do not get any benefit from that.

I found that there's a per-VM preference to "Reclaim disk space on shutdown". Please can this be set true by default? Or provide a way to set it via prlctl, so I can script setting it when I create new VMs via vagrant?

I can't think of any compelling reasons why you wouldn't want to reclaim disk space after shutting down a VM - seems the ideal time to perform clean up. In my (limited) experience it only takes a few moments to complete even when freeing 50-60 GiB.
 
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
 
Back
Top