Maximally shrink LVM PV and LUKS container / partition

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












I want to remove my swap LVM LV out of its current LUKS container, and have it unencrypted instead.



I want to avoid something like gparted to have control of every step in the process.



  1. After doing lvremove on the swap LV and pvmove to get contiguous PV extents, how do I work out the minimum size to pass to pvresize?


  2. Given the now shrunken PV, how do I work out exactly how many sectors or MiB the LUKS container should contain, so I can shrink the LUKS partition to match?


  3. Am I right in thinking that a LUKS container doesn't have a length, and is shrunk by simply shrinking the partition?


  4. Is there a way of checking at the LVM level that the final extents of the PV are still accessible, ie, that they are indeed inside the shrunken partition's addressable sectors?







share|improve this question





















  • Doesn't pvresize use the smallest possible size by default?
    – ajeh
    Aug 3 at 18:30
















up vote
0
down vote

favorite












I want to remove my swap LVM LV out of its current LUKS container, and have it unencrypted instead.



I want to avoid something like gparted to have control of every step in the process.



  1. After doing lvremove on the swap LV and pvmove to get contiguous PV extents, how do I work out the minimum size to pass to pvresize?


  2. Given the now shrunken PV, how do I work out exactly how many sectors or MiB the LUKS container should contain, so I can shrink the LUKS partition to match?


  3. Am I right in thinking that a LUKS container doesn't have a length, and is shrunk by simply shrinking the partition?


  4. Is there a way of checking at the LVM level that the final extents of the PV are still accessible, ie, that they are indeed inside the shrunken partition's addressable sectors?







share|improve this question





















  • Doesn't pvresize use the smallest possible size by default?
    – ajeh
    Aug 3 at 18:30












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I want to remove my swap LVM LV out of its current LUKS container, and have it unencrypted instead.



I want to avoid something like gparted to have control of every step in the process.



  1. After doing lvremove on the swap LV and pvmove to get contiguous PV extents, how do I work out the minimum size to pass to pvresize?


  2. Given the now shrunken PV, how do I work out exactly how many sectors or MiB the LUKS container should contain, so I can shrink the LUKS partition to match?


  3. Am I right in thinking that a LUKS container doesn't have a length, and is shrunk by simply shrinking the partition?


  4. Is there a way of checking at the LVM level that the final extents of the PV are still accessible, ie, that they are indeed inside the shrunken partition's addressable sectors?







share|improve this question













I want to remove my swap LVM LV out of its current LUKS container, and have it unencrypted instead.



I want to avoid something like gparted to have control of every step in the process.



  1. After doing lvremove on the swap LV and pvmove to get contiguous PV extents, how do I work out the minimum size to pass to pvresize?


  2. Given the now shrunken PV, how do I work out exactly how many sectors or MiB the LUKS container should contain, so I can shrink the LUKS partition to match?


  3. Am I right in thinking that a LUKS container doesn't have a length, and is shrunk by simply shrinking the partition?


  4. Is there a way of checking at the LVM level that the final extents of the PV are still accessible, ie, that they are indeed inside the shrunken partition's addressable sectors?









share|improve this question












share|improve this question




share|improve this question








edited Aug 3 at 16:22
























asked Aug 3 at 16:07









Tom Hale

5,61712370




5,61712370











  • Doesn't pvresize use the smallest possible size by default?
    – ajeh
    Aug 3 at 18:30
















  • Doesn't pvresize use the smallest possible size by default?
    – ajeh
    Aug 3 at 18:30















Doesn't pvresize use the smallest possible size by default?
– ajeh
Aug 3 at 18:30




Doesn't pvresize use the smallest possible size by default?
– ajeh
Aug 3 at 18:30










2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










1.)



From the man page:




pvresize will refuse to shrink PhysicalVolume if it has allocated extents after where its new end would be.




So you could just do this by trial & error. Actually pvresize will tell you part of it:



/dev/dm-7: cannot resize to 17564 extents as 18620 are allocated.


To determine the exact size, you need to know the PE size (e.g. 4MiB) and 1st PE offset (e.g. 1MiB). And of course, the number of the last allocated extent.



pvs -o pv_name,pe_start,vg_extent_size,seg_pe_ranges


So the total size could for example be 1MiB (1st PE) + 18620 * 4MiB (PE size).



