Bug 1613746

Summary: vol-resize with --shrink can't shrink encrypted volume
Product: Red Hat Enterprise Linux 7 Reporter: Meina Li <meili>
Component: libvirtAssignee: John Ferlan <jferlan>
Status: CLOSED ERRATA QA Contact: Meina Li <meili>
Severity: unspecified Docs Contact:
Priority: unspecified    
Version: 7.6CC: dyuan, hhan, jdenemar, lmen, mtessun, tburke, xuzhang, yisun
Target Milestone: rcKeywords: Regression
Target Release: ---   
Hardware: x86_64   
OS: Linux   
Whiteboard:
Fixed In Version: libvirt-4.5.0-8.el7 Doc Type: No Doc Update
Doc Text:
undefined
Story Points: ---
Clone Of:
: 1622534 (view as bug list) Environment:
Last Closed: 2018-10-30 09:58:28 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Bug Depends On:    
Bug Blocks: 1622534    

Description Meina Li 2018-08-08 09:34:12 UTC
Description of problem:
vol-resize with --shrink doesn't shrink encrypted volume

Version-Release number of selected component (if applicable):
libvirt-4.5.0-6.el7.x86_64
qemu-kvm-rhev-2.12.0-9.el7.x86_64

How reproducible:
100%

Steps to Reproduce:
1.Prepare an encrypted volume encrypt1.img.

# virsh secret-list 
 UUID                                  Usage
--------------------------------------------------------------------------------
 33406385-1633-425a-a7e8-a02243603283  volume 

# qemu-img info /var/lib/libvirt/images/encrypt1.img 
image: /var/lib/libvirt/images/encrypt1.img
file format: luks
virtual size: 200M (209715200 bytes)
disk size: 256K
encrypted: yes
Format specific information:
    ivgen alg: plain64
    hash alg: sha256
    cipher alg: aes-256
    uuid: daa9aeff-8230-4e76-ba0b-660c2a0f172f
...

2. Resize the encrypted volume.
# virsh vol-resize /var/lib/libvirt/images/encrypt1.img 100M --shrink
error: Failed to change size of volume 'encrypt1.img' to 100M
error: internal error: Child process (/usr/bin/qemu-img resize --object secret,id=encrypt1.img_encrypt0,file=/var/run/libvirt/storage/default.encrypt1.img.secret.AoZ4XV --image-opts driver=luks,key-secret=encrypt1.img_encrypt0,file.filename=/var/lib/libvirt/images/encrypt1.img 104857600) unexpected exit status 1: 2018-08-08 09:15:46.693+0000: 8719: debug : virFileClose:111 : Closed fd 25
2018-08-08 09:15:46.693+0000: 8719: debug : virFileClose:111 : Closed fd 27
2018-08-08 09:15:46.693+0000: 8719: debug : virFileClose:111 : Closed fd 22
qemu-img: warning: Shrinking an image will delete all data beyond the shrunken image's end. Before performing such an operation, make sure there is no important data there.
qemu-img: Use the --shrink option to perform a shrink operation.

Actual results:
vol-resize with --shrink doesn't shrink encrypted volume

Expected results:
# virsh vol-resize /var/lib/libvirt/images/encrypt1.img 100M --shrink
Size of volume 'encrypt1.img' successfully changed to 100M

Additional info:
Resize luks encrypted volume in libvirt-3.9.0-14.el7_5.7.x86_64 successfully.

Comment 4 John Ferlan 2018-08-17 20:02:17 UTC
Posted a patch upstream:

https://www.redhat.com/archives/libvir-list/2018-August/msg01135.html

Testing:

# virsh vol-info demo.luks default
Name:           demo.luks
Type:           file
Capacity:       100.00 MiB
Allocation:     256.00 KiB

# virsh vol-dumpxml demo.luks default
<volume type='file'>
  <name>demo.luks</name>
  <key>/home/vm-images/demo.luks</key>
