{"id":1349,"date":"2015-10-09T17:59:49","date_gmt":"2015-10-10T00:59:49","guid":{"rendered":"http:\/\/jim-zimmerman.com\/?p=1349"},"modified":"2015-10-22T18:01:40","modified_gmt":"2015-10-23T01:01:40","slug":"centos-7-create-encrypted-partition","status":"publish","type":"post","link":"https:\/\/jim-zimmerman.com\/?p=1349","title":{"rendered":"CentOS 7 &#8211; Create encrypted partition"},"content":{"rendered":"<p>Note: This is to create a new partition that is encrypted.  Do not do this on an existing partion, because you will lose all the data on the partition.<\/p>\n<p>Note: I added a  summarization to the end of this post to provide a bit more clarity about the volume names, etc.<\/p>\n<p>Add the disk to the system and identify it.  I used the following:<br \/>\nSee if it is there already:<\/p>\n<blockquote><p># fdisk -l<\/p><\/blockquote>\n<p>If not, scan for it on all your buses:<\/p>\n<blockquote><p># echo &#8220;- &#8211; -&#8221; > \/sys\/class\/scsi_host\/host0\/scan<br \/>\n# echo &#8220;- &#8211; -&#8221; > \/sys\/class\/scsi_host\/host1\/scan<br \/>\n# echo &#8220;- &#8211; -&#8221; > \/sys\/class\/scsi_host\/host2\/scan <\/p><\/blockquote>\n<p>Check again:<\/p>\n<blockquote><p># fdisk -l<\/p><\/blockquote>\n<p>Create a volume:<br \/>\nAdd the physical disk:<\/p>\n<blockquote><p># pvcreate \/dev\/sdb<\/p><\/blockquote>\n<p>Create a volume group;<\/p>\n<blockquote><p># vgcreate centos_test \/dev\/sdb<\/p><\/blockquote>\n<p>Activate the volume group:<\/p>\n<blockquote><p># vgchange -a y  centos_test <\/p><\/blockquote>\n<p>Create the volume:<\/p>\n<blockquote><p># lvcreate -l 100%FREE -n test centos_test<\/p><\/blockquote>\n<p>Write random data to the partition.  This is important when reusing a volume.<\/p>\n<blockquote><p># shred -v &#8211;iterations=1 \/dev\/centos_test\/test<\/p><\/blockquote>\n<p>Install cryptsetup:<\/p>\n<blockquote><p># yum install cryptsetup<\/p><\/blockquote>\n<p>Initialize the volume and set the passphrase:<\/p>\n<blockquote><p># cryptsetup &#8211;verbose &#8211;verify-passphrase luksFormat \/dev\/centos_test\/test<\/p><\/blockquote>\n<p>Open the volume and setup the mapping:<\/p>\n<blockquote><p># cryptsetup luksOpen \/dev\/centos_test test <\/p><\/blockquote>\n<p>Create the filesystem:<\/p>\n<blockquote><p># mkfs.ext3 \/dev\/mapper\/centos_test-test <\/p><\/blockquote>\n<p>Mount it:<\/p>\n<blockquote><p># mount \/dev\/mapper\/centos_test-test \/mnt <\/p><\/blockquote>\n<p>Add the volume to be mounted at boot to the crypttab file:<br \/>\n# vi \/etc\/crypttab<br \/>\n&#8230;<br \/>\ncentos_test-test \/dev\/centos_test\/test none<br \/>\n&#8230;<\/p>\n<p>Add the mount to the fstab:<\/p>\n<blockquote><p># vi \/etc\/fstab<br \/>\n&#8230;<br \/>\n\/dev\/mapper\/centos_test-test \/mnt  ext3    defaults        1 2<br \/>\n&#8230;<\/p><\/blockquote>\n<p>Restore selinux context:<\/p>\n<blockquote><p># \/sbin\/restorecon -v -R \/mnt <\/p><\/blockquote>\n<p>I was not getting prompted for the passphrase at boot.  So, I had to boot into single user mode.  When I did, I was prompted for the passphrase and the partition mounted fine.  I needed to do remove the rhgb parameter from the boot parameters to be prompted when booting into multi-user mode:<\/p>\n<blockquote><p># cd \/etc\/default<\/p><\/blockquote>\n<p>Remove the rhgb parameter from kernel parameters.<\/p>\n<blockquote><p># vi grub <\/p><\/blockquote>\n<p>I removed the rhgb parameter from this line:<\/p>\n<blockquote><p>GRUB_CMDLINE_LINUX=&#8221;rd.lvm.lv=centos\/swap vconsole.font=latarcyrheb-sun16 crashkernel=auto  vconsole.keymap=us rd.lvm.lv=centos\/root quiet&#8221;<\/p><\/blockquote>\n<p>Update grub with the new settings:<\/p>\n<blockquote><p># grub2-mkconfig -o \/boot\/grub2\/grub.cfg <\/p><\/blockquote>\n<p>When you reboot, you will be prompted for the passphrase you set when prompted in the cryptsetup.<\/p>\n<blockquote><p># shutdown -r now<\/p><\/blockquote>\n<p>Here is a short summary.  Pay particular attention to the luksOpen and mount command and the format of the crypttab and fstab files.  Hopefully, these names will make it easier to keep straight.:<\/p>\n<blockquote><p># fdisk -l<br \/>\n# fdisk \/dev\/sdb<br \/>\n# shred -v &#8211;iterations=1 \/dev\/sdb<br \/>\n# pvcreate \/dev\/sdb<br \/>\n# vgcreate vgtest \/dev\/sdb<br \/>\n# vgchange -a y vgtest<br \/>\n# lvcreate -l 100%FREE -n lvtest vgtest<br \/>\n# shred -v &#8211;iterations=1 \/dev\/vgtest\/lvtest<br \/>\n# cryptsetup &#8211;verbose &#8211;verify-passphrase luksFormat \/dev\/vgtest\/lvtest<br \/>\n# cryptsetup luksOpen <strong>\/dev\/mapper\/vgtest-lvtest<\/strong> lvtest<br \/>\n# mkfs -t ext4 \/dev\/mapper\/lvtest<br \/>\n# mount <strong>\/dev\/mapper\/lvtest<\/strong> \/mnt<\/p>\n<p># vi \/etc\/crypttab<br \/>\n&#8230;<br \/>\nlvtest\t<strong>\/dev\/mapper\/vgtest-lvtest<\/strong>\tnone<br \/>\n&#8230;<\/p>\n<p># vi \/etc\/fstab<br \/>\n&#8230;<br \/>\n<strong>\/dev\/mapper\/lvtest<\/strong>\t\/mnt\text4\tdefaults\t1 2<br \/>\n&#8230;\n<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Note: This is to create a new partition that is encrypted. Do not do this on an existing partion, because you will lose all the data on the partition. Note: I added a summarization to the end of this post to provide a bit more clarity about the volume names, etc. Add the disk to [&#038;hellip<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1349","post","type-post","status-publish","format-standard","hentry","category-documentation"],"share_on_mastodon":{"url":"","error":""},"_links":{"self":[{"href":"https:\/\/jim-zimmerman.com\/index.php?rest_route=\/wp\/v2\/posts\/1349","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jim-zimmerman.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jim-zimmerman.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jim-zimmerman.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jim-zimmerman.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1349"}],"version-history":[{"count":7,"href":"https:\/\/jim-zimmerman.com\/index.php?rest_route=\/wp\/v2\/posts\/1349\/revisions"}],"predecessor-version":[{"id":1363,"href":"https:\/\/jim-zimmerman.com\/index.php?rest_route=\/wp\/v2\/posts\/1349\/revisions\/1363"}],"wp:attachment":[{"href":"https:\/\/jim-zimmerman.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1349"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jim-zimmerman.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1349"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jim-zimmerman.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1349"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}