2.)



You need to know the LUKS header size / data offset. Usually this is 4096 sectors, i.e. 2MiB. Check with cryptsetup luksDump, Payload offset.



So your new partition size is the LUKS Payload offset plus whatever size you resized the PV itself to.



3.)



Yes and no. LUKS doesn't keep the size in its metadata, so if you are going to close the LUKS container, or even reboot, then yes, it will just use the size of the block device itself.



But for an online resize, you would need to set it with cryptsetup resize, to whatever size you used in pvresize.



4.)



I sometimes do this by passing the offending device in read-only mode to a qemu/KVM instance, running some Linux rescue system. It's hard to check on the host (with, say, a read-only loop device) because LVM doesn't like to see duplicate PV UUIDs, or VG/LV names. LVM will refuse to activate, should the PV turn out to be smaller than expected.






share|improve this answer























  • Excellent, thank you. Note for self and others: cryptsetup resize --size takes 512-byte sectors, so multiply PV MBs by 2048.
    – Tom Hale
    Aug 4 at 3:43










  • Wouldn't it be best practice to use pvresize --setphysicalvolumesize <smaller-partition-size>M? (The units are always powers of 2, regardless of capitalisation). Would you consider adding this to your answer?
    – Tom Hale
    Aug 4 at 4:10











  • Always use -v with cryptsetup, or be sure to check its exit status.
    – Tom Hale
    Aug 4 at 4:14










  • Get PE size from: sudo pvdisplay /dev/mapper/<NAME>
    – Tom Hale
    yesterday


















up vote
0
down vote













Consider pvshrink which does most of the calculations in frostschutz's answer, and has a --test mode which doesn't make any changes.






