Image of the glider from the Game of Life by John Conway
Skip to content

Encrypted ZFS Filesystems On Linux

This is just a quick post about getting a fully kernel-space encrypted ZFS filesystem setup with GNU/Linux, while still keeping all the benefits of what ZFS offers. Rather than using dmcrypt and LUKS, which would bypass a lot of the features ZFS brings to the table, encryptfs is our ticket. The reason this is so elegant, is because Oracle has not released the source code to ZFS after version 28. Version 32 contains the code to create native ZFS encrypted filesystems. So, we need to rely on a 3rd party utility.

First, create your ZPOOL:

# zpool create rpool raidz1 sdb sdc sdd sde sdf

Then create your ZFS filesystem:

# zfs create rpool/private

Lastly, install the ecryptfs software, and make the encrypted filesystem by mounting it, and follow the prompts:

# mount -t ecryptfs /rpool/private /rpool/private
Select key type to use for newly created files: 
 1) tspi
 2) passphrase
Selection: 2
Select cipher: 
 1) aes: blocksize = 16; min keysize = 16; max keysize = 32
 2) blowfish: blocksize = 8; min keysize = 16; max keysize = 56
 3) des3_ede: blocksize = 8; min keysize = 24; max keysize = 24
 4) twofish: blocksize = 16; min keysize = 16; max keysize = 32
 5) cast6: blocksize = 16; min keysize = 16; max keysize = 32
 6) cast5: blocksize = 8; min keysize = 5; max keysize = 16
Selection [aes]: 
Select key bytes: 
 1) 16
 2) 32
 3) 24
Selection [16]: 
Enable plaintext passthrough (y/n) [n]: 
Enable filename encryption (y/n) [n]: y
Filename Encryption Key (FNEK) Signature [53aad9b192678a8a]: 
Attempting to mount with the following options:
Mounted eCryptfs

Notice that I enabled filename encryption, as I don't want anyone getting any of my USB drives to decipher what I'm trying to hide. This will mount the encrypted filesystem "on top" of the ZFS filesystem, allowing you to keep all the COW and error correcting goodness, while keeping your data 100% safe:

# mount | grep rpool
rpool on /pool type zfs (rw,relatime,xattr)
rpool/private on /rpool/private type zfs (rw,relatime,xattr)
/rpool/private on /rpool/private type ecryptfs (rw,relatime,ecryptfs_fnek_sig...(snip))

Works like a charm.

