zfsinstall.sh 4.4 KB

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