review install
[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 freebsd-boot -s 512k -l gptboot0 ${DESTDISK}
37 gpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 ${DESTDISK}
38 gpart add -a 1m -s 32G -t freebsd-swap -l swap0 ${DESTDISK}
39 gpart add -a 1m -t freebsd-zfs -l zfs0 ${DESTDISK}
40 gpart set -a active ${DESTDISK}
41
42 gpart show ${DESTDISK}  
43
44 sysctl vfs.zfs.min_auto_ashift=12
45
46 echo set up zfs pool: ${ZPOOL} alt mount: ${DESTDIR}
47 echo =================
48
49 zpool create  -m none -f -R ${DESTDIR} ${ZPOOL} ${DESTDISK}p3 ada1
50 if [ "${?}" -ne 0 ]; then
51         echo "unable to create zpool"
52         exit 1
53 fi
54
55
56 for I in ROOT VAR HOME DATA; do
57         zfs create -o mountpoint=none -o canmount=off  ${ZPOOL}/${I}
58 done
59
60 zfs create -o atime=off -o mountpoint=/ ${ZPOOL}/ROOT/master
61
62 zfs create -o mountpoint=/usr/obj ${ZPOOL}/ROOT/obj
63 zfs create -o compression=off -o mountpoint=/usr/src ${ZPOOL}/ROOT/src
64 zfs create -o mountpoint=/usr/doc -o compression=on ${ZPOOL}/ROOT/doc
65 # useless ? as far /tmp mountpoint will be mounted by tmpfs
66 zfs create -o mountpoint=/tmp ${ZPOOL}/ROOT/tmp
67
68 zfs create -o mountpoint=/usr/local ${ZPOOL}/LOCAL
69 zfs create -o mountpoint=/usr/local/etc ${ZPOOL}/LOCAL/config
70
71
72 zfs create -o mountpoint=/var ${ZPOOL}/VAR/master
73 zfs create -o mountpoint=/var/crash ${ZPOOL}/VAR/crash
74 zfs create -o exec=off -o setuid=off -o mountpoint=/var/db ${ZPOOL}/VAR/db
75 zfs create -o compression=lz4 -o exec=on -o setuid=off -o mountpoint=/var/db/pkg ${ZPOOL}/VAR/db/pkg
76 zfs create -o compression=on -o exec=off -o setuid=off -o mountpoint=/var/mail ${ZPOOL}/VAR/mail
77 zfs create -o compression=on -o exec=off -o setuid=off -o mountpoint=/var/log ${ZPOOL}/VAR/log
78 zfs create -o exec=off -o setuid=off -o mountpoint=/var/run ${ZPOOL}/VAR/run
79 zfs create -o exec=off -o setuid=off -o mountpoint=/var/tmp ${ZPOOL}/VAR/tmp
80
81 zfs create -o mountpoint=/usr/home ${ZPOOL}/HOME/master
82
83 zpool set bootfs=${ZPOOL}/ROOT/master ${ZPOOL}
84
85 chmod 1777 ${DESTDIR}/tmp ${DESTDIR}/var/tmp
86
87 zfs list -r ${ZPOOL}
88
89 cd ${DESTDIR}/tmp
90 if [ "${?}" -ne 0 ]; then
91         echo zfs mountpoints arent ready
92         exit 1
93 fi
94
95 echo Fetching and Extracting base files into ${DESTDIR}
96 echo =======================================
97
98
99 for I in base.txz kernel.txz lib32.txz; do
100         fetch http://ftp.freebsd.org/pub/FreeBSD/releases/amd64/12.0-BETA2/${I}
101         tar --unlink -pJxf ${I} -C ${DESTDIR}
102 done
103
104 echo writing configuration files
105 echo ==========================
106
107 cat << EOF >> ${DESTDIR}/etc/fstab
108 /dev/${DESTDISK}p3 none        swap    sw  0   0
109 tmpfs   /tmp    tmpfs   rw,mode=1777    0       0
110 EOF
111
112 cat << EOF >> ${DESTDIR}/boot/loader.conf
113 zfs_load=YES
114 tmpfs_load=YES
115 EOF
116
117 cat << EOF >> ${DESTDIR}/boot/loader.conf.local
118 vfs.root.mountfrom="zfs:${ZPOOL}/ROOT/master"
119 EOF
120
121 cat << EOF >> ${DESTDIR}/etc/rc.conf
122 sshd_enable="YES"
123 sendmail_enable="NONE"
124 # kld_list="vmm cuse4bsd cpuctl filemon"
125 ntpd_enable="YES"
126 # Set dumpdev to "AUTO" to enable crash dumps, "NO" to disable
127 dumpdev="AUTO"
128 zfs_enable="YES"
129 EOF
130
131 COPYCAT="/etc"
132 cd ${SCRIPTBASE}${COPYCAT} || exit 1
133 echo installing files into ${COPYCAT}
134 for I in *.conf*; do
135         if [ -r "${I}" -a -f ${I} ]; then
136                 install -o root -g wheel -m 0644  ${I} ${DESTDIR}${COPYCAT}/
137         fi
138 done
139 COPYCAT="/etc/rc.conf.d"
140 cd ${SCRIPTBASE}${COPYCAT} || exit 1
141 install -o root -g wheel -d -m 0755 ${DESTDIR}${COPYCAT} || exit 1
142 echo installing files into ${COPYCAT}
143 install  -o root -g wheel -m 0644 * ${DESTDIR}${COPYCAT}/
144 COPYCAT="/etc/rc.conf.d/network"
145 cd ${SCRIPTBASE}${COPYCAT} || exit 1
146 install -o root -g wheel -d -m 0755 ${DESTDIR}${COPYCAT} || exit 1
147 echo installing files into ${COPYCAT}
148 install  -o root -g wheel -m 0644 * ${DESTDIR}${COPYCAT}/
149
150 chroot ${DESTDIR} passwd
151 chroot ${DESTDIR} adduser
152
153 read -p "Do you wish to open a shell ?" yn
154 case $yn in
155         [Nn]* ) exit 0;;
156         [Yy]* ) ;;
157         * ) echo "Please answer yes or no.";;
158 esac
159 chroot ${DESTDIR} tcsh
160 exit 0
161