Cryptsetup: Command failed with code 22: Invalid argument

After installing Ubuntu with encrypted LVM, I decided to do go ahead and encrypt the external drives the server uses with cryptsetup LUKS command. But for some reason I kept getting this error: Command failed with code 22: Invalid argument

I plan to write up a detailed guide on encrypting drives, but in the meantime I just wanted to post how to fix this error.

Using Cryptsetup

When you’re using the installer, setting up encryption doesn’t involve the command line; the GUI lets you choose your settings through its menus and then it does the rest. When you’re encrypting external drives, you have to write the commands yourself.

After you get your drives, partitions, etc. set up the way you want them and you’re ready to create your encrypted volume, all you have to do is enter this simple line:

cryptsetup luksFormat -vy

The cryptsetup program calls the luksFormat function to encrypt your device. Your device name goes after your options there. The “v” is for verbose, and the “y” is to prompt you for a passphrase. That way you’ll be able to de-/-encrypt the volume at will.

If you entered the right information, you receive this warning from cryptsetup:

WARNING!
========
This will overwrite data on /dev/mapper/enclosure-data irrevocably.

Are you sure? (Type uppercase yes):

Here’s where the trouble starts.

Command failed with code 22: Invalid argument

For a reason unknown to me at the time, I kept getting this error at this point of the encryption process:

Command failed with code 22: Invalid argument

I was driving myself crazy going over each part of the command to figure out what I had done wrong.

It wasn’t until I reread the cryptsetup prompt that I realized my mistake wasn’t in the original command; the mistake was in my response to cryptsetup‘s warning.

WARNING!
========
This will overwrite data on /dev/mapper/enclosure-data irrevocably.

Are you sure? (Type uppercase yes):

With many prompts in Ubuntu, you can just type “y” for yes and “n” for no.

Not the case with cryptsetup. The error I was getting was because I was just typing “y” at this prompt. The letter alone doesn’t cut it. To get this command to go through you have to type out “YES” in all uppercase letters.

Once you enter that correctly, the program should proceed by asking you to enter the passphrase. After you verify the passphrase and the volume is set up, you’ll get a confirmation of the process.