You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

177 lines
4.2KB

  1. #!/bin/sh
  2. # Check:
  3. # https://wiki.freebsd.org/RootOnZFS/GPTZFSBoot/9.0-RELEASE
  4. # http://wp.strahlert.net/wordpress/zfs-2/expanding-zpool/
  5. # Tested on FreeBSD 10 and 11
  6. ###############################################################
  7. # WARNING: Go through line by line and adjust where necessary #
  8. ###############################################################
  9. # Create Partition Table
  10. echo "Create Partition Table"
  11. gpart create -s gpt ada0 # Main HDD
  12. gpart create -s gpt ada1 # Main HDD
  13. # Optional if you have SSDs for ZIL and L2ARC
  14. # gpart create -s gpt ada2 # ZIL and L2ARC SSD
  15. # gpart create -s gpt ada3 # ZIL and L2ARC SSD
  16. # Create Boot Partition
  17. echo "Create Boot Partition"
  18. gpart add -a 4k -s 512k -t freebsd-boot ada0
  19. gpart add -a 4k -s 512k -t freebsd-boot ada1
  20. # Create Swap Partitions
  21. echo "Create Swap Partitions"
  22. gpart add -a 4k -s 8G -t freebsd-swap -l swap0 ada0
  23. gpart add -a 4k -s 8G -t freebsd-swap -l swap1 ada1
  24. # Create Main Partitions
  25. echo "Create Main Partitions"
  26. gpart add -a 4k -t freebsd-zfs -l disk0 ada0
  27. gpart add -a 4k -t freebsd-zfs -l disk1 ada1
  28. # Write Bootcode
  29. echo "Write Bootcode"
  30. gpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 ada0
  31. gpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 ada1
  32. # Create ZIL Partions
  33. # echo "Create ZIL Partions"
  34. # gpart add -a 4k -b 2048 -s 10G -t freebsd-zfs -l zil0 ada2
  35. # gpart add -a 4k -b 2048 -s 10G -t freebsd-zfs -l zil1 ada3
  36. # Create L2ARC Partitions
  37. # echo "Create L2ARC Partitions"
  38. # gpart add -a 4k -t freebsd-zfs -l l2arc0 ada2
  39. # gpart add -a 4k -t freebsd-zfs -l l2arc1 ada3
  40. # Load ZFS extensions
  41. kldload opensolaris.ko
  42. kldload zfs.ko
  43. # Force ZFS to use 4k sectors
  44. sysctl vfs.zfs.min_auto_ashift=12
  45. # Create ZFS Pool
  46. echo "Create ZFS Pool"
  47. zpool create -f tank mirror /dev/gpt/disk0 /dev/gpt/disk1
  48. # Set proper mountpoint
  49. echo "Setting Mountpoint"
  50. zfs set mountpoint=/ tank
  51. # Export and import the Pool
  52. zpool export tank
  53. zpool import -o altroot=/mnt -o cachefile=/var/tmp/zpool.cache tank
  54. # Enable Compression
  55. echo "Enabling Compression"
  56. zfs set compression=lz4 tank
  57. # Add ZIL and L2ARC
  58. # echo "Add ZIL and L2ARC"
  59. # zpool add tank log mirror /dev/gpt/zil0 /dev/gpt/zil1
  60. # zpool add tank cache /dev/gpt/l2arc0 /dev/gpt/l2arc1
  61. # Set BOOTFS
  62. echo "Set BOOTFS"
  63. zpool set bootfs=tank tank
  64. # Copy FreeBSD files
  65. echo "Installing FreeBSD"
  66. cd /usr/freebsd-dist
  67. export DESTDIR=/mnt
  68. for f in base.txz lib32.txz kernel.txz doc.txz ports.txz src.txz;do
  69. (cat $f | tar --unlink -xvpJf - -C ${DESTDIR:-/});
  70. done
  71. cp /var/tmp/zpool.cache /mnt/boot/zfs/
  72. echo "Enter hostname FQDN"
  73. read HOSTNAME
  74. echo "Enter last public IP octet"
  75. read IP_ENDING
  76. echo "Enter username"
  77. read USERNAME
  78. cat > /mnt/etc/rc.conf << RCCONF
  79. hostname="$HOSTNAME"
  80. zfs_enable="YES"
  81. # Network
  82. defaultrouter="0.0.0.0"
  83. ifconfig_igb0="inet 0.0.0.$IP_ENDING/32"
  84. # Services
  85. sendmail_enable="NONE"
  86. sshd_enable="YES"
  87. RCCONF
  88. cat > /mnt/etc/fstab << FSTAB
  89. # Device Mountpoint FStype Options Dump Pass#
  90. /dev/gpt/swap0 none swap sw 0 0
  91. /dev/gpt/swap1 none swap sw 0 0
  92. FSTAB
  93. cat >> /mnt/boot/loader.conf << LOADER
  94. zfs_load="YES"
  95. vfs.root.mountfrom="zfs:tank"
  96. vfs.zfs.arc_max="8G"
  97. LOADER
  98. cat >> /mnt/etc/sysctl.conf << SYSCTL
  99. vfs.zfs.min_auto_ashift=12
  100. SYSCTL
  101. cat > /mnt/etc/resolv.conf << RESOLV
  102. nameserver 0.0.0.0
  103. nameserver 0.0.0.0
  104. RESOLV
  105. # Mount a devfs to have /dev/random /dev/zero etc in our chroot
  106. mount -t devfs none /mnt/dev
  107. # Bootstap pkg and install minimal packages for ansible
  108. chroot -u root -g wheel /mnt/ env ASSUME_ALWAYS_YES=YES pkg bootstrap
  109. chroot -u root -g wheel /mnt/ env ASSUME_ALWAYS_YES=YES pkg install sudo zsh
  110. # Add user
  111. chroot -u root -g wheel /mnt/ pw useradd -n $USERNAME -u 1001 -s /usr/local/bin/zsh -m -d /home/$USERNAME -G wheel -h 0
  112. # Fetch user pub key from github
  113. mkdir -p /mnt/home/$USERNAME/.ssh
  114. # This fetches the pub key from the sepcified github users and adds them
  115. # to the .authorized_keys of the new system user
  116. echo "List of Github users for pubkey retrieval (space separated):"
  117. read users
  118. for user in $users; do
  119. fetch https://github.com/$user.keys --no-verify-peer -o - >> /mnt/home/deploy/.ssh/authorized_keys
  120. done
  121. chown -R 1001:1001 /mnt/home/$USERNAME/.ssh