zfsinstall.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 -a 4k -t freebsd-boot -s 512k -l gptboot0 ${DESTDISK}
  28. gpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 ${DESTDISK}
  29. gpart add -a 1m -s 32G -t freebsd-swap -l swap0 ${DESTDISK}
  30. gpart add -a 1m -t freebsd-zfs -l zfs0 ${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 ada1
  37. if [ "${?}" -ne 0 ]; then
  38. echo "unable to create zpool"
  39. exit 1
  40. fi
  41. for I in ROOT VAR HOME DATA; 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. zfs create -o mountpoint=/usr/doc -o compression=on ${ZPOOL}/ROOT/doc
  48. # useless ? as far /tmp mountpoint will be mounted by tmpfs
  49. zfs create -o mountpoint=/tmp ${ZPOOL}/ROOT/tmp
  50. zfs create -o mountpoint=/usr/local ${ZPOOL}/LOCAL
  51. zfs create -o mountpoint=/usr/local/etc ${ZPOOL}/LOCAL/config
  52. zfs create -o mountpoint=/var ${ZPOOL}/VAR/master
  53. zfs create -o mountpoint=/var/crash ${ZPOOL}/VAR/crash
  54. zfs create -o exec=off -o setuid=off -o mountpoint=/var/db ${ZPOOL}/VAR/db
  55. zfs create -o compression=lz4 -o exec=on -o setuid=off -o mountpoint=/var/db/pkg ${ZPOOL}/VAR/db/pkg
  56. zfs create -o compression=on -o exec=off -o setuid=off -o mountpoint=/var/mail ${ZPOOL}/VAR/mail
  57. zfs create -o compression=on -o exec=off -o setuid=off -o mountpoint=/var/log ${ZPOOL}/VAR/log
  58. zfs create -o exec=off -o setuid=off -o mountpoint=/var/run ${ZPOOL}/VAR/run
  59. zfs create -o exec=off -o setuid=off -o mountpoint=/var/tmp ${ZPOOL}/VAR/tmp
  60. zfs create -o mountpoint=/usr/home ${ZPOOL}/HOME/master
  61. zpool set bootfs=${ZPOOL}/ROOT/master ${ZPOOL}
  62. chmod 1777 ${DESTDIR}/tmp ${DESTDIR}/var/tmp
  63. zfs list -r ${ZPOOL}
  64. cd ${DESTDIR}/tmp
  65. if [ "${?}" -ne 0 ]; then
  66. echo zfs mountpoints arent ready
  67. exit 1
  68. fi
  69. echo Fetching and Extracting base files into ${DESTDIR}
  70. echo =======================================
  71. for I in base.txz kernel.txz lib32.txz; do
  72. fetch http://ftp.freebsd.org/pub/FreeBSD/releases/amd64/12.0-BETA2/${I}
  73. tar --unlink -pJxf ${I} -C ${DESTDIR}
  74. done
  75. echo writing configuration files
  76. echo ==========================
  77. cat << EOF >> ${DESTDIR}/etc/fstab
  78. /dev/${DESTDISK}p3 none swap sw 0 0
  79. tmpfs /tmp tmpfs rw,mode=1777 0 0
  80. EOF
  81. cat << EOF >> ${DESTDIR}/boot/loader.conf
  82. zfs_load=YES
  83. tmpfs_load=YES
  84. EOF
  85. cat << EOF >> ${DESTDIR}/boot/loader.conf.local
  86. vfs.root.mountfrom="zfs:${ZPOOL}/ROOT/master"
  87. EOF
  88. cat << EOF >> ${DESTDIR}/etc/rc.conf
  89. sshd_enable="YES"
  90. sendmail_enable="NONE"
  91. # kld_list="vmm cuse4bsd cpuctl filemon"
  92. ntpd_enable="YES"
  93. # Set dumpdev to "AUTO" to enable crash dumps, "NO" to disable
  94. dumpdev="AUTO"
  95. zfs_enable="YES"
  96. EOF
  97. COPYCAT="/etc"
  98. cd ${SCRIPTBASE}${COPYCAT} || exit 1
  99. echo installing files into ${COPYCAT}
  100. for I in *.conf*; do
  101. if [ -r "${I}" -a -f ${I} ]; then
  102. install -o root -g wheel -m 0644 ${I} ${DESTDIR}${COPYCAT}/
  103. fi
  104. done
  105. COPYCAT="/etc/rc.conf.d"
  106. cd ${SCRIPTBASE}${COPYCAT} || exit 1
  107. install -o root -g wheel -d -m 0755 ${DESTDIR}${COPYCAT} || exit 1
  108. echo installing files into ${COPYCAT}
  109. install -o root -g wheel -m 0644 * ${DESTDIR}${COPYCAT}/
  110. COPYCAT="/etc/rc.conf.d/network"
  111. cd ${SCRIPTBASE}${COPYCAT} || exit 1
  112. install -o root -g wheel -d -m 0755 ${DESTDIR}${COPYCAT} || exit 1
  113. echo installing files into ${COPYCAT}
  114. install -o root -g wheel -m 0644 * ${DESTDIR}${COPYCAT}/
  115. chroot ${DESTDIR} passwd
  116. chroot ${DESTDIR} adduser
  117. read -p "Do you wish to open a shell ?" yn
  118. case $yn in
  119. [Nn]* ) exit 0;;
  120. [Yy]* ) ;;
  121. * ) echo "Please answer yes or no.";;
  122. esac
  123. chroot ${DESTDIR} tcsh
  124. exit 0