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.

249 lines
8.0 KiB

  1. #!/bin/bash
  2. # --- ROCK-COPYRIGHT-NOTE-BEGIN ---
  3. #
  4. # This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  5. # Please add additional copyright information _after_ the line containing
  6. # the ROCK-COPYRIGHT-NOTE-END tag. Otherwise it might get removed by
  7. # the ./scripts/Create-CopyPatch script. Do not edit this copyright text!
  8. #
  9. # ROCK Linux: rock-src/target/lvp/x86/release_skeleton/scripts/encrypted
  10. # ROCK Linux is Copyright (C) 1998 - 2006 Clifford Wolf
  11. #
  12. # This program is free software; you can redistribute it and/or modify
  13. # it under the terms of the GNU General Public License as published by
  14. # the Free Software Foundation; either version 2 of the License, or
  15. # (at your option) any later version. A copy of the GNU General Public
  16. # License can be found at Documentation/COPYING.
  17. #
  18. # Many people helped and are helping developing ROCK Linux. Please
  19. # have a look at http://www.rocklinux.org/ and the Documentation/TEAM
  20. # file for details.
  21. #
  22. # --- ROCK-COPYRIGHT-NOTE-END ---
  23. type_encrypted="encrypted"
  24. entrosource="${LVP_ENTROPY_SOURCE}"
  25. process_encrypted(){
  26. target="livesystem"
  27. echo "Loading kernel module for linear mode"
  28. grep -q linear /proc/mdstat || modprobe linear
  29. if ! grep -q linear /proc/mdstat ; then
  30. echo "Sorry, you don't have linear RAID support in your kernel."
  31. echo "Since V0.4.2 this is required for encrypted LVPs to use"
  32. echo "available disk space more efficiently."
  33. exit 1
  34. fi
  35. modprobe dm_mod 2>/dev/null
  36. modprobe dm_crypt 2>/dev/null
  37. modprobe aes_i586 2>/dev/null
  38. if [ ! -c /dev/mapper/control ] || ! grep -q aes /proc/crypto ; then
  39. echo "There seems to be a problem with your crypto support."
  40. echo "Please make sure that you have both the device mapper"
  41. echo "and its crypto support running as well as support for"
  42. echo "the aes cipher."
  43. exit 1
  44. fi
  45. echo -n "Checking necessary filesystem size ... "
  46. filesize=0
  47. while read file ; do
  48. [ -f "${file}" ] || continue
  49. thisfilesize=`ls -l "${file}" | sed 's,[ \t][ \t]*, ,g' | cut -f5 -d' '`
  50. filesize=$(( ${filesize} + ${thisfilesize} ))
  51. done < ${moviefiles}
  52. echo "${filesize} Byte (`human_readable ${filesize}`)"
  53. echo -n "Checking Livesystem size ... "
  54. livesize=`du -sb ${target} --exclude=lvp.data? | cut -f1`
  55. livesize=$(( ${livesize} + `du -sb isolinux | cut -f1` ))
  56. echo "`human_readable ${livesize}`"
  57. filesize=$(( ${filesize} + ${livesize} ))
  58. echo
  59. echo "Total space needed: $(( ${filesize} / 1024 / 1024 )) MB"
  60. if [ $(( ${filesize} / 1024 / 1024 )) -gt ${size} ] ; then
  61. echo
  62. echo "This may be more than fits onto your medium."
  63. echo "You specified ${size} MB to fit onto your medium."
  64. echo "If you are sure that this is okay, please continue."
  65. echo "If not, please truncate your filelist."
  66. confirm "Continue"
  67. [ ${?} -eq 1 ] && exit 1
  68. fi
  69. echo "Searching for free loopdevices ..."
  70. filesize=$(( ( ${size} * 1048576 ) - ${livesize} ))
  71. needed_pseudofs=$(( (${filesize} / 2147481600) + 1 ))
  72. needed_loopdevices=${needed_pseudofs}
  73. for loopdevice in /dev/loop/* ; do
  74. [ ${needed_loopdevices} -eq 0 ] && continue
  75. if losetup ${loopdevice} ${moviefiles} 2>/dev/null ; then
  76. # I reuse the ${moviefiles} tmpfile here since
  77. # associating it with a loopdevice doesn't do any harm
  78. echo "Using ${loopdevice}"
  79. eval "loopdevice_${needed_loopdevices}=\"${loopdevice}\""
  80. losetup -d ${loopdevice}
  81. needed_loopdevices=$(( ${needed_loopdevices} - 1 ))
  82. fi
  83. done
  84. if [ ${needed_loopdevices} -gt 0 ] ; then
  85. echo "Not enough free loop-devices found!"
  86. echo "Please either free ${needed_loopdevices} more loop-devices"
  87. echo "(check with losetup <loopdevice>) or increase the"
  88. echo "number of available loop-devices in your kernel."
  89. exit 1
  90. fi
  91. echo "Okay, Now creating files that will hold the pseudo filesystems"
  92. unset ddparam
  93. unset cpparam
  94. [ `dd --help | grep -c stat` -eq 1 ] && ddparam="conv=stat"
  95. [ `cp --help | grep -c "print copyprogress"` -eq 1 ] && cpparam="-D"
  96. if [ -e ${target}/lvp.data1 ] ; then
  97. for x in ${target}/lvp.data* ; do
  98. if [ ${x##*lvp.data} -gt ${needed_pseudofs} ] ; then\
  99. echo "Found ${x##*/}, but we don't need it. Deleting it."
  100. rm -f ${x}
  101. fi
  102. done
  103. fi
  104. filesystem=0
  105. while [ ${filesystem} -lt ${needed_pseudofs} ] ; do
  106. filesystem=$(( ${filesystem} + 1 ))
  107. echo "Filesystem ${filesystem} of ${needed_pseudofs}"
  108. file="${target}/lvp.data${filesystem}"
  109. if [ ${filesystem} -lt ${needed_pseudofs} ] ; then
  110. size=2147481600 # iso9660 limitation
  111. else
  112. size=$(( ${filesize} - ( ${filesystem} - 1 ) * 2147481600 ))
  113. size=$(( ( ${size} / 2048 ) * 2048 )) # so we have a round number
  114. fi
  115. if [ -f ${file} ] ; then
  116. thisfilesize=`stat -c %s "${file}"`
  117. if [ ${thisfilesize} -eq ${size} ] ; then
  118. echo "lvp.data${filesystem} already exists and has correct filesize. Using it."
  119. else
  120. echo "lvp.data${filesystem} already exists but has wrong filesize. Deleting it"
  121. rm -f ${target}/lvp.data${filesystem}
  122. fi
  123. fi
  124. [ -f ${target}/lvp.data${filesystem} ] && continue
  125. dd if=/dev/${entrosource} of=${target}/lvp.data${filesystem} bs=2k count=$(( ${size} / 2048 )) ${ddparam}
  126. done
  127. echo "Creating mountpoint"
  128. rm -rf ${target}/mnt*
  129. mkdir ${target}/mnt1
  130. echo "Now I need a passphrase for encrypting the filesystems."
  131. passphrase="MEEP"
  132. passphrase_confirm="MOOP"
  133. while [ "${passphrase}" != "${passphrase_confirm}" ] ; do
  134. read -p "Enter passphrase: " -s passphrase
  135. echo
  136. if [ "${passphrase:19}" = "" ] ; then
  137. echo "The Passphrase must be at least 20 characters!"
  138. passphrase="MEEP"
  139. passphrase_confirm="MOOP"
  140. continue
  141. fi
  142. read -p "Confirm: " -s passphrase_confirm
  143. echo
  144. if [ "${passphrase}" != "${passphrase_confirm}" ] ; then
  145. echo "The passphrases do not match."
  146. fi
  147. done
  148. echo "Creating filesystems and mounting pseudo-filesystems"
  149. lvpdata=1
  150. while [ ${lvpdata} -le ${needed_pseudofs} ] ; do
  151. eval "lodev=\${loopdevice_${lvpdata}}"
  152. file="${target}/lvp.data${lvpdata}"
  153. echo "Setting up loopdevice ${lvpdata}"
  154. losetup ${lodev} ${file}
  155. lvpdata=$(( ${lvpdata} + 1 ))
  156. done
  157. echo "Setting up linear device"
  158. mddev=""
  159. for x in /dev/md/* ; do
  160. [ -n "${mddev}" ] && break
  161. mdadm --misc -Q ${x} | grep -q "not active" && mddev="${x}"
  162. done
  163. mdloopdevs=""
  164. lvpdata=1
  165. while [ ${lvpdata} -le ${needed_pseudofs} ] ; do
  166. eval "mdloopdevs=\"\${mdloopdevs} \${loopdevice_${lvpdata}}\""
  167. lvpdata=$(( ${lvpdata} + 1 ))
  168. done
  169. ${target}/sbin/mdadm --build ${mddev} -l linear --force -n ${needed_pseudofs} ${mdloopdevs}
  170. passphrase="`echo ${passphrase} | md5sum`"
  171. passphrase=${passphrase%% *}
  172. echo 0 `/sbin/blockdev --getsize ${mddev}` crypt aes-plain ${passphrase} 0 ${mddev} 0 | /sbin/dmsetup create lvp_data_$$
  173. echo "Creating filesystem"
  174. mkfs.ext2 -m 0 /dev/mapper/lvp_data_$$ >/dev/null 2>&1
  175. echo "Mounting filesystem"
  176. mount /dev/mapper/lvp_data_$$ ${target}/mnt1
  177. rm -rf ${target}/mnt1/*
  178. continue=0
  179. while read file ; do
  180. [ -f "${file}" ] || continue
  181. [ ${continue} -eq 1 ] && break
  182. unset targetdir
  183. thisfile=`stat -c %s "${file}"`
  184. for dir in ${target}/mnt? ; do
  185. # I leave this here for historical reasons, maybe we need it again some day
  186. avail=`df -P ${dir} | grep / | sed 's, *, ,g' | cut -f4 -d' '`
  187. avail=$(( ${avail%K} * 1024 ))
  188. [ -z "${targetdir}" -a ${avail} -gt ${thisfile} ] && targetdir=${dir}
  189. done
  190. if [ -z "${targetdir}" ] ; then
  191. echo "Not enough space available for ${file}. Skipping remaining files." >&2
  192. continue=1
  193. fi
  194. [ ${continue} -eq 1 ] && continue
  195. echo "Copying ${file} to ${targetdir}/${file##*/}"
  196. cp ${cpparam} "${file}" "${targetdir}/${file##*/}"
  197. environment="`echo ${file} | tr '[. \-!]' '_'`"
  198. eval "export file_${environment##*/}=\"${targetdir#*${target}}/${file##*/}\""
  199. done < ${moviefiles}
  200. lvpxml=${target}/mnt1/lvp.xml
  201. process_create_lvpxml
  202. echo "Umounting filesystem ${mddev}"
  203. umount livesystem/mnt1
  204. echo "Shutting down ${mddev}"
  205. dmsetup remove /dev/mapper/lvp_data_$$
  206. mdadm -S ${mddev}
  207. lvpdata=${needed_pseudofs}
  208. while [ ${lvpdata} -ge 1 ] ; do
  209. eval "lodev=\${loopdevice_${lvpdata}}"
  210. echo "Shutting down loopdevice ${lodev}"
  211. losetup -d ${lodev}
  212. lvpdata=$(( ${lvpdata} - 1 ))
  213. done
  214. exit 0
  215. }