customize_sysrescuecd.sh 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #!/bin/bash
  2. work_dir="/mnt"
  3. selected=""
  4. step=0
  5. PROGIMG="${0}"
  6. XIFS=$IFS
  7. #IFS=$' '
  8. clean_cmds=""
  9. tempfs="/mnt/tempfs/fsimage"
  10. test -n "${1}" && tempfs_size=${1} || tempfs_size=4000
  11. squashfs_name="sysrcd.dat"
  12. squashfs_mountpoint="/livemnt/squashfs"
  13. squashfs_dst="customcd/files"
  14. portage_tree_name="portage-latest.tar.xz"
  15. source "$( dirname "${BASH_SOURCE[0]}" )/functions.sh"
  16. do_squashfs()
  17. {
  18. local squashfs_exclude="${squashfs_dst}/usr/portage ${squashfs_dst}/var/cache/ ${squashfs_dst}/proc ${squashfs_dst}/dev ${squashfs_dst}/sys"
  19. test $# -gt 0 && local squashfs_outdir="${1}" || local squashfs_outdir=${work_dir}
  20. [ "$(freespace ${squashfs_outdir})" -lt 400 ] && { echo "${FUNCNAME[0]}: Not enough room in ${squashfs_outdir}"; return 1; }
  21. # check that the files have been extracted
  22. [ "$(ls -A ${squashfs_dst}/ 2>/dev/null | wc -l)" -eq 0 ] && { echo "${FUNCNAME[0]}: ${squashfs_dst} is empty, your must extract the files first."; return 1; }
  23. # check that there are no remaining filesystems mounted
  24. for curfs in proc; do
  25. local curpath="${squashfs_dst}/${curfs}"
  26. local dircnt="$(ls -A ${curpath} 2>/dev/null | wc -l)"
  27. [ ${dircnt} -gt 0 ] && { echo "${FUNCNAME[0]}: The directory ${curpath} must be empty"; return 1; }
  28. done
  29. try mksquashfs ${squashfs_dst}/ ${squashfs_outdir}/${squashfs_name} -e ${squashfs_exclude}
  30. md5sum ${squashfs_outdir}/${squashfs_name} > ${squashfs_outdir}/${squashfs_name%????}.md5
  31. # Change permissions to allow the file to be sent by thttpd for PXE-boot
  32. chmod 666 ${squashfs_outdir}/${squashfs_name}
  33. }
  34. do_isogen()
  35. {
  36. curtime="$(date +%Y%m%d-%H%M)"
  37. ISO_VOLUME="SYSRESCCD"
  38. test $# -gt 0 && local iso_outdir="${1}" || local iso_outdir="${work_dir}"
  39. # check for free space
  40. [ "$(freespace ${iso_outdir})" -lt 500 ] && { echo "Not enough room in ${iso_outdir}"; return 1; }
  41. # ---- copy critical files and directories
  42. for curfile in version isolinux
  43. do
  44. scp "/livemnt/boot/${curfile} ${iso_outdir}" || { echo "${FUNCNAME[0]}: cannot copy ${curfile} to ${iso_outdir}"; return 1; }
  45. done
  46. # ---- copy optionnal files and directories
  47. for curfile in boot bootprog bootdisk efi ntpasswd usb_inst usb_inst.sh usbstick.htm
  48. do
  49. scp "/livemnt/boot/${curfile} ${iso_outdir}" || echo "${FUNCNAME[0]}: cannot copy ${curfile} to ${iso_outdir} (non critical error, maybe be caused by \"docache\")"
  50. done
  51. [ -d "${iso_outdir}/isolinux" ] || { echo "${FUNCNAME[0]}: you must create a squashfs filesystem before making iso"; return 1; }
  52. # Set keymap in isolinux.cfg
  53. if [ -n "${KEYMAP}" ]; then
  54. echo "Keymap to be loaded: ${KEYMAP}"
  55. copy_files "${iso_outdir}/isolinux/isolinux.cfg ${iso_outdir}/isolinux/isolinux.bak"
  56. sed -i -r -e "s: setkmap=[a-z0-9]+::g ; s:APPEND:APPEND setkmap=${KEYMAP}:gi" ${iso_outdir}/isolinux/isolinux.cfg
  57. fi
  58. echo "Volume name of the CDRom: ${ISO_VOLUME}"
  59. try "xorriso -as mkisofs -joliet -rock \
  60. -omit-version-number -disable-deep-relocation \
  61. -b isolinux/isolinux.bin -c isolinux/boot.cat \
  62. -no-emul-boot -boot-load-size 4 -boot-info-table \
  63. -eltorito-alt-boot -e boot/grub/efi.img -no-emul-boot \
  64. -o ${iso_outdir}/sysresccd-${curtime}.iso \
  65. -volid ${ISO_VOLUME} ${iso_outdir}" || { echo "${FUNCNAME[0]}: mkisofs failed"; return 1; }
  66. md5sum ${iso_outdir}/sysresccd-${curtime}.iso > ${iso_outdir}/sysresccd-${curtime}.md5
  67. echo "Final ISO image: ${iso_outdir}/sysresccd-${curtime}.iso"
  68. }
  69. printf "\nThis script helps to customize sysrescuecd.\n"
  70. execution_premission "Sure you want to run this? " || die
  71. #execution_premission "Create ${work_dir}?" && { mkdir -p ${work_dir} || die } || { die "Work directory not selected" }
  72. options="$(find /mnt/* -maxdepth 0 -type d) new..."
  73. if prompt_select "Select directory you want to use." ; then
  74. case ${selected} in
  75. "new...") work_dir=$(prompt_new_dir /mnt);;
  76. *) work_dir=${selected};;
  77. esac
  78. else
  79. die
  80. fi
  81. fdisk -l
  82. options=$(find /dev/* -maxdepth 0 -name "sd??*" -or -name "hd??*")
  83. if prompt_select "Select work partition."; then
  84. work_part="${selected}"
  85. if execution_premission "Use filesystem container on work partition? "; then
  86. echo "Mounting work partition ${tempfs%\/*}"
  87. try mkdir -p ${tempfs%\/*} && cleanup rm -r ${tempfs%\/*}
  88. try mount ${work_part} ${tempfs%\/*} && { cleanup wait_umount ${tempfs%\/*}; cleanup umount -d ${tempfs%\/*}; }
  89. if [ ! -w ${tempfs} ]; then
  90. echo
  91. read -p "Enter size of new container (M) " tempfs_size
  92. echo "Creating filesystem container ${tempfs} of ${tempfs_size}M..."
  93. pv -EE -s ${tempfs_size}M -S -B 4k /dev/zero > ${tempfs} || echo "Cannot create filesystem container ${tempfs} of ${tempfs_size}M"
  94. options=$(find /sbin/* /usr/sbin/* -maxdepth 0 -name "mkfs.*")
  95. if prompt_select "Select filesystem"; then
  96. ${selected}
  97. read -p "Enter additional params " params
  98. try ${selected} ${params} ${tempfs}
  99. fi
  100. fi
  101. echo "Mounting filesystem container ${tempfs}"
  102. try mount -o loop ${tempfs} ${work_dir} && { cleanup wait_umount ${work_dir}; cleanup umount -d ${work_dir}; }
  103. else
  104. #if execution_premission "Format work partition? "; then
  105. #fi
  106. echo "Mounting work partition ${work_part}"
  107. try mount ${work_part} ${work_dir} && { cleanup wait_umount ${work_dir}; cleanup umount -d ${work_dir}; }
  108. fi
  109. else
  110. die
  111. fi
  112. squashfs_dst="${work_dir}/${squashfs_dst}"
  113. squashfs_src=$(find / -maxdepth 5 -size +200M -type f -name ${squashfs_name} -or -name *.squashfs)
  114. if [ -n "${squashfs_src}" ]; then
  115. options="${squashfs_src} skip"
  116. if prompt_select "Select source squashfs image: "; then
  117. try mkdir "${squashfs_mountpoint}"
  118. squashfs_src="${selected}"
  119. try mount -t squashfs -o loop ${selected} ${squashfs_mountpoint}
  120. try mkdir -p ${squashfs_dst}
  121. rsync -atiH ${squashfs_mountpoint}/* ${squashfs_dst} | pv -s $(df -i ${squashfs_mountpoint} | tail -n 1 | awk '{print $3}') > /dev/null || echo "Cannot copy the files from ${selected}"
  122. fi
  123. else
  124. echo "No squashfs found on disk."
  125. fi
  126. snapshot_list="http://ftp.osuosl.org/pub/funtoo/funtoo-current/snapshots/${portage_tree_name} \
  127. http://mirror.yandex.ru/gentoo-distfiles/snapshots/${portage_tree_name} \
  128. http://gentoo.osuosl.org/snapshots/${portage_tree_name}"
  129. if [ -n "${snapshot_list}" ]; then
  130. options="${snapshot_list} skip"
  131. if prompt_select "Select new portage tree to download? "; then
  132. try curl --progress-bar -L -o ${work_dir}/${portage_tree_name} -C - ${selected} && { cleanup rm -r ${work_dir}/${portage_tree_name}; } || echo "Cannot download new portage tree from ${selected}"
  133. fi
  134. else
  135. echo "Using snapshots found on disk."
  136. fi
  137. snapshot_list=$(find / -maxdepth 5 -type f -name "portage-*.tar.*")
  138. if [ -n "${snapshot_list}" ]; then
  139. options="${snapshot_list} skip"
  140. if prompt_select "Extract new portage tree? "; then
  141. decrunch "${selected}" "${squashfs_dst}/usr/" && cleanup rm -r ${squashfs_dst}/usr/portage || echo "Cannot extract the files from the ${selected}"
  142. fi
  143. else
  144. echo "No snapshots found on disk."
  145. fi
  146. if execution_premission "Chroot in the sysresccd environment? "; then
  147. try mkdir -p ${squashfs_dst}/proc
  148. try mkdir -p ${squashfs_dst}/dev
  149. try mkdir -p ${squashfs_dst}/sys
  150. try mount -o bind /proc ${squashfs_dst}/proc
  151. try mount -o bind /dev ${squashfs_dst}/dev
  152. try mount -o bind /sys ${squashfs_dst}/sys
  153. echo "Now you are in chrooted environment."
  154. echo "You can emerge some packages as usual."
  155. try scp -L /etc/resolv.conf ${squashfs_dst}/etc/
  156. try chroot ${squashfs_dst} env-update; source /etc/profile; gcc-config $(gcc-config -c); /bin/bash
  157. try chroot ${squashfs_dst} sysresccd-cleansys devtools; rm -rf /var/log/* /usr/sbin/sysresccd-* /usr/share/sysreccd
  158. fi
  159. if execution_premission "Create new ${squashfs_name}?"; then
  160. dir_list=$(find /mnt/* -maxdepth 1 -type d)
  161. if [ -n "${dir_list}" ]; then
  162. options="${dir_list} new..."
  163. if prompt_select "Select directory you want to use for output ${squashfs_name}. "; then
  164. case ${selected} in
  165. "new...") output=$(prompt_new_dir /mnt);;
  166. *) output=${selected};;
  167. esac
  168. else
  169. die
  170. fi
  171. fi
  172. try umount ${squashfs_dst}/proc
  173. wait_umount ${squashfs_dst}/proc
  174. do_squashfs ${output}
  175. if execution_premission "Create new ISO image?"; then
  176. options=$(find /livemnt/boot/isolinux/maps/* -maxdepth 0 -type f -name "*.ktl" -printf "%f\n" | sed -e "s!.ktl!!g")
  177. if prompt_select "Select default keymap"; then
  178. KEYMAP=${selected}
  179. fi
  180. options="$(find /mnt/* -maxdepth 1 -type d) new..."
  181. if prompt_select "Select directory you want to use for output iso."; then
  182. case ${selected} in
  183. "new...") output=$(prompt_new_dir /mnt);;
  184. *) output=${selected};;
  185. esac
  186. fi
  187. do_isogen ${output}
  188. fi
  189. fi
  190. proceed_cleanup
  191. exit 0