{"authors":["utzer"],"canonical_url":"https://newblog.utzer.de/2020/02/12/create-linux-dm-crypt-btrfs-raid-1/","categories":["Linux"],"content":"To do this you need two empty drives, with the guide below all data on the drive will be lost if it is not empty!\n\n#### 1\\. Install cryptsetup\n\n```\napt-get install cryptsetup\n```\n\n#### 2\\. Create the dm-crypt\n\n```\ncryptsetup -y -v luksFormat /dev/sdb\ncryptsetup -y -v luksFormat /dev/sdc\n```\n\n(Make sure to use the correct device. To show available devices use \"lsblk\".)\n\n#### 3\\. Mount the cryptsetup disk\n\n```\ncryptsetup luksOpen /dev/sdb disk1\ncryptsetup luksOpen /dev/sdc disk2\n```\n\n#### 4\\. Check if this was successful\n\n```\nls -l /dev/mapper/disk*\n\nor\nsudo cryptsetup -v status disk1\nsudo cryptsetup -v status disk2\n```\n\n#### 5\\. Create BTRFS volume RAID 1, this means all data and meta-data is mirrored, which means if you use 2 drives with i.e. 4TB each you can only use 4TG over all in the BTRFS mounted volume.\n\n```\nmkfs.btrfs -m raid1 -d raid1 /dev/mapper/disk1 /dev/mapper/disk2\n```\n\nYou could list more than two drive, for example to increase redundancy, but if you want to use more drive to increase storage you would have to use a different Raid level.\n\n#### 6\\. Once you created a multi-device file system, you can use any device in the FS for the mount command\n\n```\nmount -t btrfs -o noatime,compress=zstd /dev/mapper/disk1 /mnt/btrfs\n```\n\n#### 7\\. Creat a subvolume for each purpose/for each data storage folder you want to use the RAID 1 for\n\n```\nbtrfs subvolume create /mnt/btrfs/subvolume1_data\n```\n\nFor example if you want to store you /srv/http and your /var/lib/mysql on the BTRFS RAID drive you should create one subvolume for each, of them, i.e.\n\n```\nbtrfs subvolume create /mnt/btrfs/subvolume_http\nbtrfs subvolume create /mnt/btrfs/subvolume_mysql\n```\n\nThose subvolumes can than be mounted in the respective folders. Before you mount them you should set the permissions/ownership of the folder as same as their mount folder (in this case check permissions of /srv/www and /var/lib/mysql), then move all files and folders from the current folder to the subvolume (see how to mount it below) and then mount the subvolume to the folder).\n\n#### 8\\. Mount the subvolume\n\n```\nmount -t btrfs -o subvolume=subvolume1_data,noatime,compress=zstd /dev/mapper/disk1 /mnt/data\n```\n\nThis uses zstd compression for the mount, this is invoked only by setting the mount parameter. For details on compression see [here](https://btrfs.wiki.kernel.org/index.php/Compression).\n\nYou can mount this subvolume, also the main btrfs volume, to any folder you want, lets assume you want to mount it to /home/$USER/data, so instead of the last command you want to do this (if already mounted before, just do \"umount /mnt/data\" if you already mounted it)\n\n```\nmount -t btrfs -o subvolume=subvolume1_data,noatime,compress=zstd /dev/mapper/disk1 /home/$USER/data\n```\n\n(folder must exists, \"mkdir /home/$USER/data\" if you want to create it)\n\nNow the subvolume is available in /home/$USER/data, see \"df -h\" to check.\n\nThe output will contain a line like this:  \n /dev/mapper/disk1 3.7T 17M 3.7T 1% /home/YOUR\\_USER\\_NAME/data\n\n#### 9\\. Change permission of the subvolume\n\n```\nchown -R $USER:$USER ~/data\n```\n\nNow you can write and read from the folder, you have full access to it, which also means you have full access to the subvolume content and the permissions are inherited by folders and files you create in the folder \"data\".\n\n#### 10\\. Show BTRFS filesystem\n\n```\nsudo btrfs filesystem show\n```\n\nThe output will look like this\n\n```\nLabel: none  uuid: 690fbca6-f764-4873-9f2c-f60c40197a14\n        Total devices 2 FS bytes used 656.00KiB\n        devid    1 size 3.64TiB used 2.03GiB path /dev/mapper/disk2\n        devid    2 size 3.64TiB used 2.03GiB path /dev/mapper/disk1\n```\n\nThere is a great cheat sheet for BTRFS [here](https://blog.programster.org/btrfs-cheatsheet).\n\nPlease leave a comment if you have a suggestion.\n","content_format":"markdown","date":"2020-02-12T08:35:36Z","description":"To do this you need two empty drives, with the guide below all data on the drive will be lost if it is not empty! #### 1\\. Install cryptsetup ``` apt-get install cryptsetup ``` #### 2\\. Create the dm-crypt ``` cryptsetup -y -v luksFormat /dev/sdb cryptsetup -y -v luksFormat /dev/sdc ``` (Make sure t","html_url":"https://newblog.utzer.de/2020/02/12/create-linux-dm-crypt-btrfs-raid-1/","json_url":"https://newblog.utzer.de/2020/02/12/create-linux-dm-crypt-btrfs-raid-1/index.json","lastmod":"2020-02-12T08:35:36Z","markdown_url":"https://newblog.utzer.de/2020/02/12/create-linux-dm-crypt-btrfs-raid-1/index.md","media":[],"schema_version":1,"tags":["Backup","BTRFS","dm-crypt","encryption","Filesystem","linux","RAID"],"title":"Create Linux dm-crypt BTRFS RAID 1","type":"blog_post"}