zfsinstall.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #!/bin/sh
  2. DESTDISKS="ada0 ada1"
  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. for D in ${DESTDISKS}; do
  15. if [ -z "${DESTDISK_1}" ]; then
  16. DESTDISK_1=${D}
  17. fi
  18. echo checking sanity ${D}
  19. echo =============
  20. gpart show ${D} 1>/dev/null
  21. if [ "${?}" -eq 0 ]; then
  22. gpart destroy -F ${D}
  23. if [ "${?}" -ne 0 ]; then
  24. echo unable to reset ${D}
  25. exit 1
  26. fi
  27. fi
  28. echo partitioning ${D}
  29. echo =============
  30. N=$( echo ${D} | tr -c -d '0-9' )
  31. gpart create -s gpt ${D}
  32. gpart add -a 4k -t efi -s 200M -l efiboot${N} ${D}
  33. gpart bootcode -p /boot/boot1.efifat -i 1 ${D}
  34. gpart add -a 4k -t freebsd-boot -s 512k -l gptboot${N} ${D}
  35. gpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 2 ${D}
  36. gpart add -a 1m -s 32G -t freebsd-swap -l swap${N} ${D}
  37. gpart add -a 1m -t freebsd-zfs -l zfs${N} ${D}
  38. gpart set -a active ${D}
  39. gpart show ${D}
  40. done
  41. sysctl vfs.zfs.min_auto_ashift=12
  42. echo set up zfs pool: ${ZPOOL} alt mount: ${DESTDIR}
  43. echo =================
  44. zpool create -m none -f -R ${DESTDIR} ${ZPOOL} /dev/gpt/zfs*
  45. if [ "${?}" -ne 0 ]; then
  46. echo "unable to create zpool"
  47. exit 1
  48. fi
  49. for I in ROOT VAR HOME DATA; do
  50. zfs create -o mountpoint=none -o canmount=off ${ZPOOL}/${I}
  51. done
  52. zfs create -o atime=off -o mountpoint=/ ${ZPOOL}/ROOT/master
  53. zfs create -o mountpoint=/usr/obj ${ZPOOL}/ROOT/obj
  54. zfs create -o compression=off -o mountpoint=/usr/src ${ZPOOL}/ROOT/src
  55. zfs create -o mountpoint=/usr/doc -o compression=on ${ZPOOL}/ROOT/doc
  56. # useless ? as far /tmp mountpoint will be mounted by tmpfs
  57. zfs create -o mountpoint=/tmp ${ZPOOL}/ROOT/tmp
  58. zfs create -o mountpoint=/usr/local ${ZPOOL}/LOCAL
  59. zfs create -o mountpoint=/usr/local/etc ${ZPOOL}/LOCAL/config
  60. zfs create -o mountpoint=/var ${ZPOOL}/VAR/master
  61. zfs create -o mountpoint=/var/crash ${ZPOOL}/VAR/crash
  62. zfs create -o exec=off -o setuid=off -o mountpoint=/var/db ${ZPOOL}/VAR/db
  63. zfs create -o compression=lz4 -o exec=on -o setuid=off -o mountpoint=/var/db/pkg ${ZPOOL}/VAR/db/pkg
  64. zfs create -o compression=on -o exec=off -o setuid=off -o mountpoint=/var/mail ${ZPOOL}/VAR/mail
  65. zfs create -o compression=on -o exec=off -o setuid=off -o mountpoint=/var/log ${ZPOOL}/VAR/log
  66. zfs create -o exec=off -o setuid=off -o mountpoint=/var/run ${ZPOOL}/VAR/run
  67. zfs create -o exec=off -o setuid=off -o mountpoint=/var/tmp ${ZPOOL}/VAR/tmp
  68. zfs create -o mountpoint=/usr/home ${ZPOOL}/HOME/master
  69. zpool set bootfs=${ZPOOL}/ROOT/master ${ZPOOL}
  70. chmod 1777 ${DESTDIR}/tmp ${DESTDIR}/var/tmp
  71. zfs list -r ${ZPOOL}
  72. cd ${DESTDIR}/tmp
  73. if [ "${?}" -ne 0 ]; then
  74. echo zfs mountpoints are not ready
  75. exit 1
  76. fi
  77. echo Fetching and Extracting base files into ${DESTDIR}
  78. echo =======================================
  79. for I in base.txz kernel.txz lib32.txz; do
  80. fetch http://ftp.freebsd.org/pub/FreeBSD/releases/amd64/12.0-BETA3/${I}
  81. tar --unlink -pJxf ${I} -C ${DESTDIR}
  82. done
  83. echo writing configuration files
  84. echo ==========================
  85. cat << EOF >> ${DESTDIR}/etc/fstab
  86. /dev/${DESTDISK_1}p3 none swap sw 0 0
  87. tmpfs /tmp tmpfs rw,mode=1777 0 0
  88. EOF
  89. cat << EOF >> ${DESTDIR}/boot/loader.conf
  90. zfs_load=YES
  91. tmpfs_load=YES
  92. EOF
  93. cat << EOF >> ${DESTDIR}/boot/loader.conf.local
  94. vfs.root.mountfrom="zfs:${ZPOOL}/ROOT/master"
  95. EOF
  96. COPYCAT="/etc"
  97. cd ${SCRIPTBASE}${COPYCAT} || exit 1
  98. echo installing files into ${COPYCAT}
  99. for I in *.conf*; do
  100. if [ -r "${I}" -a -f ${I} ]; then
  101. install -o root -g wheel -m 0644 ${I} ${DESTDIR}${COPYCAT}/
  102. fi
  103. done
  104. COPYCAT="/etc/rc.conf.d"
  105. cd ${SCRIPTBASE}${COPYCAT} || exit 1
  106. echo installing files into ${COPYCAT}
  107. install -o root -g wheel -d -m 0755 ${DESTDIR}${COPYCAT} || exit 1
  108. for I in *; do
  109. if [ -r "${I}" -a -f ${I} ]; then
  110. install -o root -g wheel -m 0644 * ${DESTDIR}${COPYCAT}/
  111. fi
  112. done
  113. COPYCAT="/etc/rc.conf.d/network"
  114. cd ${SCRIPTBASE}${COPYCAT} || exit 1
  115. install -o root -g wheel -d -m 0755 ${DESTDIR}${COPYCAT} || exit 1
  116. echo installing files into ${COPYCAT}
  117. install -o root -g wheel -m 0644 * ${DESTDIR}${COPYCAT}/
  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