install_funtoo.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. #!/bin/bash
  2. stage_name="stage3-latest.tar.xz"
  3. stage_site="build.funtoo.org/funtoo-current/"
  4. base_dir=$( dirname "${BASH_SOURCE[0]}" )
  5. saved_file="$( basename "${BASH_SOURCE[0]}" ).prev"
  6. tempfs="/mnt/tempfs/fsimage"
  7. cfg_prefix="._cfg0000_"
  8. local_overlay_name="local"
  9. write_save=1
  10. work_dir=
  11. root_part=
  12. boot_part=
  13. swap_part=
  14. tempfs_part=
  15. hostname=
  16. locales=
  17. timezone=
  18. source "$( dirname "${BASH_SOURCE[0]}" )/functions.sh"
  19. printf "\nThis script helps to install funtoo"
  20. execution_premission "Sure you want to run this? " || die
  21. if [ -f "${saved_file}" ]; then
  22. saved_file="$(find ${base_dir} ~/* -name ${saved_file} -type f)"
  23. #echo ${saved_file}
  24. execution_premission "Continue previous installation?" && { source ${saved_file} || echo "" > ${saved_file}; }
  25. cleanup rm -r ${saved_file}
  26. fi
  27. part_list="$(get_partitions_list)"
  28. #echo tempfs_part=${tempfs_part}
  29. if ! [ -e "${tempfs_part}" ] || ! [ ${tempfs_part} == "skip" ]; then
  30. if execution_premission "Use virtual disk image? "; then
  31. echo
  32. read -p "Enter location of virtual disk image (default: ${tempfs})" tempfs
  33. options="${part_list}"
  34. if prompt_select "Select partition containing ${tempfs}."; then
  35. tempfs_part="${selected}"
  36. test ${write_save} && { save_var "tempfs_part" ${saved_file}; }
  37. fi
  38. echo "Mounting work partition ${tempfs%\/*}"
  39. try mkdir -p ${tempfs%\/*} && cleanup rm -r ${tempfs%\/*}
  40. try mount ${tempfs_part} ${tempfs%\/*} && { cleanup wait_umount ${tempfs%\/*}; cleanup umount ${tempfs%\/*}; }
  41. if [ ! -w ${tempfs} ]; then
  42. echo
  43. read -p "Enter size of new virtual disk image (M) " tempfs_size
  44. echo "Creating raw virtual disk image ${tempfs} of ${tempfs_size}M..."
  45. pv -EE -s ${tempfs_size}M -S -B 4k /dev/zero > ${tempfs} || echo "Cannot create virtual disk image ${tempfs} of ${tempfs_size}M"
  46. fi
  47. work_disk=$(losetup -f)
  48. try losetup -P ${work_disk} ${tempfs} && cleanup losetup -d ${work_disk}
  49. else
  50. tempfs_part="skip"
  51. test ${write_save} && { save_var "tempfs_part" ${saved_file}; }
  52. fi
  53. fi
  54. disk_list="$(lsblk -d -r | awk 'NR>1 { print $1 }') skip"
  55. if ! [ -d "${work_dir}" ]; then
  56. fdisk -l
  57. options="${disk_list}"
  58. while execution_premission "Partition disks? "; do
  59. if prompt_select "Select disk for partitioning."; then
  60. work_disk="${selected}"
  61. test ${write_save} && { save_var "work_disk" ${saved_file}; }
  62. options="fdisk gdisk skip"
  63. if prompt_select "Select partitioning programm."; then
  64. try ${selected} ${work_disk}
  65. fi
  66. fi
  67. done
  68. fi
  69. #if [ -w "${tempfs}" ]; then
  70. # try kpartx -a -v ${tempfs} && cleanup kpartx -d ${tempfs}
  71. #fi
  72. part_list="$(get_partitions_list)" #reread partlist
  73. if ! [ -d "${work_dir}" ]; then
  74. options="$(find /mnt/* -maxdepth 0 -type d)"
  75. options="${options//${tempfs%\/*}/} new..."
  76. if prompt_select "Select directory you want to use as the installation mount point."; then
  77. case ${selected} in
  78. "new...") work_dir=$(prompt_new_dir /mnt);;
  79. *) work_dir=${selected};;
  80. esac
  81. test ${write_save} && { save_var "work_dir" ${saved_file}; }
  82. fi
  83. fi
  84. if ! [ -e "${root_part}" ]; then
  85. options="${part_list}"
  86. if prompt_select "Select root partition."; then
  87. root_part="${selected}"
  88. prompt_format ${selected}
  89. test ${write_save} && { save_var "root_part" ${saved_file}; }
  90. else
  91. die
  92. fi
  93. fi
  94. part_list="${part_list//${root_part}/}"
  95. if ! [ -e "${boot_part}" ]; then
  96. if [ -n "${part_list//[[:cntrl:]]/}" ]; then
  97. options="${part_list} skip"
  98. if prompt_select "Select boot partition."; then
  99. boot_part="${selected}"
  100. execution_premission "Format boot partition? " && { try mkfs.vfat -F 32 ${boot_part}; }
  101. fi
  102. test ${write_save} && { save_var "boot_part" ${saved_file}; }
  103. fi
  104. fi
  105. #disk_list="$(find /dev/* -maxdepth 0 -name "sd?" -or -name "hd?")"
  106. part_list="${disk_list//${root_part%[[:digit:]]*}/} ${part_list//${boot_part}/}"
  107. if ! [ -e "${swap_part}" ]; then
  108. if [ -n "${part_list//[[:cntrl:]]/}" ]; then
  109. options="${part_list} skip"
  110. if prompt_select "Select swap partition."; then
  111. swap_part="${selected}"
  112. try mkswap ${swap_part}
  113. try swapon ${swap_part} && { cleanup swapoff ${swap_part}; }
  114. test ${write_save} && { save_var "swap_part" ${saved_file}; }
  115. fi
  116. fi
  117. fi
  118. printf "\nMounting partitions. \n"
  119. try mount ${root_part} ${work_dir} && { cleanup wait_umount ${work_dir}; cleanup umount -d ${work_dir}; }
  120. try mkdir -p ${work_dir}/boot
  121. ! [ -e ${boot_part} ] && try mount ${boot_part} ${work_dir}/boot && { cleanup wait_umount ${work_dir}/boot; cleanup umount -d ${work_dir}/boot; }
  122. if ! [ -f "${stage}" ]; then
  123. if execution_premission "Download new stage?"; then
  124. printf "\nLoading file list..."
  125. stage_list=$(wget -m -r -np -nd -e robots=off --spider -l4 -A ${stage_name} ${stage_site} 2>&1 | grep -Eio https\:\/\/.+${stage_name})
  126. #cleanup "rm -r ${stage_site}"
  127. options="${stage_list} skip"
  128. if prompt_select "Select stage to download."; then
  129. echo ${work_dir}/${selected##*\/}
  130. try download ${selected} ${work_dir} && { cleanup rm -r "${work_dir}/${selected##*\/}"; cleanup rm -r ~/.wget-hsts; cleanuo rm ~/index*.tmp; } || echo "Cannot download ${selected##*\/}"
  131. #try download "${selected}.DIGESTS.asc" ${work_dir} && cleanup rm -r "${work_dir}/${selected##*\/}.DIGESTS.asc" || echo "Cannot download ${selected##*\/}.DIGESTS.asc"
  132. #try download "${selected}.DIGESTS" ${work_dir} && cleanup rm -r "${work_dir}/${selected##*\/}.DIGESTS" || echo "Cannot download ${selected##*\/}.DIGESTS"
  133. try download ${selected}".hash.txt" ${work_dir} && cleanup rm -r "${work_dir}/${selected##*\/}.hash.txt" || echo "Cannot download ${selected}.hash.txt"
  134. #gpg --keyserver hkp://keys.gnupg.net --recv-keys 0xBB572E0E2D182910
  135. #gpg --verify ${work_dir}/${selected##*\/}.DIGESTS.asc
  136. echo "Verifing checksum..."
  137. try echo $(cat ${work_dir}/${selected##*\/}.hash.txt | awk -F' ' '{ if ($1 ~ "SHA512") next; print $1; exit}') ${work_dir}/${selected##*\/} | sha256sum -c -
  138. fi
  139. fi
  140. fi
  141. stage_list=$(find / -maxdepth 5 -type f -name "${stage_name}")
  142. if [ -n "${stage_list}" ]; then
  143. options="${stage_list} skip"
  144. if prompt_select "Select stage to extract."; then
  145. stage="${selected}"
  146. decrunch --xattrs-include='*.*' --numeric-owner ${stage} ${work_dir} || die "Cannot extract ${stage}"
  147. test ${write_save} && { save_var "stage" ${saved_file}; }
  148. fi
  149. else
  150. if [ ! -x "${work_dir}/bin/bash" ]; then
  151. die "No ${stage_name} found on disk."
  152. fi
  153. fi
  154. template_list=$(find / ${base_dir} -maxdepth 2 -type f -name "*.template")
  155. if [ -n "${template_list}" ]; then
  156. if execution_premission "Install templates files? "; then
  157. for file in ${template_list}
  158. do
  159. output=$(sed -n "1s|#||g;1 p" ${file})
  160. orig_file="${work_dir}${output//[[:cntrl:]][[:blank:]]/}"
  161. echo "${file} > ${orig_file}"
  162. output=$(sed -n "2,$ p" ${file})
  163. if [ $? -eq 0 ]; then
  164. if [ ! -d "${orig_file%\/*}" ]; then
  165. if [ -e "${orig_file%\/*}" ] || [ -L "${orig_file%\/*}" ]; then
  166. try rm ${orig_file%\/*}
  167. fi
  168. try mkdir -p "${orig_file%\/*}"
  169. fi
  170. echo "${output}" > ${orig_file}
  171. [ -f "${orig_file}" ] || echo "Cannot write ${orig_file}"
  172. else
  173. echo "sed \"2,\$ p\" ${file} failed"
  174. fi
  175. done
  176. #repos_list="$(find ${work_dir}/etc/portage/repos.conf -type f )"
  177. #echo ${repos_list}
  178. #for file in ${repos_list}
  179. #do
  180. # overlay_location=$(cat ${file} | awk -F= '{ if ($1 ~ "location") print $2}')
  181. # echo ${overlay_location}
  182. # if ! [ -d ${overlay_location} ]; then
  183. # try mkdir -p ${overlay_location}
  184. # fi
  185. #done
  186. fi
  187. fi
  188. if execution_premission "Edit config files? "; then
  189. if [ -z "${password}" ]; then
  190. read -p "Enter new password for root " password
  191. test ${write_save} && ! [ -z password ] && { save_var "password" ${saved_file}; }
  192. fi
  193. shadow="$(openssl passwd -1 ${password}):$(( $(date +%s)/86400 )):0:::::"
  194. sed -e "s|^\(root:\).*|\1"${shadow}"|" ${work_dir}/etc/shadow > ${work_dir}/etc/${cfg_prefix}shadow
  195. if [ -z "${hostname}" ]; then
  196. read -p "Enter hostname " hostname
  197. test ${write_save} && { save_var "hostname" ${saved_file}; }
  198. fi
  199. sed -e "s|^\(hostname=\).*|\1\""${hostname}"\"|" ${work_dir}/etc/conf.d/hostname > ${work_dir}/etc/conf.d/${cfg_prefix}hostname
  200. if execution_premission "Enable automatic root login?"; then
  201. if [ -z "${autologin}" ]; then
  202. test ${write_save} && { save_var "autologin" ${saved_file}; }
  203. fi
  204. sed -e "s|\bc1:12345:respawn:/sbin/agetty\b|& -a root|" ${work_dir}/etc/inittab > ${work_dir}/etc/${cfg_prefix}inittab
  205. fi
  206. zoneinfo="${work_dir}/usr/share/zoneinfo"
  207. if [ -z "${timezone}" ]; then
  208. options="$(find ${zoneinfo}/* -maxdepth 2 -type f ! -name "*.*" | sed -e "s|${zoneinfo}||g") skip"
  209. if prompt_select "Select timezone? "; then
  210. timezone=${selected}
  211. test ${write_save} && { save_var "timezone" ${saved_file}; }
  212. fi
  213. fi
  214. try ln -rsf /usr/share/zoneinfo${timezone} ${work_dir}/etc/localtime || echo "Cannot set timezone ${selected}"
  215. if [ -z "${hwclock}" ]; then
  216. options="UTC local skip"
  217. if prompt_select "Select hardware clock mode? "; then
  218. hwclock=${selected}
  219. test ${write_save} && { save_var "hwclock" ${saved_file}; }
  220. fi
  221. fi
  222. sed -e "s|^\(clock=\).*|\1\""${hwclock}"\"|" ${work_dir}/etc/conf.d/hwclock > ${work_dir}/etc/conf.d/${cfg_prefix}hwclock
  223. if [ -z "${locales}" ]; then
  224. cat ${work_dir}/etc/locale.gen > ${work_dir}/etc/${cfg_prefix}locale.gen
  225. while execution_premission "Add language? "; do
  226. options="$(cat ${work_dir}/usr/share/i18n/SUPPORTED | sed -e "s| UTF-8||g" | grep 'UTF-8') skip"
  227. if prompt_select "Select language? "; then
  228. locales+="${selected} "
  229. fi
  230. done
  231. test ${write_save} && { save_var "locales" ${saved_file}; }
  232. fi
  233. for locale in ${locales}
  234. do
  235. echo "${locale} UTF-8" >> ${work_dir}/etc/${cfg_prefix}locale.gen
  236. done
  237. if [ -z "${keymap}" ]; then
  238. keymap_dir="${work_dir}/usr/share/keymaps"
  239. options="$(find ${keymap_dir}/* -maxdepth 3 -type f -name "*.map*" -printf "%f\n" | sed -e "s|.map.*||g" | sort) skip"
  240. if prompt_select "Select keymap? "; then
  241. keymap="${selected}"
  242. fi
  243. test ${write_save} && { save_var "keymap" ${saved_file}; }
  244. fi
  245. sed -e "s|^\(keymap=\).*|\1\""${keymap}"\"|" ${work_dir}/etc/conf.d/keymaps > ${work_dir}/etc/conf.d/${cfg_prefix}keymaps
  246. sed -e "s|^\(consolefont=\).*|\1\"ter-u16b\"|" ${work_dir}/etc/conf.d/consolefont > ${work_dir}/etc/conf.d/${cfg_prefix}consolefont
  247. if [ -a ${work_dir}/etc/runlevels/boot/consolefont ]; then
  248. ln -rs /etc/init.d/conslefont ${work_dir}/etc/runlevels/boot/consolefont
  249. fi
  250. fstabgen "${root_part}:/:defaults:0:1 ${boot_part}:/boot:noauto,noatime:1:2 ${swap_part}:swap:sw:0:0" "${work_dir}/etc/fstab"
  251. sed -e "s|^\(MAKEOPTS=\).*|\1\"-j"$(( $(nproc)+1 ))" --quiet\"|" ${work_dir}/etc/genkernel.conf > ${work_dir}/etc/${cfg_prefix}genkernel.conf
  252. sed -e "s|^\(MAKEOPTS=\).*|\1\"-j"$(( $(nproc)+1 ))" --quiet\"|" ${work_dir}/etc/portage/make.conf > ${work_dir}/etc/portage/${cfg_prefix}make.conf
  253. sed -i "s|^\(LINGUAS=\).*|\1\""${locales//_*/}"\"|" ${work_dir}/etc/portage/${cfg_prefix}make.conf
  254. sed -i "s|^\(L10N=\).*|\1\""${locales//_*/}"\"|" ${work_dir}/etc/portage/${cfg_prefix}make.conf
  255. fi
  256. if execution_premission "Enable dhcp network? "; then
  257. ln -rsf ${work_dir}/etc/init.d/netif.tmpl ${work_dir}/etc/init.d/net.eth0
  258. echo template=dhcpcd > ${work_dir}/etc/conf.d/${cfg_prefix}net.eth0
  259. ln -rsf ${work_dir}/etc/init.d/dhcpcd ${work_dir}/etc/runlevels/default/dhcpcd
  260. cp /etc/resolv.conf ${work_dir}/etc/resolv.conf
  261. echo "nameserver 8.8.8.8" >> ${work_dir}/etc/resolv.conf
  262. echo "nameserver 8.8.4.4" >> ${work_dir}/etc/resolv.conf
  263. fi
  264. if execution_premission "Create blank local overlay? "; then
  265. echo
  266. read -p "Enter name of the local overlay (default: "${local_overlay_name}") " entered_overlay_name
  267. if [ -n "${entered_overlay_name}" ]; then
  268. local_overlay_name=${entered_overlay_name}
  269. fi
  270. mkdir -p ${work_dir}/var/overlay/${local_overlay_name}
  271. git clone https://github.com/funtoo/skeleton-overlay.git ${work_dir}/var/overlay/${local_overlay_name}
  272. echo ${local_overlay_name} > ${work_dir}/var/overlay/${local_overlay_name}/profiles/repo_name
  273. echo "masters = gentoo" >> ${work_dir}/var/overlay/${local_overlay_name}/metadata/layout.conf
  274. echo -e "[DEFAULT]/nmain-repo = gentoo/n["${local_overlay_name}"]\
  275. /nlocation = /var/overlay/${local_overlay_name}\
  276. /nauto-sync = no/npriority = 10/n" > ${work_dir}/etc/portage/repos.conf/${local_overlay_name}.conf
  277. fi
  278. if execution_premission "Remove /usr/share/{info,man,doc,gtk-doc} directories, and remove man packages from minimal system profile?"; then
  279. rm -r ${work_dir}/usr/share/{info,man,doc,gtk-doc}
  280. packages_path=${work_dir}/var/git/meta-repo/kits/core-kit/profiles/funtoo/1.0/linux-gnu/flavor/minimal/packages
  281. sed -i "s|^\(\*virtual/man\).*|\1\#\*virtual/man|" ${packages_path}
  282. sed -i "s|^\(\*sys-apps/man-pages\).*|\1\#\*sys-apps/man-pages|" ${packages_path}
  283. chattr +i ${packages_path}
  284. fi
  285. if execution_premission "Chroot in the new system environment? "; then
  286. try mount -t proc none ${work_dir}/proc && { cleanup wait_umount ${work_dir}/proc; cleanup umount ${work_dir}/proc; }
  287. try mount --rbind /sys ${work_dir}/sys && { cleanup umount -l ${work_dir}/sys; }
  288. try mount --rbind /dev ${work_dir}/dev && { cleanup umount -l ${work_dir}/dev; }
  289. profile="\
  290. etc-update; env-update && source /etc/profile \n\
  291. locale-gen; env-update && source /etc/profile \n\
  292. echo -e \"\nNow you are in chrooted environment.\
  293. \nselect default languge via eselect locale set\
  294. \nsync portage tree and merge packages you need\" \n\
  295. rm -rf ~/.bash_login"
  296. echo -e ${profile} > ${work_dir}/root/.bash_login
  297. chmod +x ${work_dir}/root/.bash_login
  298. env -i HOME=/root TERM=$TERM SHELL=/bin/bash chroot ${work_dir} /root/.bash_login
  299. env -i HOME=/root TERM=$TERM SHELL=/bin/bash chroot ${work_dir} /bin/bash --login
  300. fi
  301. proceed_cleanup
  302. exit 0