Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Enable trimming QEMU disks #1102

Merged
merged 1 commit into from
Oct 14, 2022
Merged

Enable trimming QEMU disks #1102

merged 1 commit into from
Oct 14, 2022

Conversation

chrisx8
Copy link
Contributor

@chrisx8 chrisx8 commented Oct 14, 2022

This would enable trimming of QEMU virtual disks, by adding the discard=on argument to -drive flag for basedisk or diffdisk. As a result, running fstrim -a in the guest would reduce the disk space consumed by QEMU disks (basedisk or diffdisk), and reduce the disk usage of Lima guests.

Current behavior: there's no easy way to reduce the disk usage of diffdisk, and running fstrim -a in the guest has no effect.

Resolves #356

* Resolves #356
* Add 'discard=on' argument to '-drive' flag for basedisk and diffdisk, so that
running `fstrim` in the guest would reduce the size of QEMU virtual
disks.

Signed-off-by: Chris Xiao <30990835+chrisx8@users.noreply.github.com>
Copy link
Member

@jandubois jandubois left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

I'm so happy this actually works! 😄

@jandubois jandubois merged commit 020d7ee into lima-vm:master Oct 14, 2022
@jandubois
Copy link
Member

FYI: Alpine doesn't have fstrim -a, but requires mountpoint, e.g. fstrim /tmp. It still releases all unused space from the diffdisk.

@CzBiX
Copy link

CzBiX commented Oct 14, 2022

Does basedisk really need discard too?

@chrisx8
Copy link
Contributor Author

chrisx8 commented Oct 14, 2022

Looks like Lima only uses either basedisk or diffdisk, so I'd say yes.

@jandubois
Copy link
Member

Looks like Lima only uses either basedisk or diffdisk, so I'd say yes.

We never write to basedisk though: it either is a read-only ISO, or it is the base for the diffdisk; all modifications should always go to the diffdisk unless I misunderstand how this works. So I don't think it is needed for the basedisk, but does it hurt? Is there any performance/memory cost associated with turning it on?

@chrisx8
Copy link
Contributor Author

chrisx8 commented Oct 14, 2022

lima/pkg/qemu/qemu.go

Lines 435 to 439 in 020d7ee

if diskSize, _ := units.RAMInBytes(*cfg.LimaYAML.Disk); diskSize > 0 {
args = append(args, "-drive", fmt.Sprintf("file=%s,if=virtio,discard=on", diffDisk))
} else if !isBaseDiskCDROM {
args = append(args, "-drive", fmt.Sprintf("file=%s,if=virtio,discard=on", baseDisk))
}

This code snippet suggests that only one of basedisk or diffdisk is used on the QEMU VM. If QEMU is set to boot from the diffdisk, it knows about the basedisk.

There shouldn't be any performance impact during normal use. However, fstrim may take slightly more time, although I can't really tell the difference when I was testing.

@jandubois
Copy link
Member

Ah yes, the only time we use the basedisk directly is when it is not an ISO and the diffdisk size is 0. So everything looks correct.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enable trimming of the diffdisk
4 participants