{ 21 } Comments

  1. Loïc | August 22, 2012 at 2:06 am | Permalink

    Seems great! 🙂

    But I don’t think dedup and/or compression will continue to work as they did before, no?

  2. Aaron Toponce | August 22, 2012 at 3:24 am | Permalink

    Yes they will. Of course they will. The eCryptfs filesystem resides "on top" of ZFS. So, as far as ZFS is concerned, it's just standard, run-of-the-mill data. It doesn't know any different. So, all of the benefits ZFS brings are still there. No compromises are made. Unlike ZFS on top of LUKS.

  3. Loïc | August 22, 2012 at 8:40 am | Permalink

    Ok then, seems really great 🙂

  4. Brozer | August 24, 2012 at 2:06 pm | Permalink

    Is it really correct to say there are no compromises at all? Granted, any exceptions would be somewhat minimal, and without source access we don't know exactly how Oracle has chosen to implement encryption, BPR etc. But in principle it seems like some ZFS operations would work better if performed on the data as a step before encryption in the chain. It seems likely that dedup, to take Loïc's example, would be more effective if it was done and then the result was randomized rather then after the result was randomized. Compression would be useless (though that of course can get handled farther up) and I don't know if snapshots would be quite as efficient.

    It's true that it's mostly transparent though, but in the end I still hope the illumos team comes through with their own native implementation as well as the improvements suggested by Delphix.

  5. Aaron Toponce | August 25, 2012 at 8:29 am | Permalink

    Deduplication happens at the block level, beneath ZFS. Regardless if the data is encrypted or not, deduplication is still possible. Now, granted, encrypting very similar, yet different data, can result in highly pseudorandom data. This can make data deduplication very difficult, but that doesn't change that with version 32, it's still the same problem, as it uses 256-bit AES. Same is said for compression. So, it's not eCryptFS that is causing these issues, but the disagreement in the technologies. These are the same issues you'll face with the latest version from Oracle. Regardless, the point is that ZFS knows nothing about the data it receives, only what it can do with it after the fact.

  6. Rudd-O | October 22, 2012 at 12:03 pm | Permalink

    I don't understand what compromises you make with LUKS + ZFS. I have used that and there are no compromises. ZFS works very well with it.

  7. Jonny | October 23, 2012 at 7:31 am | Permalink

    Deduplication will work in that you can turn it on, but you wont find any duplicate data.

    Encrypting duplicate files/blocks will result in different data even when using the same key, since the Initialisation Vectors will be different.

    In ZFS they got around this by changing the way the IV was calculated when dedup is turned on (see this blog post:

    So as far as I can tell using 3rd party encryption will make dedup pointless

  8. Aaron Toponce | October 23, 2012 at 8:20 am | Permalink

    Not quite. The initialization vector is a nonce, and there is only one. It is what is responsible for kickstarting the CBC process. Further, deduplication sits underneath the encryption layer, not on top. At that level, the blocks are nothing more than zeros and ones. Now, granted, encryption algorithms are designed to mimic random data, and it is very hard to compress or deduplicate random data. With that said, it can still be done, and it's works just fine.

  9. Anonymous | February 1, 2013 at 12:09 am | Permalink

    "Now, granted, encryption algorithms are designed to mimic random data, and it is very hard to compress or deduplicate random data. With that said, it can still be done, and it’s works just fine."

    If you can compress your encrypted data, the encryption algorithm is egregiously leaking data. Same goes for deduplication - a crypto implementation that allows one to tell from ciphertext only that the plaintexts are the same is broken

  10. MN | April 6, 2013 at 1:42 pm | Permalink

    Hi Aaron,
    Can you help clarify what you mean by "using dmcrypt and LUKS, which would bypass a lot of the features ZFS brings to the table"? As I understand it, using LUKS would just change the underlying raw device to be an encrypted device. Since ZFS would then use that encrypted raw device, we would still be able to take advantage of all it's features. I probably am missing something so any clarification will help!


  11. EGL | May 16, 2013 at 6:12 am | Permalink

    Great article! But I'm very curious, as MN, which disadvantages you get when using LUKS below ZFS. As far as I can see, dedup and compression are going to work properly with LUKS as opposed to ecryptfs. What I'm not sure about is are things like 4k sectors and such...

    Thank you for clarification!

  12. Hiki | June 25, 2013 at 7:09 am | Permalink

    You can't share an ecrypfs mount over the network though, smb, nfs etc won't work (sftp does work).

  13. Michael | August 31, 2013 at 3:55 pm | Permalink

    Im very interested in the drawbacks of using LUKS underneath ZFS as well! Please enlighten us as this is somewhat of a dream setup for many linux users. Thanks in advance!

  14. crypt0s | December 28, 2013 at 3:35 am | Permalink

    "...which would bypass a lot of the features ZFS brings to the table..."

    for a ZFS mirror:
    - cpu load is doubled
    - extra attack vector (doubled data)

    ...what are the other problems to watch for?

  15. Josh Enders | February 23, 2014 at 4:00 am | Permalink

    Hey Aaron, thanks for the post and series on ZFS. After testing eCryptfs with ZFS, I have unfortunately found a major trade-off that wasn't mentioned. When filename encryption is enabled, as is suggested, there is a limit posed on pre-encrypted filenames to be less than 143 characters in length. [1] Filenames longer than this produce an encrypted name that exceeds the length a ZFS filesystem can store for a filename (255 bytes) and worse yet, results in an unencrypted file.


  16. Matt | April 12, 2014 at 12:05 am | Permalink

    Aaron, could you state which features of ZFS are bypassed if you use ZFS on top of a LUKS block device? I can think of none. However, using encryptfs on top of ZFS will prevent effective use of ZFS compression and/or deduplication.

  17. Aaron Toponce | April 12, 2014 at 3:35 pm | Permalink

    It's not that features are bypassed. It's that you can no longer guarantee file integrity. Putting LUKS between the disk and ZFS opens up the possibility that data could get corrupt between LUKS and the disk, and ZFS would not know about it. the ZFS developers spent years closing all the bit rot holes. Putting LUKS underneath ZFS opens one up.

  18. Mike DiGrazia | October 29, 2014 at 10:17 am | Permalink

    Aaron, A lot of people here are asking for clarification on how using zfs on top of block level encryption could impact data integrity. I am very interested as well. As I see it, LUKS would present a "plaintext" block level device to zfs and therefore any changes below LUKS would show up above. Granted changing a single bit on the physical storage could affect multiple nearby bits after decryption, this would still result in the violation of zfs checksum and initiate repair. There would probably be a bit more work to repair a single rotten bit but since bit rot is slow and rare I don't see this impacting performance too much, and the repair should still get done -especially in a raidz pool. I am really interested to know if I'm wrong and learn more about how this works.

  19. Aaron Toponce | December 9, 2014 at 10:50 am | Permalink

    Hmm. You may be right. I may need to think about this further, and possibly do some testing.

  20. Mike | December 1, 2015 at 2:31 pm | Permalink

    Great article! It is a been a year or so, Aaron. Have you been able to do any testing as in post 19? I would like to keep using ZFS and have LUKS encryption for my 3 disk zpool. Thank you.

  21. Carlos Lopez | August 11, 2016 at 11:15 am | Permalink

    dm-crypt/LuKS uses block ciphers, so any bit error destroys the whole block. But not more than that.

    For example, AES128 uses 128 bits (16 bytes) sized blocks, and AES256 uses 256bits (32 bytes) sized blocks.

    Old hard drives will have a sector of at least 512 bytes, which is 16 times bigger than the block cipher size of AES256.

    Modern hard drivers now use 4k block sectors (128 times bigger than the block cipher size of AES256)

    When a hard drive is damaged and one bad sector is found you don't lose just one bit, but the whole sector.

    So, there is really no difference between having dm-crypt on a hard drive or not. You are going to always lose at least one whole block sector worth of bytes, which is many times bigger than the block cipher block size.

    And ZFS should have no problem dealing with this. Really...

{ 2 } Trackbacks

  1. [...] Leave a Comment TrackBack URI [...]

  2. [...] [...]

Post a Comment

Your email is never published nor shared.