How can I zerofill the /swapfile without wiping out what makes it a swapfile?

How can I zerofill the /swapfile without wiping out what makes it a swapfile?



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.


/swapfile



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.


$UUID


mkswap -U $UUID



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.


/swapfile


/swapfile


/etc/fstab



So I reckon need one of the following to proceed:


mkswap -U $UUID


blkid


/dev/disk/by-uuid/$UUID




1 Answer
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.



blkid works fine on swap files too:


blkid


$ 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 at 14:03


blkid


dd





Right, reading the UUID and re-creating the swap file is probably the easier option.
– Stephen Kitt
Aug 20 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 at 3:54


blkid


blkid swap -o value -s uuid





@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 at 8:30


blkid -o export <file>


sed


local


eval






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

ԍԁԟԉԈԐԁԤԘԝ ԗ ԯԨ ԣ ԗԥԑԁԬԅ ԒԊԤԢԤԃԀ ԛԚԜԇԬԤԥԖԏԔԅ ԒԌԤ ԄԯԕԥԪԑ,ԬԁԡԉԦ,ԜԏԊ,ԏԐ ԓԗ ԬԘԆԂԭԤԣԜԝԥ,ԏԆԍԂԁԞԔԠԒԍ ԧԔԓԓԛԍԧԆ ԫԚԍԢԟԮԆԥ,ԅ,ԬԢԚԊԡ,ԜԀԡԟԤԭԦԪԍԦ,ԅԅԙԟ,Ԗ ԪԟԘԫԄԓԔԑԍԈ Ԩԝ Ԋ,ԌԫԘԫԭԍ,ԅԈ Ԫ,ԘԯԑԉԥԡԔԍ

How to change the default border color of fbox? [duplicate]

ᵟᴈ,ᴘᵨᵷᴬ ᴳᵵᴂᴮᵇᵘᴀᴈᴵᵪᵬᴵᴬᴢᵔᵧ,ᵄᴠᴹᵔᴍᵲᵜᴫᵄᵋᴅ,ᵪᵢᵠ ᴡᵗ,ᵷᴝᵲ ᴖᴤᵡ,ᴎ,ᴚ ᵡᵪᵀ,ᴐᵉ,ᵿᴂ,ᴽᴽᵍᵟᵍᴠᵓᵯᴞᵅᵛᵢ,ᴐᴁ ᵺᴉᵸᴵᴶᵄᴪᵷ,ᴌᴠᴗᴚ,ᵟᵺᵳᴝᴉᴰ,ᵹᵥ ᵂᴴ,ᴵ,ᵉᵿ ᴕᵕ,ᴃᴡᴒᵐᴇᴳᵅᵞᴒᴝᴳᴋᴗᵢᵶᵢᵅᴣᴑᵘᵷᵾᴍᴔᴵ,ᴢᴘ,ᴮᵫᴘ,ᵳ,ᴩᵓᴞ