On Sun, 2014-11-16 at 23:53 -0500, cprise wrote:
> I discovered this actually works without having to duplicate any root
> image files.
>
> First shut down the target templatevm and create a new appvm (I'll
> call it 'fstrim-temp') using a trusted template (and without
> networking) and start it, then:
> dom0$ qvm-block -A fstrim-temp
> dom0:/var/lib/qubes/vm-templates/fedora-20-x64/root.img
> Next, in the appvm...
> $ cd; mkdir rootimg; sudo mount /dev/xvdi rootimg
> $ sudo fstrim -m 4096 /home/user/rootimg
> $ sudo umount rootimg
> $ sudo halt
Nice. I wanted to learn how to do that and am glad it's so simple.
> I also found out you can run this from an appvm that depends on the
> target template (though I'm not sure if that is really safe).
I spent some time thinking about this because it made an interesting
puzzle. I imagine that it's safe as long as each running dependent
AppVM has booted from root.img and an empty root-cow.img file and
root.img hasn't changed since then. In that case, the AppVM's root
filesystem has evolved from root.img with blocks written by the AppVM
being written to /dev/xvdc2. So the discards that the AppVM will see
are of the blocks that were free at boot time and have never been
written by the AppVM since then. I'd think that a typical filesystem
implementation wouldn't rely on the contents of such blocks. The only
way a filesystem could break that wouldn't be totally gratuitous would
be to notice that a free block happens to contain the same data it wants
to store and reference the block without rewriting it, but my
understanding is filesystems don't do that because it's slower on
average than just writing.
If root-cow.img is nonempty when the AppVM boots, or root.img changes
later, then the AppVM's view of the filesystem may reference blocks of
root.img that are no longer referenced by the filesystem in root.img, so
trimming root.img could corrupt the AppVM's view.
If the TemplateVM is running and no dependent AppVMs are running,
trimming will cause no immediate problem but may break the invariant
that (root.img + root-cow.img) is a valid filesystem, so it will be
unsafe to start any dependent AppVMs until the TemplateVM stops and is
committed, so it's probably better not to do this.
I could imagine the Qubes VM Manager keeping track of which TemplateVMs
might need trimming, showing an icon like the "outdated" one for AppVMs,
and automatically trimming them when safe. It would be better if
dm-snapshot would just gain discard support, though there are design
issues there I haven't fully explored. I don't think either is worth
doing right now. :/
Matt