Note

The Funtoo Linux project has transitioned to "Hobby Mode" and this wiki is now read-only.

Difference between revisions of "Rootfs over encrypted lvm"

From Funtoo
Jump to navigation Jump to search
 
(40 intermediate revisions by the same user not shown)
Line 10: Line 10:


<console>
<console>
##r### ##b##dd if=/dev/zero of=/dev/sda3 bs=100M  
# ##i##dd if=/dev/zero of=/dev/sda3 bs=100M  
##r### ##b##dd if=/dev/urandom of=/dev/sda3 bs=100M
# ##i##dd if=/dev/urandom of=/dev/sda3 bs=100M
</console>
</console>
The <code>dd</code> part is optional, only for security reason by overwriting the lingering data on the device with random data. It takes around 6 hours to complete for a 200GB drive.
The <code>dd</code> part is optional, and the command only needs to be run for security reasons (i.e only if you had top secret files on your drive). The command overwrites the lingering data on the device with random data. It takes around 6 hours to complete for a 200GB drive.


Note that you will get a message about reaching the end of the device when the <code>dd</code> command has finished. This behavior is intended.
{{Note}} You will get a message about reaching the end of the device when the <code>dd</code> command has finished. This behavior is intended.


= Encrypting the drive =
= Encrypting the drive =
<console>
<console>
##r### ##b##cryptsetup --cipher aes-xts-plain64 luksFormat /dev/sda3
# ##i##cryptsetup --cipher aes-xts-plain64 luksFormat /dev/sda3
##r### ##b##cryptsetup luksOpen /dev/sda3 dmcrypt_root
# ##i##cryptsetup luksOpen /dev/sda3 dmcrypt_root
</console>
</console>


Line 28: Line 27:
= Create logical volumes =
= Create logical volumes =
<console>
<console>
##r### ##b##pvcreate /dev/mapper/dmcrypt_root
# ##i##pvcreate /dev/mapper/dmcrypt_root
##r### ##b##vgcreate vg /dev/mapper/dmcrypt_root
# ##i##vgcreate vg /dev/mapper/dmcrypt_root
##r### ##b##lvcreate -L10G --name root vg           
# ##i##lvcreate -L10G --name root vg           
##r### ##b##lvcreate -L2G --name swap vg
# ##i##lvcreate -L2G --name swap vg
##r### ##b##lvcreate -L5G --name portage vg
# ##i##lvcreate -L5G --name portage vg
##r### ##b##lvcreate -l 100%FREE -nhome vg
# ##i##lvcreate -l 100%FREE -nhome vg
</console>
</console>
Feel free to specify your desired size by altering the numbers after the -L flag. For example, to make your portage dataset 20GB's, use the flag -L20G instead of -L5G.
Feel free to specify your desired size by altering the numbers after the -L flag. For example, to make your portage dataset 20GB's, use the flag -L20G instead of -L5G.
Line 39: Line 38:
= Create a filesystem on volumes =
= Create a filesystem on volumes =
<console>
<console>
##r### ##b##mkfs.ext2 /dev/sda1
# ##i##mkfs.ext2 /dev/sda1
##r### ##b##mkswap /dev/mapper/vg-swap
# ##i##mkswap /dev/mapper/vg-swap
##r### ##b##mkfs.ext4 /dev/mapper/vg-root
# ##i##mkfs.ext4 /dev/mapper/vg-root
##r### ##b##mkfs.ext4 /dev/mapper/vg-portage
# ##i##mkfs.ext4 /dev/mapper/vg-portage
##r### ##b##mkfs.ext4 /dev/mapper/vg-home
# ##i##mkfs.ext4 /dev/mapper/vg-home
</console>
</console>


