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