installer.sh.in 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. #!/bin/sh
  2. #-
  3. # Copyright (c) 2012 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/${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]d'); do
  119. if [ "$(cat /sys/block/$dev/device/type)" != "5" ]; then
  120. echo "/dev/$dev"
  121. size=$(cat /sys/block/$dev/size)
  122. sectorsize=$(cat /sys/block/$dev/queue/hw_sector_size)
  123. gbytes="$(($size * $sectorsize / 1024 / 1024 / 1024))"
  124. echo "size:${gbytes}GB;sector_size:$sectorsize"
  125. fi
  126. done
  127. }
  128. show_partitions() {
  129. local dev fstype fssize p part
  130. set -- $(show_disks)
  131. while [ $# -ne 0 ]; do
  132. disk=$(basename $1)
  133. shift 2
  134. # ATA/SCSI/SATA
  135. for p in /sys/block/$disk/$disk*; do
  136. if [ -d $p ]; then
  137. part=$(basename $p)
  138. fstype=$(blkid /dev/$part|awk '{print $3}'|sed -e 's/TYPE=\"\(.*\)\"/\1/g')
  139. [ "$fstype" = "iso9660" ] && continue
  140. fssize=$(lsblk -r /dev/$part|awk '{print $4}'|grep -v SIZE)
  141. echo "/dev/$part"
  142. echo "size:${fssize:-unknown};fstype:${fstype:-none}"
  143. fi
  144. done
  145. # LVM
  146. for p in $(ls /dev/mapper 2>/dev/null|grep -v control); do
  147. [ "$p" = "live-rw" ] && continue
  148. fstype=$(blkid /dev/$part|awk '{print $3}'|sed -e 's/TYPE=\"\(.*\)\"/\1/g')
  149. fssize=$(lsblk -r /dev/mapper/$p|awk '{print $4}'|grep -v SIZE)
  150. echo "/dev/mapper/$p"
  151. echo "size:${fssize:-unknown};fstype:${fstype:-none}"
  152. done
  153. # Software raid (md)
  154. for p in $(ls -d /dev/md* 2>/dev/null|grep '[0-9]'); do
  155. if cat /proc/mdstat|grep -qw $(echo $p|sed -e 's|/dev/||g'); then
  156. fstype=$(blkid /dev/$part|awk '{print $3}'|sed -e 's/TYPE=\"\(.*\)\"/\1/g')
  157. fssize=$(lsblk -r /dev/$p|awk '{print $4}'|grep -v SIZE)
  158. echo "$p"
  159. echo "size:${fssize:-unknown};fstype:${fstype:-none}"
  160. fi
  161. done
  162. done
  163. }
  164. menu_filesystems() {
  165. local dev fstype fssize mntpoint reformat
  166. while true; do
  167. DIALOG --title " Select the partition to edit " --menu "$MENULABEL" \
  168. ${MENUSIZE} $(show_partitions)
  169. [ $? -ne 0 ] && return
  170. dev=$(cat $ANSWER)
  171. DIALOG --title " Select the filesystem type for $dev " \
  172. --menu "$MENULABEL" ${MENUSIZE} \
  173. "vfat" "FAT32" \
  174. "btrfs" "Oracle's Btrfs" \
  175. "ext2" "Linux ext2 (no journaling)" \
  176. "ext3" "Linux ext3 (journal)" \
  177. "ext4" "Linux ext4 (journal)" \
  178. "swap" "Linux swap" \
  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 -r $dev|awk '{print $4}'|grep -v SIZE)
  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="de German fr French it Italian en English es Spanish ru Russia us American"
  245. while true; do
  246. DIALOG --title " Select your keymap " --menu "$MENULABEL" 14 70 14 ${KEYMAPS}
  247. if [ $? -eq 0 ]; then
  248. set_option KEYMAP "$(cat $ANSWER)"
  249. loadkeys "$(cat $ANSWER)"
  250. KEYBOARD_DONE=1
  251. break
  252. else
  253. return
  254. fi
  255. done
  256. }
  257. set_keymap() {
  258. local KEYMAP=$(get_option KEYMAP)
  259. sed -i -e "s|KEYMAP=.*|KEYMAP=$KEYMAP|g" $TARGETDIR/etc/vconsole.conf
  260. }
  261. menu_locale() {
  262. local _locales="$(localectl --no-pager list-locales)"
  263. local _LOCALES=
  264. for f in ${_locales}; do
  265. _LOCALES="${_LOCALES} ${f} -"
  266. done
  267. while true; do
  268. DIALOG --title " Select your locale " --menu "$MENULABEL" 14 70 14 ${_LOCALES}
  269. if [ $? -eq 0 ]; then
  270. set_option LANG "$(cat $ANSWER)"
  271. LOCALE_DONE=1
  272. break
  273. else
  274. return
  275. fi
  276. done
  277. }
  278. set_locale() {
  279. local LOCALE=$(get_option LOCALE)
  280. sed -i -e "s|LANG=.*|LANG=$KEYMAP|g" $TARGETDIR/etc/locale.conf
  281. }
  282. menu_timezone() {
  283. local _tzones="$(timedatectl --no-pager list-timezones)"
  284. local _TIMEZONES=
  285. for f in ${_tzones}; do
  286. _TIMEZONES="${_tzones} ${f} -"
  287. done
  288. while true; do
  289. DIALOG --title " Select your timezone " --menu "$MENULABEL" 14 70 14 ${_TIMEZONES}
  290. if [ $? -eq 0 ]; then
  291. set_option TIMEZONE "$(cat $ANSWER)"
  292. TIMEZONE_DONE=1
  293. break
  294. else
  295. return
  296. fi
  297. done
  298. }
  299. set_timezone() {
  300. local TIMEZONE="$(get_option TIMEZONE)"
  301. ln -sf /usr/share/zoneinfo/${TIMEZONE} $TARGETDIR/etc/localtime
  302. }
  303. menu_hostname() {
  304. while true; do
  305. DIALOG --inputbox "Set the machine hostname:" ${INPUTSIZE}
  306. if [ $? -eq 0 ]; then
  307. set_option HOSTNAME "$(cat $ANSWER)"
  308. HOSTNAME_DONE=1
  309. break
  310. else
  311. return
  312. fi
  313. done
  314. }
  315. set_hostname() {
  316. echo $(get_option HOSTNAME) > $TARGETDIR/etc/hostname
  317. }
  318. menu_rootpassword() {
  319. local _firstpass= _secondpass= _desc=
  320. while true; do
  321. if [ -n "${_firstpass}" ]; then
  322. _desc="Enter the root password again (password won't be displayed)"
  323. else
  324. _desc="Enter the root password (password won't be displayed)"
  325. fi
  326. DIALOG --passwordbox "${_desc}" ${MSGBOXSIZE}
  327. if [ $? -eq 0 ]; then
  328. if [ -z "${_firstpass}" ]; then
  329. _firstpass="$(cat $ANSWER)"
  330. else
  331. _secondpass="$(cat $ANSWER)"
  332. fi
  333. if [ -n "${_firstpass}" -a -n "${_secondpass}" ]; then
  334. if [ "${_firstpass}" != "${_secondpass}" ]; then
  335. DIALOG --infobox "Passwords do not match! please reenter it again" 6 80
  336. unset _firstpass _secondpass
  337. sleep 2 && continue
  338. fi
  339. set_option ROOTPASSWORD "${_firstpass}"
  340. ROOTPASSWORD_DONE=1
  341. break
  342. fi
  343. else
  344. return
  345. fi
  346. done
  347. }
  348. set_rootpassword() {
  349. echo "root:$(get_option ROOTPASSWORD)" | chpasswd -R $TARGETDIR -c SHA512
  350. }
  351. menu_bootloader() {
  352. while true; do
  353. DIALOG --title " Select the disk to install the bootloader" \
  354. --menu "$MENULABEL" ${MENUSIZE} $(show_disks)
  355. if [ $? -eq 0 ]; then
  356. set_option BOOTLOADER "$(cat $ANSWER)"
  357. BOOTLOADER_DONE=1
  358. break
  359. else
  360. return
  361. fi
  362. done
  363. }
  364. set_bootloader() {
  365. local dev=$(get_option BOOTLOADER) grub_args=
  366. # Check if it's an EFI system via efivars module.
  367. if [ -n "$EFI_SYSTEM" ]; then
  368. grub_args="--target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=void_grub --recheck"
  369. fi
  370. echo "Running grub-install $grub_args $dev..." >$LOG
  371. chroot $TARGETDIR grub-install $grub_args $dev >$LOG 2>&1
  372. if [ $? -ne 0 ]; then
  373. DIALOG --msgbox "${BOLD}${RED}ERROR:${RESET} \
  374. failed to install GRUB to $dev!\nCheck $LOG for errors." ${MSGBOXSIZE}
  375. DIE 1
  376. fi
  377. echo "Running grub-mkconfig on $TARGETDIR..." >$LOG
  378. chroot $TARGETDIR grub-mkconfig -o /boot/grub/grub.cfg >$LOG 2>&1
  379. if [ $? -ne 0 ]; then
  380. DIALOG --msgbox "${BOLD}${RED}ERROR${RESET}: \
  381. failed to run grub-mkconfig!\nCheck $LOG for errors." ${MSGBOXSIZE}
  382. DIE 1
  383. fi
  384. }
  385. test_network() {
  386. rm -f xtraeme.asc && \
  387. xbps-uhelper fetch http://xbps.nopcode.org/live/xtraeme.asc >$LOG 2>&1
  388. if [ $? -eq 0 ]; then
  389. DIALOG --msgbox "Network is working properly!" ${MSGBOXSIZE}
  390. NETWORK_DONE=1
  391. return 1
  392. fi
  393. DIALOG --msgbox "Network is unaccessible, please setup it properly." ${MSGBOXSIZE}
  394. }
  395. configure_wifi() {
  396. local dev="$1"
  397. DIALOG --form "Wireless configuration for $dev:" 0 0 0 \
  398. "SSID:" 1 1 "add your AP name here" 1 21 20 0 \
  399. "Encryption:" 2 1 "Only supported: WEP or WPA-PSK" 2 21 20 0 \
  400. "Passphrase:" 3 1 "Enter the AP passphrase here" 3 21 20 0 || return 1
  401. }
  402. configure_eth() {
  403. local dev="$1"
  404. DIALOG --yesno "Do you want to use DHCP for $dev?" ${YESNOSIZE}
  405. if [ $? -eq 0 ]; then
  406. configure_net_dhcp $dev
  407. elif [ $? -eq 1 ]; then
  408. configure_net_static $dev
  409. fi
  410. }
  411. iface_setup() {
  412. ifconfig $1|grep -q 'inet addr:'
  413. return $?
  414. }
  415. configure_net_dhcp() {
  416. local dev="$1"
  417. iface_setup $dev
  418. if [ $? -eq 1 ]; then
  419. udhcpc -q -t 10 -n -i $dev 2>&1 | tee $LOG | \
  420. DIALOG --progressbox "Initializing $dev via DHCP..." ${WIDGET_SIZE}
  421. if [ $? -ne 0 ]; then
  422. DIALOG --msgbox "${BOLD}${RED}ERROR:${RESET} failed to run udhcpc. See $LOG for details." ${MSGBOXSIZE}
  423. return 1
  424. fi
  425. iface_setup $dev
  426. if [ $? -eq 1 ]; then
  427. DIALOG --msgbox "${BOLD}${RED}ERROR:${RESET} DHCP request failed for $dev. Check $LOG for errors." ${MSGBOXSIZE}
  428. return 1
  429. fi
  430. fi
  431. test_network
  432. if [ $? -eq 1 ]; then
  433. set_option NETWORK "${dev} dhcp"
  434. fi
  435. }
  436. configure_net_static() {
  437. local ip gw dns1 dns2 dev=$1
  438. DIALOG --form "Static IP configuration for $dev:" 0 0 0 \
  439. "IP address:" 1 1 "192.168.0.2" 1 21 20 0 \
  440. "Gateway:" 2 1 "192.168.0.1" 2 21 20 0 \
  441. "DNS Primary" 3 1 "8.8.8.8" 3 21 20 0 \
  442. "DNS Secondary" 4 1 "8.8.4.4" 4 21 20 0 || return 1
  443. set -- $(cat $ANSWER)
  444. ip=$1; gw=$2; dns1=$3; dns2=$4
  445. echo "running: ifconfig $dev $IPADDR up" >$LOG
  446. ifconfig $dev $ip up >$LOG 2>&1
  447. if [ $? -ne 0 ]; then
  448. DIALOG --msgbox "${BOLD}${RED}ERROR:${RESET} Failed to setup $dev interface." ${MSGBOXSIZE}
  449. return 1
  450. fi
  451. route add default gw $gw >$LOG 2>&1
  452. if [ $? -ne 0 ]; then
  453. DIALOG --msgbox "${BOLD}${RED}ERROR:${RESET} failed to setup your gateway." ${MSGBOXSIZE}
  454. return 1
  455. fi
  456. echo "nameserver $dns1" >/etc/resolv.conf
  457. echo "nameserver $dns2" >>/etc/resolv.conf
  458. test_network
  459. if [ $? -eq 1 ]; then
  460. set_option NETWORK "${dev} static $ip $gw $dns1 $dns2"
  461. fi
  462. }
  463. menu_network() {
  464. local dev addr f DEVICES
  465. for f in $(ls /sys/class/net); do
  466. [ "$f" = "lo" ] && continue
  467. addr=$(cat /sys/class/net/$f/address)
  468. DEVICES="$DEVICES $f $addr"
  469. done
  470. DIALOG --title " Select the network interface to configure " \
  471. --menu "$MENULABEL" ${MENUSIZE} ${DEVICES}
  472. if [ $? -eq 0 ]; then
  473. dev=$(cat $ANSWER)
  474. if $(echo $dev|grep -q "wlan[0-9]" 2>/dev/null); then
  475. configure_wifi $dev
  476. else
  477. configure_eth $dev
  478. fi
  479. fi
  480. }
  481. validate_filesystems() {
  482. local mnts dev size fstype mntpt mkfs rootfound swapfound fmt
  483. local usrfound efi_system_partition
  484. unset TARGETFS
  485. mnts=$(grep -E '^MOUNTPOINT.*' $CONF_FILE)
  486. set -- ${mnts}
  487. while [ $# -ne 0 ]; do
  488. dev=$2; fstype=$3; size=$4; mntpt="$5"; mkfs=$6
  489. shift 6
  490. if [ "$mntpt" = "/" ]; then
  491. rootfound=1
  492. elif [ "$mntpt" = "/usr" ]; then
  493. usrfound=1
  494. elif [ "$fstype" = "vfat" -a "$mntpt" = "/boot/efi" ]; then
  495. efi_system_partition=1
  496. fi
  497. if [ "$mkfs" -eq 1 ]; then
  498. fmt="NEW FILESYSTEM: "
  499. fi
  500. if [ -z "$TARGETFS" ]; then
  501. TARGETFS="${fmt}$dev ($size) mounted on $mntpt as ${fstype}\n"
  502. else
  503. TARGETFS="${TARGETFS}${fmt}${dev} ($size) mounted on $mntpt as ${fstype}\n"
  504. fi
  505. done
  506. if [ -z "$rootfound" ]; then
  507. DIALOG --msgbox "${BOLD}${RED}ERROR:${RESET} \
  508. the mount point for the root filesystem (/) has not yet been configured." ${MSGBOXSIZE}
  509. return 1
  510. elif [ -n "$usrfound" ]; then
  511. DIALOG --msgbox "${BOLD}${RED}ERROR:${RESET} \
  512. /usr mount point has been configured but is not supported, please remove it to continue." ${MSGBOXSIZE}
  513. return 1
  514. elif [ -n "$EFI_SYSTEM" -a -z "$efi_system_partition" ]; then
  515. DIALOG --msgbox "${BOLD}${RED}ERROR:${RESET} \
  516. The EFI System Partition has not yet been configured, please create it\n
  517. as FAT32, mountpoint /boot/efi and at least with 100MB of size." ${MSGBOXSIZE}
  518. fi
  519. FILESYSTEMS_DONE=1
  520. }
  521. create_filesystems() {
  522. local mnts dev mntpt fstype mkfs size rv uuid
  523. mnts=$(grep -E '^MOUNTPOINT.*' $CONF_FILE)
  524. set -- ${mnts}
  525. while [ $# -ne 0 ]; do
  526. dev=$2; fstype=$3; mntpt="$5"; mkfs=$6
  527. shift 6
  528. # swap partitions
  529. if [ "$fstype" = "swap" ]; then
  530. swapoff $dev >/dev/null 2>&1
  531. if [ "$mkfs" -eq 1 ]; then
  532. mkswap $dev >$LOG 2>&1
  533. if [ $? -ne 0 ]; then
  534. DIALOG --msgbox "${BOLD}${RED}ERROR:${RESET} \
  535. failed to create swap on ${dev}!\ncheck $LOG for errors." ${MSGBOXSIZE}
  536. DIE 1
  537. fi
  538. fi
  539. swapon $dev >$LOG 2>&1
  540. if [ $? -ne 0 ]; then
  541. DIALOG --msgbox "${BOLD}${RED}ERROR:${RESET} \
  542. failed to activate swap on $dev!\ncheck $LOG for errors." ${MSGBOXSIZE}
  543. DIE 1
  544. fi
  545. # Add entry for target fstab
  546. uuid=$(blkid $dev|awk '{print $2}'|sed -e 's/UUID=\"\(.*\)\"/\1/g')
  547. echo "UUID=$uuid none swap sw 0 0" >>$TARGET_FSTAB
  548. continue
  549. fi
  550. if [ "$mkfs" -eq 1 ]; then
  551. case "$fstype" in
  552. vfat) MKFS="mkfs.vfat -F32"; modprobe vfat >$LOG 2>&1;;
  553. xfs) MKFS="mkfs.xfs -f"; modprobe xfs >$LOG 2>&1;;
  554. ext2) MKFS="mke2fs"; modprobe ext2 >$LOG 2>&1;;
  555. ext3) MKFS="mke2fs -j"; modprobe ext3 >$LOG 2>&1;;
  556. ext4) MKFS="mke2fs -t ext4"; modprobe ext4 >$LOG 2>&1;;
  557. btrfs) MKFS="mkfs.btrfs"; modprobe btrfs >$LOG 2>&1;;
  558. esac
  559. DIALOG --infobox "Creating filesystem $fstype on $dev for $mntpt ..." 8 60
  560. echo "Running $MKFS $dev..." >$LOG
  561. $MKFS $dev >$LOG 2>&1; rv=$?
  562. if [ $rv -ne 0 ]; then
  563. DIALOG --msgbox "${BOLD}${RED}ERROR:${RESET} \
  564. failed to create filesystem $fstype on $dev!\ncheck $LOG for errors." ${MSGBOXSIZE}
  565. DIE 1
  566. fi
  567. fi
  568. # Mount rootfs the first one.
  569. [ "$mntpt" != "/" ] && continue
  570. mkdir -p $TARGETDIR
  571. echo "Mounting $dev on $mntpt ($fstype)..." >$LOG
  572. mount -t $fstype $dev $TARGETDIR >$LOG 2>&1
  573. if [ $? -ne 0 ]; then
  574. DIALOG --msgbox "${BOLD}${RED}ERROR:${RESET} \
  575. failed to mount $dev on ${mntpt}! check $LOG for errors." ${MSGBOXSIZE}
  576. DIE 1
  577. fi
  578. # Add entry to target fstab
  579. uuid=$(blkid $dev|awk '{print $2}'|sed -e 's/UUID=\"\(.*\)\"/\1/g')
  580. echo "UUID=$uuid $mntpt $fstype defaults 0 1" >>$TARGET_FSTAB
  581. done
  582. # mount all filesystems in target rootfs
  583. mnts=$(grep -E '^MOUNTPOINT.*' $CONF_FILE)
  584. set -- ${mnts}
  585. while [ $# -ne 0 ]; do
  586. dev=$2; fstype=$3; mntpt="$5"
  587. shift 6
  588. [ "$mntpt" = "/" -o "$fstype" = "swap" ] && continue
  589. mkdir -p ${TARGETDIR}${mntpt}
  590. echo "Mounting $dev on $mntpt ($fstype)..." >$LOG
  591. mount -t $fstype $dev ${TARGETDIR}${mntpt} >$LOG 2>&1
  592. if [ $? -ne 0 ]; then
  593. DIALOG --msgbox "${BOLD}${RED}ERROR:${RESET} \
  594. failed to mount $dev on $mntpt! check $LOG for errors." ${MSGBOXSIZE}
  595. DIE
  596. fi
  597. # Add entry to target fstab
  598. uuid=$(blkid $dev|awk '{print $2}'|sed -e 's/UUID=\"\(.*\)\"/\1/g')
  599. echo "UUID=$uuid $mntpt $fstype defaults 0 1" >>$TARGET_FSTAB
  600. done
  601. }
  602. mount_filesystems() {
  603. for f in sys proc dev; do
  604. [ ! -d $TARGETDIR/$f ] && mkdir $TARGETDIR/$f
  605. echo "Mounting $TARGETDIR/$f..." >$LOG
  606. mount --bind /$f $TARGETDIR/$f >$LOG 2>&1
  607. done
  608. }
  609. umount_filesystems() {
  610. local f
  611. for f in sys proc dev; do
  612. echo "Unmounting $TARGETDIR/$f..." >$LOG
  613. umount $TARGETDIR/$f >$LOG 2>&1
  614. done
  615. local mnts=$(grep -E '^MOUNTPOINT.*$' $CONF_FILE)
  616. set -- ${mnts}
  617. while [ $# -ne 0 ]; do
  618. local dev=$2; local fstype=$3; local mntpt=$5
  619. shift 6
  620. if [ "$fstype" = "swap" ]; then
  621. echo "Disabling swap space on $dev..." >$LOG
  622. swapoff $dev >$LOG 2>&1
  623. continue
  624. fi
  625. if [ "$mntpt" != "/" ]; then
  626. echo "Unmounting $TARGETDIR/$mntpt..." >$LOG
  627. umount $TARGETDIR/$mntpt >$LOG 2>&1
  628. fi
  629. done
  630. echo "Unmounting $TARGETDIR..." >$LOG
  631. umount $TARGETDIR >$LOG 2>&1
  632. }
  633. install_packages() {
  634. local _grub=
  635. if [ -n "$EFI_SYSTEM" ]; then
  636. _grub="grub-x86_64-efi"
  637. else
  638. _grub="grub"
  639. fi
  640. mkdir -p $TARGETDIR/boot/grub
  641. stdbuf -oL xbps-install -C /tmp/xbps.conf \
  642. -r $TARGETDIR -Sy base-system ${_grub} 2>&1 | \
  643. DIALOG --title "Installing base system packages..." \
  644. --programbox 24 80
  645. if [ $? -ne 0 ]; then
  646. DIE 1
  647. fi
  648. }
  649. menu_install() {
  650. # Don't continue if filesystems are not ready.
  651. validate_filesystems || return 1
  652. ROOTPASSWORD_DONE="$(get_option ROOTPASSWORD)"
  653. BOOTLOADER_DONE="$(get_option BOOTLOADER)"
  654. if [ -z "$FILESYSTEMS_DONE" ]; then
  655. DIALOG --msgbox "${BOLD}Required filesystems were not configured, \
  656. please do so before starting the installation.${RESET}" ${MSGBOXSIZE}
  657. return 1
  658. elif [ -z "$ROOTPASSWORD_DONE" ]; then
  659. DIALOG --msgbox "${BOLD}The root password has not been configured, \
  660. please do so before starting the installation.${RESET}" ${MSGBOXSIZE}
  661. return 1
  662. elif [ -z "$BOOTLOADER_DONE" ]; then
  663. DIALOG --msgbox "${BOLD}The disk to install the bootloader has not been \
  664. configured, please do so before starting the installation.${RESET}" ${MSGBOXSIZE}
  665. return 1
  666. fi
  667. DIALOG --yesno "${BOLD}The following operations will be executed:${RESET}\n\n
  668. ${BOLD}${TARGETFS}${RESET}\n
  669. ${BOLD}${RED}WARNING: data on partitions will be COMPLETELY DESTROYED for new \
  670. filesystems.${RESET}\n\n
  671. ${BOLD}Do you want to continue?${RESET}" 20 80 || return
  672. unset TARGETFS
  673. # Create and mount filesystems
  674. create_filesystems
  675. # mount required fs
  676. mount_filesystems
  677. # If source not set use defaults.
  678. if [ -z "$SOURCE_DONE" ]; then
  679. enable_localsrc
  680. fi
  681. # Install required packages.
  682. install_packages
  683. DIALOG --infobox "Applying installer settings..." 4 60
  684. # copy target fstab.
  685. install -Dm644 $TARGET_FSTAB $TARGETDIR/etc/fstab
  686. # set up keymap, locale, timezone, hostname and root passwd.
  687. set_keymap
  688. set_locale
  689. set_timezone
  690. set_hostname
  691. set_rootpassword
  692. # Copy /etc/skel files for root.
  693. cp $TARGETDIR/etc/skel/.[bix]* $TARGETDIR/root
  694. # network settings for target
  695. if [ -n "$NETWORK_DONE" ]; then
  696. local net=$(grep -E '^NETWORK.*' $CONF_FILE)
  697. set -- ${net}
  698. local dev=$2; local type=$3; local ip=$4; local gw=$5
  699. local dns1=$6; local dns2=$7
  700. if [ "$type" = "dhcp" ]; then
  701. # if user had dhcp enabled, enable dhcpcd service for that device.
  702. chroot $TARGETDIR systemctl enable dhcpcd.service >$LOG 2>&1
  703. else
  704. # static IP through dhcpcd.
  705. mv $TARGETDIR/etc/dhcpcd.conf $TARGETDIR/etc/dhdpcd.conf.orig
  706. echo "# Static IP configuration set by the void-installer for $dev." \
  707. >$TARGETDIR/etc/dhcpcd.conf
  708. echo "interface $dev" >>$TARGETDIR/etc/dhcpcd.conf
  709. echo "static ip_address=$ip" >>$TARGETDIR/etc/dhcpcd.conf
  710. echo "static routers=$gw" >>$TARGETDIR/etc/dhcpcd.conf
  711. echo "static domain_name_servers=$dns1 $dns2" >>$TARGETDIR/etc/dhcpcd.conf
  712. chroot $TARGETDIR systemctl enable dhcpcd.service >$LOG 2>&1
  713. fi
  714. fi
  715. # install bootloader.
  716. set_bootloader
  717. sync && sync && sync
  718. # unmount all filesystems.
  719. umount_filesystems
  720. # installed successfully.
  721. DIALOG --yesno "${BOLD}Void Linux has been installed successfully!${RESET}\n
  722. Do you want to reboot the system?" ${YESNOSIZE}
  723. if [ $? -eq 0 ]; then
  724. shutdown -r now
  725. else
  726. return
  727. fi
  728. }
  729. enable_localsrc() {
  730. # local and fallback to official remote.
  731. echo "repositories = { /packages, http://xbps.nopcode.org/repos/current }" >/tmp/xbps.conf
  732. }
  733. enable_netsrc() {
  734. # only remote.
  735. echo "repositories = { http://xbps.nopcode.org/repos/current }" > /tmp/xbps.conf
  736. }
  737. menu_source() {
  738. local src=
  739. DIALOG --title " Select installation source " \
  740. --menu "$MENULABEL" 8 70 0 \
  741. "Local" "Packages from ISO image" \
  742. "Network" "Packages from official remote reposity"
  743. case "$(cat $ANSWER)" in
  744. "Local") src="local"; enable_localsrc;;
  745. "Network") src="net"; enable_netsrc; menu_network;;
  746. *) return 1;;
  747. esac
  748. SOURCE_DONE=1
  749. set_option SOURCE $src
  750. }
  751. menu() {
  752. if [ -z "$DEFITEM" ]; then
  753. DEFITEM="Keyboard"
  754. fi
  755. DIALOG --default-item $DEFITEM \
  756. --extra-button --extra-label "Settings" \
  757. --title " Void Linux installation menu " \
  758. --menu "$MENULABEL" 10 70 0 \
  759. "Keyboard" "Set system keyboard" \
  760. "Source" "Set source installation" \
  761. "Hostname" "Set system hostname" \
  762. "Locale" "Set system locale" \
  763. "Timezone" "Set system time zone" \
  764. "RootPassword" "Set system root password" \
  765. "BootLoader" "Set disk to install bootloader" \
  766. "Partition" "Partition disk(s)" \
  767. "Filesystems" "Configure filesystems and mount points" \
  768. "Install" "Start installation with saved settings" \
  769. "Exit" "Exit installation"
  770. if [ $? -eq 3 ]; then
  771. # Show settings
  772. DIALOG --title "Saved settings for installation" --textbox $CONF_FILE 14 60
  773. return
  774. fi
  775. case $(cat $ANSWER) in
  776. "Keyboard") menu_keymap && [ -n "$KEYBOARD_DONE" ] && DEFITEM="Source";;
  777. "Source") menu_source && [ -n "$SOURCE_DONE" ] && DEFITEM="Hostname";;
  778. "Hostname") menu_hostname && [ -n "$HOSTNAME_DONE" ] && DEFITEM="Locale";;
  779. "Locale") menu_locale && [ -n "$LOCALE_DONE" ] && DEFITEM="Timezone";;
  780. "Timezone") menu_timezone && [ -n "$TIMEZONE_DONE" ] && DEFITEM="RootPassword";;
  781. "RootPassword") menu_rootpassword && [ -n "$ROOTPASSWORD_DONE" ] && DEFITEM="BootLoader";;
  782. "BootLoader") menu_bootloader && [ -n "$BOOTLOADER_DONE" ] && DEFITEM="Partition";;
  783. "Partition") menu_partitions && [ -n "$PARTITIONS_DONE" ] && DEFITEM="Filesystems";;
  784. "Filesystems") menu_filesystems && [ -n "$FILESYSTEMS_DONE" ] && DEFITEM="Install";;
  785. "Install") menu_install;;
  786. "Exit") DIE;;
  787. *) DIALOG --yesno "Abort Installation?" ${YESNOSIZE} && DIE
  788. esac
  789. }
  790. #
  791. # main()
  792. #
  793. DIALOG --title "${BOLD}${RED} Enter the void ... ${RESET}" --msgbox "\n
  794. Welcome to the Void Linux installation. A simple and minimal \
  795. Linux distribution made from scratch and built from the source package tree \
  796. available for XBPS, a new alternative binary package system.\n\n
  797. The installation should be pretty straightforward, if you are in trouble \
  798. please join us at ${BOLD}#xbps on irc.freenode.org${RESET}.\n\n
  799. ${BOLD}http://www.voidlinux.eu${RESET}\n\n" 16 80
  800. while true; do
  801. menu
  802. done
  803. exit 0
  804. # vim: set ts=4 sw=4 et: