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.

256 lines
9.2 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. echo "Remember to change the status of ${device} to encrypted using stone."
  102. echo "Press enter to continue"
  103. read < /dev/console
  104. continue
  105. fi # }}}
  106. if [ "${status}" == "decrypt" ] ; then # {{{
  107. echo "Decrypting ${device} on ${mountpoint}"
  108. echo
  109. echo "WARNING! Although this is usually safe you should make sure that your backups"
  110. echo "are recent and working. Just in case something happens (power loss, ...)."
  111. echo "If this process is interrupted your filesystem WILL BE INACCESSIBLE!"
  112. echo
  113. pass1="MEEP"
  114. pass2="MOOP"
  115. while [ "${pass1}" != "${pass2}" ] ; do
  116. echo -n "Please enter the passphrase for decryption: "
  117. read -s pass1 < /dev/console
  118. echo
  119. echo -n "Please confirm the passphrase: "
  120. read -s pass2 < /dev/console
  121. echo
  122. [ "${pass1}" != "${pass2}" ] && echo "The passphrases do not match!"
  123. done
  124. pass1="`echo ${pass1} | md5sum`"
  125. pass1=${pass1%% *}
  126. encryptedname=${device//\//_}_encrypted
  127. encryptedname=${encryptedname#_}
  128. echo -n "Setting up decryption now ... "
  129. echo 0 `/sbin/blockdev --getsize ${device}` crypt aes-plain ${pass1} 0 ${device} 0 | /sbin/dmsetup create ${encryptedname}
  130. echo "done"
  131. echo "I will now do a sanity check of the harddisk. This means"
  132. echo "that the encryption process will be simulated by writing"
  133. echo "to /dev/null instead of the encrypted partition."
  134. echo "This ensures that the whole disk is readable and the"
  135. echo "copy process won't fail because of a bad harddisk."
  136. echo
  137. echo -n "Press enter to continue ... "
  138. read </dev/console
  139. if /bin/dd if=/dev/zero of=/dev/null conv=stat count=1 2>/dev/null ; then
  140. echo "Starting dd, this will take some time. Go have some coffee :-)"
  141. dd if=${device} of=/dev/null bs=1k conv=stat # conv=stat is my personal patch -- BRS
  142. error=${?}
  143. else
  144. echo "Starting dd, this will take some time. No output will happen"
  145. echo "while this is running. Go have some coffee :-)"
  146. dd if=${device} of=/dev/null bs=1k
  147. error=${?}
  148. fi
  149. if [ ${error} != 0 ] ; then
  150. echo "An error occured!"
  151. echo "Cowardly refusing to decrypt ${device}!"
  152. echo "Starting a shell."
  153. echo
  154. exec /bin/bash
  155. continue
  156. fi
  157. echo "Now the critical part of the decryption process starts."
  158. echo "I'm now copying the data bytewise from the encrypted device to the"
  159. echo "unencrypted loopdevice. Make absolutely sure that this process won't be"
  160. echo "interrupted!"
  161. echo
  162. echo -n "Press enter to start decrypting ... "
  163. read < /dev/console
  164. if /bin/dd if=/dev/zero of=/dev/null conv=stat count=1 2>/dev/null ; then
  165. echo "Starting dd, this will take some time. Go have some coffee :-)"
  166. dd if=/dev/mapper/${encryptedname} of=${device} bs=1k conv=stat # conv=stat is my personal patch
  167. else
  168. echo "Starting dd, this will take some time. No output will happen"
  169. echo "while this is running. Go have some coffee :-)"
  170. dd if=/dev/mapper/${encryptedname} of=${device} bs=1k
  171. fi
  172. echo "Decrypting the data is done."
  173. echo -n "Shutting down encryption ... "
  174. /sbin/dmsetup remove ${encryptedname}
  175. echo -n "done"
  176. echo -n "Mounting ${device} on ${mountpoint} now ... "
  177. if ! initrd_mount ${device} /root/${mountpoint} ; then
  178. echo "FAILED"
  179. echo "Couldn't mount ${device} on ${mountpoint}"
  180. echo "Starting a shell"
  181. echo
  182. exec /bin/bash
  183. fi
  184. [ "${mountpoint}" == "/" ] && rootfsmounted=1
  185. echo "done"
  186. echo "Remember to change the status of ${device} to 'plain' using stone."
  187. echo "Press enter to continue"
  188. read < /dev/console
  189. continue
  190. fi # }}}
  191. if [ "${status}" == "swap" ] ; then # {{{
  192. echo "Creating encrypted swap on ${device}"
  193. echo "setting up encryption"
  194. echo -n "gathering entropy ... "
  195. ent=""
  196. while [ -z "${ent:128}" ] ; do
  197. read -n 1 e < /dev/random
  198. [ "${e}" == "\n" ] && continue
  199. ent="${ent}${e}"
  200. echo -n "."
  201. done
  202. ent="`echo ${ent} | md5sum`"
  203. ent=${ent%% *}
  204. echo
  205. echo -n "setting up encryption ... "
  206. encryptedname=${device//\//_}_encrypted
  207. encryptedname=${encryptedname#_}
  208. echo 0 `/sbin/blockdev --getsize ${device}` crypt aes-plain ${ent} 0 ${device} 0 | /sbin/dmsetup create ${encryptedname}
  209. echo "done"
  210. echo -n "creating swapspace ... "
  211. if /sbin/mkswap /dev/mapper/${encryptedname} >/dev/null 2>&1 ; then
  212. echo "success"
  213. echo -n "activating swapspace ... "
  214. if /sbin/swapon /dev/mapper/${encryptedname} ; then
  215. echo "success"
  216. else
  217. echo "failed"
  218. echo "booting without swap!"
  219. fi
  220. else
  221. echo "failed"
  222. echo "booting without swap!"
  223. fi
  224. fi # }}}
  225. if [ "${status}" == "encrypted" ] ; then # {{{
  226. run=1
  227. while [ ${run} -eq 1 ] ; do
  228. echo "Please enter the passphrase for ${device} on ${mountpoint}"
  229. echo -n "Passphrase: "
  230. read -s passphrase < /dev/console
  231. echo
  232. passphrase="`echo ${passphrase} | md5sum`"
  233. passphrase=${passphrase%% *}
  234. encryptedname=${device//\//_}_encrypted
  235. encryptedname=${encryptedname#_}
  236. echo 0 `/sbin/blockdev --getsize ${device}` crypt aes-plain ${passphrase} 0 ${device} 0 | /sbin/dmsetup create ${encryptedname}
  237. if initrd_mount /dev/mapper/${encryptedname} /root/${mountpoint} < /dev/console ; then
  238. run=0
  239. echo "Success"
  240. else
  241. /sbin/dmsetup remove ${encryptedname}
  242. echo "Couldn't mount ${device} on ${mountpoint}"
  243. echo -n "Continue without it [y/n] ? "
  244. read -n 1 yn < /dev/console
  245. echo
  246. [ "${yn}" == "y" ] && run=0
  247. fi
  248. done
  249. [ "${mountpoint}" == "/" ] && rootfsmounted=1
  250. continue
  251. fi # }}}
  252. done < /etc/dm/mounts