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.

237 lines
7.3 KiB

  1. #!/bin/bash
  2. type_encrypted="encrypted"
  3. entrosource="${LVP_ENTROPY_SOURCE}"
  4. process_encrypted(){
  5. target="livesystem"
  6. for module in twofish blowfish serpent ; do
  7. eval "encryption_${module}=0"
  8. modprobe loop_${module} >/dev/null 2>&1 # fails for kernel >= 2.5.48 if module already loaded
  9. [ `lsmod | grep -c "loop_${module}"` -eq 1 ] && eval "encryption_${module}=1"
  10. done
  11. encryption_available=0
  12. [ ${encryption_twofish} -eq 1 -o ${encryption_blowfish} -eq 1 -o ${encryption_serpent} -eq 1 ] && \
  13. encryption_available=1
  14. if [ ${encryption_available} -eq 0 ] ; then
  15. echo "Sorry, you do not have loop-aes on your system. Please have a look at"
  16. echo "http://loop-aes.sourceforge.net"
  17. exit 1
  18. fi
  19. eval "avail=\${encryption_${LVP_ENCRYPTION}}"
  20. if [ "${avail}" == "0" ] ; then
  21. echo "Sorry, ${LVP_ENCRYPTION} is not available on your system."
  22. echo "Please check your kernel configuration."
  23. exit 1
  24. fi
  25. echo "Loading kernel module for linear mode"
  26. modprobe linear 2>&1 >/dev/null || true
  27. if ! grep -q linear /proc/mdstat ; then
  28. echo "Sorry, you don't have linear RAID support in your kernel."
  29. echo "Since V0.4.2 this is required for encrypted LVPs to use"
  30. echo "available disk space more efficiently."
  31. exit 1
  32. fi
  33. echo -n "Checking necessary filesystem size ... "
  34. filesize=0
  35. while read file ; do
  36. [ ! -f "${file}" ] && continue
  37. thisfilesize=`ls -l "${file}" | sed 's,[ \t][ \t]*, ,g' | cut -f5 -d' '`
  38. filesize=$(( ${filesize} + ${thisfilesize} ))
  39. done < ${moviefiles}
  40. echo "${filesize} Byte (`human_readable ${filesize}`)"
  41. echo -n "Checking Livesystem size ... "
  42. livesize=`du -sb ${target} --exclude=lvp.data? | cut -f1`
  43. livesize=$(( ${livesize} + `du -sb isolinux | cut -f1` ))
  44. echo "`human_readable ${livesize}`"
  45. filesize=$(( ${filesize} + ${livesize} ))
  46. echo
  47. echo "Total space needed: $(( ${filesize} / 1024 / 1024 )) MB"
  48. if [ $(( ${filesize} / 1024 / 1024 )) -gt ${size} ] ; then
  49. echo
  50. echo "This may be more than fits onto your medium."
  51. echo "You specified ${size} MB to fit onto your medium."
  52. echo "If you are sure that this is okay, please continue."
  53. echo "If not, please truncate your filelist."
  54. confirm "Continue"
  55. [ ${?} -eq 1 ] && exit 1
  56. fi
  57. echo "Searching for free loopdevices ..."
  58. filesize=$(( ( ${size} * 1048576 ) - ${livesize} ))
  59. needed_pseudofs=$(( (${filesize} / 2147481600) + 1 ))
  60. needed_loopdevices=${needed_pseudofs}
  61. for loopdevice in /dev/loop/* ; do
  62. [ ${needed_loopdevices} -eq 0 ] && continue
  63. if losetup ${loopdevice} ${moviefiles} 2>/dev/null ; then
  64. # I reuse the ${moviefiles} tmpfile here since associating it
  65. # with a loopdevice doesn't do any harm
  66. echo "Using ${loopdevice}"
  67. eval "loopdevice_${needed_loopdevices}=\"${loopdevice}\""
  68. losetup -d ${loopdevice}
  69. needed_loopdevices=$(( ${needed_loopdevices} - 1 ))
  70. fi
  71. done
  72. if [ ${needed_loopdevices} -gt 0 ] ; then
  73. echo "Not enough free loop-devices found!"
  74. echo "Please either free ${needed_loopdevices} more loop-devices"
  75. echo "(check with losetup -a) or increase the"
  76. echo "number of available loop-devices."
  77. exit 1
  78. fi
  79. echo "Okay, Now creating files that will hold the pseudo filesystems"
  80. unset ddparam
  81. [ `dd --help | grep -c stat` -eq 1 ] && ddparam="conv=stat"
  82. unset cpparam
  83. cp -D ${moviefiles} /dev/null 2>&1 >/dev/null && cpparam="-D"
  84. if [ -e ${target}/lvp.data1 ] ; then
  85. for x in ${target}/lvp.data* ; do
  86. if [ ${x##*lvp.data} -gt ${needed_pseudofs} ] ; then\
  87. echo "Found ${x##*/}, but we don't need it. Deleting it."
  88. rm -f ${x}
  89. fi
  90. done
  91. fi
  92. filesystem=0
  93. while [ ${filesystem} -lt ${needed_pseudofs} ] ; do
  94. filesystem=$(( ${filesystem} + 1 ))
  95. echo "Filesystem ${filesystem} of ${needed_pseudofs}"
  96. file="${target}/lvp.data${filesystem}"
  97. if [ ${filesystem} -lt ${needed_pseudofs} ] ; then
  98. size=2147481600
  99. else
  100. size=$(( ${filesize} - ( ${filesystem} - 1 ) * 2147481600 ))
  101. size=$(( ( ${size} / 2048 ) * 2048 ))
  102. fi
  103. if [ -f ${file} ] ; then
  104. thisfilesize=`ls -l "${file}" | sed 's, *, ,g' | cut -f5 -d' '`
  105. if [ ${thisfilesize} -eq ${size} ] ; then
  106. echo "lvp.data${filesystem} already exists and has correct filesize. Using it."
  107. else
  108. echo "lvp.data${filesystem} already exists but has wrong filesize. Deleting it"
  109. rm -f ${target}/lvp.data${filesystem}
  110. fi
  111. fi
  112. [ -f ${target}/lvp.data${filesystem} ] && continue
  113. dd if=/dev/${entrosource} of=${target}/lvp.data${filesystem} \
  114. bs=2k count=$(( ${size} / 2048 )) ${ddparam}
  115. done
  116. echo "Creating mountpoint"
  117. rm -rf ${target}/mnt*
  118. mkdir ${target}/mnt1
  119. echo "Using ${LVP_ENCRYPTION} encryption."
  120. echo "Now I need a passphrase for encrypting the filesystems."
  121. passphrase="MEEP"
  122. passphrase_confirm="MOOP"
  123. while [ "${passphrase}" != "${passphrase_confirm}" ] ; do
  124. read -p "Enter passphrase: " -s passphrase
  125. echo
  126. if [ "${passphrase:20}" = "" ] ; then
  127. echo "The Passphrase must be at least 20 characters!"
  128. passphrase="MEEP"
  129. passphrase_confirm="MOOP"
  130. continue
  131. fi
  132. read -p "Confirm: " -s passphrase_confirm
  133. echo
  134. if [ "${passphrase}" != "${passphrase_confirm}" ] ; then
  135. echo "The passphrases do not match."
  136. fi
  137. done
  138. echo "Creating filesystems and mounting pseudo-filesystems"
  139. lvpdata=1
  140. while [ ${lvpdata} -le ${needed_pseudofs} ] ; do
  141. eval "lodev=\${loopdevice_${lvpdata}}"
  142. file="${target}/lvp.data${lvpdata}"
  143. echo "Setting up loopdevice ${lvpdata}"
  144. echo "${passphrase}" | losetup -p 0 -e ${LVP_ENCRYPTION}256 ${lodev} ${file}
  145. lvpdata=$(( ${lvpdata} + 1 ))
  146. done
  147. echo "Setting up linear device"
  148. mddev=""
  149. for x in /dev/md/* ; do
  150. [ ! -z "${mddev}" ] && break
  151. [ `mdadm --misc -Q ${x} | grep -c "not active"` -eq 1 ] && mddev="${x}"
  152. done
  153. mdloopdevs=""
  154. lvpdata=1
  155. while [ ${lvpdata} -le ${needed_pseudofs} ] ; do
  156. eval "mdloopdevs=\"\${mdloopdevs} \${loopdevice_${lvpdata}}\""
  157. lvpdata=$(( ${lvpdata} + 1 ))
  158. done
  159. ${target}/sbin/mdadm --build ${mddev} -l linear --force -n ${needed_pseudofs} ${mdloopdevs}
  160. echo "Creating filesystem on ${mddev}"
  161. mkfs.ext2 -m 0 ${mddev} >/dev/null 2>&1
  162. echo "Mounting filesystem ${mddev}"
  163. mount ${mddev} ${target}/mnt1
  164. rm -rf ${target}/mnt1/*
  165. continue=0
  166. while read file ; do
  167. [ ! -f "${file}" ] && continue
  168. [ ${continue} -eq 1 ] && break
  169. unset targetdir
  170. thisfile=`ls -l "${file}" | sed 's, *, ,g' | cut -f5 -d' '`
  171. for dir in ${target}/mnt? ; do # I leave this here for historical reasons, maybe we need it again some day
  172. avail=`df -P ${dir} | grep / | sed 's, *, ,g' | cut -f4 -d' '`
  173. avail=$(( ${avail} * 1024 ))
  174. [ -z "${targetdir}" -a ${avail} -gt ${thisfile} ] && targetdir=${dir}
  175. done
  176. if [ -z "${targetdir}" ] ; then
  177. echo "Not enough space available for ${file}. Skipping remaining files." >&2
  178. continue=1
  179. fi
  180. [ ${continue} -eq 1 ] && continue
  181. echo "Copying ${file} to ${targetdir}/${file##*/}"
  182. cp ${cpparam} "${file}" "${targetdir}/${file##*/}"
  183. environment="`echo ${file} | tr '[. \-!]' '_'`"
  184. eval "export file_${environment##*/}=\"${targetdir#*${target}}/${file##*/}\""
  185. done < ${moviefiles}
  186. lvpxml=${target}/mnt1/lvp.xml
  187. process_create_lvpxml
  188. echo "Umounting filesystem ${mddev}"
  189. umount ${mddev}
  190. echo "Shutting down ${mddev}"
  191. mdadm ${mddev} -S
  192. lvpdata=${needed_pseudofs}
  193. while [ ${lvpdata} -ge 1 ] ; do
  194. eval "lodev=\${loopdevice_${lvpdata}}"
  195. echo "Shutting down loopdevice ${lodev}"
  196. losetup -d ${lodev}
  197. lvpdata=$(( ${lvpdata} - 1 ))
  198. done
  199. exit 0
  200. }