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.

217 lines
6.7 KiB

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