share|improve this answer





















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f460374%2fmaximally-shrink-lvm-pv-and-luks-container-partition%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    1.)



    From the man page:




    pvresize will refuse to shrink PhysicalVolume if it has allocated extents after where its new end would be.




    So you could just do this by trial & error. Actually pvresize will tell you part of it:



    /dev/dm-7: cannot resize to 17564 extents as 18620 are allocated.


    To determine the exact size, you need to know the PE size (e.g. 4MiB) and 1st PE offset (e.g. 1MiB). And of course, the number of the last allocated extent.



    pvs -o pv_name,pe_start,vg_extent_size,seg_pe_ranges


    So the total size could for example be 1MiB (1st PE) + 18620 * 4MiB (PE size).



    2.)



    You need to know the LUKS header size / data offset. Usually this is 4096 sectors, i.e. 2MiB. Check with cryptsetup luksDump, Payload offset.



    So your new partition size is the LUKS Payload offset plus whatever size you resized the PV itself to.



    3.)



    Yes and no. LUKS doesn't keep the size in its metadata, so if you are going to close the LUKS container, or even reboot, then yes, it will just use the size of the block device itself.



    But for an online resize, you would need to set it with cryptsetup resize, to whatever size you used in pvresize.



    4.)



    I sometimes do this by passing the offending device in read-only mode to a qemu/KVM instance, running some Linux rescue system. It's hard to check on the host (with, say, a read-only loop device) because LVM doesn't like to see duplicate PV UUIDs, or VG/LV names. LVM will refuse to activate, should the PV turn out to be smaller than expected.






    share|improve this answer























    • Excellent, thank you. Note for self and others: cryptsetup resize --size takes 512-byte sectors, so multiply PV MBs by 2048.
      – Tom Hale
      Aug 4 at 3:43










    • Wouldn't it be best practice to use pvresize --setphysicalvolumesize <smaller-partition-size>M? (The units are always powers of 2, regardless of capitalisation). Would you consider adding this to your answer?
      – Tom Hale
      Aug 4 at 4:10











    • Always use -v with cryptsetup, or be sure to check its exit status.
      – Tom Hale
      Aug 4 at 4:14










    • Get PE size from: sudo pvdisplay /dev/mapper/<NAME>
      – Tom Hale
      yesterday















    up vote
    1
    down vote



    accepted










    1.)



    From the man page:




    pvresize will refuse to shrink PhysicalVolume if it has allocated extents after where its new end would be.




    So you could just do this by trial & error. Actually pvresize will tell you part of it:



    /dev/dm-7: cannot resize to 17564 extents as 18620 are allocated.


    To determine the exact size, you need to know the PE size (e.g. 4MiB) and 1st PE offset (e.g. 1MiB). And of course, the number of the last allocated extent.



    pvs -o pv_name,pe_start,vg_extent_size,seg_pe_ranges


    So the total size could for example be 1MiB (1st PE) + 18620 * 4MiB (PE size).



    2.)



    You need to know the LUKS header size / data offset. Usually this is 4096 sectors, i.e. 2MiB. Check with cryptsetup luksDump, Payload offset.



    So your new partition size is the LUKS Payload offset plus whatever size you resized the PV itself to.



    3.)



    Yes and no. LUKS doesn't keep the size in its metadata, so if you are going to close the LUKS container, or even reboot, then yes, it will just use the size of the block device itself.



    But for an online resize, you would need to set it with cryptsetup resize, to whatever size you used in pvresize.



    4.)



    I sometimes do this by passing the offending device in read-only mode to a qemu/KVM instance, running some Linux rescue system. It's hard to check on the host (with, say, a read-only loop device) because LVM doesn't like to see duplicate PV UUIDs, or VG/LV names. LVM will refuse to activate, should the PV turn out to be smaller than expected.






    share|improve this answer























    • Excellent, thank you. Note for self and others: cryptsetup resize --size takes 512-byte sectors, so multiply PV MBs by 2048.
      – Tom Hale
      Aug 4 at 3:43










    • Wouldn't it be best practice to use pvresize --setphysicalvolumesize <smaller-partition-size>M? (The units are always powers of 2, regardless of capitalisation). Would you consider adding this to your answer?
      – Tom Hale
      Aug 4 at 4:10











    • Always use -v with cryptsetup, or be sure to check its exit status.
      – Tom Hale
      Aug 4 at 4:14










    • Get PE size from: sudo pvdisplay /dev/mapper/<NAME>
      – Tom Hale
      yesterday













    up vote
    1
    down vote



    accepted







    up vote
    1
    down vote



    accepted






    1.)



    From the man page:




    pvresize will refuse to shrink PhysicalVolume if it has allocated extents after where its new end would be.




    So you could just do this by trial & error. Actually pvresize will tell you part of it:



    /dev/dm-7: cannot resize to 17564 extents as 18620 are allocated.


    To determine the exact size, you need to know the PE size (e.g. 4MiB) and 1st PE offset (e.g. 1MiB). And of course, the number of the last allocated extent.



    pvs -o pv_name,pe_start,vg_extent_size,seg_pe_ranges


    So the total size could for example be 1MiB (1st PE) + 18620 * 4MiB (PE size).



    2.)



    You need to know the LUKS header size / data offset. Usually this is 4096 sectors, i.e. 2MiB. Check with cryptsetup luksDump, Payload offset.



    So your new partition size is the LUKS Payload offset plus whatever size you resized the PV itself to.



    3.)



    Yes and no. LUKS doesn't keep the size in its metadata, so if you are going to close the LUKS container, or even reboot, then yes, it will just use the size of the block device itself.



    But for an online resize, you would need to set it with cryptsetup resize, to whatever size you used in pvresize.



    4.)



    I sometimes do this by passing the offending device in read-only mode to a qemu/KVM instance, running some Linux rescue system. It's hard to check on the host (with, say, a read-only loop device) because LVM doesn't like to see duplicate PV UUIDs, or VG/LV names. LVM will refuse to activate, should the PV turn out to be smaller than expected.






    share|improve this answer















    1.)



    From the man page:




    pvresize will refuse to shrink PhysicalVolume if it has allocated extents after where its new end would be.




    So you could just do this by trial & error. Actually pvresize will tell you part of it:



    /dev/dm-7: cannot resize to 17564 extents as 18620 are allocated.


    To determine the exact size, you need to know the PE size (e.g. 4MiB) and 1st PE offset (e.g. 1MiB). And of course, the number of the last allocated extent.



    pvs -o pv_name,pe_start,vg_extent_size,seg_pe_ranges


    So the total size could for example be 1MiB (1st PE) + 18620 * 4MiB (PE size).



    2.)



    You need to know the LUKS header size / data offset. Usually this is 4096 sectors, i.e. 2MiB. Check with cryptsetup luksDump, Payload offset.



    So your new partition size is the LUKS Payload offset plus whatever size you resized the PV itself to.



    3.)



    Yes and no. LUKS doesn't keep the size in its metadata, so if you are going to close the LUKS container, or even reboot, then yes, it will just use the size of the block device itself.



    But for an online resize, you would need to set it with cryptsetup resize, to whatever size you used in pvresize.



    4.)



    I sometimes do this by passing the offending device in read-only mode to a qemu/KVM instance, running some Linux rescue system. It's hard to check on the host (with, say, a read-only loop device) because LVM doesn't like to see duplicate PV UUIDs, or VG/LV names. LVM will refuse to activate, should the PV turn out to be smaller than expected.







    share|improve this answer















    share|improve this answer



    share|improve this answer








    edited Aug 3 at 17:20


























    answered Aug 3 at 17:14









    frostschutz

    23.6k14370




    23.6k14370











    • Excellent, thank you. Note for self and others: cryptsetup resize --size takes 512-byte sectors, so multiply PV MBs by 2048.
      – Tom Hale
      Aug 4 at 3:43










    • Wouldn't it be best practice to use pvresize --setphysicalvolumesize <smaller-partition-size>M? (The units are always powers of 2, regardless of capitalisation). Would you consider adding this to your answer?
      – Tom Hale
      Aug 4 at 4:10











    • Always use -v with cryptsetup, or be sure to check its exit status.
      – Tom Hale
      Aug 4 at 4:14










    • Get PE size from: sudo pvdisplay /dev/mapper/<NAME>
      – Tom Hale
      yesterday

















    • Excellent, thank you. Note for self and others: cryptsetup resize --size takes 512-byte sectors, so multiply PV MBs by 2048.
      – Tom Hale
      Aug 4 at 3:43










    • Wouldn't it be best practice to use pvresize --setphysicalvolumesize <smaller-partition-size>M? (The units are always powers of 2, regardless of capitalisation). Would you consider adding this to your answer?
      – Tom Hale
      Aug 4 at 4:10











    • Always use -v with cryptsetup, or be sure to check its exit status.
      – Tom Hale
      Aug 4 at 4:14










    • Get PE size from: sudo pvdisplay /dev/mapper/<NAME>
      – Tom Hale
      yesterday
















    Excellent, thank you. Note for self and others: cryptsetup resize --size takes 512-byte sectors, so multiply PV MBs by 2048.
    – Tom Hale
    Aug 4 at 3:43




    Excellent, thank you. Note for self and others: cryptsetup resize --size takes 512-byte sectors, so multiply PV MBs by 2048.
    – Tom Hale
    Aug 4 at 3:43












    Wouldn't it be best practice to use pvresize --setphysicalvolumesize <smaller-partition-size>M? (The units are always powers of 2, regardless of capitalisation). Would you consider adding this to your answer?
    – Tom Hale
    Aug 4 at 4:10





    Wouldn't it be best practice to use pvresize --setphysicalvolumesize <smaller-partition-size>M? (The units are always powers of 2, regardless of capitalisation). Would you consider adding this to your answer?
    – Tom Hale
    Aug 4 at 4:10













    Always use -v with cryptsetup, or be sure to check its exit status.
    – Tom Hale
    Aug 4 at 4:14




    Always use -v with cryptsetup, or be sure to check its exit status.
    – Tom Hale
    Aug 4 at 4:14












    Get PE size from: sudo pvdisplay /dev/mapper/<NAME>
    – Tom Hale
    yesterday





    Get PE size from: sudo pvdisplay /dev/mapper/<NAME>
    – Tom Hale
    yesterday













    up vote
    0
    down vote













    Consider pvshrink which does most of the calculations in frostschutz's answer, and has a --test mode which doesn't make any changes.






    share|improve this answer

























      up vote
      0
      down vote













      Consider pvshrink which does most of the calculations in frostschutz's answer, and has a --test mode which doesn't make any changes.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        Consider pvshrink which does most of the calculations in frostschutz's answer, and has a --test mode which doesn't make any changes.






        share|improve this answer













        Consider pvshrink which does most of the calculations in frostschutz's answer, and has a --test mode which doesn't make any changes.







        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered Aug 4 at 4:30









        Tom Hale

        5,61712370




        5,61712370






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f460374%2fmaximally-shrink-lvm-pv-and-luks-container-partition%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay