<div dir="auto"><div>Hey Martin, wow thanks for the detailed info!<div dir="auto"><br></div><div dir="auto">I'll definitely look into this, but I kind of figured it out, I believe.</div><div dir="auto"><br></div><div dir="auto">So, I keep the images in the read-only squashfs, boot the live system and use snapshot writeback:</div><div dir="auto">virsh snapshot-create-as with options --disk-only --diskspec</div><div dir="auto"><br></div><div dir="auto">This pretty much fixes my problem, and only disk writes occur in tmpfs, instead of the entire image being copied.</div><div dir="auto"><br></div><div dir="auto">BUT, now I have to reduce the disk space within the  VM to almost 0 to save space. </div><div dir="auto"><br></div><div dir="auto">I'm reading up on qemu-img convert and virt-sparsify to accomplish this, but I will also try your method now, it seems more professional.</div><div dir="auto"><br></div><div dir="auto">Thank you for your amazing input and patience, I love this community. Sharing this information is the literal definition of freedom IMO, thank you all!</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Nov 25, 2021, 11:40 AM Martin Kletzander <<a href="mailto:mkletzan@redhat.com" target="_blank" rel="noreferrer">mkletzan@redhat.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On Wed, Nov 24, 2021 at 06:21:22PM +0100, Elias Mobery wrote:<br>
>Hey Martin & Michal, thank you both for the replies!<br>
><br>
>Yes, sorry I messed up that seclabel entry, thanks. I fixed it and there is<br>
>no more error, but the images are still copied unfortunately.<br>
><br>
>@Michal yes, I have been reading the Debian Live Manual like a contract,<br>
>after messing with qemu.conf and permissions for days, I think its<br>
>something in the live build and not libvirt.<br>
><br>
>@Martin yeah both virsh start and virt-manager cause the images to be<br>
>copied to /run..... first.<br>
><br>
>Yes, when I boot the Debian Live ISO, plug in the USB (mounted rw), copy<br>
>the images to the live system from the USB and click run in virt-manager,<br>
>the VM starts instantly and no copies are made in /run....<br>
><br>
>I use ISO loopback and toram to load the system directly into ram via tmpfs<br>
>mount. That's my grub.cfg<br>
><br>
>menuentry "debian-live.iso" { set iso="/ISO/debian-live.iso" set<br>
>root=(hd0,5) loopback loop $iso linux (loop)/live/vmlinuz-4.19.0-6-amd64<br>
>boot=live components toram findiso=$iso initrd<br>
>(loop)/live/initrd.img-4.19.0-6-amd64 }<br>
><br>
>So, the squashfs is mounted read-only in<br>
>/run/live/rootfs/filesystem.squashfs and its /dev/loop1<br>
><br>
>/proc/mounts says:<br>
><br>
>tmpfs /run/live/overlay rw<br>
>overlay rw  lowerdir=/run/live/rootfs/filesystem.squashfs<br>
>upperdir=/run/live/overlay/rw  (same place where the images are copied)<br>
><br>
<br>
I do not think this is relevant, but sometimes overlayfs wants to have a<br>
workdir as well.<br>
<br>
>So you think that because the images are part of the squashfs which is<br>
>read-only, they are first copied to /run/live/overlay/rw... to be written<br>
>to?<br>
><br>
<br>
Are they really copied?  I can't think of many more things why this<br>
would happen.  Maybe some locking code does that in the backend (both<br>
libvirt and qemu lock parts of the disks).<br>
<br>
>I always thought that tmpfs is mounted on top of squash, so it's as if the<br>
>squashfs were writable, without having to literally copy between the two<br>
>filesystems.<br>
><br>
<br>
It is not mounted over, if you mount over something you lose the<br>
lowerdir, that's why it needs to be done with overlayfs which does the<br>
copy-on-write.<br>
<br>
That leads me to another idea.  Since you want copy-on-write for the<br>
images, I presume, the image will get copied anyway once the guest<br>
writes even a single byte to the image.  Unfortunately overlayfs is not<br>
the best way for this scenario due to the granularity it offers<br>
(per-file, not per-block or anything smaller).  What I think you<br>
actually want in this case is to have a qcow image with the original<br>
image used as backing.  You can create those with libvirt or even<br>
without it, just using qemu-img, for example like this:<br>
<br>
   qemu-img create -b /path/to/non-modified.image -f qcow2 -F<br>
   format_of_backing_file /path/to/new-image.qcow2<br>
