{"id":1170,"date":"2012-05-23T21:44:53","date_gmt":"2012-05-24T01:44:53","guid":{"rendered":"https:\/\/lowtek.ca\/roo\/?p=1170"},"modified":"2012-05-23T21:44:53","modified_gmt":"2012-05-24T01:44:53","slug":"how-to-add-second-drive-to-luks-ubuntu","status":"publish","type":"post","link":"https:\/\/lowtek.ca\/roo\/2012\/how-to-add-second-drive-to-luks-ubuntu\/","title":{"rendered":"How To: Add 2nd drive to LUKS on Ubuntu"},"content":{"rendered":"<p><a href=\"https:\/\/lowtek.ca\/roo\/wp-content\/uploads\/2012\/05\/luks-logo.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1171\" title=\"luks-logo\" src=\"https:\/\/lowtek.ca\/roo\/wp-content\/uploads\/2012\/05\/luks-logo.png\" alt=\"\" width=\"330\" height=\"112\" \/><\/a><\/p>\n<p>One of my work machines runs Ubuntu, to protect the data stored on this machine an encrypted file system is used. The file system encryption is <a href=\"http:\/\/en.wikipedia.org\/wiki\/Linux_Unified_Key_Setup\">LUKS<\/a> based and is applied to a filesystem at creation time, thus to encrypt the system drive it is applied at install time. In my case encryption was an option built into the installer I used.<\/p>\n<p>After running the system for a while I wanted to add a 2nd drive for additional storage and backup. One solution would be to follow <a href=\"http:\/\/natetron.net\/b\/?p=10\">this post<\/a>\u00a0or <a href=\"http:\/\/www.howtoforge.com\/automatically-unlock-luks-encrypted-drives-with-a-keyfile\">this one<\/a>, both use a key file stored on the first drive to open the second. Ideally I want the crypt password supplied once on boot to unlock both drives.<\/p>\n<p>It seems from searching around that <a href=\"http:\/\/ubuntuforums.org\/showthread.php?p=11904197\">this can<\/a> <a href=\"http:\/\/www.linuxquestions.org\/questions\/slackware-14\/lvm-ontop-of-encrypted-file-system-2-drives-908866\/\">be done<\/a>, but it isn&#8217;t clear if hacking the stock scripts is needed or not. I also found the posts to be somewhat lacking step-by-step details. So I&#8217;ll try to provide a better how-to here.<\/p>\n<p><strong>Phase 1<\/strong> &#8211; gather some data about our current system.<\/p>\n<p>Determine which disk I want to change. <span style=\"color: #ff0000;\">Be very careful, modifying the wrong physical disk could be very bad.<\/span><\/p>\n<p><code>$ sudo fdisk -l<\/code><\/p>\n<p><code>Disk \/dev\/sda: 250.1 GB, 250059350016 bytes<br \/>\n255 heads, 63 sectors\/track, 30401 cylinders<br \/>\nUnits = cylinders of 16065 * 512 = 8225280 bytes<br \/>\nSector size (logical\/physical): 512 bytes \/ 512 bytes<br \/>\nI\/O size (minimum\/optimal): 512 bytes \/ 512 bytes<br \/>\nDisk identifier: 0xe3e5464a<\/code><\/p>\n<p><code>Device Boot Start End Blocks Id System<br \/>\n\/dev\/sda1 * 1 34 273073+ 83 Linux<br \/>\n\/dev\/sda2 35 30401 243922927+ 83 Linux<\/code><\/p>\n<p><code>Disk \/dev\/sdb: 250.1 GB, 250059350016 bytes<br \/>\n255 heads, 63 sectors\/track, 30401 cylinders<br \/>\nUnits = cylinders of 16065 * 512 = 8225280 bytes<br \/>\nSector size (logical\/physical): 512 bytes \/ 512 bytes<br \/>\nI\/O size (minimum\/optimal): 512 bytes \/ 512 bytes<br \/>\nDisk identifier: 0x51e1dd3f<\/code><\/p>\n<p><code>Device Boot Start End Blocks Id System<br \/>\n\/dev\/sdb1 1 30401 244196001 7 HPFS\/NTFS<\/code><\/p>\n<p>So the plan is to change <strong>\/dev\/sdb<\/strong> into a new encrypted volume.<\/p>\n<p>Let&#8217;s now look at the existing encrypted file system to determine how it is configured. I happen to know mine is called <strong>lvm_crypt<\/strong>, you should be able to sort this out by looking in \/dev\/mapper.<\/p>\n<p><code>$ sudo cryptsetup status lvm_crypt<br \/>\n\/dev\/mapper\/lvm_crypt is active:<br \/>\ncipher: aes-xts-plain<br \/>\nkeysize: 512 bits<br \/>\ndevice: \/dev\/sda2<br \/>\noffset: 4040 sectors<br \/>\nsize: 487841815 sectors<br \/>\nmode: read\/write<\/code><\/p>\n<p>We&#8217;ll want to mimic the cipher and keysize to keep\u00a0things at the same security level.<\/p>\n<p><strong>Phase 2<\/strong> &#8211; creating an encrypted filesystem<\/p>\n<p>It appears from my experience that the type of the partition doesn&#8217;t matter, but for completeness we&#8217;ll repartition the drive to be a Linux partition. <span style=\"color: #ff0000;\">Again, when doing this be very careful you are specifying the correct disk &#8211; it will destroy information.<\/span><\/p>\n<p><code>$ sudo fdisk \/dev\/sdb<\/code><\/p>\n<p><code>WARNING: DOS-compatible mode is deprecated. It's strongly recommended to<br \/>\nswitch off the mode (command 'c') and change display units to<br \/>\nsectors (command 'u').<\/code><\/p>\n<p><code>Command (m for help): d<br \/>\nSelected partition 1<\/code><\/p>\n<p><code>Command (m for help): n<br \/>\nCommand action<br \/>\ne extended<br \/>\np primary partition (1-4)<br \/>\np<br \/>\nPartition number (1-4): 1<br \/>\nFirst cylinder (1-30401, default 1):<br \/>\nUsing default value 1<br \/>\nLast cylinder, +cylinders or +size{K,M,G} (1-30401, default 30401):<br \/>\nUsing default value 30401<\/code><\/p>\n<p><code>Command (m for help): w<br \/>\nThe partition table has been altered!<\/code><\/p>\n<p><code>Calling ioctl() to re-read partition table.<br \/>\nSyncing disks.<\/code><\/p>\n<p>Now we have a newly partitioned disk. The above may seem somewhat cryptic, we&#8217;re simply removing the one existing partition and creating a new one. The default fdisk partition type is a Linux partition suitable for our needs.<\/p>\n<p>Now we create the encrypted filesystem on the new partition, supplying additional parameters to match the key size and cipher of the system volume.<\/p>\n<p><code>$ sudo cryptsetup luksFormat \/dev\/sdb1 --key-size=512 --cipher=aes-xts-plain<br \/>\n<\/code><br \/>\n<code>WARNING!<br \/>\n========<br \/>\nThis will overwrite data on \/dev\/sdb1 irrevocably.<br \/>\n<\/code><br \/>\n<code>Are you sure? (Type uppercase yes): YES<br \/>\nEnter LUKS passphrase:<br \/>\nVerify passphrase:<br \/>\n<\/code><br \/>\nI used the same passphrase as the system volume for convenience (and the hope that I could type it in once on boot).<\/p>\n<p>Now we open it and give it a \/dev\/mapper name, and then format the volume.<\/p>\n<p><code>$\u00a0sudo cryptsetup luksOpen \/dev\/sdb1 data_crypt<br \/>\n$ sudo mkfs.ext4 \/dev\/mapper\/data_crypt<br \/>\n<\/code><br \/>\nAt this point we could get paranoid and fill the new volume with random data to prevent any latent zeros on the disk from reducing the set of data an attacker would need to examine. I&#8217;m not that paranoid about this system.<\/p>\n<p><strong>Phase 3<\/strong> &#8211; mounting the encrypted filesystem at boot time<\/p>\n<p>We&#8217;ll be messing with a couple of files: \/etc\/crypttab, \/etc\/fstab\/ and the initramfs. My working theory is that \/etc\/crypttab is used to mount the crypt&#8217;d filesystems and\u00a0\/etc\/fstab is used to mount the parititions (in that order). The initramfs is stored on the unencrypted boot volume and contains a snapshot of the configuration files we need to bootstrap. I found <a href=\"http:\/\/forums.fedoraforum.org\/showthread.php?t=232019\">this post<\/a> somewhat helpful in figuring this part out.<\/p>\n<p>First we use blkid to determine the UUIDs.<\/p>\n<p><code>$ sudo blkid \/dev\/sda1<br \/>\n\/dev\/sda1: UUID=\"a7357d62-71ad-47d5-89cb-fd0f42576644\" TYPE=\"ext4\"<br \/>\n$ sudo blkid \/dev\/sda2<br \/>\n\/dev\/sda2: UUID=\"e4ff5a5f-39f7-4f3e-a45e-737229d95e10\" TYPE=\"crypto_LUKS\"<br \/>\n$ sudo blkid \/dev\/sdb1<br \/>\n\/dev\/sdb1: UUID=\"06114da2-138f-401c-9c84-d4a2e6e83bd1\" TYPE=\"crypto_LUKS\"<\/code><\/p>\n<p>So we add the following line to \/etc\/crypttab:<\/p>\n<p><code>data_crypt UUID=06114da2-138f-401c-9c84-d4a2e6e83bd1 none luks<br \/>\n<\/code><br \/>\nAnd we now add one line to \/etc\/fstab:<\/p>\n<p>\/<code>dev\/mapper\/data_crypt \u00a0 \u00a0 \u00a0 \u00a0\/data \u00a0 \u00a0 \u00a0 \u00a0ext4 \u00a0 \u00a0 \u00a0 \u00a0defaults \u00a0 \u00a0 \u00a0 \u00a00 \u00a0 \u00a0 \u00a0 \u00a02<\/code><\/p>\n<p>Before we reboot, we need to update the initramfs so these configuration changes will be seen at boot time.<\/p>\n<p><code>$ sudo update-initramfs -u<br \/>\n<\/code><br \/>\nAssuming all went as planned, you&#8217;re done. Reboot and test it out.<\/p>\n<p>My journey wasn&#8217;t &#8220;as planned&#8221;, I made several silly mistakes. Providing the wrong UUID causing a failure to open the encrypted volume, and using the wrong name in fstab. Both easy to diagnose by reading the error messages (and logs) carefully and walking through the steps manually. Measure twice, cut once has an application here.<\/p>\n<p>I wasn&#8217;t successful in getting to a single password entry on boot. I&#8217;m prompted twice for the passphrase at boot time, this is easy enough to do. I&#8217;m sure I could crawl in and modify the boot scripts to remember and re-try but that&#8217;d result in a non-standard configuration causing upgrade pain the in future. Not worth it for a system I rarely reboot.<\/p>\n<p><strong>Phase 4<\/strong> &#8211; bonus marks, testing recovery via LiveCD<\/p>\n<p>I wanted to verify that I could recover from a failure resulting in an inability to boot the system from the hard disk.<\/p>\n<p>This turned out to be really easy. Boot the Live CD, then unlock the volumes:<\/p>\n<p><code>$ sudo cryptsetup luksOpen \/dev\/sda2 lvm_crypt<br \/>\n$ sudo cryptsetup luksOpen \/dev\/sdb1 data_crypt<\/code><\/p>\n<p>Now mount them. My system volume is a <a href=\"http:\/\/en.wikipedia.org\/wiki\/Logical_Volume_Manager_(Linux)\">LVM<\/a>, and the new volume is just plain old ext4. I found a <a href=\"http:\/\/jim-zimmerman.com\/?p=587\">helpful post<\/a> on mounting LVMs from rescue mode:<\/p>\n<p><code>$ sudo lvm vgscan -v<br \/>\n$ sudo lvm vgchange -a y<br \/>\n$ sudo lvm lvs --all<br \/>\n$ sudo mount \/dev\/mapper\/ubuntu-root \/mnt<\/code><\/p>\n<p>Mounting the ext4 partition is simply<\/p>\n<p><code>$ sudo mount \/dev\/mapper\/data_crypt \/mnt2<\/code><\/p>\n<p>That&#8217;s it, a second volume added to an existing LUKS system &#8211; and confidence we can mount both volumes from a LiveCD in the case of failure.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of my work machines runs Ubuntu, to protect the data stored on this machine an encrypted file system is used. The file system encryption is LUKS based and is applied to a filesystem at creation time, thus to encrypt the system drive it is applied at install time. In my case encryption was an &hellip; <a href=\"https:\/\/lowtek.ca\/roo\/2012\/how-to-add-second-drive-to-luks-ubuntu\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How To: Add 2nd drive to LUKS on Ubuntu&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,12],"tags":[],"class_list":["post-1170","post","type-post","status-publish","format-standard","hentry","category-computing","category-how-to"],"_links":{"self":[{"href":"https:\/\/lowtek.ca\/roo\/wp-json\/wp\/v2\/posts\/1170","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lowtek.ca\/roo\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lowtek.ca\/roo\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lowtek.ca\/roo\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lowtek.ca\/roo\/wp-json\/wp\/v2\/comments?post=1170"}],"version-history":[{"count":6,"href":"https:\/\/lowtek.ca\/roo\/wp-json\/wp\/v2\/posts\/1170\/revisions"}],"predecessor-version":[{"id":1177,"href":"https:\/\/lowtek.ca\/roo\/wp-json\/wp\/v2\/posts\/1170\/revisions\/1177"}],"wp:attachment":[{"href":"https:\/\/lowtek.ca\/roo\/wp-json\/wp\/v2\/media?parent=1170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lowtek.ca\/roo\/wp-json\/wp\/v2\/categories?post=1170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lowtek.ca\/roo\/wp-json\/wp\/v2\/tags?post=1170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}