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