TRIM with linux guest for minimal img file


Recommended Posts

Hi all,

I've found this and subsequently this thread, but nothing about linux guests.

I would like to have my img files as small as possible (of course), so I tried to set it up as an SSD similar to Windows.
This seems to work now, but I'm not very experienced with this stuff so this is a request for improvements as well as a how-to for others with similar little experience.

When creating the VM, I chose:
Primary vDisk bus: SCSI
Primary vDisk type: qcow2 (if I choose raw here the img file will be full size even before first boot)
Manually edited the VM XML file and added discard='unmap'
 

    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2' cache='writeback' discard='unmap'/>
      <source file='/mnt/user/domains/Geraldine/vdisk1.img' index='2'/>
      <backingStore/>
      <target dev='hdc' bus='scsi'/>
      <serial>vdisk1</serial>
      <boot order='1'/>
      <alias name='scsi0-0-0-2'/>
      <address type='drive' controller='0' bus='0' target='0' unit='2'/>
    </disk>


Now I installed ubuntu server minimal. This tells me to check lsblk --discard to see if my (virtual) drive supports trim, so I did:
 

phil@geraldine:~$ lsblk --discard
NAME                      DISC-ALN DISC-GRAN DISC-MAX DISC-ZERO
sda                              0        4K       1G         0
├─sda1                           0        4K       1G         0
├─sda2                           0        4K       1G         0
└─sda3                           0        4K       1G         0
  └─ubuntu--vg-ubuntu--lv        0        4K       1G         0
sr0                              0        0B       0B         0


So the DISC-GRAN and DISC-MAX beeing non-zero tells me it does.

Also, lvm.conf has issue_discards = 1
 

phil@geraldine:~$ fgrep issue_discards /etc/lvm/lvm.conf
        # Configuration option devices/issue_discards.
        issue_discards = 1


But my /etc/fstab knows nothing about discard:
 

phil@geraldine:~$ fgrep discard /etc/fstab
phil@geraldine:~$

 

so i added it manually
 

phil@geraldine:~$ fgrep discard /etc/fstab
/dev/disk/by-id/dm-uuid-LVM-kcbZKFWiEPoidmRd4HaCFzVI2y7GhD9QHQdKDxrBf5cDOvyjd4PTF0iX0wfApBXL / ext4 defaults,discard 0 1
/dev/disk/by-uuid/84b65076-9b7c-4efe-8ce8-06d5688c737f /boot ext4 defaults,discard 0 1
/dev/disk/by-uuid/0E6B-E392 /boot/efi vfat defaults,discard 0 1
phil@geraldine:~$

 

also did update-initramfs -c -k $(uname -r) because I wasn't sure if I had to

and rebooted. Now the discs are mounted with the discard option.
 

phil@geraldine:~$ mount | grep discard
/dev/mapper/ubuntu--vg-ubuntu--lv on / type ext4 (rw,relatime,discard)
/dev/sda2 on /boot type ext4 (rw,relatime,discard)
/dev/sda1 on /boot/efi type vfat (rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=iso8859-1,shortname=mixed,errors=remount-ro,discard)


Now hdparm still does not mention TRIM:
 

phil@geraldine:~$ sudo hdparm -I /dev/sda

/dev/sda:

ATA device, with non-removable media
Standards:
        Likely used: 1
Configuration:
        Logical         max     current
        cylinders       0       0
        heads           0       0
        sectors/track   0       0
        --
        Logical/Physical Sector size:           512 bytes
        device size with M = 1024*1024:           0 MBytes
        device size with M = 1000*1000:           0 MBytes
        cache/buffer size  = unknown
Capabilities:
        IORDY not likely
        Cannot perform double-word IO
        R/W multiple sector transfer: not supported
        DMA: not supported
        PIO: pio0


But it seems to be working:
 

phil@geraldine:~$ sudo fstrim -A -v
/boot/efi: 1 GiB (1118625792 bytes) trimmed on /dev/disk/by-uuid/0E6B-E392
/boot: 1.8 GiB (1898135552 bytes) trimmed on /dev/disk/by-uuid/84b65076-9b7c-4efe-8ce8-06d5688c737f
/: 54.7 GiB (58719956992 bytes) trimmed on /dev/disk/by-id/dm-uuid-LVM-kcbZKFWiEPoidmRd4HaCFzVI2y7GhD9QHQdKDxrBf5cDOvyjd4PTF0iX0wfApBXL


So, please feel free to comment. Thank you.

 

Link to comment

I learned that kvm does not remove the space freed by trim from the img file automatically. This can be done with the qemu-img convert command.

 

qemu-img convert -c -O qcow2 input.img output.img

 

This converts a disk image into another format - in this case, qcow2 format. Since input is qcow2 format already it is not converted, just copied - but empty space is omitted from the copy. The -c option also enables compression, which slows down access to the image but saves a lot of space.
 

So to clean up and compress your image, power down your VM, open a root console, cd into the matching directory (usually /mnt/user/<VMName>) and do the following:
 

mv vdisk1.img vdisk1.img.bak
qemu-img convert -c -O qcow2 vdisk1.img.bak vdisk1.img
chown nobody vdisk1.img
chgrp users vdisk1.img
chmod 777 vdisk1.img

 

If your VM starts up fine, you can delete the vdisk.img.bak file.

 

 

Link to comment
On 1/8/2024 at 4:24 AM, motophil said:

(if I choose raw here the img file will be full size even before first boot)

It only appears that way in one view. It's a sparse file, meaning the full capacity is shown, but only the in use sections are allocated and take up space. You can easily have multiple 250GB sparse files on a single 250GB disk, but you will run into issues when more than 250GB worth of data is actually used in any combination of the files.

Link to comment

Try these switches to the ls command

 

 ls -lsh
total 151M
65M -rwxrwxrwx 1 nobody users 20G Dec 30 15:02 vdisk1.img*
86M -rwxrwxrwx 1 nobody users 10G Dec 30 15:02 vdisk2.img*

 

The size on the left is the allocated space.

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.