installer.sh.in 29 KB

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