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.

236 lines
7.4 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. [ `lsmod | grep -c "loop_${module}"` -eq 0 ] && 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. grep -q linear /proc/mdstat || modprobe linear
  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. unset cpparam
  82. [ `dd --help | grep -c stat` -eq 1 ] && ddparam="conv=stat"
  83. [ `cp --help | grep -c "print copyprogress"` -eq 1 ] && 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 # iso9660 limitation
  99. else
  100. size=$(( ${filesize} - ( ${filesystem} - 1 ) * 2147481600 ))
  101. size=$(( ( ${size} / 2048 ) * 2048 )) # so we have a round number
  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} bs=2k count=$(( ${size} / 2048 )) ${ddparam}
  114. done
  115. echo "Creating mountpoint"
  116. rm -rf ${target}/mnt*
  117. mkdir ${target}/mnt1
  118. echo "Using ${LVP_ENCRYPTION} encryption."
  119. echo "Now I need a passphrase for encrypting the filesystems."
  120. passphrase="MEEP"
  121. passphrase_confirm="MOOP"
  122. while [ "${passphrase}" != "${passphrase_confirm}" ] ; do
  123. read -p "Enter passphrase: " -s passphrase
  124. echo
  125. if [ "${passphrase:20}" = "" ] ; then
  126. echo "The Passphrase must be at least 20 characters!"
  127. passphrase="MEEP"
  128. passphrase_confirm="MOOP"
  129. continue
  130. fi
  131. read -p "Confirm: " -s passphrase_confirm
  132. echo
  133. if [ "${passphrase}" != "${passphrase_confirm}" ] ; then
  134. echo "The passphrases do not match."
  135. fi
  136. done
  137. echo "Creating filesystems and mounting pseudo-filesystems"
  138. lvpdata=1
  139. while [ ${lvpdata} -le ${needed_pseudofs} ] ; do
  140. eval "lodev=\${loopdevice_${lvpdata}}"
  141. file="${target}/lvp.data${lvpdata}"
  142. echo "Setting up loopdevice ${lvpdata}"
  143. echo "${passphrase}" | losetup -p 0 -e ${LVP_ENCRYPTION}256 ${lodev} ${file}
  144. lvpdata=$(( ${lvpdata} + 1 ))
  145. done
  146. echo "Setting up linear device"
  147. mddev=""
  148. for x in /dev/md/* ; do
  149. [ ! -z "${mddev}" ] && break
  150. [ `mdadm --misc -Q ${x} | grep -c "not active"` -eq 1 ] && mddev="${x}"
  151. done
  152. mdloopdevs=""
  153. lvpdata=1
  154. while [ ${lvpdata} -le ${needed_pseudofs} ] ; do
  155. eval "mdloopdevs=\"\${mdloopdevs} \${loopdevice_${lvpdata}}\""
  156. lvpdata=$(( ${lvpdata} + 1 ))
  157. done
  158. ${target}/sbin/mdadm --build ${mddev} -l linear --force -n ${needed_pseudofs} ${mdloopdevs}
  159. echo "Creating filesystem on ${mddev}"
  160. mkfs.ext2 -m 0 ${mddev} >/dev/null 2>&1
  161. echo "Mounting filesystem ${mddev}"
  162. mount ${mddev} ${target}/mnt1
  163. rm -rf ${target}/mnt1/*
  164. continue=0
  165. while read file ; do
  166. [ ! -f "${file}" ] && continue
  167. [ ${continue} -eq 1 ] && break
  168. unset targetdir
  169. thisfile=`ls -l "${file}" | sed 's, *, ,g' | cut -f5 -d' '`
  170. for dir in ${target}/mnt? ; do # I leave this here for historical reasons, maybe we need it again some day
  171. avail=`df -P ${dir} | grep / | sed 's, *, ,g' | cut -f4 -d' '`
  172. avail=$(( ${avail} * 1024 ))
  173. [ -z "${targetdir}" -a ${avail} -gt ${thisfile} ] && targetdir=${dir}
  174. done
  175. if [ -z "${targetdir}" ] ; then
  176. echo "Not enough space available for ${file}. Skipping remaining files." >&2
  177. continue=1
  178. fi
  179. [ ${continue} -eq 1 ] && continue
  180. echo "Copying ${file} to ${targetdir}/${file##*/}"
  181. cp ${cpparam} "${file}" "${targetdir}/${file##*/}"
  182. environment="`echo ${file} | tr '[. \-!]' '_'`"
  183. eval "export file_${environment##*/}=\"${targetdir#*${target}}/${file##*/}\""
  184. done < ${moviefiles}
  185. lvpxml=${target}/mnt1/lvp.xml
  186. process_create_lvpxml
  187. echo "Umounting filesystem ${mddev}"
  188. umount ${mddev}
  189. echo "Shutting down ${mddev}"
  190. mdadm -S ${mddev}
  191. lvpdata=${needed_pseudofs}
  192. while [ ${lvpdata} -ge 1 ] ; do
  193. eval "lodev=\${loopdevice_${lvpdata}}"
  194. echo "Shutting down loopdevice ${lodev}"
  195. losetup -d ${lodev}
  196. lvpdata=$(( ${lvpdata} - 1 ))
  197. done
  198. exit 0
  199. }