= Basic system setup =
= Basic system setup =
<console>
<console>
##r### ##b##swapon /dev/mapper/vg-swap
# ##i##swapon /dev/mapper/vg-swap
##r### ##b##mkdir /mnt/funtoo
# ##i##mkdir /mnt/funtoo
##r### ##b##mount /dev/mapper/vg-root /mnt/funtoo
# ##i##mount /dev/mapper/vg-root /mnt/funtoo
##r### ##b##mkdir /mnt/funtoo/boot
# ##i##mkdir -p /mnt/funtoo/{boot,usr/portage,home}
##r### ##b##mkdir -p /mnt/funtoo/usr/portage
# ##i##mount /dev/sda1 /mnt/funtoo/boot
##r### ##b##mkdir /mnt/funtoo/home
# ##i##mount /dev/mapper/vg-portage /mnt/funtoo/usr/portage
##r### ##b##mount /dev/sda1 /mnt/funtoo/boot
# ##i##mount /dev/mapper/vg-home /mnt/funtoo/home
##r### ##b##mount /dev/mapper/vg-portage /mnt/funtoo/usr/portage
##r### ##b##mount /dev/mapper/vg-home /mnt/funtoo/home
</console>
</console>
Now perform all the steps required for basic system install, please follow [http://docs.funtoo.org/wiki/Funtoo_Linux_Installation]
Now perform all the steps required for basic system install, please follow [http://docs.funtoo.org/wiki/Funtoo_Linux_Installation]
don't forget to emerge next packages:
don't forget to emerge the following before your install is finished:


<pre># emerge cryptsetup lvm2 grub foo-sources</pre>
* '''cryptsetup'''
* '''lvm2'''
* '''a bootloader (grub recommended)'''
* '''kernel sources (gentoo-sources recommended)'''


Re-emerge sys-apps/busybox and sys-fs/cryptsetup with the "static" USE flag
= Editing the fstab =
Fire up your favorite text editor to edit <code>/etc/fstab</code>. You want to put the following in the file:
<pre>
# <fs>                  <mountpoint>  <type>    <opts>                          <dump/pass>
/dev/sda1              /boot        ext2      noauto,noatime                  1 2
/dev/mapper/vg-swap    none          swap      sw                              0 0
/dev/mapper/vg-root    /             ext4      noatime,nodiratime,defaults    0 1
/dev/sr0                /mnt/cdrom    auto      noauto,ro                      0 0
/dev/mapper/vg-portage  /usr/portage  ext4      noatime,nodiratime              0 0
/dev/mapper/vg-home    /home        ext4      noatime,nodiratime              0 0
</pre>


= Kernel options =
= Kernel options =
Important, do not miss this part.
{{Note}}This part is particularly important: pay close attention.
Under General setup --->
{{kernelop
<pre>[*] Initial RAM filesystem and RAM disk (initramfs/initrd) support</pre>
|'''General setup --->'''
|'''[*] Initial RAM filesystem and RAM disk (initramfs/initrd) support'''
}}


Under Device Drivers --->  
{{kernelop
<pre>Generic Driver Options --->
|'''Device Drivers --->''' <br> '''Generic Driver Options --->'''
  [*] Maintain a devtmpfs filesystem to mount at /dev
|'''[*] Maintain a devtmpfs filesystem to mount at /dev''' <br>
[*] Multiple devices driver support  --->
}}
  <*>Device Mapper Support
  <*> Crypt target support
</pre>


Under Cryptographic API --->
{{kernelop
<pre>-*-AES cipher algorithms
|'''Device Drivers --->''' <br> '''[*] Multiple devices driver support --->'''
|'''<*>Device Mapper Support''' <br> '''<*> Crypt target support'''
}}


<*> XTS support (EXPERIMENTAL)</pre>
{{kernelop
|'''Cryptographic API --->'''
|'''-*-AES cipher algorithms''' <br> '''<*> XTS support'''
}}


= Initramfs setup and configuration =
== Better-initramfs ==
'''Build your initramfs with [https://bitbucket.org/piotrkarbowski/better-initramfs better-initramfs] project.'''


= Initramfs setup and configuration =
{{note}}better-initramfs supports neither dynamic modules nor udev, so you should compile your kernel with built-in support for your block devices.
Build your initramfs with [https://bitbucket.org/piotrkarbowski/better-initramfs better-initramfs] project.


{{fancynote|better-initramfs supports neither dynamic modules nor udev, so you should compile your kernel with built-in support for your block devices.}}
<console>
# ##i##cd /opt
# ##i##git clone git://github.com/slashbeast/better-initramfs.git
# ##i##cd better-initramfs
# ##i##less README.rst
# ##i##bootstrap/bootstrap-all
# ##i##make prepare
# ##i##make image
</console>


<pre># git clone git@bitbucket.org:piotrkarbowski/better-initramfs.git
Copy resulting <code>initramfs.cpio.gz</code> to <code>/boot</code>:
# cd better-initramfs
<console># ##i##cp output/initramfs.cpio.gz /boot</console>
# less README.rst
# bootstrap/bootstrap-all
# make prepare
# make image
</pre>


Copy resulting <code>initramfs.cpio.gz</code> to <code>/boot</code>.
Alternatively, a pre-compiled binary initramfs is available at https://bitbucket.org/piotrkarbowski/better-initramfs/downloads
<pre># cp output/initramfs.cpio.gz /boot
<console>
</pre>
# ##i##wget https://bitbucket.org/piotrkarbowski/better-initramfs/downloads/release-x86_64-v0.7.2.tar.bz2
Alternatively pre-compiled binary initramfs available at https://bitbucket.org/piotrkarbowski/better-initramfs/downloads
# ##i##tar xf release-x86_64-v0.5.tar.bz2
<pre># wget https://bitbucket.org/piotrkarbowski/better-initramfs/downloads/release-x86_64-v0.7.2.tar.bz2
# ##i##cd release*
# tar xf release-x86_64-v0.5.tar.bz2
# ##i##gzip initramfs.cpio
# cd release*
# ##i##cp initramfs.cpio.gz /boot
# gzip initramfs.cpio
</console>
# cp initramfs.cpio.gz /boot</pre>


Remember, better-initramfs project is a work in progress, so you need to update from time to time. It can be done easily with <code>git</code>. Go to the better-initramfs source dir and follow:
Remember, better-initramfs project is a work in progress, so you need to update from time to time. It can be done easily with <code>git</code>. Go to the better-initramfs source dir and follow:
<pre># git pull
<console>
# less ChangeLog
# ##i##cd /opt/better-initramfs
</pre>
# ##i##git pull
Please, read the ChangeLog carefuly and do necessary updates, to <code>/etc/boot.conf</code>, the example config below. Please, backup working <code>initramfs.cpio.gz</code> and <code>/etc/boot.conf</code> before updating initramfs.
# ##i##less ChangeLog
</console>
{{Note}}Please read the ChangeLog carefuly and perform necessary updates to <code>/etc/boot.conf</code>. Also, please backup the working <code>/boot/initramfs.cpio.gz</code> and <code>/etc/boot.conf</code> before updating better-initramfs.


= Genkernel approach =
== Genkernel ==
Funtoo's genkernel capable to create initramfs for encrypted drive. Compile and install kernel and initramfs of your favorite kernel sources:
Funtoo's genkernel capable to create initramfs for encrypted drive. Compile and install kernel and initramfs of your favorite kernel sources:
<pre>genkernel --kernel-config=/path/to/your/custom-kernel-config --no-mrproper --makeopts=-j5 --install --lvm --luks all</pre>
<console>
# ##i##genkernel --kernel-config=/path/to/your/custom-kernel-config --no-mrproper --makeopts=-j5 --install --lvm --luks all </console>
Configure the bootloader as described above, with correct kernel and initramfs images names. An example for genkernel and grub2:
Configure the bootloader as described above, with correct kernel and initramfs images names. An example for genkernel and grub2:


Line 127: Line 148:
   kernel kernel-genkernel-x86_64-2.6.39
   kernel kernel-genkernel-x86_64-2.6.39
   initrd initramfs-genkernel-x86_64-2.6.39
   initrd initramfs-genkernel-x86_64-2.6.39
   params += crypt_root=/dev/sda2 dolvm real_root=/dev/mapper/vg-root  rootfstype=ext4 resume=swap:/dev/mapper/vg-swap quiet
   params += crypt_root=/dev/sda3 dolvm real_root=/dev/mapper/vg-root  rootfstype=ext4 resume=swap:/dev/mapper/vg-swap quiet
}</pre>}}
}</pre>}}