...
    <encryption format='luks'>
      <secret type='passphrase' uuid='6fd3f62d-9fe7-4a4d-a869-7acd6376d8ea'/>
    </encryption>
  </target>
</volume>

# virsh vol-info bzvol.qcow2 default
Name:           bzvol.qcow2
Type:           file
Capacity:       100.00 MiB
Allocation:     260.00 KiB

# virsh vol-dumpxml bzvol.qcow2 default
<volume type='file'>
  <name>bzvol.qcow2</name>
  <key>/home/vm-images/bzvol.qcow2</key>
...
  <target>
...
    <format type='qcow2'/>
...


# virsh vol-resize demo.luks 50M default --shrink
Size of volume 'demo.luks' successfully changed to 50M

# virsh vol-resize demo.luks 100M default
Size of volume 'demo.luks' successfully changed to 100M

# virsh vol-resize bzvol.qcow2 50M default --shrink
Size of volume 'bzvol.qcow2' successfully changed to 50M

# virsh vol-resize bzvol.qcow2 100M default
Size of volume 'bzvol.qcow2' successfully changed to 100M

Comment 5 John Ferlan 2018-08-24 13:16:47 UTC
Patch pushed upstream:

commit b04d1b6a35f9fb826f7bb83fdac364a127ec75ca
Author: John Ferlan <jferlan>
Date:   Fri Aug 17 15:53:29 2018 -0400

    storage: Add --shrink to qemu-img command when shrinking vol
    
    ...
    
    When shrinking the capacity of a qcow2 or luks volume using
    the qemu-img program, the --shrink qualifier must be added.
    
    Signed-off-by: John Ferlan <jferlan>
    Reviewed-by: Daniel P. Berrangé <berrange>

...

$ git describe b04d1b6a35f9fb826f7bb83fdac364a127ec75ca
v4.6.0-304-gb04d1b6a35
$

Comment 10 Meina Li 2018-09-07 06:18:15 UTC
Verified on libvirt-4.5.0-9.el7.x86_64.

Test steps:
1.Prepare an encrypted volume and a qcow2 volume.
# virsh vol-dumpxml encrypt.luks default
<volume type='file'>
  <name>encrypt.luks</name>
  <key>/var/lib/libvirt/images/encrypt.luks</key>
  ...
  <target>
    <path>/var/lib/libvirt/images/encrypt.luks</path>
    ...
    <encryption format='luks'>
      <secret type='passphrase' uuid='4d55f845-32f6-49e0-b5c8-f062f89955d4'/>
    </encryption>
  </target>
</volume>

# virsh vol-list default
 name               path
------------------------------------------------------------------------------
 encrypt.luks         /var/lib/libvirt/images/encrypt.luks
 lmn.qcow2            /var/lib/libvirt/images/lmn.qcow2
 noencrypt.qcow2      /var/lib/libvirt/images/noencrypt.qcow2

2. Check the capability of two volumes.
# virsh vol-info encrypt.luks default
Name:       encrypt.luks
Type:       file
Capability:       500.00 MiB
Allocation:       256.00 KiB
# virsh vol-info noencrypt.qcow2 default
Name:       noencrypt.qcow2
Type:       file
Capability:       500.00 MiB
Allocation:       196.00 KiB

3. Resize the two volumes.
# virsh vol-resize /var/lib/libvirt/images/encrypt.luks 300M --shrink
Size of volume 'encrypt.luks' successfully changed to 300M
# virsh vol-info encrypt.luks default
Name:       encrypt.luks
Type:       file
Capability:       300.00 MiB
Allocation:       256.00 KiB

# virsh vol-resize noencrypt.qcow2 300M default --shrink
Size of volume noencrypt.qcow2 successfully changed to 300M
# virsh vol-info noencrypt.qcow2 default
Name:       noencrypt.qcow2
Type:       file
Capability:       300.00 MiB
Allocation:       260.00 KiB

Comment 12 errata-xmlrpc 2018-10-30 09:58:28 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory, and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

https://access.redhat.com/errata/RHSA-2018:3113