3

at the moment I do some tests with loop devices on Linux/Ubuntu. In a small test script I have the following commands:

#!/bin/sh
rm testfile.x
dd if=/dev/zero of=testfile.x bs=1M count=1200
losetup /dev/loop0 testfile.x
cryptsetup --batch-mode --key-file k.kf --cipher aes-xts-plain64 --key-size 512 --hash sha512 --iter-time 5000 --use-random luksFormat /dev/loop0
losetup -d /dev/loop0

Now my problem:

Sometimes the cryptsetup line fails, because the loop device disappears (as if by magic) ... during the the script run.

And I get an error:

Device /dev/loop0 is too small.

So, is there a way to prevent/disable this?

Update 1:

Here is the full output of my script (2 runs: 1 success, 1 fail):

root@test:/home/tester/test# ./x.sh
1200+0 records in
1200+0 records out
1258291200 bytes (1.3 GB) copied, 0.954065 s, 1.3 GB/s
Device /dev/loop0 is too small.
loop: can't delete device /dev/loop0: No such device or address
root@test:/home/tester/test# ./x.sh
1200+0 records in
1200+0 records out
1258291200 bytes (1.3 GB) copied, 0.953886 s, 1.3 GB/s

Update 2:

If i change the count in the dd to 100 (~105MB) there are no problems (tested 10 runs with success). So there must be any autoclear/timeout or something else.

Update 3 (solution):

#!/bin/sh                   
FILE=testfile.x                 
cryptsetup --batch-mode --key-file k.kf --cipher aes-xts-plain64 --key-size 512 --hash sha512 --iter-time 5000 --use-random luksFormat $FILE
cryptsetup luksOpen -d k.kf $FILE hellohello
cryptsetup luksClose hellohello

This one works without any errors, thank you @frostschutz.

  • The loop device disappears because you're detatching it at the end of the script. How often is sometimes? – garethTheRed Nov 25 '14 at 8:23
  • I'd suggest you to add something like sleep 10 after cryptsetup. – Danatela Nov 25 '14 at 8:29
  • well, the losetup -d /dev/loop0 also throws an error, if the device is already away, so this is not the problem. – user4013391 Nov 25 '14 at 8:45
  • sometimes means for example 10 attempts and 3 fail, or 4 or 5 ... sorry i don`t have any excat stats – user4013391 Nov 25 '14 at 8:46
2

Loop devices have some pitfalls. Many operations are not instant, for example, when you losetup -d the loop device will be kept alive until it is no longer in use. Devices can be in use even if you're not aware of it, due to background processes scanning devices for uuids and other things.

Maybe you should leave the choice of loop device to losetup by using --find --show. If one loop device is unusable for any reason, it should pick another. You'd have to adapt your script to store it in a variable then.

lodev=$(losetup --find --show textfile.x)
cryptsetup ... $lodev

cryptsetup is also entirely capable of creating its own loop device, so you could just forgo losetup altogether and have cryptsetup work with the file itself. This should be less prone to errors.

cryptsetup ... testfile.x

This also works for luksOpen and the loop device will be kept around until luksClose (again, until it's no longer in use).

| improve this answer | |
  • Thank you for the reply i will check this and do some tests – user4013391 Nov 25 '14 at 13:18

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.