Impossible to create sparse luks2 volumes with large offsets.
Issue description
luksFormat potentially writes a bunch of zero sectors to a sparse file unnecessarily, thus causing a sparse file to lose much of its sparseness.
Steps for reproducing the issue
This should result in a file with less than a 1Gb of disk usage, but instead its 5Gb.
truncate -s 10G /tmp/test.luks.10G
cryptsetup --debug --debug-json luksFormat --type luks2 --pbkdf=pbkdf2 --pbkdf-force-iterations 1000 \
--offset $(((5<<30)/(1<<9))) /tmp/test.luks.10G
du -sh /tmp/test.luks.10G
Debug log
# Crypto backend (OpenSSL 1.1.1f 31 Mar 2020) initialized in cryptsetup library version 2.3.3.
...
# Header JSON:# {
"keyslots":{
},
"tokens":{
},
"segments":{
"0":{
"type":"crypt",
"offset":"5368709120",
"size":"dynamic",
"iv_tweak":"0",
"encryption":"aes-xts-plain64",
"sector_size":512
}
},
"digests":{
"0":{
"type":"pbkdf2",
"keyslots":[
],
"segments":[
"0"
],
"hash":"sha256",
"iterations":1000,
"salt":"C9OG6J87P0lqxJcUt+9lLKBunbUmyBdRgtaeGQ/w0pk=",
"digest":"HZIn51/CfF377xf+he/L7zKNHRrGOxM3ZRbfYLh25zM="
}
},
"config":{
"json_size":"12288",
"keyslots_size":"134217728"
}
}# Wiping LUKS areas (0x000000 - 0x140000000) with zeroes.
# Wiping keyslots area (0x008000 - 0x8008000) with random data.
I believe the issue can be resolved by changing this line in LUKS2_wipe_header_areas
to be length = get_max_offset(hdr);
. Currently the wipe area is everything up to the first segment.