mirror of the now-defunct rocklinux.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

265 lines
9.4 KiB

  1. #!/bin/bash
  2. read a b version c < /proc/version
  3. while read device mountpoint status ; do
  4. [ "${status}" == "plain" ] && continue
  5. if [ ${rootfsmounted} -eq 0 -a "${mountpoint}" != "/" ] ; then # we need to have the rootfs mounted for all other filesystems to be mountable
  6. echo "Mounting rootfs (${rootfs}) on /root"
  7. initrd_mount ${rootfs} /root
  8. rootfsmounted=1
  9. fi
  10. if [ "${status}" == "encrypt" ] ; then # {{{
  11. echo "Encrypting ${device} on ${mountpoint}"
  12. echo
  13. echo "WARNING! Although this is usually safe you should make sure that your backups"
  14. echo "are recent and working. Just in case something happens (power loss, ...)."
  15. echo "If this process is interrupted your filesystem WILL BE INACCESSIBLE!"
  16. echo
  17. pass1="MEEP"
  18. pass2="MOOP"
  19. while [ "${pass1}" != "${pass2}" ] ; do
  20. echo -n "Please enter the passphrase for encryption: "
  21. read -s pass1 < /dev/console
  22. echo
  23. if [ -z "${pass1:20}" ] ; then
  24. echo "Your passphrase is short and may thus be insecure."
  25. echo -n "Enter it again to use it anyway: "
  26. read -s passa < /dev/console
  27. echo
  28. if [ "${passa}" != "${pass1}" ] ; then
  29. passa="ABCD"
  30. pass1="MEEP"
  31. pass2="MOOP"
  32. continue
  33. fi
  34. passa="ABCD"
  35. fi
  36. echo -n "Please confirm the passphrase: "
  37. read -s pass2 < /dev/console
  38. echo
  39. [ "${pass1}" != "${pass2}" ] && echo "The passphrases do not match!"
  40. done
  41. pass1="`echo ${pass1} | md5sum`"
  42. pass1=${pass1%% *}
  43. encryptedname=${device//\//_}_encrypted
  44. encryptedname=${encryptedname#_}
  45. echo -n "Setting up encryption now ... "
  46. echo 0 `/sbin/blockdev --getsize ${device}` crypt aes-plain ${pass1} 0 ${device} 0 | /sbin/dmsetup create ${encryptedname}
  47. echo "done"
  48. echo "I will now do a sanity check of the harddisk. This means"
  49. echo "that the encryption process will be simulated by writing"
  50. echo "to /dev/null instead of the encrypted partition."
  51. echo "This ensures that the whole disk is readable and the"
  52. echo "copy process won't fail because of a bad harddisk."
  53. echo
  54. echo -n "Press enter to continue ... "
  55. read </dev/console
  56. if /bin/dd if=/dev/zero of=/dev/null conv=stat count=1 2>/dev/null ; then
  57. echo "Starting dd, this will take some time. Go have some coffee :-)"
  58. dd if=${device} of=/dev/null bs=1k conv=stat # conv=stat is my personal patch -- BRS
  59. error=${?}
  60. else
  61. echo "Starting dd, this will take some time. No output will happen"
  62. echo "while this is running. Go have some coffee :-)"
  63. dd if=${device} of=/dev/null bs=1k
  64. error=${?}
  65. fi
  66. if [ ${error} != 0 ] ; then
  67. echo "An error occured!"
  68. echo "Cowardly refusing to encrypt ${device}!"
  69. /sbin/dmsetup remove ${encryptedname}
  70. echo "Starting a shell"
  71. echo
  72. exec /bin/bash
  73. continue
  74. fi
  75. echo "Now the critical part of the encryption process starts."
  76. echo "I'm now copying the data bytewise from the unencrypted device to the"
  77. echo "encrypted loopdevice. Make absolutely sure that this process won't be"
  78. echo "interrupted!"
  79. echo
  80. echo -n "Press enter to start encrypting ... "
  81. read < /dev/console
  82. if /bin/dd if=/dev/zero of=/dev/null conv=stat count=1 2>/dev/null ; then
  83. echo "Starting dd, this will take some time. Go have some coffee :-)"
  84. dd if=${device} of=/dev/mapper/${encryptedname} bs=1k conv=stat # conv=stat is my personal patch -- BRS
  85. else
  86. echo "Starting dd, this will take some time. No output will happen"
  87. echo "while this is running. Go have some coffee :-)"
  88. dd if=${device} of=/dev/mapper/${encryptedname} bs=1k
  89. fi
  90. echo "Encrypting the data is done."
  91. echo -n "Mounting encrypted ${device} on ${mountpoint} now ... "
  92. if ! initrd_mount /dev/mapper/${encryptedname} /root/${mountpoint} ; then
  93. echo "FAILED"
  94. echo "Couldn't mount /dev/mapper/${encryptedname} on ${mountpoint}"
  95. echo "Starting a shell"
  96. echo
  97. exec /bin/bash
  98. fi
  99. [ "${mountpoint}" == "/" ] && rootfsmounted=1
  100. echo "done"
  101. sed -e "s,^${device}\(.*\)encrypt,${device}\\1encrypted," -i /root/etc/conf/dm/mounts
  102. recreateinitrd=1
  103. echo "Press enter to continue"
  104. read < /dev/console
  105. continue
  106. fi # }}}
  107. if [ "${status}" == "decrypt" ] ; then # {{{
  108. echo "Decrypting ${device} on ${mountpoint}"
  109. echo
  110. echo "WARNING! Although this is usually safe you should make sure that your backups"
  111. echo "are recent and working. Just in case something happens (power loss, ...)."
  112. echo "If this process is interrupted your filesystem WILL BE INACCESSIBLE!"
  113. echo
  114. pass1="MEEP"
  115. pass2="MOOP"
  116. while [ "${pass1}" != "${pass2}" ] ; do
  117. echo -n "Please enter the passphrase for decryption: "
  118. read -s pass1 < /dev/console
  119. echo
  120. echo -n "Please confirm the passphrase: "
  121. read -s pass2 < /dev/console
  122. echo
  123. [ "${pass1}" != "${pass2}" ] && echo "The passphrases do not match!"
  124. done
  125. pass1="`echo ${pass1} | md5sum`"
  126. pass1=${pass1%% *}
  127. encryptedname=${device//\//_}_encrypted
  128. encryptedname=${encryptedname#_}
  129. echo -n "Setting up decryption now ... "
  130. echo 0 `/sbin/blockdev --getsize ${device}` crypt aes-plain ${pass1} 0 ${device} 0 | /sbin/dmsetup create ${encryptedname}
  131. echo "done"
  132. echo "I will now do a sanity check of the harddisk. This means"
  133. echo "that the encryption process will be simulated by writing"
  134. echo "to /dev/null instead of the encrypted partition."
  135. echo "This ensures that the whole disk is readable and the"
  136. echo "copy process won't fail because of a bad harddisk."
  137. echo
  138. echo -n "Press enter to continue ... "
  139. read </dev/console
  140. if /bin/dd if=/dev/zero of=/dev/null conv=stat count=1 2>/dev/null ; then
  141. echo "Starting dd, this will take some time. Go have some coffee :-)"
  142. dd if=${device} of=/dev/null bs=1k conv=stat # conv=stat is my personal patch -- BRS
  143. error=${?}
  144. else
  145. echo "Starting dd, this will take some time. No output will happen"
  146. echo "while this is running. Go have some coffee :-)"
  147. dd if=${device} of=/dev/null bs=1k
  148. error=${?}
  149. fi
  150. if [ ${error} != 0 ] ; then
  151. echo "An error occured!"
  152. echo "Cowardly refusing to decrypt ${device}!"
  153. echo "Starting a shell."
  154. echo
  155. exec /bin/bash
  156. continue
  157. fi
  158. echo "Now the critical part of the decryption process starts."
  159. echo "I'm now copying the data bytewise from the encrypted device to the"
  160. echo "unencrypted loopdevice. Make absolutely sure that this process won't be"
  161. echo "interrupted!"
  162. echo
  163. echo -n "Press enter to start decrypting ... "
  164. read < /dev/console
  165. if /bin/dd if=/dev/zero of=/dev/null conv=stat count=1 2>/dev/null ; then
  166. echo "Starting dd, this will take some time. Go have some coffee :-)"
  167. dd if=/dev/mapper/${encryptedname} of=${device} bs=1k conv=stat # conv=stat is my personal patch
  168. else
  169. echo "Starting dd, this will take some time. No output will happen"
  170. echo "while this is running. Go have some coffee :-)"
  171. dd if=/dev/mapper/${encryptedname} of=${device} bs=1k
  172. fi
  173. echo "Decrypting the data is done."
  174. echo -n "Shutting down encryption ... "
  175. /sbin/dmsetup remove ${encryptedname}
  176. echo -n "done"
  177. echo -n "Mounting ${device} on ${mountpoint} now ... "
  178. if ! initrd_mount ${device} /root/${mountpoint} ; then
  179. echo "FAILED"
  180. echo "Couldn't mount ${device} on ${mountpoint}"
  181. echo "Starting a shell"
  182. echo
  183. exec /bin/bash
  184. fi
  185. [ "${mountpoint}" == "/" ] && rootfsmounted=1
  186. echo "done"
  187. sed -e "s,^${device}\(.*\)decrypt,${device}\\1plain," -i /root/etc/conf/dm/mounts
  188. recreateinitrd=1
  189. echo "Press enter to continue"
  190. read < /dev/console
  191. continue
  192. fi # }}}
  193. if [ "${status}" == "swap" ] ; then # {{{
  194. echo "Creating encrypted swap on ${device}"
  195. echo "setting up encryption"
  196. echo -n "gathering entropy ... "
  197. ent=""
  198. while read -n 1 -t 1 e < /dev/random ; do
  199. [ -n "${ent:128}" ] && break
  200. [ "${e}" == "\n" ] && continue
  201. ent="${ent}${e}"
  202. echo -n "."
  203. done
  204. [ -z "${ent:128}" ] && echo -n " switching to urandom ... "
  205. while read -n 1 e < /dev/urandom ; do
  206. [ -n "${ent:128}" ] && break
  207. [ "${e}" == "\n" ] && continue
  208. ent="${ent}${e}"
  209. echo -n "."
  210. done
  211. ent="`echo ${ent} | md5sum`"
  212. ent=${ent%% *}
  213. echo " done"
  214. echo -n "setting up encryption ... "
  215. encryptedname=${device//\//_}_encrypted
  216. encryptedname=${encryptedname#_}
  217. echo 0 `/sbin/blockdev --getsize ${device}` crypt aes-plain ${ent} 0 ${device} 0 | /sbin/dmsetup create ${encryptedname}
  218. echo "done"
  219. echo -n "creating swapspace ... "
  220. if /sbin/mkswap /dev/mapper/${encryptedname} >/dev/null 2>&1 ; then
  221. echo "success"
  222. echo -n "activating swapspace ... "
  223. if /sbin/swapon /dev/mapper/${encryptedname} ; then
  224. echo "success"
  225. else
  226. echo "failed"
  227. echo "booting without swap!"
  228. fi
  229. else
  230. echo "failed"
  231. echo "booting without swap!"
  232. fi
  233. fi # }}}
  234. if [ "${status}" == "encrypted" ] ; then # {{{
  235. run=1
  236. while [ ${run} -eq 1 ] ; do
  237. echo "Please enter the passphrase for ${device} on ${mountpoint}"
  238. echo -n "Passphrase: "
  239. read -s passphrase < /dev/console
  240. echo
  241. passphrase="`echo ${passphrase} | md5sum`"
  242. passphrase=${passphrase%% *}
  243. encryptedname=${device//\//_}_encrypted
  244. encryptedname=${encryptedname#_}
  245. echo 0 `/sbin/blockdev --getsize ${device}` crypt aes-plain ${passphrase} 0 ${device} 0 | /sbin/dmsetup create ${encryptedname}
  246. if initrd_mount /dev/mapper/${encryptedname} /root/${mountpoint} < /dev/console ; then
  247. run=0
  248. echo "Success"
  249. else
  250. /sbin/dmsetup remove ${encryptedname}
  251. echo "Couldn't mount ${device} on ${mountpoint}"
  252. echo -n "Continue without it [y/n] ? "
  253. read -n 1 yn < /dev/console
  254. echo
  255. [ "${yn}" == "y" ] && run=0
  256. fi
  257. done
  258. [ "${mountpoint}" == "/" ] && rootfsmounted=1
  259. continue
  260. fi # }}}
  261. done < /etc/dm/mounts