{"id":887,"date":"2021-12-19T22:59:25","date_gmt":"2021-12-19T20:59:25","guid":{"rendered":"https:\/\/valentijn.sessink.nl\/?p=887"},"modified":"2022-03-16T17:26:11","modified_gmt":"2022-03-16T15:26:11","slug":"dell-xps-add-disk-encryption","status":"publish","type":"post","link":"https:\/\/valentijn.sessink.nl\/?p=887","title":{"rendered":"Dell XPS &#8211; add disk encryption"},"content":{"rendered":"\n<p>The Dell XPS and Precision laptops with Linux pre-installed are great for personal and business use. Unfortunately, they lack an important feature: their disks have been factory pre-installed without encryption. Read below how to add that to the system without having to reinstall.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>The factory setup for Dell XPS and Precision with Linux is using just plain \/dev\/nvm0n1p1, p2 and p3 unencrypted partitions. So no matter how fancy your password is &#8211; all of your data may be exposed, should your laptop fall in the wrong hands. Fixing this is a relatively straight forward job, which I&#8217;ll describe below. You will need a large micro SD card &#8211; large enough to contain all of your data, plus an Ubuntu live CD (another 4Gb). I used a 256G micro SD card. (In the example a much smaller micro SD card is used).<\/p>\n\n\n\n<p>What we&#8217;ll do is: 1) prepare your system for LVM2 and LUKS, and make a backup; 2) prepare the micro SD card; 3) start the system from this card; 4) copy the root file system to an LVM2 volume on this card. 5) We&#8217;ll check &#8211; double-check; 6) then erase \/dev\/nvm0n1p3; make a new p3 and a p4; 7) we&#8217;ll prepare p4 to be an LVM2 physical volume and finally use pvmove to put the root file system back.<\/p>\n\n\n\n<p>Step 1. Install lvm2 and cryptsetup on your laptop. (Should you happen to have an Ubuntu <em>minimal<\/em> install, then these packages have not been installed, so we better make sure they are there). So go <em>apt-get install lvm2 cryptsetup.<\/em> Then make a proper backup. Doesn&#8217;t matter where you put it, but do make a backup. I personally use &#8220;rsync&#8221; a lot, something like sudo rsync -ax &#8211;numeric-ids &#8211;delete \/ \/media\/&#8230;.\/something  should do the job.<\/p>\n\n\n\n<p>Step 2. Download an installer CD for Ubuntu. Write it to your micro SD card using the Startup Disk Creator. This will create a 3Gb &#8220;iso9660&#8221; filesystem (a &#8220;DVD&#8221;), and an EFI partition of 4Mb. In the example below, our SD card can be found in <em>\/dev\/mmcblk0<\/em>.<\/p>\n\n\n\n<p>Now use &#8220;fdisk&#8221; to add an extra partition. Please note the &#8220;wipe&#8221; option &#8211; seemingly necessary to prevent fdisk from removing the iso9660 signature.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ sudo fdisk <strong>--wipe never<\/strong> \/dev\/mmcblk0\n Welcome to fdisk (util-linux 2.34).\n Changes will remain in memory only, until you decide to write them.\n Be careful before using the write command.\n Command (m for help): <strong>n<\/strong>\n Partition type\n    p   primary (2 primary, 0 extended, 2 free)\n    e   extended (container for logical partitions)\n Select (default p): <strong>p<\/strong>\n Partition number (3,4, default 3): <strong>&lt;enter&gt;<\/strong>\n First sector (5999872-30277631, default 6000640): <strong>&lt;enter&gt;<\/strong>\n Last sector, +\/-sectors or +\/-size{K,M,G,T,P} (6000640-30277631, default 30277631): <strong>&lt;enter&gt;<\/strong>\n Created a new partition 3 of type 'Linux' and of size 11,6 GiB.\n Command (m for help): <strong>w<\/strong>\n The partition table has been altered.\n Syncing disks.\n \n$ <strong>sudo fdisk -l \/dev\/mmcblk0<\/strong>\n Disk \/dev\/mmcblk0: 14,45 GiB, 15502147584 bytes, 30277632 sectors\n Units: sectors of 1 * 512 = 512 bytes\n Sector size (logical\/physical): 512 bytes \/ 512 bytes\n I\/O size (minimum\/optimal): 512 bytes \/ 512 bytes\n Disklabel type: dos\n Disk identifier: 0x2cf4ba3a\n Device         Boot   Start      End  Sectors  Size Id Type\n \/dev\/mmcblk0p1 *          0  5999871  5999872  2,9G  0 Empty\n \/dev\/mmcblk0p2      5271500  5279499     8000  3,9M ef EFI (FAT-12\/16\/32)\n \/dev\/mmcblk0p3      6000640 30277631 24276992 11,6G 83 LinuxDisk \/dev\/mmcblk0: 14,45<\/pre>\n\n\n\n<p>Let&#8217;s make this an LVM2 physical volume right away:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ <strong>sudo pvcreate \/dev\/mmcblk0p3 <\/strong>\n   Physical volume \"\/dev\/mmcblk0p3\" successfully created.\n$ <strong>sudo vgcreate vg42 \/dev\/mmcblk0p3<\/strong>\n   Volume group \"vg42\" successfully created\n$ <strong>sudo lvcreate -l 100%FREE -n root vg42<\/strong>\n   Logical volume \"root\" created.\n$ <strong>sudo mkfs -t ext4 \/dev\/vg42\/root<\/strong>\n mke2fs 1.45.5 (07-Jan-2020)\n Discarding device blocks: done                            \n Creating filesystem with 3034112 4k blocks and 758880 inodes\n Filesystem UUID: 6fe69999-5ee7-46ae-9c82-a4352806dd36\n Superblock backups stored on blocks: \n         32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208\n Allocating group tables: done                            \n Writing inode tables: done                            \n Creating journal (16384 blocks): done\n Writing superblocks and filesystem accounting information: done <\/pre>\n\n\n\n<p>As you can see, I am not encrypting the SD card. This means that you should properly wipe your copy once you&#8217;re done, because all of your files will be readable from this micro SD card. You could argue that an encrypted \/dev\/mmcblk0p3 is better, feel free to implement that, it&#8217;s not hard (cryptsetup luksFormat, then luksOpen and finally make the lvm disk on the luks opened device).<\/p>\n\n\n\n<p>3) Now start your laptop from this live Ubuntu system on the new SD card. That will probably involve some BIOS setup trickery, telling your laptop to &#8220;boot from sd card&#8221; and turning &#8220;fast bios&#8221; off. Please ask the internet how to proceed, it&#8217;s not in this blog post.<\/p>\n\n\n\n<p>Select &#8220;Try Ubuntu&#8221; once the graphical desktop is visible. Then start a terminal and type &#8220;sudo -i&#8221; to become root.<\/p>\n\n\n\n<p>4) mount both the original root-filesystem from your laptop and the newly made LVS partition. Proceed as follows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># <strong>mkdir \/mnt\/source \/mnt\/target<\/strong>\n# <strong>mount -o ro \/dev\/nvm0n1p3 \/mnt\/source<\/strong>\n# <strong>mount \/dev\/vg42\/root \/mnt\/target<\/strong><\/pre>\n\n\n\n<p>You are now ready to copy everything from source to destination. Choose your favourite copying program. You can use &#8220;cp&#8221; (but mind any hidden directories that your root fs could theoretically have) or use &#8220;rsync&#8221;:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># <strong>cp -a \/mnt\/source\/* \/mnt\/target\/<\/strong>\nor use\n# <strong>rsync -a \/mnt\/source\/ \/mnt\/target\/<\/strong>\n... or when you are resuming an aborted copy, you might want to\n# <strong>rsync -a --delete \/mnt\/source\/ \/mnt\/target\/<\/strong> <\/pre>\n\n\n\n<p>So let&#8217;s assume you copied everything and are ready to try start your new root filesystem. This is going to be a bit tricky: we are going to tell the operating system to start from our new \/dev\/vg42\/root, but in order to do that, we must change files in the <em>original<\/em> root directory.<\/p>\n\n\n\n<p>Yes, you read that right: the boot sequence of the laptop will, for now, be unchanged and it will first try to start a boot loader from the EFI partition; and in turn, this bootloader will fetch its configuration from the \/boot directory inside \/dev\/nvm0n1p3. So here is our next step:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># <strong>mount -o remount,rw \/mnt\/source\/<\/strong>\nFire up your favourite editor and edit grub.cfg\n# <strong>nano \/mnt\/source\/boot\/grub\/grub.cfg<\/strong><\/pre>\n\n\n\n<p>Find menuentry &#8220;Ubuntu&#8221; and change the root filesystem from <em>UUID=&#8230;.<\/em> to \/dev\/vg42\/root:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">menuentry 'Ubuntu' --class ubuntu --class [...] {\n[...]\n         fi\n         linux   \/vmlinuz-5.11.0-43-generic <strong>root=\/dev\/vg42\/root<\/strong> ro  quiet splash $vt_handoff\n         initrd  \/initrd.img-5.11.0-43-generic\n }<\/pre>\n\n\n\n<p>Also, to not annoy the OS too much, we&#8217;ll change \/etc\/fstab to be in sync with our new setup: our root filesystem now is on LVM2, and is called \/dev\/vg42\/root.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># <strong>nano \/mnt\/target\/etc\/fstab<\/strong>\nchange UUID=...... to \/dev\/vg42\/root\n# <strong>umount \/mnt\/target<\/strong><\/pre>\n\n\n\n<p>If you&#8217;re feeling confident, adventurous or simply irresponsible, you could use <em>sed<\/em> to change the file for you, replace the <em>nano<\/em> command above with:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># <strong>sed -i 's#^UUID.* \/ #\/dev\/vg42\/root \/ #' \/mnt\/target\/etc\/fstab<\/strong>(<\/pre>\n\n\n\n<p>Unmount \/mnt\/source and reboot the laptop. It should now boot with \/dev\/vg42\/root as its root filesystem. (However, beware: if you run &#8220;update-grub&#8221;, your change will be gone and the original \/dev\/nvm0n1p3 will be used for root filesystem). We&#8217;re half way now, and ready to encrypt the disk.<\/p>\n\n\n\n<p>This is the tricky part: removal of the original root filesystem, repartitioning the hard drive and setting up the encrypted filesystem. Oh, for reasons too complicated to explain, my example doesn&#8217;t use a GPT partition table. That is why the question about a &#8220;primary&#8221; or &#8220;extended&#8221; partition comes up. Please ignore this.<\/p>\n\n\n\n<p>We will use fdisk to remove the original \/dev\/nvm0n1p3, add a small 500Mb boot partition \/dev\/nvm0n1p3 and an encrypted \/dev\/nvm0n1p4:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">~# <strong>fdisk \/dev\/nvm0n1<\/strong>\nCommand (m for help): <strong>d<\/strong>\n Partition number (1-3, default 3): <strong>3<\/strong>\n Partition 3 has been deleted.\n Command (m for help): <strong>n<\/strong>\n Partition type\n    p   primary (2 primary, 0 extended, 2 free)\n    e   extended (container for logical partitions)\n Select (default p): <strong>p<\/strong>\n Partition number (3,4, default 3): <strong>3<\/strong>\n First sector (10741760-488397167, default 10741760): <strong>&lt;enter&gt;<\/strong>\n Last sector, +\/-sectors or +\/-size{K,M,G,T,P} (10741760-488397167, default 488397167): <strong>+500M<\/strong>\n Created a new partition 3 of type 'Linux' and of size 500 MiB.\n Partition #3 contains a ext4 signature.\n Do you want to remove the signature? [Y]es\/[N]o: <strong>y<\/strong>\n The signature will be removed by a write command.\n Command (m for help): <strong>n<\/strong>\n Partition type\n    p   primary (3 primary, 0 extended, 1 free)\n    e   extended (container for logical partitions)\n Select (default e): <strong>p<\/strong>\n Selected partition 4\n First sector (11765760-488397167, default 11765760): <strong>&lt;enter&gt;<\/strong>\n Last sector, +\/-sectors or +\/-size{K,M,G,T,P} (11765760-488397167, default 488397167): <strong>&lt;enter&gt;<\/strong>\n Created a new partition 4 of type 'Linux' and of size 227,3 GiB.\n Command (m for help): <strong>w<\/strong>\n The partition table has been altered.\n Syncing disks.\n~# <strong>mkfs -t ext4 \/dev\/nvm0n1p3 <\/strong>\n mke2fs 1.45.5 (07-Jan-2020)\n Discarding device blocks: done                            \n Creating filesystem with 128000 4k blocks and 128000 inodes\n Filesystem UUID: 61094874-5c78-4688-b1b2-133314347e6e\n Superblock backups stored on blocks: \n     32768, 98304\n Allocating group tables: done                            \n Writing inode tables: done                            \n Creating journal (4096 blocks): done\n Writing superblocks and filesystem accounting information: done<\/pre>\n\n\n\n<p>Now partitioning is done, we&#8217;ll first populate the new \/boot partition. This one is <em>not<\/em> encrypted, which means someone with physical access to your laptop could still try to mess up your kernel and\/or initrd in order to gain access to your files. It is possible to encrypt grub and the \/boot partition &#8211; <a href=\"https:\/\/help.ubuntu.com\/community\/Full_Disk_Encryption_Howto_2019\">read all about it<\/a>. But hey: here we are just encrypting your root and home, we aren&#8217;t hardening your laptop. So let&#8217;s proceed:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">~# <strong>umount \/boot\/efi <\/strong>\n~# <strong>mv \/boot\/ \/boot_orig<\/strong>\n~# <strong>mkdir \/boot<\/strong>\n~# <strong>mount \/dev\/nvm0n1p3 \/boot<\/strong>\n~# <strong>mv \/boot_orig\/* \/boot<\/strong>\n~# <strong>rmdir \/boot_orig<\/strong>\n~# <strong>echo \"UUID=<code>`lsblk -no UUID \/dev\/nvm0n1p3`<\/code> \/boot ext4 defaults 0 2\" &gt;&gt; \/etc\/fstab<\/strong>\n~# <strong>mount \/boot\/efi<\/strong><\/pre>\n\n\n\n<p>Our nvm0n1p3 is ready as a \/boot disk. Please note that in order for the grub bootloader to find it, we will need to run grub-install &#8211; we will do that after our encrypted partition is ready. Let&#8217;s proceed to encrypt nvm0n1p4:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">~# <strong>cryptsetup luksFormat \/dev\/nvm0n1p4 <\/strong>\n WARNING!\n This will overwrite data on \/dev\/nvm0n1p4 irrevocably.\n Are you sure? (Type uppercase yes): <strong>YES<\/strong>\n Enter passphrase for \/dev\/nvm0n1p4: <strong>&lt;some-very-secret-password&gt;<\/strong>\n Verify passphrase: <strong>&lt;some-very-secret-password&gt;<\/strong>\n~# <strong>cryptsetup luksOpen \/dev\/nvm0n1p4 datavault<\/strong>\n Enter passphrase for \/dev\/nvm0n1p4: <strong>&lt;some-very-secret-password&gt;<\/strong>\n~# <strong>echo \"datavault UUID=<code>`lsblk -no UUID \/dev\/nvm0n1p4<\/code>` none luks,discard\" &gt;&gt; \/etc\/crypttab<\/strong> \n~# <strong>grub-install<\/strong>\n Installing for x86_64-efi platform.\n Installation finished. No error reported.\n~# <strong>update-initramfs -u -k all<\/strong>\nupdate-initramfs: Generating \/boot\/initrd.img-5.11.0-43-generic\nupdate-initramfs: Generating \/boot\/initrd.img-5.11.0-27-generic\n~# <strong>update-grub<\/strong>\n Sourcing file <code>\/etc\/default\/grub' Sourcing file<\/code>\/etc\/default\/grub.d\/init-select.cfg'\n Generating grub configuration file \u2026\n Found linux image: \/boot\/vmlinuz-5.11.0-43-generic\n Found initrd image: \/boot\/initrd.img-5.11.0-43-generic\n Found linux image: \/boot\/vmlinuz-5.11.0-27-generic\n Found initrd image: \/boot\/initrd.img-5.11.0-27-generic\n Adding boot menu entry for UEFI Firmware Settings\n done<\/pre>\n\n\n\n<p>All right. We are almost there. Let&#8217;s try to reboot. If all goes well, we will have a booted Linux machine, starting from \/dev\/vg42\/root, and because we have setup a crypttab entry, it will ask for a password during the boot sequence.<\/p>\n\n\n\n<p>If all is well, you can re-login. Check that \/dev\/mapper\/datavault exists and if it does, proceed to move all data back to the internal disk:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">~# <strong>pvcreate \/dev\/mapper\/datavault <\/strong>\n   Physical volume \"\/dev\/mapper\/datavault\" successfully created.\n~# <strong>vgextend vg42 \/dev\/mapper\/datavault <\/strong>\n   Volume group \"vg42\" successfully extended\n~# <strong>pvmove \/dev\/mmcblk0p3 <\/strong>\n   \/dev\/mmcblk0p3: Moved: 0,17%\n   \/dev\/mmcblk0p3: Moved: 3,95%\n   \/dev\/mmcblk0p3: Moved: 7,59%\n   \/dev\/mmcblk0p3: Moved: 11,20%\n   \/dev\/mmcblk0p3: Moved: 14,85%\n\u2026.<\/pre>\n\n\n\n<p>This could take a while. In the mean time, you can use your laptop. And when Moved is a 100% ready, you&#8217;ll just need to remove \/dev\/mmcblk0p3 from the LVM2 volumes. For some odd reason (hidden deep inside the initrd), removing PV \/dev\/mmcblk0p3 will prevent initrd from asking the crypt password for \/dev\/nvm0n1p4. You must re-generate initrd to fix this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">~# <strong>vgreduce vg42 \/dev\/mmcblk0p3<\/strong>\nremoved \"\/dev\/mmcblk0p3\" from volume group \"vg42\"\n~# <strong>pvremove \/dev\/mmcblk0p3<\/strong>\nLabels on physical volume \"\/dev\/mmcblk0p3\" successfully wiped\n~#  <strong>update-initramfs -u -k all<\/strong>\nupdate-initramfs: Generating \/boot\/initrd.img-5.11.0-43-generic\nupdate-initramfs: Generating \/boot\/initrd.img-5.11.0-27-generic<\/pre>\n\n\n\n<p>You may now eject the SD card and reboot once more. Your root disk has been encrypted. (Don&#8217;t forget to erase the micro SD card that you used to copy the files to. Do not just use &#8220;blkdiscard&#8221;, but empty the disk with &#8220;dd&#8221; or by other means if you are <em>really<\/em> paranoid).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Dell XPS and Precision laptops with Linux pre-installed are great for personal and business use. Unfortunately, they lack an important feature: their disks have been factory pre-installed without encryption. Read below how to add that to the system without having to reinstall.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[164,163,165,9,166,167],"class_list":["post-887","post","type-post","status-publish","format-standard","hentry","category-happy-hacking","tag-dell-precision","tag-dell-xps13","tag-encryption","tag-linux","tag-root-disk-encryption","tag-xps13-encryption"],"_links":{"self":[{"href":"https:\/\/valentijn.sessink.nl\/index.php?rest_route=\/wp\/v2\/posts\/887","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/valentijn.sessink.nl\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/valentijn.sessink.nl\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/valentijn.sessink.nl\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/valentijn.sessink.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=887"}],"version-history":[{"count":27,"href":"https:\/\/valentijn.sessink.nl\/index.php?rest_route=\/wp\/v2\/posts\/887\/revisions"}],"predecessor-version":[{"id":950,"href":"https:\/\/valentijn.sessink.nl\/index.php?rest_route=\/wp\/v2\/posts\/887\/revisions\/950"}],"wp:attachment":[{"href":"https:\/\/valentijn.sessink.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=887"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/valentijn.sessink.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=887"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/valentijn.sessink.nl\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=887"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}