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