= Grub2 configuration =
= Bootloader Configuration =
== Grub2 configuration ==
An example of <code>/etc/boot.conf</code> for better-initramfs
An example of <code>/etc/boot.conf</code> for better-initramfs
{{code|/etc/boot.conf|<pre>
{{code|/etc/boot.conf|<pre>
Line 141: Line 164:
   kernel bzImage[-v]
   kernel bzImage[-v]
   initrd /initramfs.cpio.gz
   initrd /initramfs.cpio.gz
   params += enc_root=/dev/sda2 lvm luks root=/dev/mapper/vg-root  rootfstype=ext4 resume=swap:/dev/mapper/vg-swap quiet
   params += enc_root=/dev/sda3 lvm luks root=/dev/mapper/vg-root  rootfstype=ext4 resume=swap:/dev/mapper/vg-swap quiet
}</pre>}}
}</pre>}}


{{code|/etc/fstab|
Now, run <code>boot-update</code> to write the configuration files to <code>/boot/grub/grub.cfg</code>
<pre>
# <fs>                 <mountpoint>  <type>   <opts>                          <dump/pass>
/dev/sda1              /boot         ext2      noauto,noatime                  1 2
/dev/mapper/vg-swap    none          swap      sw                              0 0
/dev/mapper/vg-root    /            ext4      noatime,nodiratime,defaults    0 1
/dev/sr0                /mnt/cdrom    auto      noauto,ro                      0 0
/dev/mapper/vg-portage  /usr/portage  reiserfs  noatime,nodiratime              0 0
/dev/mapper/vg-home    /home        xfs      noatime,nodiratime,osyncisdsync 0 0</pre>}}


