zfsinstall.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #!/bin/sh
  2. DESTDISK="ada0"
  3. DESTDIR="/mnt/zfs"
  4. ZPOOL="zroot"
  5. SCRIPTBASE=${PWD}
  6. # check if the zpool already exists
  7. ZPOOL_EXISTS=` zpool list -H | cut -f 1 | grep "${ZPOOL}"`
  8. if [ "${?}" -ne 1 ]; then
  9. echo "One zpool called ${ZPOOL} already exists, you have to destroy it first:"
  10. echo " # zpool destroy ${ZPOOL}"
  11. exit 1
  12. fi
  13. # check if the disk is ok for writing
  14. echo checking sanity ${DESTDISK}
  15. echo =============
  16. gpart show ${DESTDISK} 1>/dev/null
  17. if [ "${?}" -eq 0 ]; then
  18. gpart destroy -F ${DESTDISK}
  19. if [ "${?}" -ne 0 ]; then
  20. echo unable to reset ${DESTDISK}
  21. exit 1
  22. fi
  23. fi
  24. echo partitioning ${DESTDISK}
  25. echo =============
  26. gpart create -s gpt ${DESTDISK}
  27. gpart add -t freebsd-boot -s 1024 -l gptboot0 ${DESTDISK}
  28. gpart add -a 1m -s 32G -t freebsd-swap -l gptswap0 ${DESTDISK}
  29. gpart add -a 1m -t freebsd-zfs -l syszfs ${DESTDISK}
  30. gpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 ${DESTDISK}
  31. gpart set -a active ${DESTDISK}
  32. gpart show ${DESTDISK}
  33. sysctl vfs.zfs.min_auto_ashift=12
  34. echo set up zfs pool: ${ZPOOL} alt mount: ${DESTDIR}
  35. echo =================
  36. zpool create -m none -f -R ${DESTDIR} ${ZPOOL} ${DESTDISK}p3
  37. if [ "${?}" -ne 0 ]; then
  38. echo "unable to create zpool"
  39. exit 1
  40. fi
  41. for I in ROOT VAR HOME; do
  42. zfs create -o mountpoint=none -o canmount=off ${ZPOOL}/${I}
  43. done
  44. zfs create -o atime=off -o mountpoint=/ ${ZPOOL}/ROOT/master
  45. zfs create -o mountpoint=/usr/obj ${ZPOOL}/ROOT/obj
  46. zfs create -o compression=off -o mountpoint=/usr/src ${ZPOOL}/ROOT/src
  47. # useless ? as far /tmp mountpoint will be mounted by tmpfs
  48. zfs create -o mountpoint=/tmp ${ZPOOL}/ROOT/tmp
  49. zfs create -o mountpoint=/usr/local ${ZPOOL}/LOCAL
  50. zfs create -o mountpoint=/usr/local/etc ${ZPOOL}/LOCAL/config
  51. zfs create -o copies=1 -o mountpoint=/usr/src -o compression=off ${ZPOOL}/src
  52. zfs create -o copies=1 -o mountpoint=/usr/doc -o compression=on ${ZPOOL}/doc
  53. zfs create -o copies=1 -o mountpoint=/usr/obj -o compression=on ${ZPOOL}/obj
  54. zfs create -o mountpoint=/var ${ZPOOL}/VAR/master
  55. zfs create -o mountpoint=/var/crash ${ZPOOL}/VAR/crash
  56. zfs create -o exec=off -o setuid=off -o mountpoint=/var/db ${ZPOOL}/VAR/db
  57. zfs create -o compression=lz4 -o exec=on -o setuid=off -o mountpoint=/var/db/pkg ${ZPOOL}/VAR/db/pkg
  58. zfs create -o compression=on -o exec=off -o setuid=off -o mountpoint=/var/mail ${ZPOOL}/VAR/mail
  59. zfs create -o compression=on -o exec=off -o setuid=off -o mountpoint=/var/log ${ZPOOL}/VAR/log
  60. zfs create -o exec=off -o setuid=off -o mountpoint=/var/run ${ZPOOL}/VAR/run
  61. zfs create -o exec=off -o setuid=off -o mountpoint=/var/tmp ${ZPOOL}/VAR/tmp
  62. zfs create -o mountpoint=/usr/home ${ZPOOL}/HOME/master
  63. zpool set bootfs=${ZPOOL}/ROOT/master ${ZPOOL}
  64. chmod 1777 ${DESTDIR}/tmp ${DESTDIR}/var/tmp
  65. zfs list -r ${ZPOOL}
  66. cd ${DESTDIR}/tmp
  67. if [ "${?}" -ne 0 ]; then
  68. echo zfs mountpoints arent ready
  69. exit 1
  70. fi
  71. echo Fetching and Extracting base files into ${DESTDIR}
  72. echo =======================================
  73. for I in base.txz kernel.txz lib32.txz; do
  74. fetch http://ftp.freebsd.org/pub/FreeBSD/snapshots/amd64/12.0-ALPHA8/${I}
  75. tar --unlink -pJxf ${I} -C ${DESTDIR}
  76. done
  77. echo writing configuration files
  78. echo ==========================
  79. cat << EOF >> ${DESTDIR}/etc/fstab
  80. /dev/${DESTDISK}p2 none swap sw 0 0
  81. tmpfs /tmp tmpfs rw,mode=1777 0 0
  82. EOF
  83. cat << EOF >> ${DESTDIR}/boot/loader.conf
  84. zfs_load=YES
  85. tmpfs_load=YES
  86. EOF
  87. cat << EOF >> ${DESTDIR}/boot/loader.conf.local
  88. vfs.root.mountfrom="zfs:${ZPOOL}/ROOT/master"
  89. EOF
  90. cat << EOF >> ${DESTDIR}/etc/rc.conf
  91. sshd_enable="YES"
  92. sendmail_enable="NONE"
  93. kld_list="vmm cuse4bsd cpuctl filemon"
  94. ntpd_enable="YES"
  95. # Set dumpdev to "AUTO" to enable crash dumps, "NO" to disable
  96. dumpdev="AUTO"
  97. zfs_enable="YES"
  98. EOF
  99. cd ${SCRIPTBASE}
  100. for I in resolv.conf rc.conf.local; do
  101. if [ -r "${I}" ]; then
  102. echo installing ${I}
  103. install -o root -g wheel -m 0644 ${I} ${DESTDIR}/etc/
  104. fi
  105. done
  106. for I in loader.conf.local; do
  107. if [ -r "${I}" ]; then
  108. echo installing ${I}
  109. install -o root -g wheel -m 0644 ${I} ${DESTDIR}/boot/
  110. fi
  111. done
  112. echo installing rc.conf.d
  113. install -o root -g wheel -d -m 0755 ${DESTDIR}/etc/rc.conf.d
  114. if [ -d "./rc.conf.d" ]; then
  115. echo installing files into rc.conf.d
  116. install -o root -g wheel -m 0644 ./rc.conf.d/* ${DESTDIR}/etc/rc.conf.d/
  117. fi
  118. chroot ${DESTDIR} passwd
  119. chroot ${DESTDIR} adduser
  120. read -p "Do you wish to open a shell ?" yn
  121. case $yn in
  122. [Nn]* ) exit 0;;
  123. [Yy]* ) ;;
  124. * ) echo "Please answer yes or no.";;
  125. esac
  126. chroot ${DESTDIR} tcsh
  127. exit 0