<br>
For more information please read the qemu-img manual page to make sure<br>
you use it properly, I just wrote the above from the top of my head, not<br>
tested it.  The backing file path can also be relative.<br>
<br>
If you specify the new image for the machines then the granularity<br>
changes to way smaller blocks than the whole file making it occupy<br>
less space in case of changes.<br>
<br>
>Also I made the same build with Virtualbox and there is no copying of the<br>
>images to /run/live/overlay.<br>
><br>
>I will do some serious research into squashfs and tmpfs rn.<br>
><br>
<br>
If you want to figure out when it happens I would suggest enabling debug<br>
logs for libvirt, starting the qemu VM with --paused, but maybe it will<br>
need some more debugging on lower layers.  Anyway I really believe the<br>
approach suggested above is what will lead to better results for you.<br>
<br>
Have a nice day<br>
Martin<br>
<br>
><br>
><br>
><br>
>On Wed, Nov 24, 2021, 5:10 PM Martin Kletzander <<a href="mailto:mkletzan@redhat.com" rel="noreferrer noreferrer" target="_blank">mkletzan@redhat.com</a>> wrote:<br>
><br>
>> On Wed, Nov 24, 2021 at 04:01:34PM +0100, Elias Mobery wrote:<br>
>> >Hello Michal, thank you for the reply!<br>
>> ><br>
>> >I've carefully tested everything you suggested, thanks.<br>
>> ><br>
>> >I set dynamic_ownership=0 and use these hooks during the live build for<br>
>> >permissions. (I googled a lot, and apparently libvirt needs the images to<br>
>> >be executable too)<br>
>> ><br>
>> >chown -R libvirt-qemu:kvm  /var/lib/libvirt/images<br>
>> >chmod -R g+rwx /var/lib/libvirt/images<br>
>> ><br>
>><br>
>> I do not see why would the images need to be executable, you might need<br>
>> an executable bit set on the directory so that your and qemu user can<br>
>> browse it.<br>
>><br>
>> >Booting the live debian iso everything works in virt-manager, but again,<br>
>> >after clicking "run", a copy of the vm image is created in<br>
>> >/run/live/overlay/rw/var/lib/libvirt/images and only then does the VM<br>
>> start.<br>
>> ><br>
>><br>
>> Does that happen when you try to run it with virsh instead of<br>
>> virt-manager?  Are you connected to the system daemon?<br>
>><br>
>> >Either it's still being chowned or chmodded somehow, or it's something<br>
>> >else, but I can't stop this copy being made.<br>
>> ><br>
>> >Interestingly, when I boot the Live debian iso and then copy the images<br>
>> >into /var/lib/libvirt/images from my USB stick, the VM starts immediately<br>
>> >without creating any copies in the /run/live.... directory. So my guess is<br>
>> >that maybe the squashfs could be the issue?<br>
>> ><br>
>><br>
>> Oh, interesting, is the USB stick filesystem mounted R/W and the live<br>
>> ISO filesystem mounted read-only?  How is the overlay mounted?<br>
>><br>
>> >Editing the XML<br>
>> ><br>
>> ><source file='/var/lib/libvirt/images/vm1.qcow2'><br>
>> >      <seclabel relabel='no'/><br>
>> >    </source><br>
>> ><br>
>> >This results in an error:<br>
>> >Unsupported Configuration:<br>
>> >Security driver model 'null' not available<br>
>> ><br>
>><br>
>> For issues like this it is most helpful to check the documentation:<br>
>><br>
>>    <a href="https://libvirt.org/formatdomain.html" rel="noreferrer noreferrer noreferrer" target="_blank">https://libvirt.org/formatdomain.html</a><br>
>><br>
>> where you can see it is just a missing attribute `model`, in this case<br>
>> model="dac".<br>
>><br>
>> >Here I tried setting security_driver=none in qemu.conf but same error<br>
>> after.<br>
>> ><br>
>> ></devices><br>
>> >    <seclabel type='none'/><br>
>> >  </domain><br>
>> ><br>
>><br>
>> Same here.<br>
>><br>
>> >This also returns an Error but I'm still googling to understand it<br>
>> properly.<br>
>> ><br>
>> >XML document failed to validate against schema<br>
>> >Unable to validate doc against /usr/share/libvirt/schemas/domain.rng<br>
>> >Invalid element relabel  for element seclabel<br>
>> >Extra element seclabel in interleave<br>
>> >Element domain failed to validate content<br>
>> ><br>
>> >Thanks again so much for your helo, I've been messing with this for weeks<br>
>> >now and it's killing me.<br>
>> ><br>
>> >On Tue, Nov 23, 2021, 9:43 PM Michal Prívozník <<a href="mailto:mprivozn@redhat.com" rel="noreferrer noreferrer" target="_blank">mprivozn@redhat.com</a>><br>
>> wrote:<br>
>> ><br>
>> >> On 11/23/21 17:25, Elias Mobery wrote:<br>
>> >> ><br>
>> >> > Hi everyone!<br>
>> >> ><br>
>> >> > I've built a Debian Live ISO with packages qemu and libvirt to run a<br>
>> VM<br>
>> >> > in the live environment.<br>
>> >> ><br>
>> >> > The guest images are placed in  /var/lib/libvirt/images and 2GB each.<br>
>> >> ><br>
>> >> > Everything works great, except for one issue.<br>
>> >> ><br>
>> >> > When starting a VM, libvirt automatically issues a chown command to<br>
>> the<br>
>> >> > images, changing ownership.<br>
>> >> ><br>
>> >> > This results in a copy of the images being created in<br>
>> >> > /run/live/overlay/rw/var/lib/libvirt/images<br>
>> >> ><br>
>> >> > I don't want these copies to be made but can't stop it.<br>
>> >> ><br>
>> >> > I've tried editing qemu.conf user/group, dynamic ownership etc.<br>
>> without<br>
>> >> > any luck.<br>
>> >> ><br>
>> >> > Is there a way to STOP libvirt from changing the ownership of these<br>
>> >> images?<br>
>> >> ><br>
>> >> ><br>
>> >><br>
>> >> Setting dynamic_ownership=0 in qemu.conf is the way to go (don't forget<br>
>> >> to restart the daemon after you made the change).<br>
>> >><br>
>> >> Alternatively, you can set <seclabel/> either for whole domain or<br>
>> >> individual disks, e.g. like this:<br>
>> >><br>
>> >>   <disk type='file' device='disk'><br>
>> >>     <driver name='qemu' type='qcow2'/><br>
>> >>     <source file='/var/lib/libvirt/images/vm1.qcow2'><br>
>> >>       <seclabel relabel='no'/><br>
>> >>     </source><br>
>> >>   </disk><br>
>> >><br>
>> >> or for whole domain:<br>
>> >><br>
>> >>     ...<br>
>> >>     </devices><br>
>> >>     <seclabel type='none'/><br>
>> >>   </domain><br>
>> >><br>
>> >> I'm not sure what your setup is, but if chown() is a problem then what<br>
>> >> if guest tries to write onto its disk? Wouldn't that create a copy in<br>
>> >> overlay?<br>
>> >><br>
>> >> Michal<br>
>> >><br>
>> >><br>
>><br>
</blockquote></div></div></div>