= Lilo configuration =
== Lilo configuration ==
For oldschool geeks, an example for lilo bootloader. Emerge lilo with device-mapper support
For oldschool geeks, an example for lilo bootloader. Emerge lilo with device-mapper support
<pre>
<console>
# echo 'sys-boot/lilo device-mapper' >> /etc/portage/package.use/lilo
# ##i##echo 'sys-boot/lilo device-mapper' >> /etc/portage/package.use/lilo
# emerge lilo</pre>
# ##i##emerge lilo
</console>


{{code|/etc/lilo.conf|<pre>append="init=/linuxrc dolvm crypt_root=/dev/sda2 real_root=/dev/mapper/vg-root"
{{code|/etc/lilo.conf|<pre>append="init=/linuxrc dolvm crypt_root=/dev/sda2 real_root=/dev/mapper/vg-root"
Line 172: Line 188:
label=funtoo
label=funtoo
</pre>}}
</pre>}}
= Syslinux bootloader setup =
 
Syslinux is another advanced bootloader which you can find on all live CD's.  
== Syslinux bootloader setup ==
Syslinux is another advanced bootloader which you can find on all live CD's. Syslinux bootloader does not require additional BIOS boot partition. /dev/sda2 is the root partition.
<pre>
<pre>
# emerge syslinux
# emerge syslinux
Line 191: Line 208:
= Final steps =
= Final steps =
Umount everything, close encrypted drive and reboot
Umount everything, close encrypted drive and reboot
<pre>umount /mnt/funtoo/proc (/dev, /home, /usr/portage, /boot)
<console>
vgchange -a n
# ##i##umount -l -v /mnt/funtoo/{dev, proc, home, usr/portage, boot}
cryptsetup luksClose /dev/sda2 dmcrypt_root</pre>
# ##i##vgchange -a n
# ##i##cryptsetup luksClose /dev/sda2 dmcrypt_root
</console>
After reboot you will get the following:
After reboot you will get the following:
<pre>>>> better-initramfs started. Kernel version 2.6.35-gentoo-r10
<pre>>>> better-initramfs started. Kernel version 2.6.35-gentoo-r10
Line 226: Line 245:
* [[gentoo-wiki:Root filesystem over LVM2, DM-Crypt and RAID|Root filesystem over LVM2, DM-Crypt, and RAID]]
* [[gentoo-wiki:Root filesystem over LVM2, DM-Crypt and RAID|Root filesystem over LVM2, DM-Crypt, and RAID]]
* [http://wiki.archlinux.org/index.php/System_Encryption_with_LUKS_for_dm-crypt System Encryption with LUKS for dm-crypt]
* [http://wiki.archlinux.org/index.php/System_Encryption_with_LUKS_for_dm-crypt System Encryption with LUKS for dm-crypt]
* [http://en.wikipedia.org/wiki/Logical_volume_management Wikipedia article on LVM]
* [https://wiki.archlinux.org/index.php/Dm-crypt_with_LUKS Arch Wiki article]


[[Category:HOWTO]]
[[Category:HOWTO]]

Revision as of 15:55, December 10, 2013

This howto describes how to setup LVM and rootfs with cryptoLUKS-encrypted drive

Prepare the hard drive and partitions

This is an example partition scheme, you may want to choose differently. /dev/sda1 used as /boot. /dev/sda2 will be encrypted drive with LVM.

  • /dev/sda1 -- /boot partition.
  • /dev/sda2 -- BIOS boot partition (not needed for MBR - only needed if you are using GPT) This step required for GRUB2. For more info, see: [1] for more information on GPT and MBR.
  • /dev/sda3 -- / partition, will be the drive with LUKS and LVM.
root # dd if=/dev/zero of=/dev/sda3 bs=100M 
root # dd if=/dev/urandom of=/dev/sda3 bs=100M

The dd part is optional, and the command only needs to be run for security reasons (i.e only if you had top secret files on your drive). The command overwrites the lingering data on the device with random data. It takes around 6 hours to complete for a 200GB drive.

   Note

{{{1}}}

You will get a message about reaching the end of the device when the dd command has finished. This behavior is intended.

Encrypting the drive

root # cryptsetup --cipher aes-xts-plain64 luksFormat /dev/sda3
root # cryptsetup luksOpen /dev/sda3 dmcrypt_root

There you'll be prompted to enter your password phrase for encrypted drive, type your paranoid password there.

Create logical volumes

root # pvcreate /dev/mapper/dmcrypt_root
root # vgcreate vg /dev/mapper/dmcrypt_root
root # lvcreate -L10G --name root vg           
root # lvcreate -L2G --name swap vg
root # lvcreate -L5G --name portage vg
root # lvcreate -l 100%FREE -nhome vg

Feel free to specify your desired size by altering the numbers after the -L flag. For example, to make your portage dataset 20GB's, use the flag -L20G instead of -L5G.

Create a filesystem on volumes

root # mkfs.ext2 /dev/sda1
root # mkswap /dev/mapper/vg-swap
root # mkfs.ext4 /dev/mapper/vg-root
root # mkfs.ext4 /dev/mapper/vg-portage
root # mkfs.ext4 /dev/mapper/vg-home

Basic system setup

root # swapon /dev/mapper/vg-swap
root # mkdir /mnt/funtoo
root # mount /dev/mapper/vg-root /mnt/funtoo
root # mkdir -p /mnt/funtoo/{boot,usr/portage,home}
root # mount /dev/sda1 /mnt/funtoo/boot
root # mount /dev/mapper/vg-portage /mnt/funtoo/usr/portage
root # mount /dev/mapper/vg-home /mnt/funtoo/home

Now perform all the steps required for basic system install, please follow [2] don't forget to emerge the following before your install is finished:

  • cryptsetup
  • lvm2
  • a bootloader (grub recommended)
  • kernel sources (gentoo-sources recommended)

Editing the fstab

Fire up your favorite text editor to edit /etc/fstab. You want to put the following in the file:

# <fs>                  <mountpoint>  <type>    <opts>                          <dump/pass>
/dev/sda1               /boot         ext2      noauto,noatime                  1 2
/dev/mapper/vg-swap     none          swap      sw                              0 0
/dev/mapper/vg-root     /             ext4      noatime,nodiratime,defaults     0 1
/dev/sr0                /mnt/cdrom    auto      noauto,ro                       0 0
/dev/mapper/vg-portage  /usr/portage  ext4      noatime,nodiratime              0 0
/dev/mapper/vg-home     /home         ext4      noatime,nodiratime              0 0

Kernel options

   Note

{{{1}}}

This part is particularly important: pay close attention.








Initramfs setup and configuration

Better-initramfs

Build your initramfs with better-initramfs project.

   Note

{{{1}}}

better-initramfs supports neither dynamic modules nor udev, so you should compile your kernel with built-in support for your block devices.

root # cd /opt
root # git clone git://github.com/slashbeast/better-initramfs.git
root # cd better-initramfs
root # less README.rst
root # bootstrap/bootstrap-all
root # make prepare
root # make image

Copy resulting initramfs.cpio.gz to /boot:

root # cp output/initramfs.cpio.gz /boot

Alternatively, a pre-compiled binary initramfs is available at https://bitbucket.org/piotrkarbowski/better-initramfs/downloads

root # wget https://bitbucket.org/piotrkarbowski/better-initramfs/downloads/release-x86_64-v0.7.2.tar.bz2
root # tar xf release-x86_64-v0.5.tar.bz2
root # cd release*
root # gzip initramfs.cpio
root # cp initramfs.cpio.gz /boot

Remember, better-initramfs project is a work in progress, so you need to update from time to time. It can be done easily with git. Go to the better-initramfs source dir and follow:

root # cd /opt/better-initramfs
root # git pull
root # less ChangeLog
   Note

{{{1}}}

Please read the ChangeLog carefuly and perform necessary updates to /etc/boot.conf. Also, please backup the working /boot/initramfs.cpio.gz and /etc/boot.conf before updating better-initramfs.

Genkernel

Funtoo's genkernel capable to create initramfs for encrypted drive. Compile and install kernel and initramfs of your favorite kernel sources:

root # genkernel --kernel-config=/path/to/your/custom-kernel-config --no-mrproper --makeopts=-j5 --install --lvm --luks all 

Configure the bootloader as described above, with correct kernel and initramfs images names. An example for genkernel and grub2:

Template:Code


Bootloader Configuration

Grub2 configuration

An example of /etc/boot.conf for better-initramfs Template:Code

Now, run boot-update to write the configuration files to /boot/grub/grub.cfg

Lilo configuration

For oldschool geeks, an example for lilo bootloader. Emerge lilo with device-mapper support

root # echo 'sys-boot/lilo device-mapper' >> /etc/portage/package.use/lilo
root # emerge lilo

Template:Code

Syslinux bootloader setup

Syslinux is another advanced bootloader which you can find on all live CD's. Syslinux bootloader does not require additional BIOS boot partition. /dev/sda2 is the root partition.

# emerge syslinux
# mkdir /boot/extlinux
# extlinux --install /boot/extlinux
# dd bs=440 conv=notrunc count=1 if=/usr/share/syslinux/mbr.bin of=/dev/sda
- or -
# sgdisk /dev/sda --attributes=1:set:2
# dd bs=440 conv=notrunc count=1 if=/usr/share/syslinux/gptmbr.bin of=/dev/sda, for GPT partition

Template:Code

Final steps

Umount everything, close encrypted drive and reboot

root # umount -l -v /mnt/funtoo/{dev, proc, home, usr/portage, boot} 
root # vgchange -a n
root # cryptsetup luksClose /dev/sda2 dmcrypt_root

After reboot you will get the following:

>>> better-initramfs started. Kernel version 2.6.35-gentoo-r10
>>> Create all the symlinks to /bin/busybox.
>>> Initiating /dev/dir
>>> Getting LVM volumes up (if any)
Reding all physical volumes. This make take awhile...
No volume group found
No volume group found
>>> Opening encrypted partition and mapping to /dev/mapper/dmcrypt_root
Enter passphrase fore /dev/sda2:

Type your password

>>> Again, getting LVM volumes up (if any, after map dmcrypt).
  Reading all physical volumes.  This may take a while...
  Found volume group "vg" using metadata type lvm2
  4 logical volume(s) in volume group "vg" now active
>>> Mounting rootfs to /newroot
>>> Umounting /sys and /proc.
>>> Switching root to /newroot and executing /sbin/init.
INIT: version 2.88 booting
Loading /libexec/rc/console/keymap
  OpenRC 0.6.1 is starting up Funtoo Linux (x86_64)
...boot messages omitted for clarity
   
orion login: oleg
Password:
Last login: Thu Oct 14 20:49:21 EEST 2010 on tty1
oleg@orion ~ %

Additional links