How to non-interactively supply a passphrase to 'dmcrypt luksFormat'?

Solution 1:

The first thing to do is to call the right command: it's cryptsetup, not dmcrypt.

cryptsetup luksFormat /dev/vda2

The second thing is that you can pass another argument to read the passphrase from a file, or from standard input (using -).

echo -n "This isn't a very secure passphrase." | cryptsetup luksFormat /dev/vda2 -

Note that the -n flag is necessary in echo to prevent a line feed from being appended to the password.

See the cryptsetup man page for other ways to pass the key material in.

Solution 2:

How to send passphrase with sudo

echo 'passphraze' | echo 'sudopass' | sudo -S cryptsetup luksOpen /dev/sda5 media -d -