aa8a465fdd5a461ef62fd8626a381f3843d5551c
[zfsinstaller.git] / zfsinstall.sh
1 #!/bin/sh
2
3 DESTDISK="ada0"
4 DESTDIR="/mnt/zfs"
5 ZPOOL="zroot"
6
7 SCRIPTBASE=${PWD}
8
9 # check if the zpool already exists
10
11 ZPOOL_EXISTS=` zpool list -H | cut -f 1 | grep "${ZPOOL}"` 
12 if [ "${?}" -ne 1 ]; then
13         echo "One zpool called ${ZPOOL} already exists, you have to destroy it first:"
14         echo "  # zpool destroy ${ZPOOL}"
15         exit 1
16 fi
17
18 # check if the disk is ok for writing
19
20 echo checking sanity ${DESTDISK} 
21 echo =============
22 gpart show ${DESTDISK}  1>/dev/null
23 if [ "${?}" -eq 0 ]; then
24
25         gpart destroy -F ${DESTDISK}
26         if [ "${?}" -ne 0 ]; then
27                 echo unable to reset ${DESTDISK}
28                 exit 1
29         fi
30 fi
31
32 echo partitioning ${DESTDISK} 
33 echo =============
34
35 gpart create -s gpt ${DESTDISK}
36 gpart add -a 4k -t efi -s 200M -l efiboot0 ${DESTDISK}
37 gpart bootcode -p /boot/boot1.efifat -i 1 ${DESTDISK}
38 gpart add -a 4k -t freebsd-boot -s 512k -l gptboot0 ${DESTDISK}
39 gpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 2 ${DESTDISK}
40 gpart add -a 1m -s 32G -t freebsd-swap -l swap0 ${DESTDISK}
41 gpart add -a 1m -t freebsd-zfs -l zfs0 ${DESTDISK}
42 gpart set -a active ${DESTDISK}
43
44 gpart show ${DESTDISK}  
45
46 sysctl vfs.zfs.min_auto_ashift=12
47
48 echo set up zfs pool: ${ZPOOL} alt mount: ${DESTDIR}
49 echo =================
50
51 zpool create  -m none -f -R ${DESTDIR} ${ZPOOL} ${DESTDISK}p4 ada1
52 if [ "${?}" -ne 0 ]; then
53         echo "unable to create zpool"
54         exit 1
55 fi
56
57
58 for I in ROOT VAR HOME DATA; do
59         zfs create -o mountpoint=none -o canmount=off  ${ZPOOL}/${I}
60 done
61
62 zfs create -o atime=off -o mountpoint=/ ${ZPOOL}/ROOT/master
63
64 zfs create -o mountpoint=/usr/obj ${ZPOOL}/ROOT/obj
65 zfs create -o compression=off -o mountpoint=/usr/src ${ZPOOL}/ROOT/src
66 zfs create -o mountpoint=/usr/doc -o compression=on ${ZPOOL}/ROOT/doc
67 # useless ? as far /tmp mountpoint will be mounted by tmpfs
68 zfs create -o mountpoint=/tmp ${ZPOOL}/ROOT/tmp
69
70 zfs create -o mountpoint=/usr/local ${ZPOOL}/LOCAL
71 zfs create -o mountpoint=/usr/local/etc ${ZPOOL}/LOCAL/config
72
73
74 zfs create -o mountpoint=/var ${ZPOOL}/VAR/master
75 zfs create -o mountpoint=/var/crash ${ZPOOL}/VAR/crash
76 zfs create -o exec=off -o setuid=off -o mountpoint=/var/db ${ZPOOL}/VAR/db
77 zfs create -o compression=lz4 -o exec=on -o setuid=off -o mountpoint=/var/db/pkg ${ZPOOL}/VAR/db/pkg
78 zfs create -o compression=on -o exec=off -o setuid=off -o mountpoint=/var/mail ${ZPOOL}/VAR/mail
79 zfs create -o compression=on -o exec=off -o setuid=off -o mountpoint=/var/log ${ZPOOL}/VAR/log
80 zfs create -o exec=off -o setuid=off -o mountpoint=/var/run ${ZPOOL}/VAR/run
81 zfs create -o exec=off -o setuid=off -o mountpoint=/var/tmp ${ZPOOL}/VAR/tmp
82
83 zfs create -o mountpoint=/usr/home ${ZPOOL}/HOME/master
84
85 zpool set bootfs=${ZPOOL}/ROOT/master ${ZPOOL}
86
87 chmod 1777 ${DESTDIR}/tmp ${DESTDIR}/var/tmp
88
89 zfs list -r ${ZPOOL}
90
91 cd ${DESTDIR}/tmp
92 if [ "${?}" -ne 0 ]; then
93         echo zfs mountpoints arent ready
94         exit 1
95 fi
96
97 echo Fetching and Extracting base files into ${DESTDIR}
98 echo =======================================
99
100
101 for I in base.txz kernel.txz lib32.txz; do
102         fetch http://ftp.freebsd.org/pub/FreeBSD/releases/amd64/12.0-BETA2/${I}
103         tar --unlink -pJxf ${I} -C ${DESTDIR}
104 done
105
106 echo writing configuration files
107 echo ==========================
108
109 cat << EOF >> ${DESTDIR}/etc/fstab
110 /dev/${DESTDISK}p3 none        swap    sw  0   0
111 tmpfs   /tmp    tmpfs   rw,mode=1777    0       0
112 EOF
113
114 cat << EOF >> ${DESTDIR}/boot/loader.conf
115 zfs_load=YES
116 tmpfs_load=YES
117 EOF
118
119 cat << EOF >> ${DESTDIR}/boot/loader.conf.local
120 vfs.root.mountfrom="zfs:${ZPOOL}/ROOT/master"
121 EOF
122
123 cat << EOF >> ${DESTDIR}/etc/rc.conf
124 sshd_enable="YES"
125 sendmail_enable="NONE"
126 # kld_list="vmm cuse4bsd cpuctl filemon"
127 ntpd_enable="YES"
128 # Set dumpdev to "AUTO" to enable crash dumps, "NO" to disable
129 dumpdev="AUTO"
130 zfs_enable="YES"
131 EOF
132
133 cd ${SCRIPTBASE}
134 for I in resolv.conf rc.conf.local; do
135         if [ -r "${I}" ]; then
136                 echo installing ${I}
137                 install -o root -g wheel -m 0644  ${I} ${DESTDIR}/etc/
138         fi
139 done
140
141 for I in loader.conf.local; do
142         if [ -r "${I}" ]; then
143                 echo installing ${I}
144                 install -o root -g wheel -m 0644 ${I} ${DESTDIR}/boot/
145         fi
146 done
147
148 echo installing rc.conf.d
149 install -o root -g wheel -d -m 0755 ${DESTDIR}/etc/rc.conf.d 
150 if [ -d "./rc.conf.d" ]; then 
151         echo installing files into rc.conf.d
152         install -o root -g wheel -m 0644 ./rc.conf.d/* ${DESTDIR}/etc/rc.conf.d/
153 fi
154
155 chroot ${DESTDIR} passwd
156 chroot ${DESTDIR} adduser
157
158 read -p "Do you wish to open a shell ?" yn
159 case $yn in
160         [Nn]* ) exit 0;;
161         [Yy]* ) ;;
162         * ) echo "Please answer yes or no.";;
163 esac
164 chroot ${DESTDIR} tcsh
165 exit 0
166