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