<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>wohli.org</title>
  
  
  <link href="/atom.xml" rel="self"/>
  
  <link href="https://blog.wohli.org/"/>
  <updated>2018-03-07T07:30:34.834Z</updated>
  <id>https://blog.wohli.org/</id>
  
  <author>
    <name>Martin Wohli</name>
    
  </author>
  
  <generator uri="http://hexo.io/">Hexo</generator>
  
  <entry>
    <title>Create btrfs subvolumes retrospectively</title>
    <link href="https://blog.wohli.org/2017/06/19/Create-btrfs-subvolumes-retrospectively/"/>
    <id>https://blog.wohli.org/2017/06/19/Create-btrfs-subvolumes-retrospectively/</id>
    <published>2017-06-19T11:28:42.000Z</published>
    <updated>2018-03-07T07:30:34.834Z</updated>
    
    <content type="html"><![CDATA[<p>This posts describes how to create subvolumes in btrfs retrospectively (with existing data).<br><a id="more"></a><br>I’m using archlinux, but most of the things should be applicable to other distors as well.<br>It is mostly a list of the required commands, with a tiny bit of explanations :).<br>All the commands are real world examples from my system, so you need to adapt device names etc. to your setup.</p><h3 id="Move-the-top-level-volume-to-a-subvolume"><a href="#Move-the-top-level-volume-to-a-subvolume" class="headerlink" title="Move the top-level volume to a subvolume"></a>Move the top-level volume to a subvolume</h3><ul><li>create a snapshot<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo btrfs subvol snapshot / /@</span><br></pre></td></tr></table></figure></li></ul><p>(“@” seems to be a common name for the main subvolume)</p><ul><li><p>set the new snapshot as default subvolume</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">sudo btrfs subvol list /</span><br><span class="line"># outputs: ID 330 gen 1354 top level 5 path @</span><br><span class="line"># use the id from the output above to </span><br><span class="line">sudo btrfs subvol set-default 330 /</span><br></pre></td></tr></table></figure></li><li><p>if it is not the root volume, skip to the last point of this section</p></li><li><p>adapt grub to boot from the new subvolume</p><ul><li><p>mount the new subvolume</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">cd /mnt</span><br><span class="line">sudo mkdir newroot</span><br><span class="line">sudo mount -o subvol=@ /dev/mapper/cryptroot newroot</span><br></pre></td></tr></table></figure></li><li><p>edit newroot/etc/fstab to include subvol=@ in the / options<br>This should not be needed theoretically since it is the default volume, but grub-mkconfig did not produce a correct config for me without it.</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">UUID=2240f8f3-3dfa-4aad-a62f-899f5970cb3d / btrfs defaults,subvol=@,noatime,discard,space_cache,autodefrag,compress=lzo</span><br><span class="line"> 0 0</span><br></pre></td></tr></table></figure></li><li><p>chroot into the new subvolume and update grub</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">for i in /dev /dev/pts /proc /sys /run; do sudo mount --bind $i newroot$i; done</span><br><span class="line">sudo mount /dev/mapper/cryptboot newroot/boot</span><br><span class="line">sudo mount /dev/sdc1 newroot/boot/efi</span><br><span class="line">sudo mount --bind /sys/firmware/efi/efivars newroot/sys/firmware/efi/efivars </span><br><span class="line">sudo chroot /mnt/newroot</span><br><span class="line">grub-mkconfig -o /boot/grub/grub.cfg</span><br><span class="line">grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=arch-grub --recheck</span><br></pre></td></tr></table></figure></li><li><p>unmount and reboot (at your own risk of course :D)</p></li></ul></li><li><p>delete the old data from the toplevel </p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">cd /mnt</span><br><span class="line">sudo mkdir oldroot</span><br><span class="line">sudo mount -o subvolid=5 /dev/mapper/cryptroot oldroot</span><br><span class="line">cd oldroot</span><br><span class="line"># CAREFULLY delete everything except @ subvolume</span><br></pre></td></tr></table></figure></li></ul><p><br></p><h3 id="Move-existing-directory-to-a-subvolume"><a href="#Move-existing-directory-to-a-subvolume" class="headerlink" title="Move existing directory to a subvolume"></a>Move existing directory to a subvolume</h3><p>In this example the directory /home/martin/develop will be replaced with a subvolume.</p><ul><li><p>create the new subvolume</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">sodo mkdir /mnt/toplevel</span><br><span class="line">sudo mount -o subvolid=5 /dev/mapper/cryptroot /mnt/toplevel</span><br><span class="line">sudo btrfs subvol create /mnt/toplevel/@develop</span><br></pre></td></tr></table></figure></li><li><p>move the data to the new subvolume in the most efficient way (as far as I know) by creating reflinks</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo cp -ax --reflink=always /mnt/toplevel/@/home/martin/develop/. /mnt/toplevel/@develop</span><br></pre></td></tr></table></figure></li><li><p>delete the old directory</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"># DO THIS AT YOUR OWN RISK</span><br><span class="line">rm -rf /home/martin/develop</span><br></pre></td></tr></table></figure></li><li><p>mount the new subvolume instead</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">mkdir /home/martin/develop</span><br><span class="line">sudo mount -o subvol=@develop /dev/mapper/cryptroot /home/martin/develop</span><br></pre></td></tr></table></figure></li><li><p>add new fstab entry to mount the new subvolume automatically</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">UUID=2240f8f3-3dfa-4aad-a62f-899f5970cb3d /home/martin/develop btrfs defaults,subvol=@develop,noatime,discard,space_cac</span><br><span class="line">he,autodefrag,compress=lzo 0 0</span><br></pre></td></tr></table></figure></li></ul><p>Note that the top-level volume always has the ID 5.</p><p>Reference: <a href="https://btrfs.wiki.kernel.org/index.php/UseCases#Can_I_take_a_snapshot_of_a_directory.3F" target="_blank" rel="noopener">https://btrfs.wiki.kernel.org/index.php/UseCases#Can_I_take_a_snapshot_of_a_directory.3F</a></p><p><br></p><p>Thanks for reading, I hope it was helpful to you :)</p>]]></content>
    
    <summary type="html">
    
      &lt;p&gt;This posts describes how to create subvolumes in btrfs retrospectively (with existing data).&lt;br&gt;
    
    </summary>
    
      <category term="Linux" scheme="https://blog.wohli.org/categories/Linux/"/>
    
    
      <category term="Linux" scheme="https://blog.wohli.org/tags/Linux/"/>
    
      <category term="Arch" scheme="https://blog.wohli.org/tags/Arch/"/>
    
      <category term="Btrfs" scheme="https://blog.wohli.org/tags/Btrfs/"/>
    
  </entry>
  
  <entry>
    <title>Encrypted btrfs RAID as root</title>
    <link href="https://blog.wohli.org/2017/04/23/Encrypted-Btrfs-RAID-as-root/"/>
    <id>https://blog.wohli.org/2017/04/23/Encrypted-Btrfs-RAID-as-root/</id>
    <published>2017-04-23T05:44:12.000Z</published>
    <updated>2018-03-07T07:30:34.834Z</updated>
    
    <content type="html"><![CDATA[<p>This post describes how to setup a fully encrypted root btrfs on two disks, running in RAID 0.<br><a id="more"></a></p><h3 id="Disclaimer"><a href="#Disclaimer" class="headerlink" title="Disclaimer"></a>Disclaimer</h3><p>Some things are distribution agnostic, but other things (especially the how to make it boot) are arch linux specific.<br>Make sure you have a backup of everything before you start this. If you lose data or destroy your system, it is your own fault :D.</p><p>I also don’t claim that this setup is fully secure (I only encrypt to protect the data of my customers in case my laptop gets stolen or something, not to protect my super secret secrets from the NSA :) ).</p><h3 id="Concept"><a href="#Concept" class="headerlink" title="Concept"></a>Concept</h3><p>To unlock the RAID disks with one password, I went for also encrypting the boot partition and store a key to unlock the root filesystem in the initramfs.</p><p>I have two SSDs one with 128GB and one with 120GB. The partitions used for raid 0 should be the same size. Different sizes is possible, but the left over size of the bigger partition won’t be used (but we don’t have to make them precisely equal for it to work).</p><p>I put boot efi and swap on the larger disk and use two 120GB partitions for the RAID.</p><p>Resulting disk layout:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">NAME           FSTYPE         SIZE</span><br><span class="line">sdY                         119,2G</span><br><span class="line">├─sdY1         vfat          1000M</span><br><span class="line">├─sdY2         crypto_LUKS   1000M</span><br><span class="line">│ └─cryptboot  ext4           998M</span><br><span class="line">├─sdY3         swap             4G</span><br><span class="line">└─sdY4         crypto_LUKS  113,3G</span><br><span class="line">  └─cryptroot  btrfs        113,3G</span><br><span class="line">sdX                         111,8G</span><br><span class="line">└─sdX1         crypto_LUKS  111,8G</span><br><span class="line">  └─cryptroot2 btrfs        111,8G</span><br></pre></td></tr></table></figure></p><p>(I’m not going to use the swap … just created it because of the space difference)</p><h3 id="Formating-the-disks"><a href="#Formating-the-disks" class="headerlink" title="Formating the disks"></a>Formating the disks</h3><ul><li>format the efi partition fat32</li><li><p>setup LUKS and format the boot partition with ext4</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">sudo cryptsetup luksFormat /dev/sdX2</span><br><span class="line">sudo cryptsetup open /dev/sdX2 cryptboot</span><br><span class="line">sudo mkfs.ext4 /dev/mapper/cryptboot</span><br></pre></td></tr></table></figure></li><li><p>setup LUKS and format the root partition with btrfs</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">sudo cryptsetup luksFormat /dev/sdX4</span><br><span class="line">sudo cryptsetup open /dev/sdX4 cryptroot</span><br><span class="line">sudo mkfs.btrfs /dev/mapper/cryptroot</span><br></pre></td></tr></table></figure></li><li><p>setup LUKS on the second drive </p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sudo cryptsetup luksFormat /dev/sdY1</span><br><span class="line">sudo cryptsetup open /dev/sdY1 cryptroot2</span><br></pre></td></tr></table></figure></li><li><p>mount first btrfs and add the partition from the other disk to it</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">cd /mnt</span><br><span class="line">sudo mkdir cryptroot</span><br><span class="line">sudo mount -o compress=lzo /dev/mapper/cryptroot cryptroot/</span><br><span class="line"></span><br><span class="line">sudo btrfs device add /dev/mapper/cryptroot2 /mnt/cryptroot</span><br><span class="line"># doing it like this also works if the drive is added later (the balance can take a while in this case)</span><br><span class="line">sudo btrfs balance start -dconvert=raid0 -mconvert=raid1 /mnt/cryptroot</span><br></pre></td></tr></table></figure></li></ul><h3 id="Copy-the-root-filesystem-to-the-new-btrfs-RAID-or-make-a-new-install-on-it"><a href="#Copy-the-root-filesystem-to-the-new-btrfs-RAID-or-make-a-new-install-on-it" class="headerlink" title="Copy the root filesystem to the new btrfs RAID (or make a new install on it)"></a>Copy the root filesystem to the new btrfs RAID (or make a new install on it)</h3><p>Now you can install your system, or copy your existing root to the new raid btrfs.<br>In this example I will copy an existing root filesystem onto the new btrfs raid.</p><ul><li><p>create subvolumes (“@” seams to be a common name to use for the main subvolume)</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">sudo btrfs subvol create /mnt/cryptroot/@</span><br><span class="line"></span><br><span class="line">sudo btrfs subvol list /mnt/cryptroot/</span><br><span class="line"># output of above: ID 330 gen 1354 top level 5 path @</span><br><span class="line"># use the ID shown to set the &quot;@&quot; subvolume as default</span><br><span class="line">sudo btrfs subvol set-default 330 /mnt/cryptroot/</span><br></pre></td></tr></table></figure></li><li><p>maybe create more subvolumes (“@home” is common for /home)</p></li><li>copy the installation<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">sudo mkdir /mnt/newroot</span><br><span class="line">sudo mount -o subvol=@ /dev/mapper/cryptroot newroot </span><br><span class="line">sudo cp -a /mnt/oldroot/. /mnt/newroot/</span><br></pre></td></tr></table></figure></li></ul><p>(-a .. recursive copy that preserves all the things, like permissions)</p><ul><li>copy kernel to new boot<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">sudo mkdir /mnt/cryptboot</span><br><span class="line">sudo mount /dev/mapper/cryptboot /mnt/cryptboot/</span><br><span class="line">sudo cp -a /mnt/oldboot/vmlinuz-linux /mnt/cryptboot/</span><br></pre></td></tr></table></figure></li></ul><h4 id="Make-it-boot"><a href="#Make-it-boot" class="headerlink" title="Make it boot"></a>Make it boot</h4><p>WARNING: This part is arch linux specific.</p><p>Boot will be unlocked by grub via password to get to the initramfs.<br>The initramfs will unlocked root (it will have the key in it).<br>Boot must be unlocked again with key in root so that it can be mounted in the “normal” mode (so that an update can store a new kernel and initramfs).</p><ul><li>add a keyfile for unlocking to all LUKS partitions <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">sudo dd bs=512 count=4 if=/dev/random of=/mnt/cryptroot/crypto_keyfile.bin</span><br><span class="line">sudo chmod 600 /mnt/cryptboot/cryptokey.bin</span><br><span class="line"></span><br><span class="line">sudo cryptsetup luksAddKey /dev/sdX2 cryptroot/crypto_keyfile.bin</span><br><span class="line">sudo cryptsetup luksAddKey /dev/sdX4 cryptroot/crypto_keyfile.bin</span><br><span class="line">sudo cryptsetup luksAddKey /dev/sdY1 cryptroot/crypto_keyfile.bin</span><br></pre></td></tr></table></figure></li></ul><p>WARNING: use this name and path (“/crypto_keyfile.bin”), otherwise additional steps must be taken later (this is the default file name and location).</p><ul><li><p>chroot into the new system</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">for i in /dev /dev/pts /proc /sys /run; do sudo mount --bind $i /mnt/newroot$i; done</span><br><span class="line">sudo mount /dev/mapper/cryptboot newroot/boot</span><br><span class="line">sudo mount /dev/sdY1 newroot/boot/efi</span><br><span class="line">sudo mount --bind /sys/firmware/efi/efivars newroot/sys/firmware/efi/efivars </span><br><span class="line">sudo chroot /mnt/newroot</span><br></pre></td></tr></table></figure></li><li><p>add additional hook that will be used by the initramfs to unlock the second root partition</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"># copy the original hook</span><br><span class="line">cp /usr/lib/initcpio/install/encrypt /etc/initcpio/install/encrypt2</span><br><span class="line">cp /usr/lib/initcpio/hooks/encrypt  /etc/initcpio/hooks/encrypt2</span><br><span class="line"># adapt the new hook to use different names and to NOT delete the keyfile</span><br><span class="line">sed -i &quot;s/cryptdevice/cryptdevice2/&quot; /etc/initcpio/hooks/encrypt2</span><br><span class="line">sed -i &quot;s/cryptkey/cryptkey2/&quot; /etc/initcpio/hooks/encrypt2</span><br><span class="line">sed -i &quot;s/rm -f \$&#123;ckeyfile&#125;//&quot; /etc/initcpio/hooks/encrypt2</span><br></pre></td></tr></table></figure></li></ul><p>see also: <a href="https://wiki.archlinux.org/index.php/Dm-crypt/Specialties#Modifying_the_encrypt_hook_for_multiple_partitions" target="_blank" rel="noopener">https://wiki.archlinux.org/index.php/Dm-crypt/Specialties#Modifying_the_encrypt_hook_for_multiple_partitions</a><br>alternative hook see: <a href="https://bbs.archlinux.org/viewtopic.php?id=105086" target="_blank" rel="noopener">https://bbs.archlinux.org/viewtopic.php?id=105086</a></p><ul><li>add encrypt2 hook to mkinitcpio.conf BEFORE encrypt hook (because encrypt hook deletes the key file at the end)</li><li>edit “/mnt/cryptroot/etc/mkinitcpio.conf” to include your keyfile and add the proper hooks:<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">FILES=&quot;/crypto_keyfile.bin&quot;</span><br><span class="line">HOOKS=&quot; ... udev encrypt2 encrypt ... &quot; #(before &quot;filesystems&quot;)</span><br></pre></td></tr></table></figure></li></ul><p>(encrypt2 before encrypt because encrypt deletes the keyfile from the initial ram disk)</p><ul><li><p>generate the initramfs</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">mkinitcpio -p linux</span><br><span class="line"># initramfs includes the key, so only root should be able to read it</span><br><span class="line">chmod 600 /boot/initramfs-linux-fallback.img</span><br><span class="line">chmod 600 /boot/initramfs-linux.img</span><br></pre></td></tr></table></figure></li><li><p>add boot to crypttab (root does not need to be here, it will be unlocked by the intitramfs)</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cryptboot UUID=054cc16f-1257-4153-954b-6fea9c1ed931 /crypto_keyfile.bin luks,discard,nofail</span><br></pre></td></tr></table></figure></li><li><p>edit /etc/fstab (use UUID of btrfs volume, it is the same for both raid volumes)</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">UUID=8a67292b-24e7-4d4b-ba6e-e4f9f7160524 / btrfs defaults,subvol=@,noatime,discard,compress=lzo,space_cache,autodefrag 0 1</span><br><span class="line">/dev/mapper/cryptboot /boot ext4 defaults,noatime 0 2</span><br><span class="line">UUID=FD95-93A6 /boot/efi vfat defaults 0 0</span><br></pre></td></tr></table></figure></li><li><p>edit /etc/defaults/grub add cryptdevice2 with the second device</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">GRUB_CMDLINE_LINUX=&quot;cryptdevice=/dev/disk/by-uuid/e506207c-11a0-43f9-9e0b-c212bb6b2fee:cryptroot:allow-discards cryptdevice2=/dev/disk/by-uuid/abc6207c-11a0-43f9-9e0b-c212bb6b2abc:cryptroot2:allow-discards root=/dev/mapper/cryptroot&quot;</span><br><span class="line">GRUB_ENABLE_CRYPTODISK=y</span><br></pre></td></tr></table></figure></li><li><p>generate grub config and install grub</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">grub-mkconfig -o /boot/grub/grub.cfg</span><br><span class="line">grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=arch-grub --recheck</span><br></pre></td></tr></table></figure></li><li><p>sync, unmount, reboot and hope for the best :)</p></li></ul>]]></content>
    
    <summary type="html">
    
      &lt;p&gt;This post describes how to setup a fully encrypted root btrfs on two disks, running in RAID 0.&lt;br&gt;
    
    </summary>
    
      <category term="Linux" scheme="https://blog.wohli.org/categories/Linux/"/>
    
    
      <category term="Linux" scheme="https://blog.wohli.org/tags/Linux/"/>
    
      <category term="Arch" scheme="https://blog.wohli.org/tags/Arch/"/>
    
      <category term="Btrfs" scheme="https://blog.wohli.org/tags/Btrfs/"/>
    
      <category term="Encryption" scheme="https://blog.wohli.org/tags/Encryption/"/>
    
      <category term="LUKS" scheme="https://blog.wohli.org/tags/LUKS/"/>
    
  </entry>
  
  <entry>
    <title>Unlock Second LUKS Volume Automatically</title>
    <link href="https://blog.wohli.org/2017/02/21/Unlock-Second-LUKS-Automatically/"/>
    <id>https://blog.wohli.org/2017/02/21/Unlock-Second-LUKS-Automatically/</id>
    <published>2017-02-21T18:07:05.000Z</published>
    <updated>2018-03-07T07:30:34.835Z</updated>
    
    <content type="html"><![CDATA[<p>This post shows some options for unlocking additional LUKS encrypted volumes automatically (on Antergos, but most of it should apply to other distros). It assumes that you already have your root filesystem on LUKS and want to add a encrypted none root volume.<br>I’m also using systemd-boot with a none encrypted boot/EFI partition.</p><a id="more"></a><h3 id="Unlock-second-device-with-key-on-first-device"><a href="#Unlock-second-device-with-key-on-first-device" class="headerlink" title="Unlock second device with key on first device"></a>Unlock second device with key on first device</h3><p>In this setup, the first device will be unlocked with password, and the second device with a keyfile inside that first encrypted volume.<br>A LUKS partition can have up to 8 different keys. So you don’t have to ditch your password for a keyfile, you can have both (or 8 to be more precise ;) ).</p><ul><li>add a key to existing LUKS partition<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"># generate random key with 4kB</span><br><span class="line">sudo dd if=/dev/urandom of=/root/keyfile bs=1024 count=4</span><br><span class="line"># set to read only for root</span><br><span class="line">sudo chmod 0400 /root/keyfile</span><br><span class="line"># add the key to the existing LUKS (prompts for existing passphrase of that LUKS)</span><br><span class="line">sudo cryptsetup luksAddKey /dev/sdX /root/keyfile</span><br></pre></td></tr></table></figure></li></ul><p>More about this here: <a href="https://www.howtoforge.com/automatically-unlock-luks-encrypted-drives-with-a-keyfile" target="_blank" rel="noopener">https://www.howtoforge.com/automatically-unlock-luks-encrypted-drives-with-a-keyfile</a></p><ul><li><p>add second (none root) drive to crypttab </p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">crypt2    UUID=f2ee83ef-a828-4a84-a150-2ffd781b495a       /root/keyfile    luks,discard</span><br></pre></td></tr></table></figure></li><li><p>check hooks in /etc/mkinitcpio.conf </p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">HOOKS=&quot;base udev autodetect modconf block keyboard keymap encrypt lvm2 resume filesystems fsck&quot;</span><br></pre></td></tr></table></figure></li></ul><p>Note: This setup did not work with systemd-hooks (sd-encrypt)</p><ul><li><p>add the new device to fstab, if it should also be mounted automatically</p></li><li><p>rebuild initramfs</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo mkinitcpio -p linux</span><br></pre></td></tr></table></figure></li><li><p><strong>make sure you know how you can get back into your system to fix things if something went wrong and the boot is broken!</strong></p></li><li><p>reboot (read on first)</p></li></ul><h4 id="Troubleshooting-System-hangs-at-boot-now"><a href="#Troubleshooting-System-hangs-at-boot-now" class="headerlink" title="Troubleshooting: System hangs at boot now"></a>Troubleshooting: System hangs at boot now</h4><p>If the second unlocked volume contains an LVM, there could be a problem with pvscan, that makes the boot hang.<br>See this bug report: <a href="https://bugs.archlinux.org/task/41833" target="_blank" rel="noopener">https://bugs.archlinux.org/task/41833</a></p><p>A workaround for this is to:</p><ul><li><p>copy udev rule (to override default so that update does not break the workaround)</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cp /lib/initcpio/udev/69-dm-lvm-metad.rules /etc/udev/rules.d/</span><br></pre></td></tr></table></figure></li><li><p>remove “–background” from pvscan call</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">RUN+=&quot;/usr/bin/lvm pvscan --cache --activate ay --major $major --minor $minor&quot;, ENV&#123;LVM_SCANNED&#125;=&quot;1&quot;</span><br></pre></td></tr></table></figure></li></ul><hr><p>The above solution is the one that after a lot of trial and error worked for me.<br>Below are some other things I tried, they required to enter multiple passwords or did not work if the root filesystem is also on encrypted, but maybe it is also helpful to see what does not work.</p><h3 id="Unlock-with-two-password-promts"><a href="#Unlock-with-two-password-promts" class="headerlink" title="Unlock with two password promts"></a>Unlock with two password promts</h3><ul><li><p>add drives in /etc/crypttab.initramfs (NOTE: not crypttab but crypttab.initramfs, only those are unlocked by the initramfs)</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">cryptAntergos   UUID=dfe2737b-fbc3-4aa0-9851-c8eff0c76abd       main    luks,discard</span><br><span class="line">cryptDevelop    UUID=f2ee83ef-a828-4a84-a150-2ffd781b495a       main    luks,discard</span><br></pre></td></tr></table></figure></li><li><p>adapt /boot/loader/entries/antergos.conf</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">options luks.uuid=dfe2737b-fbc3-4aa0-9851-c8eff0c76abd luks.uuid=f2ee83ef-a828-4a84-a150-2ffd781b495a root=/dev/mapper/AntergosVG-AntergosRoot rw quiet</span><br></pre></td></tr></table></figure></li><li><p>in /etc/mkinitcpio.conf change the hooks to systemd hooks “encrypt” to “sd-encrypt” and add “systemd” hook before it (also lvm2 -&gt; sd-lvm2 and keymap -&gt; sd-vconsole)</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">HOOKS=&quot;base systemd autodetect modconf block keyboard sd-vconsole sd-encrypt sd-lvm2 filesystems fsck&quot;</span><br></pre></td></tr></table></figure></li><li><p>rebuild initramfs</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo mkinitcpio -p linux</span><br></pre></td></tr></table></figure></li></ul><p>sd-encrypt may gain the ability to cache a password in the future (so that the same password would not have to be entered twice), but does not seam to have this yet (not sure )</p><p>Systemd cryptsetup documentation: <a href="https://www.freedesktop.org/software/systemd/man/systemd-cryptsetup-generator.html" target="_blank" rel="noopener">https://www.freedesktop.org/software/systemd/man/systemd-cryptsetup-generator.html</a></p><h3 id="Unlock-two-none-root-LUKS-volumes-with-the-same-password"><a href="#Unlock-two-none-root-LUKS-volumes-with-the-same-password" class="headerlink" title="Unlock two none root LUKS volumes with the same password"></a>Unlock two none root LUKS volumes with the same password</h3><ul><li>use non-systemd hooks in mkinitcpio.conf</li><li>in crypttab add “keyscript=decrypt_keyctl”<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">cryptAntergos   UUID=dfe2737b-fbc3-4aa0-9851-c8eff0c76abd       main    luks,discard,keyscript=decrypt_keyctl</span><br><span class="line">cryptDevelop    UUID=f2ee83ef-a828-4a84-a150-2ffd781b495a       main    luks,discard,keyscript=decrypt_keyctl</span><br></pre></td></tr></table></figure></li></ul><p>See also (for alternative “encrypt” hooks):</p><ul><li><a href="https://bugs.archlinux.org/task/23182" target="_blank" rel="noopener">https://bugs.archlinux.org/task/23182</a></li><li><a href="https://bugs.archlinux.org/task/50901" target="_blank" rel="noopener">https://bugs.archlinux.org/task/50901</a></li></ul><hr><p>Thank you for reading, I hope that it was helpful, despite the lacking detailed descriptions :)</p><p><a rel="noopener" href="http://creativecommons.org/licenses/by/4.0/" target="_blank"><img alt="Creative Commons License" style="border-width:0" src="https://i.creativecommons.org/l/by/4.0/88x31.png"></a><br>This work is licensed under a <a rel="noopener" href="http://creativecommons.org/licenses/by/4.0/" target="_blank">Creative Commons Attribution 4.0 International License</a>.</p>]]></content>
    
    <summary type="html">
    
      &lt;p&gt;This post shows some options for unlocking additional LUKS encrypted volumes automatically (on Antergos, but most of it should apply to other distros). It assumes that you already have your root filesystem on LUKS and want to add a encrypted none root volume.&lt;br&gt;I’m also using systemd-boot with a none encrypted boot/EFI partition.&lt;/p&gt;
    
    </summary>
    
      <category term="Linux" scheme="https://blog.wohli.org/categories/Linux/"/>
    
    
      <category term="Linux" scheme="https://blog.wohli.org/tags/Linux/"/>
    
      <category term="Encryption" scheme="https://blog.wohli.org/tags/Encryption/"/>
    
      <category term="LUKS" scheme="https://blog.wohli.org/tags/LUKS/"/>
    
      <category term="Antergos" scheme="https://blog.wohli.org/tags/Antergos/"/>
    
  </entry>
  
  <entry>
    <title>Installing Ubuntu 16.10 on existing LUKS-encrypted LVM</title>
    <link href="https://blog.wohli.org/2016/10/05/Installing-Ubuntu-16-10-on-existing-LUKS-encrypted-LVM/"/>
    <id>https://blog.wohli.org/2016/10/05/Installing-Ubuntu-16-10-on-existing-LUKS-encrypted-LVM/</id>
    <published>2016-10-05T13:00:09.000Z</published>
    <updated>2018-03-07T07:30:34.835Z</updated>
    
    <content type="html"><![CDATA[<p>This is a short overview of how to install Ubuntu 16.10 on an existing LUSK-encrypted partition containing logical volumes, and using two unencrypted partitions for <em>/boot</em> and <em>/boot/efi/</em>.</p><a id="more"></a><h3 id="Disk-layout"><a href="#Disk-layout" class="headerlink" title="Disk layout"></a>Disk layout</h3><p>All the commands shown later, will be according to this layout.</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">NAME                  SIZE RO TYPE  Note</span><br><span class="line">sdc                 223,6G  0 disk  Disk that should be used</span><br><span class="line">├─sdc1                256M  0 part  unencrypted fat32 that should be used for /boot/efi</span><br><span class="line">├─sdc2                256M  0 part  unencrypted ext4 that should be used for /boot</span><br><span class="line">└─sdc3              223,1G  0 part  LUKS encrypted partition</span><br><span class="line">  └─develop         223,1G  0 crypt </span><br><span class="line">    ├─vg01-swap         8G  0 lvm   swap that should be used</span><br><span class="line">    ├─vg01-root       100G  0 lvm   should be used as root /</span><br><span class="line">    └─vg01-develop  115,1G  0 lvm   data partition that should not be touched</span><br></pre></td></tr></table></figure><h3 id="Install"><a href="#Install" class="headerlink" title="Install"></a>Install</h3><ul><li>Boot from your install medium in UEFI mode.</li><li>Start the live system (“Try Ubuntu …”)</li><li><p>Unlock the encrypted partition</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"># find partition</span><br><span class="line">lsblk</span><br><span class="line"># open it</span><br><span class="line">sudo cryptsetup luksOpen /dev/sdc3 sdc3_crypt</span><br></pre></td></tr></table></figure></li><li><p>run the installer</p><ul><li>set the root to be /dev/mapper/vg01-root select a filesystem (ext4) and check the format box</li><li>set /boot as mountpoint for /dev/sdc2</li><li>set /dev/sdc as boot device</li><li>install</li></ul></li></ul><h3 id="Manually-setup-the-boot-partition"><a href="#Manually-setup-the-boot-partition" class="headerlink" title="Manually setup the boot partition"></a>Manually setup the boot partition</h3><ul><li><p>prepare the new install for chroot </p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"># mount the root partition</span><br><span class="line">sudo mount /dev/mapper/vg01-root /mnt</span><br><span class="line">sudo mount /dev/sdc2 /mnt/boot</span><br><span class="line">sudo mount /dev/sdc1 /mnt/boot/efi</span><br><span class="line"># bind the dev, proc, and sys to the new root</span><br><span class="line">sudo mount --bind /dev /mnt/dev</span><br><span class="line">sudo mount --bind /proc /mnt/proc</span><br><span class="line">sudo mount --bind /sys /mnt/sys</span><br></pre></td></tr></table></figure></li><li><p>chroot into the new install</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo chroot /mnt</span><br></pre></td></tr></table></figure></li><li><p>find the uuid of the luks encrypted partition</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">blkid /dev/sdc3</span><br></pre></td></tr></table></figure></li><li><p>add a new file /etc/crypttab with the content (where sdc3_crypt is just a name, I used the same here as in the luksOpen above)</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"># &lt;target name&gt;&lt;source device&gt;&lt;key file&gt;&lt;options&gt;</span><br><span class="line">sdc3_crypt UUID=f2ee83ef-a828-4a84-a150-2ffd781b495a none luks,discard</span><br></pre></td></tr></table></figure></li></ul><p>The discard option is only relevant for SSD drives and may reduce the security, see the warning in the manpage <a href="http://manpages.ubuntu.com/manpages/precise/man5/crypttab.5.html" target="_blank" rel="noopener">here</a>.</p><ul><li>fix the boot setup<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"># reinstall grub</span><br><span class="line">apt-get install --reinstall grub-efi-amd64</span><br><span class="line">grub-install </span><br><span class="line"></span><br><span class="line"># update the initramfs</span><br><span class="line">update-initramfs -k all -c -v</span><br><span class="line"></span><br><span class="line"># update grub </span><br><span class="line">update-grub</span><br></pre></td></tr></table></figure></li></ul><h3 id="All-done"><a href="#All-done" class="headerlink" title="All done"></a>All done</h3><p>That should be it, reboot the system and there should be an “ubuntu” entry in the UEFI menu, boot from it and it should prompt for the LUKS password and then boot to the normal lightdm login screen. </p><p>Thank you for reading, I hope that it was helpful :)</p><p>Edit: Post edited on 2016-10-14. Changed from single partition for /boot and /efi to separate partitions and fixed errors.</p><p><a rel="noopener" href="http://creativecommons.org/licenses/by/4.0/" target="_blank"><img alt="Creative Commons License" style="border-width:0" src="https://i.creativecommons.org/l/by/4.0/88x31.png"></a><br>This work is licensed under a <a rel="noopener" href="http://creativecommons.org/licenses/by/4.0/" target="_blank">Creative Commons Attribution 4.0 International License</a>.</p>]]></content>
    
    <summary type="html">
    
      &lt;p&gt;This is a short overview of how to install Ubuntu 16.10 on an existing LUSK-encrypted partition containing logical volumes, and using two unencrypted partitions for &lt;em&gt;/boot&lt;/em&gt; and &lt;em&gt;/boot/efi/&lt;/em&gt;.&lt;/p&gt;
    
    </summary>
    
      <category term="Linux" scheme="https://blog.wohli.org/categories/Linux/"/>
    
    
      <category term="Linux" scheme="https://blog.wohli.org/tags/Linux/"/>
    
      <category term="Encryption" scheme="https://blog.wohli.org/tags/Encryption/"/>
    
      <category term="LUKS" scheme="https://blog.wohli.org/tags/LUKS/"/>
    
      <category term="Ubuntu" scheme="https://blog.wohli.org/tags/Ubuntu/"/>
    
  </entry>
  
</feed>
