8

I have a script that I run in order to optimize the disk space prior to backing up a virtual guest (really doesn't matter too much which hypervisor is used).

Modern Ubuntu versions (e.g. 18.04) seem to default to using a /swapfile rather than a partition for the purpose. That's fine, too.

However, I'd like to zerofill the swap file along with the root partition containing it.

With swap partitions this was relatively painless as there was always a way to extract the existing $UUID from the swap partition and - after zerofilling the partition - run mkswap -U $UUID to re-create said swap partition.

However, with the /swapfile I don't see how to do that. While I realize that you can't mount a /swapfile by UUID in /etc/fstab, I'd still want to retain the UUID.

So I reckon need one of the following to proceed:

  1. I need a method that can zerofill the swap file without also wiping those structures that make it a swap partition (i.e. avoid having to run mkswap -U $UUID)
  2. I need a method to retrieve the existing UUID from a swap file (as opposed to a partition where I can use anything from blkid to looking at /dev/disk/by-uuid/$UUID ...)
15
  1. This won’t clear the swap file as thoroughly as re-creating it, but if you clear it after skipping the first 4KiB (strictly speaking, the first page, which depends on your architecture), you won’t touch any of the structures which identify a swap file.

  2. blkid works fine on swap files too:

    $ mkswap swap
    Setting up swapspace version 1, size = 512 MiB (536866816 bytes)
    no label, UUID=7916b81f-1faa-4b7d-84ef-b0bf2f75dbbc
    
    $ blkid swap
    swap: UUID="7916b81f-1faa-4b7d-84ef-b0bf2f75dbbc" TYPE="swap"
    

The header format is defined in the kernel: the old format has a magic value at the end of the first page, the new format combines that with a 1KiB free area, then a number of fields (version, size, bad pages, UUID, label) which all fit comfortably inside the first page.

  • Wow, hands down the fastest answer I ever received on any StackExchange site. Thanks. I frankly never used blkid with a parameter. Guess I should be doing that rather than, for example, using dd with an offset. – 0xC0000022L Aug 20 '18 at 14:03
  • 3
    Right, reading the UUID and re-creating the swap file is probably the easier option. – Stephen Kitt Aug 20 '18 at 14:23
  • @0xC0000022L also look into the output format options of blkid. Depending on the version, you might be able to do blkid swap -o value -s uuid to extract just the UUID. – muru Aug 21 '18 at 3:54
  • @muru thanks, already did and opted for blkid -o export <file> which I pipe through sed to prepend local to each line and then eval using Bash. This way I have the values in my function as local environment variables right away. – 0xC0000022L Aug 21 '18 at 8:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.