zfsinstall.sh 4.6 KB

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