zfsinstall.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. mkdir -p /mnt/boot
  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. for D in ${DESTDISKS}; do
  85. echo copying EFI partcode at ${D}
  86. mount -t msdos /dev/${D}s1 /mnt/boot && cp ${DDESTDIR}/boot/loader.efi /mnt/boot/BOOTX64.EFI
  87. umount /mnt/boot
  88. done
  89. echo writing configuration files
  90. echo ==========================
  91. cat << EOF >> ${DESTDIR}/etc/fstab
  92. /dev/${DESTDISK_1}p3 none swap sw 0 0
  93. tmpfs /tmp tmpfs rw,mode=1777 0 0
  94. EOF
  95. cat << EOF >> ${DESTDIR}/boot/loader.conf
  96. zfs_load=YES
  97. tmpfs_load=YES
  98. EOF
  99. cat << EOF >> ${DESTDIR}/boot/loader.conf.local
  100. vfs.root.mountfrom="zfs:${ZPOOL}/ROOT/master"
  101. EOF
  102. COPYCAT="/etc"
  103. cd ${SCRIPTBASE}${COPYCAT} || exit 1
  104. echo installing files into ${COPYCAT}
  105. for I in *.conf*; do
  106. if [ -r "${I}" -a -f ${I} ]; then
  107. install -o root -g wheel -m 0644 ${I} ${DESTDIR}${COPYCAT}/
  108. fi
  109. done
  110. COPYCAT="/etc/rc.conf.d"
  111. cd ${SCRIPTBASE}${COPYCAT} || exit 1
  112. echo installing files into ${COPYCAT}
  113. install -o root -g wheel -d -m 0755 ${DESTDIR}${COPYCAT} || exit 1
  114. for I in *; do
  115. if [ -r "${I}" -a -f ${I} ]; then
  116. install -o root -g wheel -m 0644 ${I} ${DESTDIR}${COPYCAT}/
  117. fi
  118. done
  119. COPYCAT="/etc/rc.conf.d/network"
  120. cd ${SCRIPTBASE}${COPYCAT} || exit 1
  121. install -o root -g wheel -d -m 0755 ${DESTDIR}${COPYCAT} || exit 1
  122. echo installing files into ${COPYCAT}
  123. install -o root -g wheel -m 0644 * ${DESTDIR}${COPYCAT}/
  124. chroot ${DESTDIR} passwd
  125. chroot ${DESTDIR} adduser
  126. read -p "Do you wish to open a shell ?" yn
  127. case $yn in
  128. [Nn]* ) exit 0;;
  129. [Yy]* ) ;;
  130. * ) echo "Please answer yes or no.";;
  131. esac
  132. chroot ${DESTDIR} tcsh
  133. exit 0