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.

238 lines
7.6 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. echo -n "Checking necessary filesystem size ... "
  36. filesize=0
  37. while read file ; do
  38. [ ! -f "${file}" ] && continue
  39. thisfilesize=`ls -l "${file}" | sed 's,[ \t][ \t]*, ,g' | cut -f5 -d' '`
  40. filesize=$(( ${filesize} + ${thisfilesize} ))
  41. done < ${moviefiles}
  42. echo "${filesize} Byte (`human_readable ${filesize}`)"
  43. echo -n "Checking Livesystem size ... "
  44. livesize=`du -sb ${target} --exclude=lvp.data? | cut -f1`
  45. livesize=$(( ${livesize} + `du -sb isolinux | cut -f1` ))
  46. echo "`human_readable ${livesize}`"
  47. filesize=$(( ${filesize} + ${livesize} ))
  48. echo
  49. echo "Total space needed: $(( ${filesize} / 1024 / 1024 )) MB"
  50. if [ $(( ${filesize} / 1024 / 1024 )) -gt ${size} ] ; then
  51. echo
  52. echo "This may be more than fits onto your medium."
  53. echo "You specified ${size} MB to fit onto your medium."
  54. echo "If you are sure that this is okay, please continue."
  55. echo "If not, please truncate your filelist."
  56. confirm "Continue"
  57. [ ${?} -eq 1 ] && exit 1
  58. fi
  59. echo "Searching for free loopdevices ..."
  60. filesize=$(( ( ${size} * 1048576 ) - ${livesize} ))
  61. needed_pseudofs=$(( (${filesize} / 2147481600) + 1 ))
  62. needed_loopdevices=${needed_pseudofs}
  63. for loopdevice in /dev/loop/* ; do
  64. [ ${needed_loopdevices} -eq 0 ] && continue
  65. if losetup ${loopdevice} ${moviefiles} 2>/dev/null ; then
  66. # I reuse the ${moviefiles} tmpfile here since associating it
  67. # with a loopdevice doesn't do any harm
  68. echo "Using ${loopdevice}"
  69. eval "loopdevice_${needed_loopdevices}=\"${loopdevice}\""
  70. losetup -d ${loopdevice}
  71. needed_loopdevices=$(( ${needed_loopdevices} - 1 ))
  72. fi
  73. done
  74. if [ ${needed_loopdevices} -gt 0 ] ; then
  75. echo "Not enough free loop-devices found!"
  76. echo "Please either free ${needed_loopdevices} more loop-devices"
  77. echo "(check with losetup -a) or increase the"
  78. echo "number of available loop-devices."
  79. exit 1
  80. fi
  81. echo "Okay, Now creating files that will hold the pseudo filesystems"
  82. unset ddparam
  83. unset cpparam
  84. [ `dd --help | grep -c stat` -eq 1 ] && ddparam="conv=stat"
  85. [ `cp --help | grep -c "print copyprogress"` -eq 1 ] && cpparam="-D"
  86. if [ -e ${target}/lvp.data1 ] ; then
  87. for x in ${target}/lvp.data* ; do
  88. if [ ${x##*lvp.data} -gt ${needed_pseudofs} ] ; then\
  89. echo "Found ${x##*/}, but we don't need it. Deleting it."
  90. rm -f ${x}
  91. fi
  92. done
  93. fi
  94. filesystem=0
  95. while [ ${filesystem} -lt ${needed_pseudofs} ] ; do
  96. filesystem=$(( ${filesystem} + 1 ))
  97. echo "Filesystem ${filesystem} of ${needed_pseudofs}"
  98. file="${target}/lvp.data${filesystem}"
  99. if [ ${filesystem} -lt ${needed_pseudofs} ] ; then
  100. size=2147481600 # iso9660 limitation
  101. else
  102. size=$(( ${filesize} - ( ${filesystem} - 1 ) * 2147481600 ))
  103. size=$(( ( ${size} / 2048 ) * 2048 )) # so we have a round number
  104. fi
  105. if [ -f ${file} ] ; then
  106. thisfilesize=`ls -l "${file}" | sed 's, *, ,g' | cut -f5 -d' '`
  107. if [ ${thisfilesize} -eq ${size} ] ; then
  108. echo "lvp.data${filesystem} already exists and has correct filesize. Using it."
  109. else
  110. echo "lvp.data${filesystem} already exists but has wrong filesize. Deleting it"
  111. rm -f ${target}/lvp.data${filesystem}
  112. fi
  113. fi
  114. [ -f ${target}/lvp.data${filesystem} ] && continue
  115. dd if=/dev/${entrosource} of=${target}/lvp.data${filesystem} bs=2k count=$(( ${size} / 2048 )) ${ddparam}
  116. done
  117. echo "Creating mountpoint"
  118. rm -rf ${target}/mnt*
  119. mkdir ${target}/mnt1
  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. losetup ${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. passphrase="`echo ${passphrase} | md5sum`"
  161. passphrase=${passphrase%% *}
  162. echo 0 `/sbin/blockdev --getsize ${mddev}` crypt aes-plain ${passphrase} 0 ${mddev} 0 | /sbin/dmsetup create lvp_data_$$
  163. echo "Creating filesystem"
  164. mkfs.ext2 -m 0 /dev/mapper/lvp_data_$$ >/dev/null 2>&1
  165. echo "Mounting filesystem"
  166. mount /dev/mapper/lvp_data_$$ ${target}/mnt1
  167. rm -rf ${target}/mnt1/*
  168. continue=0
  169. while read file ; do
  170. [ ! -f "${file}" ] && continue
  171. [ ${continue} -eq 1 ] && break
  172. unset targetdir
  173. thisfile=`ls -l "${file}" | sed 's, *, ,g' | cut -f5 -d' '`
  174. for dir in ${target}/mnt? ; do # I leave this here for historical reasons, maybe we need it again some day
  175. avail=`df -P ${dir} | grep / | sed 's, *, ,g' | cut -f4 -d' '`
  176. avail=$(( ${avail} * 1024 ))
  177. [ -z "${targetdir}" -a ${avail} -gt ${thisfile} ] && targetdir=${dir}
  178. done
  179. if [ -z "${targetdir}" ] ; then
  180. echo "Not enough space available for ${file}. Skipping remaining files." >&2
  181. continue=1
  182. fi
  183. [ ${continue} -eq 1 ] && continue
  184. echo "Copying ${file} to ${targetdir}/${file##*/}"
  185. cp ${cpparam} "${file}" "${targetdir}/${file##*/}"
  186. environment="`echo ${file} | tr '[. \-!]' '_'`"
  187. eval "export file_${environment##*/}=\"${targetdir#*${target}}/${file##*/}\""
  188. done < ${moviefiles}
  189. lvpxml=${target}/mnt1/lvp.xml
  190. process_create_lvpxml
  191. echo "Umounting filesystem ${mddev}"
  192. umount livesystem/mnt1
  193. echo "Shutting down ${mddev}"
  194. dmsetup remove /dev/mapper/lvp_data_$$
  195. mdadm -S ${mddev}
  196. lvpdata=${needed_pseudofs}
  197. while [ ${lvpdata} -ge 1 ] ; do
  198. eval "lodev=\${loopdevice_${lvpdata}}"
  199. echo "Shutting down loopdevice ${lodev}"
  200. losetup -d ${lodev}
  201. lvpdata=$(( ${lvpdata} - 1 ))
  202. done
  203. exit 0
  204. }