installer.sh.in 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  1. #!/bin/sh
  2. #-
  3. # Copyright (c) 2012-2014 Juan Romero Pardines <[email protected]>.
  4. # 2012 Dave Elusive <[email protected]>.
  5. # All rights reserved.
  6. #
  7. # Redistribution and use in source and binary forms, with or without
  8. # modification, are permitted provided that the following conditions
  9. # are met:
  10. # 1. Redistributions of source code must retain the above copyright
  11. # notice, this list of conditions and the following disclaimer.
  12. # 2. Redistributions in binary form must reproduce the above copyright
  13. # notice, this list of conditions and the following disclaimer in the
  14. # documentation and/or other materials provided with the distribution.
  15. #
  16. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  17. # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18. # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  19. # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25. # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. #-
  27. # Make sure we don't inherit these from env.
  28. SOURCE_DONE=
  29. HOSTNAME_DONE=
  30. KEYBOARD_DONE=
  31. LOCALE_DONE=
  32. TIMEZONE_DONE=
  33. ROOTPASSWORD_DONE=
  34. BOOTLOADER_DONE=
  35. PARTITIONS_DONE=
  36. NETWORK_DONE=
  37. FILESYSTEMS_DONE=
  38. TARGETDIR=/mnt/target
  39. LOG=/dev/tty7
  40. CONF_FILE=/tmp/.void-installer.conf
  41. if [ ! -f $CONF_FILE ]; then
  42. touch -f $CONF_FILE
  43. fi
  44. ANSWER=$(mktemp -t vinstall-XXXXXXXX || exit 1)
  45. TARGET_FSTAB=$(mktemp -t vinstall-fstab-XXXXXXXX || exit 1)
  46. trap "DIE" INT TERM QUIT
  47. # disable printk
  48. if [ -w /proc/sys/kernel/printk ]; then
  49. echo 0 >/proc/sys/kernel/printk
  50. fi
  51. # Detect if this is an EFI system.
  52. if [ -e /sys/firmware/efi/systab ]; then
  53. EFI_SYSTEM=1
  54. fi
  55. # dialog colors
  56. BLACK="\Z0"
  57. RED="\Z1"
  58. GREEN="\Z2"
  59. YELLOW="\Z3"
  60. BLUE="\Z4"
  61. MAGENTA="\Z5"
  62. CYAN="\Z6"
  63. WHITE="\Z7"
  64. BOLD="\Zb"
  65. REVERSE="\Zr"
  66. UNDERLINE="\Zu"
  67. RESET="\Zn"
  68. # Properties shared per widget.
  69. MENULABEL="${BOLD}Use UP and DOWN keys to navigate \
  70. menus. Use TAB to switch between buttons and ENTER to select.${RESET}"
  71. MENUSIZE="14 60 0"
  72. INPUTSIZE="8 60"
  73. MSGBOXSIZE="8 70"
  74. YESNOSIZE="$INPUTSIZE"
  75. WIDGET_SIZE="10 70"
  76. DIALOG() {
  77. rm -f $ANSWER
  78. dialog --colors --keep-tite --no-shadow --no-mouse \
  79. --backtitle "${BOLD}${WHITE}Void Linux installation -- http://www.voidlinux.eu/ (@@MKLIVE_VERSION@@)${RESET}" \
  80. --cancel-label "Back" --aspect 20 "$@" 2>$ANSWER
  81. return $?
  82. }
  83. DIE() {
  84. rval=$1
  85. [ -z "$rval" ] && rval=0
  86. clear
  87. rm -f $ANSWER $TARGET_FSTAB
  88. # reenable printk
  89. if [ -w /proc/sys/kernel/printk ]; then
  90. echo 4 >/proc/sys/kernel/printk
  91. fi
  92. umount_filesystems
  93. exit $rval
  94. }
  95. set_option() {
  96. if grep -Eq "^${1}.*" $CONF_FILE; then
  97. sed -i -e "/^${1}.*/d" $CONF_FILE
  98. fi
  99. echo "${1} ${2}" >>$CONF_FILE
  100. }
  101. get_option() {
  102. echo $(grep -E "^${1}.*" $CONF_FILE|sed -e "s|${1}||")
  103. }
  104. show_disks() {
  105. local dev size sectorsize gbytes
  106. # IDE
  107. for dev in $(ls /sys/block|grep -E '^hd'); do
  108. if [ "$(cat /sys/block/$dev/device/media)" = "disk" ]; then
  109. # Find out nr sectors and bytes per sector;
  110. echo "/dev/$dev"
  111. size=$(cat /sys/block/$dev/size)
  112. sectorsize=$(cat /sys/block/$dev/queue/hw_sector_size)
  113. gbytes="$(($size * $sectorsize / 1024 / 1024 / 1024))"
  114. echo "size:${gbytes}GB;sector_size:$sectorsize"
  115. fi
  116. done
  117. # SATA/SCSI and Virtual disks (virtio)
  118. for dev in $(ls /sys/block|grep -E '^([sv]|xv)d'); do
  119. echo "/dev/$dev"
  120. size=$(cat /sys/block/$dev/size)
  121. sectorsize=$(cat /sys/block/$dev/queue/hw_sector_size)
  122. gbytes="$(($size * $sectorsize / 1024 / 1024 / 1024))"
  123. echo "size:${gbytes}GB;sector_size:$sectorsize"
  124. done
  125. }
  126. show_partitions() {
  127. local dev fstype fssize p part
  128. set -- $(show_disks)
  129. while [ $# -ne 0 ]; do
  130. disk=$(basename $1)
  131. shift 2
  132. # ATA/SCSI/SATA
  133. for p in /sys/block/$disk/$disk*; do
  134. if [ -d $p ]; then
  135. part=$(basename $p)
  136. fstype=$(lsblk -nfr /dev/$part|awk '{print $2}')
  137. [ "$fstype" = "iso9660" ] && continue
  138. fssize=$(lsblk -nr /dev/$part|awk '{print $4}')
  139. echo "/dev/$part"
  140. echo "size:${fssize:-unknown};fstype:${fstype:-none}"
  141. fi
  142. done
  143. # LVM
  144. for p in $(ls /dev/mapper 2>/dev/null|grep -v control); do
  145. [ "$p" = "live-rw" ] && continue
  146. [ "$p" = "live-base" ] && continue
  147. fstype=$(lsblk -nfr /dev/$part|awk '{print $2}')
  148. fssize=$(lsblk -nr /dev/mapper/$p|awk '{print $4}')
  149. echo "/dev/mapper/$p"
  150. echo "size:${fssize:-unknown};fstype:${fstype:-none}"
  151. done
  152. # Software raid (md)
  153. for p in $(ls -d /dev/md* 2>/dev/null|grep '[0-9]'); do
  154. if cat /proc/mdstat|grep -qw $(echo $p|sed -e 's|/dev/||g'); then
  155. fstype=$(lsblk -nfr /dev/$part|awk '{print $2}')
  156. fssize=$(lsblk -nr /dev/$p|awk '{print $4}')
  157. echo "$p"
  158. echo "size:${fssize:-unknown};fstype:${fstype:-none}"
  159. fi
  160. done
  161. done
  162. }
  163. menu_filesystems() {
  164. local dev fstype fssize mntpoint reformat
  165. while true; do
  166. DIALOG --title " Select the partition to edit " --menu "$MENULABEL" \
  167. ${MENUSIZE} $(show_partitions)
  168. [ $? -ne 0 ] && return
  169. dev=$(cat $ANSWER)
  170. DIALOG --title " Select the filesystem type for $dev " \
  171. --menu "$MENULABEL" ${MENUSIZE} \
  172. "btrfs" "Oracle's Btrfs" \
  173. "ext2" "Linux ext2 (no journaling)" \
  174. "ext3" "Linux ext3 (journal)" \
  175. "ext4" "Linux ext4 (journal)" \
  176. "f2fs" "Flash-Friendly Filesystem" \
  177. "swap" "Linux swap" \
  178. "vfat" "FAT32" \
  179. "xfs" "SGI's XFS"
  180. if [ $? -eq 0 ]; then
  181. fstype=$(cat $ANSWER)
  182. else
  183. continue
  184. fi
  185. if [ "$fstype" != "swap" ]; then
  186. DIALOG --inputbox "Please specify the mount point for $dev:" ${INPUTSIZE}
  187. if [ $? -eq 0 ]; then
  188. mntpoint=$(cat $ANSWER)
  189. elif [ $? -eq 1 ]; then
  190. continue
  191. fi
  192. else
  193. mntpoint=swap
  194. fi
  195. DIALOG --yesno "Do you want to create a new filesystem on $dev?" ${YESNOSIZE}
  196. if [ $? -eq 0 ]; then
  197. reformat=1
  198. elif [ $? -eq 1 ]; then
  199. reformat=0
  200. else
  201. continue
  202. fi
  203. fssize=$(lsblk -nr $dev|awk '{print $4}')
  204. set -- "$fstype" "$fssize" "$mntpoint" "$reformat"
  205. if [ -n "$1" -a -n "$2" -a -n "$3" -a -n "$4" ]; then
  206. local bdev=$(basename $dev)
  207. if grep -Eq "^MOUNTPOINT \/dev\/${bdev}.*" $CONF_FILE; then
  208. sed -i -e "/^MOUNTPOINT \/dev\/${bdev}.*/d" $CONF_FILE
  209. fi
  210. echo "MOUNTPOINT $dev $1 $2 $3 $4" >>$CONF_FILE
  211. fi
  212. done
  213. }
  214. menu_partitions() {
  215. DIALOG --title " Select the disk to partition " \
  216. --menu "$MENULABEL" ${MENUSIZE} $(show_disks)
  217. if [ $? -eq 0 ]; then
  218. local device=$(cat $ANSWER)
  219. DIALOG --title "Modify Partition Table on $device" --msgbox "\n
  220. ${BOLD}GNU parted will be executed in disk $device.${RESET}\n\n
  221. For BIOS systems, MBR or GPT partition tables are supported.\n
  222. To use GPT on PC BIOS systems an empty partition of 1MB must be added\n
  223. at the first 2GB of the disk with the TOGGLE \`bios_grub' enabled.\n
  224. ${BOLD}NOTE: you don't need this on EFI systems.${RESET}\n\n
  225. For EFI systems GPT is mandatory and a FAT32 partition with at least\n
  226. 100MB must be created with the TOGGLE \`boot', this will be used as\n
  227. EFI System Partition. This partition must have mountpoint as \`/boot/efi'.\n\n
  228. At least 2 partitions are required: swap and rootfs (/).\n
  229. For swap, RAM*2 must be really enough. For / 600MB are required.\n\n
  230. ${BOLD}WARNING: /usr is not supported as a separate partition.${RESET}\n
  231. ${BOLD}WARNING: changes made by parted are destructive, you've been warned.
  232. ${RESET}\n" 18 80
  233. if [ $? -eq 0 ]; then
  234. while true; do
  235. clear; parted $device; PARTITIONS_DONE=1; partprobe $device
  236. break
  237. done
  238. else
  239. return
  240. fi
  241. fi
  242. }
  243. menu_keymap() {
  244. local _keymaps="$(localectl --no-pager list-keymaps)"
  245. local _KEYMAPS=
  246. for f in ${_keymaps}; do
  247. _KEYMAPS="${_KEYMAPS} ${f} -"
  248. done
  249. while true; do
  250. DIALOG --title " Select your keymap " --menu "$MENULABEL" 14 70 14 ${_KEYMAPS}
  251. if [ $? -eq 0 ]; then
  252. set_option KEYMAP "$(cat $ANSWER)"
  253. loadkeys "$(cat $ANSWER)"
  254. KEYBOARD_DONE=1
  255. break
  256. else
  257. return
  258. fi
  259. done
  260. }
  261. set_keymap() {
  262. local KEYMAP=$(get_option KEYMAP)
  263. sed -i -e "s|KEYMAP=.*|KEYMAP=$KEYMAP|g" $TARGETDIR/etc/vconsole.conf
  264. }
  265. menu_locale() {
  266. local _locales="$(grep -E '\.UTF-8' /etc/default/libc-locales|awk '{print $1}'|sed -e 's/^#//')"
  267. local _LOCALES=
  268. for f in ${_locales}; do
  269. _LOCALES="${_LOCALES} ${f} -"
  270. done
  271. while true; do
  272. DIALOG --title " Select your locale " --menu "$MENULABEL" 14 70 14 ${_LOCALES}
  273. if [ $? -eq 0 ]; then
  274. set_option LOCALE "$(cat $ANSWER)"
  275. LOCALE_DONE=1
  276. break
  277. else
  278. return
  279. fi
  280. done
  281. }
  282. set_locale() {
  283. local LOCALE=$(get_option LOCALE)
  284. sed -i -e "s|LANG=.*|LANG=$LOCALE|g" $TARGETDIR/etc/locale.conf
  285. # Uncomment locale from /etc/default/libc-locales and regenerate it.
  286. sed -e "/${LOCALE}/s/^\#//" -i $TARGETDIR/etc/default/libc-locales
  287. echo "Running xbps-reconfigure -f glibc-locales ..." >$LOG
  288. chroot $TARGETDIR xbps-reconfigure -f glibc-locales >$LOG 2>&1
  289. }
  290. menu_timezone() {
  291. local _tzones="$(timedatectl --no-pager list-timezones)"
  292. local _TIMEZONES=
  293. for f in ${_tzones}; do
  294. _TIMEZONES="${_TIMEZONES} ${f} -"
  295. done
  296. while true; do
  297. DIALOG --title " Select your timezone " --menu "$MENULABEL" 14 70 14 ${_TIMEZONES}
  298. if [ $? -eq 0 ]; then
  299. set_option TIMEZONE "$(cat $ANSWER)"
  300. TIMEZONE_DONE=1
  301. break
  302. else
  303. return
  304. fi
  305. done
  306. }
  307. set_timezone() {
  308. local TIMEZONE="$(get_option TIMEZONE)"
  309. ln -sf /usr/share/zoneinfo/${TIMEZONE} $TARGETDIR/etc/localtime
  310. }
  311. menu_hostname() {
  312. while true; do
  313. DIALOG --inputbox "Set the machine hostname:" ${INPUTSIZE}
  314. if [ $? -eq 0 ]; then
  315. set_option HOSTNAME "$(cat $ANSWER)"
  316. HOSTNAME_DONE=1
  317. break
  318. else
  319. return
  320. fi
  321. done
  322. }
  323. set_hostname() {
  324. echo $(get_option HOSTNAME) > $TARGETDIR/etc/hostname
  325. }
  326. menu_rootpassword() {
  327. local _firstpass= _secondpass= _desc=
  328. while true; do
  329. if [ -n "${_firstpass}" ]; then
  330. _desc="Enter the root password again (password won't be displayed)"
  331. else
  332. _desc="Enter the root password (password won't be displayed)"
  333. fi
  334. DIALOG --passwordbox "${_desc}" ${MSGBOXSIZE}
  335. if [ $? -eq 0 ]; then
  336. if [ -z "${_firstpass}" ]; then
  337. _firstpass="$(cat $ANSWER)"
  338. else
  339. _secondpass="$(cat $ANSWER)"
  340. fi
  341. if [ -n "${_firstpass}" -a -n "${_secondpass}" ]; then
  342. if [ "${_firstpass}" != "${_secondpass}" ]; then
  343. DIALOG --infobox "Passwords do not match! please reenter it again" 6 80
  344. unset _firstpass _secondpass
  345. sleep 2 && continue
  346. fi
  347. set_option ROOTPASSWORD "${_firstpass}"
  348. ROOTPASSWORD_DONE=1
  349. break
  350. fi
  351. else
  352. return
  353. fi
  354. done
  355. }
  356. set_rootpassword() {
  357. echo "root:$(get_option ROOTPASSWORD)" | chpasswd -R $TARGETDIR -c SHA512
  358. }
  359. menu_bootloader() {
  360. while true; do
  361. DIALOG --title " Select the disk to install the bootloader" \
  362. --menu "$MENULABEL" ${MENUSIZE} $(show_disks)
  363. if [ $? -eq 0 ]; then
  364. set_option BOOTLOADER "$(cat $ANSWER)"
  365. BOOTLOADER_DONE=1
  366. break
  367. else
  368. return
  369. fi
  370. done
  371. }
  372. set_bootloader() {
  373. local dev=$(get_option BOOTLOADER) grub_args=
  374. # Check if it's an EFI system via efivars module.
  375. if [ -n "$EFI_SYSTEM" ]; then
  376. grub_args="--target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=void_grub --recheck"
  377. fi
  378. echo "Running grub-install $grub_args $dev..." >$LOG
  379. chroot $TARGETDIR grub-install $grub_args $dev >$LOG 2>&1
  380. if [ $? -ne 0 ]; then
  381. DIALOG --msgbox "${BOLD}${RED}ERROR:${RESET} \
  382. failed to install GRUB to $dev!\nCheck $LOG for errors." ${MSGBOXSIZE}
  383. DIE 1
  384. fi
  385. echo "Running grub-mkconfig on $TARGETDIR..." >$LOG
  386. chroot $TARGETDIR grub-mkconfig -o /boot/grub/grub.cfg >$LOG 2>&1
  387. if [ $? -ne 0 ]; then
  388. DIALOG --msgbox "${BOLD}${RED}ERROR${RESET}: \
  389. failed to run grub-mkconfig!\nCheck $LOG for errors." ${MSGBOXSIZE}
  390. DIE 1
  391. fi
  392. }
  393. test_network() {
  394. rm -f xtraeme.asc && \
  395. xbps-uhelper fetch http://xbps.nopcode.org/live/xtraeme.asc >$LOG 2>&1
  396. if [ $? -eq 0 ]; then
  397. DIALOG --msgbox "Network is working properly!" ${MSGBOXSIZE}
  398. NETWORK_DONE=1
  399. return 1
  400. fi
  401. DIALOG --msgbox "Network is unaccessible, please setup it properly." ${MSGBOXSIZE}
  402. }
  403. configure_wifi() {
  404. local dev="$1" ssid enc pass _wpasupconf=/etc/wpa_supplicant/wpa_supplicant.conf
  405. DIALOG --form "Wireless configuration for ${dev}\n(encryption type: wep or wpa)" 0 0 0 \
  406. "SSID:" 1 1 "" 1 16 30 0 \
  407. "Encryption:" 2 1 "" 2 16 3 0 \
  408. "Password:" 3 1 "" 3 16 30 0 || return 1
  409. set -- $(cat $ANSWER)
  410. ssid="$1"; enc="$2"; pass="$3";
  411. if [ -z "$ssid" ]; then
  412. DIALOG --msgbox "Invalid SSID." ${MSGBOXSIZE}
  413. return 1
  414. elif [ -z "$enc" -o "$enc" != "wep" -a "$enc" != "wpa" ]; then
  415. DIALOG --msgbox "Invalid encryption type (possible values: wep or wpa)." ${MSXBOXSIZE}
  416. return 1
  417. elif [ -z "$pass" ]; then
  418. DIALOG --msgbox "Invalid AP password." ${MSGBOXSIZE}
  419. fi
  420. rm -f ${_wpasupconf%.conf}-${dev}.conf
  421. cp -f ${_wpasupconf} ${_wpasupconf%.conf}-${dev}.conf
  422. if [ "$enc" = "wep" ]; then
  423. echo "network={" >> ${_wpasupconf%.conf}-${dev}.conf
  424. echo " ssid=\"$ssid\"" >> ${_wpasupconf%.conf}-${dev}.conf
  425. echo " wep_key0=\"$pass\"" >> ${_wpasupconf%.conf}-${dev}.conf
  426. echo " wep_tx_keyidx=0" >> ${_wpasupconf%.conf}-${dev}.conf
  427. echo " auth_alg=SHARED" >> ${_wpasupconf%.conf}-${dev}.conf
  428. echo "}" >> ${_wpasupconf%.conf}-${dev}.conf
  429. else
  430. wpa_passphrase "$ssid" "$pass" >> ${_wpasupconf%.conf}-${dev}.conf
  431. fi
  432. configure_net_dhcp $dev
  433. return $?
  434. }
  435. configure_net() {
  436. local dev="$1" rval
  437. DIALOG --yesno "Do you want to use DHCP for $dev?" ${YESNOSIZE}
  438. rval=$?
  439. if [ $rval -eq 0 ]; then
  440. configure_net_dhcp $dev
  441. elif [ $rval -eq 1 ]; then
  442. configure_net_static $dev
  443. fi
  444. }
  445. iface_setup() {
  446. ip addr show dev $1|grep -q 'inet '
  447. return $?
  448. }
  449. configure_net_dhcp() {
  450. local dev="$1"
  451. iface_setup $dev
  452. if [ $? -eq 1 ]; then
  453. dhcpcd -t 10 -w -4 -L $dev -e "wpa_supplicant_conf=/etc/wpa_supplicant/wpa_supplicant-${dev}.conf" 2>&1 | tee $LOG | \
  454. DIALOG --progressbox "Initializing $dev via DHCP..." ${WIDGET_SIZE}
  455. if [ $? -ne 0 ]; then
  456. DIALOG --msgbox "${BOLD}${RED}ERROR:${RESET} failed to run dhcpcd. See $LOG for details." ${MSGBOXSIZE}
  457. return 1
  458. fi
  459. iface_setup $dev
  460. if [ $? -eq 1 ]; then
  461. DIALOG --msgbox "${BOLD}${RED}ERROR:${RESET} DHCP request failed for $dev. Check $LOG for errors." ${MSGBOXSIZE}
  462. return 1
  463. fi
  464. fi
  465. test_network
  466. if [ $? -eq 1 ]; then
  467. set_option NETWORK "${dev} dhcp"
  468. fi
  469. }
  470. configure_net_static() {
  471. local ip gw dns1 dns2 dev=$1
  472. DIALOG --form "Static IP configuration for $dev:" 0 0 0 \
  473. "IP address:" 1 1 "192.168.0.2" 1 21 20 0 \
  474. "Gateway:" 2 1 "192.168.0.1" 2 21 20 0 \
  475. "DNS Primary" 3 1 "8.8.8.8" 3 21 20 0 \
  476. "DNS Secondary" 4 1 "8.8.4.4" 4 21 20 0 || return 1
  477. set -- $(cat $ANSWER)
  478. ip=$1; gw=$2; dns1=$3; dns2=$4
  479. echo "running: ip link set dev $dev up" >$LOG
  480. ip link set dev $dev up >$LOG 2>&1
  481. if [ $? -ne 0 ]; then
  482. DIALOG --msgbox "${BOLD}${RED}ERROR:${RESET} Failed to bring $dev interface." ${MSGBOXSIZE}
  483. return 1
  484. fi
  485. echo "running: ip addr add $ip dev $dev"
  486. ip addr add $ip dev $dev >$LOG 2>&1
  487. if [ $? -ne 0 ]; then
  488. DIALOG --msgbox "${BOLD}${RED}ERROR:${RESET} Failed to set ip to the $dev interface." ${MSGBOXSIZE}
  489. return 1
  490. fi
  491. ip route add $gw dev $dev >$LOG 2>&1
  492. if [ $? -ne 0 ]; then
  493. DIALOG --msgbox "${BOLD}${RED}ERROR:${RESET} failed to setup your gateway." ${MSGBOXSIZE}
  494. return 1
  495. fi
  496. echo "nameserver $dns1" >/etc/resolv.conf
  497. echo "nameserver $dns2" >>/etc/resolv.conf
  498. test_network
  499. if [ $? -eq 1 ]; then
  500. set_option NETWORK "${dev} static $ip $gw $dns1 $dns2"
  501. fi
  502. }
  503. menu_network() {
  504. local dev addr f DEVICES
  505. for f in $(ls /sys/class/net); do
  506. [ "$f" = "lo" ] && continue
  507. addr=$(cat /sys/class/net/$f/address)
  508. DEVICES="$DEVICES $f $addr"
  509. done
  510. DIALOG --title " Select the network interface to configure " \
  511. --menu "$MENULABEL" ${MENUSIZE} ${DEVICES}
  512. if [ $? -eq 0 ]; then
  513. dev=$(cat $ANSWER)
  514. if $(echo $dev|egrep -q "^wl.*" 2>/dev/null); then
  515. configure_wifi $dev
  516. else
  517. configure_net $dev
  518. fi
  519. fi
  520. }
  521. validate_filesystems() {
  522. local mnts dev size fstype mntpt mkfs rootfound swapfound fmt
  523. local usrfound efi_system_partition
  524. unset TARGETFS
  525. mnts=$(grep -E '^MOUNTPOINT.*' $CONF_FILE)
  526. set -- ${mnts}
  527. while [ $# -ne 0 ]; do
  528. dev=$2; fstype=$3; size=$4; mntpt="$5"; mkfs=$6
  529. shift 6
  530. if [ "$mntpt" = "/" ]; then
  531. rootfound=1
  532. elif [ "$mntpt" = "/usr" ]; then
  533. usrfound=1
  534. elif [ "$fstype" = "vfat" -a "$mntpt" = "/boot/efi" ]; then
  535. efi_system_partition=1
  536. fi
  537. if [ "$mkfs" -eq 1 ]; then
  538. fmt="NEW FILESYSTEM: "
  539. fi
  540. if [ -z "$TARGETFS" ]; then
  541. TARGETFS="${fmt}$dev ($size) mounted on $mntpt as ${fstype}\n"
  542. else
  543. TARGETFS="${TARGETFS}${fmt}${dev} ($size) mounted on $mntpt as ${fstype}\n"
  544. fi
  545. done
  546. if [ -z "$rootfound" ]; then
  547. DIALOG --msgbox "${BOLD}${RED}ERROR:${RESET} \
  548. the mount point for the root filesystem (/) has not yet been configured." ${MSGBOXSIZE}
  549. return 1
  550. elif [ -n "$usrfound" ]; then
  551. DIALOG --msgbox "${BOLD}${RED}ERROR:${RESET} \
  552. /usr mount point has been configured but is not supported, please remove it to continue." ${MSGBOXSIZE}
  553. return 1
  554. elif [ -n "$EFI_SYSTEM" -a -z "$efi_system_partition" ]; then
  555. DIALOG --msgbox "${BOLD}${RED}ERROR:${RESET} \
  556. The EFI System Partition has not yet been configured, please create it\n
  557. as FAT32, mountpoint /boot/efi and at least with 100MB of size." ${MSGBOXSIZE}
  558. fi
  559. FILESYSTEMS_DONE=1
  560. }
  561. create_filesystems() {
  562. local mnts dev mntpt fstype mkfs size rv uuid
  563. mnts=$(grep -E '^MOUNTPOINT.*' $CONF_FILE)
  564. set -- ${mnts}
  565. while [ $# -ne 0 ]; do
  566. dev=$2; fstype=$3; mntpt="$5"; mkfs=$6
  567. shift 6
  568. # swap partitions
  569. if [ "$fstype" = "swap" ]; then
  570. swapoff $dev >/dev/null 2>&1
  571. if [ "$mkfs" -eq 1 ]; then
  572. mkswap $dev >$LOG 2>&1
  573. if [ $? -ne 0 ]; then
  574. DIALOG --msgbox "${BOLD}${RED}ERROR:${RESET} \
  575. failed to create swap on ${dev}!\ncheck $LOG for errors." ${MSGBOXSIZE}
  576. DIE 1
  577. fi
  578. fi
  579. swapon $dev >$LOG 2>&1
  580. if [ $? -ne 0 ]; then
  581. DIALOG --msgbox "${BOLD}${RED}ERROR:${RESET} \
  582. failed to activate swap on $dev!\ncheck $LOG for errors." ${MSGBOXSIZE}
  583. DIE 1
  584. fi
  585. # Add entry for target fstab
  586. uuid=$(blkid -o value -s UUID "$dev")
  587. echo "UUID=$uuid none swap sw 0 0" >>$TARGET_FSTAB
  588. continue
  589. fi
  590. if [ "$mkfs" -eq 1 ]; then
  591. case "$fstype" in
  592. btrfs) MKFS="mkfs.btrfs"; modprobe btrfs >$LOG 2>&1;;
  593. ext2) MKFS="mke2fs"; modprobe ext2 >$LOG 2>&1;;
  594. ext3) MKFS="mke2fs -j"; modprobe ext3 >$LOG 2>&1;;
  595. ext4) MKFS="mke2fs -t ext4"; modprobe ext4 >$LOG 2>&1;;
  596. f2fs) MKFS="mkfs.f2fs"; modprobe f2fs >$LOG 2>&1;;
  597. vfat) MKFS="mkfs.vfat -F32"; modprobe vfat >$LOG 2>&1;;
  598. xfs) MKFS="mkfs.xfs -f"; modprobe xfs >$LOG 2>&1;;
  599. esac
  600. DIALOG --infobox "Creating filesystem $fstype on $dev for $mntpt ..." 8 60
  601. echo "Running $MKFS $dev..." >$LOG
  602. $MKFS $dev >$LOG 2>&1; rv=$?
  603. if [ $rv -ne 0 ]; then
  604. DIALOG --msgbox "${BOLD}${RED}ERROR:${RESET} \
  605. failed to create filesystem $fstype on $dev!\ncheck $LOG for errors." ${MSGBOXSIZE}
  606. DIE 1
  607. fi
  608. fi
  609. # Mount rootfs the first one.
  610. [ "$mntpt" != "/" ] && continue
  611. mkdir -p $TARGETDIR
  612. echo "Mounting $dev on $mntpt ($fstype)..." >$LOG
  613. mount -t $fstype $dev $TARGETDIR >$LOG 2>&1
  614. if [ $? -ne 0 ]; then
  615. DIALOG --msgbox "${BOLD}${RED}ERROR:${RESET} \
  616. failed to mount $dev on ${mntpt}! check $LOG for errors." ${MSGBOXSIZE}
  617. DIE 1
  618. fi
  619. # Add entry to target fstab
  620. uuid=$(blkid -o value -s UUID "$dev")
  621. echo "UUID=$uuid $mntpt $fstype defaults 0 1" >>$TARGET_FSTAB
  622. done
  623. # mount all filesystems in target rootfs
  624. mnts=$(grep -E '^MOUNTPOINT.*' $CONF_FILE)
  625. set -- ${mnts}
  626. while [ $# -ne 0 ]; do
  627. dev=$2; fstype=$3; mntpt="$5"
  628. shift 6
  629. [ "$mntpt" = "/" -o "$fstype" = "swap" ] && continue
  630. mkdir -p ${TARGETDIR}${mntpt}
  631. echo "Mounting $dev on $mntpt ($fstype)..." >$LOG
  632. mount -t $fstype $dev ${TARGETDIR}${mntpt} >$LOG 2>&1
  633. if [ $? -ne 0 ]; then
  634. DIALOG --msgbox "${BOLD}${RED}ERROR:${RESET} \
  635. failed to mount $dev on $mntpt! check $LOG for errors." ${MSGBOXSIZE}
  636. DIE
  637. fi
  638. # Add entry to target fstab
  639. uuid=$(blkid -o value -s UUID "$dev")
  640. echo "UUID=$uuid $mntpt $fstype defaults 0 1" >>$TARGET_FSTAB
  641. done
  642. }
  643. mount_filesystems() {
  644. for f in sys proc dev; do
  645. [ ! -d $TARGETDIR/$f ] && mkdir $TARGETDIR/$f
  646. echo "Mounting $TARGETDIR/$f..." >$LOG
  647. mount --bind /$f $TARGETDIR/$f >$LOG 2>&1
  648. done
  649. }
  650. umount_filesystems() {
  651. local f
  652. for f in sys/fs/fuse/connections sys proc dev; do
  653. echo "Unmounting $TARGETDIR/$f..." >$LOG
  654. umount $TARGETDIR/$f >$LOG 2>&1
  655. done
  656. local mnts="$(grep -E '^MOUNTPOINT.*$' $CONF_FILE)"
  657. set -- ${mnts}
  658. while [ $# -ne 0 ]; do
  659. local dev=$2; local fstype=$3; local mntpt=$5
  660. shift 6
  661. if [ "$fstype" = "swap" ]; then
  662. echo "Disabling swap space on $dev..." >$LOG
  663. swapoff $dev >$LOG 2>&1
  664. continue
  665. fi
  666. if [ "$mntpt" != "/" ]; then
  667. echo "Unmounting $TARGETDIR/$mntpt..." >$LOG
  668. umount $TARGETDIR/$mntpt >$LOG 2>&1
  669. fi
  670. done
  671. echo "Unmounting $TARGETDIR..." >$LOG
  672. umount $TARGETDIR >$LOG 2>&1
  673. }
  674. copy_rootfs() {
  675. DIALOG --title "Check /dev/tty7 for details" \
  676. --infobox "Copying live image to target rootfs, please wait ..." 4 60
  677. LANG=C cp -axvnu / $TARGETDIR >$LOG 2>&1
  678. if [ $? -ne 0 ]; then
  679. DIE 1
  680. fi
  681. }
  682. install_packages() {
  683. local _grub=
  684. if [ -n "$EFI_SYSTEM" ]; then
  685. _grub="grub-x86_64-efi"
  686. else
  687. _grub="grub"
  688. fi
  689. mkdir -p $TARGETDIR/var/db/xbps/keys
  690. cp /var/db/xbps/keys/*.plist $TARGETDIR/var/db/xbps/keys
  691. mkdir -p $TARGETDIR/boot/grub
  692. stdbuf -oL xbps-install -r $TARGETDIR -Sy base-system ${_grub} 2>&1 | \
  693. DIALOG --title "Installing base system packages..." \
  694. --programbox 24 80
  695. if [ $? -ne 0 ]; then
  696. DIE 1
  697. fi
  698. }
  699. menu_install() {
  700. # Don't continue if filesystems are not ready.
  701. validate_filesystems || return 1
  702. ROOTPASSWORD_DONE="$(get_option ROOTPASSWORD)"
  703. BOOTLOADER_DONE="$(get_option BOOTLOADER)"
  704. if [ -z "$FILESYSTEMS_DONE" ]; then
  705. DIALOG --msgbox "${BOLD}Required filesystems were not configured, \
  706. please do so before starting the installation.${RESET}" ${MSGBOXSIZE}
  707. return 1
  708. elif [ -z "$ROOTPASSWORD_DONE" ]; then
  709. DIALOG --msgbox "${BOLD}The root password has not been configured, \
  710. please do so before starting the installation.${RESET}" ${MSGBOXSIZE}
  711. return 1
  712. elif [ -z "$BOOTLOADER_DONE" ]; then
  713. DIALOG --msgbox "${BOLD}The disk to install the bootloader has not been \
  714. configured, please do so before starting the installation.${RESET}" ${MSGBOXSIZE}
  715. return 1
  716. fi
  717. DIALOG --yesno "${BOLD}The following operations will be executed:${RESET}\n\n
  718. ${BOLD}${TARGETFS}${RESET}\n
  719. ${BOLD}${RED}WARNING: data on partitions will be COMPLETELY DESTROYED for new \
  720. filesystems.${RESET}\n\n
  721. ${BOLD}Do you want to continue?${RESET}" 20 80 || return
  722. unset TARGETFS
  723. # Create and mount filesystems
  724. create_filesystems
  725. # If source not set use defaults.
  726. if [ "$(get_option SOURCE)" = "local" -o -z "$SOURCE_DONE" ]; then
  727. copy_rootfs
  728. # Disable autologin and remove motd from live image.
  729. rm -f $TARGETDIR/etc/motd $TARGETDIR/etc/systemd/system/[email protected]
  730. # Remove live user.
  731. . /etc/default/live.conf
  732. echo "Removing $USERNAME live user from targetdir ..." >$LOG
  733. chroot $TARGETDIR userdel -r $USERNAME >$LOG 2>&1
  734. DIALOG --title "Check /dev/tty7 for details" \
  735. --infobox "Rebuilding initramfs for target ..." 4 60
  736. echo "Rebuilding initramfs for target ..." >$LOG
  737. chroot $TARGETDIR dracut --force >>$LOG 2>&1
  738. DIALOG --title "Check /dev/tty7 for details" \
  739. --infobox "Removing temporary packages from target ..." 4 60
  740. echo "Removing temporaray packages from target ..." >$LOG
  741. xbps-remove -r $TARGETDIR -Ry dialog >>$LOG 2>&1
  742. rmdir $TARGETDIR/mnt/target
  743. # mount required fs
  744. mount_filesystems
  745. else
  746. # mount required fs
  747. mount_filesystems
  748. # network install, use packages.
  749. install_packages
  750. fi
  751. DIALOG --infobox "Applying installer settings..." 4 60
  752. # copy target fstab.
  753. install -Dm644 $TARGET_FSTAB $TARGETDIR/etc/fstab
  754. # set up keymap, locale, timezone, hostname and root passwd.
  755. set_keymap
  756. set_locale
  757. set_timezone
  758. set_hostname
  759. set_rootpassword
  760. # Copy /etc/skel files for root.
  761. cp $TARGETDIR/etc/skel/.[bix]* $TARGETDIR/root
  762. # network settings for target
  763. if [ -n "$NETWORK_DONE" ]; then
  764. local net="$(get_option NETWORK)"
  765. set -- ${net}
  766. local _dev="$1" _type="$2" _ip="$3" _gw="$4" _dns1="$5" _dns2="$6"
  767. if [ -z "$_type" ]; then
  768. # network type empty??!!!
  769. :
  770. elif [ "$_type" = "dhcp" ]; then
  771. if [ -f /etc/wpa_supplicant/wpa_supplicant-${_dev}.conf ]; then
  772. cp /etc/wpa_supplicant/wpa_supplicant-${_dev}.conf $TARGETDIR/etc/wpa_supplicant
  773. chroot $TARGETDIR systemctl enable dhcpcd@${_dev}.service >$LOG 2>&1
  774. else
  775. chroot $TARGETDIR systemctl enable dhcpcd.service >$LOG 2>&1
  776. fi
  777. elif [ -n "$dev" -a -n "$type" = "static" ]; then
  778. # static IP through dhcpcd.
  779. mv $TARGETDIR/etc/dhcpcd.conf $TARGETDIR/etc/dhdpcd.conf.orig
  780. echo "# Static IP configuration set by the void-installer for $_dev." \
  781. >$TARGETDIR/etc/dhcpcd.conf
  782. echo "interface $_dev" >>$TARGETDIR/etc/dhcpcd.conf
  783. echo "static ip_address=$_ip" >>$TARGETDIR/etc/dhcpcd.conf
  784. echo "static routers=$_gw" >>$TARGETDIR/etc/dhcpcd.conf
  785. echo "static domain_name_servers=$_dns1 $_dns2" >>$TARGETDIR/etc/dhcpcd.conf
  786. chroot $TARGETDIR systemctl enable dhcpcd.service >$LOG 2>&1
  787. fi
  788. fi
  789. # install bootloader.
  790. set_bootloader
  791. sync && sync && sync
  792. # unmount all filesystems.
  793. umount_filesystems
  794. # installed successfully.
  795. DIALOG --yesno "${BOLD}Void Linux has been installed successfully!${RESET}\n
  796. Do you want to reboot the system?" ${YESNOSIZE}
  797. if [ $? -eq 0 ]; then
  798. shutdown -r now
  799. else
  800. return
  801. fi
  802. }
  803. menu_source() {
  804. local src=
  805. DIALOG --title " Select installation source " \
  806. --menu "$MENULABEL" 8 70 0 \
  807. "Local" "Packages from ISO image" \
  808. "Network" "Packages from official remote reposity"
  809. case "$(cat $ANSWER)" in
  810. "Local") src="local";;
  811. "Network") src="net"; menu_network;;
  812. *) return 1;;
  813. esac
  814. SOURCE_DONE=1
  815. set_option SOURCE $src
  816. }
  817. menu() {
  818. if [ -z "$DEFITEM" ]; then
  819. DEFITEM="Keyboard"
  820. fi
  821. DIALOG --default-item $DEFITEM \
  822. --extra-button --extra-label "Settings" \
  823. --title " Void Linux installation menu " \
  824. --menu "$MENULABEL" 10 70 0 \
  825. "Keyboard" "Set system keyboard" \
  826. "Network" "Set up the network" \
  827. "Source" "Set source installation" \
  828. "Hostname" "Set system hostname" \
  829. "Locale" "Set system locale" \
  830. "Timezone" "Set system time zone" \
  831. "RootPassword" "Set system root password" \
  832. "BootLoader" "Set disk to install bootloader" \
  833. "Partition" "Partition disk(s)" \
  834. "Filesystems" "Configure filesystems and mount points" \
  835. "Install" "Start installation with saved settings" \
  836. "Exit" "Exit installation"
  837. if [ $? -eq 3 ]; then
  838. # Show settings
  839. DIALOG --title "Saved settings for installation" --textbox $CONF_FILE 14 60
  840. return
  841. fi
  842. case $(cat $ANSWER) in
  843. "Keyboard") menu_keymap && [ -n "$KEYBOARD_DONE" ] && DEFITEM="Network";;
  844. "Network") menu_network && [ -n "$NETWORK_DONE" ] && DEFITEM="Source";;
  845. "Source") menu_source && [ -n "$SOURCE_DONE" ] && DEFITEM="Hostname";;
  846. "Hostname") menu_hostname && [ -n "$HOSTNAME_DONE" ] && DEFITEM="Locale";;
  847. "Locale") menu_locale && [ -n "$LOCALE_DONE" ] && DEFITEM="Timezone";;
  848. "Timezone") menu_timezone && [ -n "$TIMEZONE_DONE" ] && DEFITEM="RootPassword";;
  849. "RootPassword") menu_rootpassword && [ -n "$ROOTPASSWORD_DONE" ] && DEFITEM="BootLoader";;
  850. "BootLoader") menu_bootloader && [ -n "$BOOTLOADER_DONE" ] && DEFITEM="Partition";;
  851. "Partition") menu_partitions && [ -n "$PARTITIONS_DONE" ] && DEFITEM="Filesystems";;
  852. "Filesystems") menu_filesystems && [ -n "$FILESYSTEMS_DONE" ] && DEFITEM="Install";;
  853. "Install") menu_install;;
  854. "Exit") DIE;;
  855. *) DIALOG --yesno "Abort Installation?" ${YESNOSIZE} && DIE
  856. esac
  857. }
  858. #
  859. # main()
  860. #
  861. DIALOG --title "${BOLD}${RED} Enter the void ... ${RESET}" --msgbox "\n
  862. Welcome to the Void Linux installation. A simple and minimal \
  863. Linux distribution made from scratch and built from the source package tree \
  864. available for XBPS, a new alternative binary package system.\n\n
  865. The installation should be pretty straightforward, if you are in trouble \
  866. please join us at ${BOLD}#xbps on irc.freenode.org${RESET}.\n\n
  867. ${BOLD}http://www.voidlinux.eu${RESET}\n\n" 16 80
  868. while true; do
  869. menu
  870. done
  871. exit 0
  872. # vim: set ts=4 sw=4 et: