ed_81

gentoo lvm on luks install (full-disk encrypt, inc. /boot)

Dec 15th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.93 KB | None | 0 0
  1. # gentoo lvm on luks install (full-disk encrypt, inc. /boot)
  2. # thanks to https://wycd.net/posts/2015-01-04-gentoo-full-disk-encryption-lvm-on-luks.html
  3. # note: you can't automate this entire script. it's merely a list of commands that you go through 1 by 1
  4. # note: systemd not supported
  5.  
  6. # download gentoo minimal installation iso from https://www.gentoo.org/downloads, write to USB stick
  7. dd if=install-amd64-minimal-20181213T214502Z.iso of=/dev/sdd bs=1M status=progress
  8.  
  9. # boot from USB, upon shell prompt, continue with commands.
  10. # note: make sure you have an internet connection
  11.  
  12. # find which HDD you want to install gentoo to. we're going to assume /dev/sda
  13. lsblk
  14.  
  15. # create new partition table
  16. dd if=/dev/zero of=/dev/sda bs=512 count=1 conv=notrunc
  17. echo -ne "n\np\n1\n\n\nt 1\n83\nw\n" | fdisk /dev/sda
  18.  
  19. # create a luks partition
  20. # to state the obvious: DON'T forget the password
  21. modprobe dm-crypt aes sha256
  22. cryptsetup luksFormat /dev/sda1
  23. cryptsetup luksOpen /dev/sda1 main
  24.  
  25. # create lvm disks, volumes and partitions on top of the luks partition
  26. # feel free to change how you partition your disk. make sure swap partition is
  27. # at least 2 times the size of your memory.
  28. # in this example, we're giving 1gb to /boot, 8gb of swap, and the root takes the rest
  29. pvcreate /dev/mapper/main
  30. vgcreate vg1 /dev/mapper/main
  31. lvcreate -L 1G       -n boot vg1
  32. lvcreate -L 8G       -n swap vg1
  33. lvcreate -l 100%FREE -n root vg1
  34.  
  35. # initialize filesystems on logical partitions
  36. mkfs.ext2 -L /boot /dev/vg1/boot
  37. mkswap    -L /swap /dev/vg1/swap
  38. mkfs.ext4 -L /     /dev/vg1/root
  39.  
  40. # mount our HDD partitions so we can write files to disk
  41. mount /dev/vg1/root /mnt/gentoo
  42. mkdir -p /mnt/gentoo/boot
  43. mount /dev/vg1/boot /mnt/gentoo/boot
  44. swapon /dev/vg1/swap
  45.  
  46. # download stage 3 tarball of your choice from from https://www.gentoo.org/downloads
  47. cd /mnt/gentoo
  48. wget http://distfiles.gentoo.org/releases/amd64/autobuilds/20181213T214502Z/stage3-amd64-20181213T214502Z.tar.xz
  49. tar xf stage3-amd64-20181213T214502Z.tar.xz
  50. rm -rf stage3-amd64-20181213T214502Z.tar.xz
  51. cp -L /etc/resolv.conf /mnt/gentoo/etc/
  52. mount -o bind /proc /mnt/gentoo/proc
  53. mount -o bind /sys /mnt/gentoo/sys
  54. mount -o bind /dev /mnt/gentoo/dev
  55.  
  56. # down the rabbit hole
  57. chroot /mnt/gentoo /bin/bash
  58. source /etc/profile && export PS1="(chroot) $PS1"
  59.  
  60. # sync portage
  61. emerge-webrsync
  62.  
  63. # set profile
  64. eselect profile list
  65. eselect profile set ###
  66.  
  67. # install vim if you're not a bitch
  68. emerge -av app-editors/vim app-misc/tmux
  69.  
  70. # set time
  71. ls /usr/share/zoneinfo
  72. echo "America/New_York" > /etc/timezone
  73. emerge --config sys-libs/timezone-data
  74.  
  75. # set locale
  76. vim /etc/locale.gen
  77. locale-gen
  78. eselect locale list
  79. eselect locale set ###
  80.  
  81. # add your HDD partitions to fstab:
  82. #     /dev/vg1/root /     ext4 defaults,noatime  0 1
  83. #     /dev/vg1/swap none  swap sw                0 0
  84. #     /dev/vg1/boot /boot ext2 noatime           0 1
  85. vim /etc/fstab
  86.  
  87. #change root password
  88. passwd
  89.  
  90. # system tools https://wiki.gentoo.org/wiki/Handbook:AMD64/Installation/Tools
  91. # dhcpcd       https://wiki.gentoo.org/wiki/Network_management_using_DHCPCD
  92. # note: additional config is required if you use wireless, read dhcpcd article
  93. emerge -av app-admin/syslog-ng sys-process/cronie net-misc/dhcpcd
  94. rc-update add syslog-ng default
  95. rc-update add cronie default
  96. rc-update add sshd default
  97. rc-update add dhcpcd default
  98.  
  99. # install genkernel with cryptsetup USE flag for luks support
  100. echo "sys-kernel/genkernel cryptsetup" >> /etc/portage/package.use/genkernel
  101. emerge -av sys-fs/cryptsetup sys-kernel/gentoo-sources sys-kernel/genkernel
  102.  
  103. # in genkernel, make sure the following is set
  104. #     # Don't overwrite /usr/src/linux/.config
  105. #     MRPROPER="no"
  106. #     # Number of logical CPUs
  107. #     MAKEOPTS="-j2"
  108. #     # Ability to open LVM partitions
  109. #     LVM="yes"
  110. #     # Ability to decrypt
  111. #     LUKS="yes"
  112. #     # Default real_root variable (redundant, because it's specified via GRUB2)
  113. #     REAL_ROOT="/dev/vg1/root"
  114. #     # This overlays arbitrary files into the initramfs
  115. #     INITRAMFS_OVERLAY="/boot/overlay"
  116. vim /etc/genkernel.conf
  117.  
  118. cd /usr/src/linux
  119. # 1. configure the kernel
  120. # https://wiki.gentoo.org/wiki/Handbook:AMD64/Installation/Kernel
  121. # 2. enable dm-crypt in the kernel config
  122. # https://wiki.gentoo.org/wiki/Dm-crypt#Kernel_Configuration
  123.  
  124. # as an example, here's my own 4.14.52 config, customize however you like, update it if using a newer kernel.
  125. # note: best to keep a backup of your configs for each kernel version
  126. wget -O .config_4.14.52 https://pastebin.com/raw/6WAuteW1
  127.  
  128. # to configure the kernel
  129. make menuconfig
  130. # you can load config files, make your modifications, and save them
  131. # loading: https://i.imgur.com/b3fwmnt.png
  132. # saving: https://i.imgur.com/hfKLGKd.png
  133. # assuming you save the new config as .config_4.14.52_modified
  134.  
  135. # this will take a while
  136. genkernel --lvm --luks --kernel-config=/usr/src/linux/.config_4.14.52_modified all
  137.  
  138. # grub
  139. echo "sys-boot/grub device-mapper" >> /etc/portage/package.use/grub
  140. emerge -av grub:2
  141. echo 'GRUB_ENABLE_CRYPTODISK=y' >> /etc/default/grub
  142. echo 'GRUB_CMDLINE_LINUX="udev dolvm crypt_root=/dev/sda1 root_key=disk.key real_root=/dev/vg1/root cryptdevice=/dev/sda1:vg1-boot"' >> /etc/default/grub
  143.  
  144. # install grub bootloader onto disk
  145. mkdir /boot/grub
  146. grub-mkconfig -o /boot/grub/grub.cfg
  147. grub-install /dev/sda
  148.  
  149. # add key so we don't have to enter luks password twice on boot
  150. # note: At rest, the key will always be safely residing on your encrypted disk.
  151. mkdir -p /boot/overlay/mnt/key
  152. dd bs=1024 count=4 if=/dev/urandom of=/boot/overlay/mnt/key/disk.key
  153. cryptsetup luksAddKey /dev/sda1 /boot/overlay/mnt/key/disk.key
  154.  
  155. # log out of chroot, remove usb/iso, reboot, login as root, then add a user
  156. useradd -m -G users,wheel,audio,video,usb -s /bin/bash joe
  157. passwd joe
  158.  
  159. SOME REMAINING RISKS
  160. Someone could still hack your BIOS
  161. Someone could still hack GRUB2
  162. Someone could still hack your hardware
  163. Someone could hold your kneecaps hostage with a $5 wrench
Add Comment
Please, Sign In to add comment