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