kernel: Replace struct device* by device_t
[dragonfly.git] / share / examples / rconfig / hammer_ccd_mirror.sh
blobbb2683a2034b93619dc56baf973b71d45b0ec1b7
1 #!/bin/sh
3 # HAMMER INSTALLATION on a CCD disk (mirror)
5 # You should modify disklist to match the disks you want to use.
6 # Please BE CAREFUL when doing so.
9 # set -x
11 disklist="da0 da1"
12 bootdisk=""
13 logfile="/tmp/hammer_ccd_mirror.log"
14 contents_dir="/tmp/contents"
17 # err exitval message
18 # Display an error and exit
20 err()
22 exitval=$1
23 shift
25 echo 1>&2 "$0: ERROR: $*"
26 exit $exitval
30 # cktmpdir
32 # Checks whether /tmp is writeable.
33 # Exit if not
35 cktmpdir()
37 mktemp "/tmp/XXX" >> ${logfile} 2>&1
39 if [ $? -ne 0 ]; then
40 err 1 "/tmp directory must be writeable"
45 # ckpreinstall
48 ckpreinstall()
50 # No pre-install checks
54 # ccdexit
56 ccdexit()
58 local status=$1
60 if [ $# -ne 0 ]; then
61 if [ ${status} -eq 0 ]; then
62 return
66 echo ""
67 echo "ccd operations were not sucessfully performed,"
68 echo "it is highly probably you have to reboot this computer now."
69 exit 1
73 # ckstatus status progname
75 # Checks exit status of programs and if it fails
76 # it exists with a message
78 ckstatus()
80 local st=$1
81 local prog=$2
83 if [ ${st} -ne 0 ]; then
84 err 1 "Failed to run $2. Check ${logfile}"
89 # ckloadmod modname
91 # Checks if a module is loaded, if not
92 # it tries to load it. In case of failure
93 # it exits
95 ckloadmod()
97 local mod=$1
99 if ! kldstat -q -m ${mod}; then
100 kldload $1 >> ${logfile} 2>&1
102 if [ $? -ne 0 ]; then
103 err 1 "Could not load ${mod}"
109 # prepdisk disk
111 # Clears the first sectors of the disk
112 # and then prepares the disk for disklabel.
113 # It also installs bootblocks in the first
114 # disk of the list.
116 prepdisk()
118 local disk=$1
120 # Hey don't shoot yourself in the foot
121 if [ ! "$disk" = "" ]; then
122 mount | fgrep ${disk} >> ${logfile} 2>&1
124 if [ $? -ne 1 ]; then
125 err 1 "${disk} is already being used, aborting"
129 if [ "${bootdisk}" = "" ]; then
130 bootdisk=${disk}
133 dd if=/dev/zero of=/dev/${disk} bs=32k count=16 >> ${logfile} 2>&1
134 ckstatus $? "dd"
136 fdisk -IB ${disk} >> ${logfile} 2>&1
137 ckstatus $? "fdisk"
139 disklabel -r -w ${disk}s1 auto >> ${logfile} 2>&1
140 ckstatus $? "disklabel"
142 if [ ! "${bootdisk}" = "" ]; then
143 disklabel -B ${disk}s1 >> ${logfile} 2>&1
144 ckstatus $? "disklabel"
149 # mklabel disk
151 # Create same labels for every disk
152 # except for the disk that will contain
153 # /boot partition
155 mklabel()
157 local disk=$1
158 local param=$2
160 disklabel ${disk}s1 > /tmp/label
161 ckstatus $? "disklabel"
163 # calculate memory
164 tmp=`sysctl hw.physmem | cut -w -f 2`
165 memtotal=`expr ${tmp} / 1024 / 1024`
167 if [ "${param}" = "root" ]; then
168 cat >> /tmp/label <<EOF
169 b: ${memtotal}m * swap
170 d: * * hammer
172 else
173 cat >> /tmp/label <<EOF
174 a: 768m 0 4.2BSD
175 d: * * ccd
178 disklabel -R ${disk}s1 /tmp/label >> ${logfile} 2>&1
179 ckstatus $? "disklabel"
183 # ccdops
185 ccdops()
187 local lst=""
188 local tmp=""
190 for disk in ${disklist}
192 tmp=${lst}
193 lst="${tmp} /dev/${disk}s1d"
194 done
196 ccdconfig ccd0 128 CCDF_MIRROR ${lst}
197 ckstatus $? "ccdconfig"
198 sleep 3 # Try to settle ops
202 # gen_rc_ccd
204 gen_rc_ccd()
206 local lst=""
207 local tmp=""
209 for disk in ${disklist}
211 tmp=${lst}
212 lst="${tmp} /dev/${disk}s1d"
213 done
215 # dump the config
216 ccdconfig -g > ${contents_dir}/etc/ccd.conf
217 ckstatus $? "ccdconfig"
219 cat >> ${contents_dir}/etc/rc.ccd << EOF
220 # call ccdconfig to restore the configuration
221 ccdconfig -C -f /etc/ccd.conf
222 sleep 5
224 # Make sure it can be executed by initrd rc
225 chmod +x ${contents_dir}/etc/rc.ccd
226 sync
229 echo "ALL DATA IN DISKS ${disklist} WILL BE LOST!"
230 echo "Press ^C to abort."
231 for n in 10 9 8 7 6 5 4 3 2 1
233 echo -n " ${n}"
234 sleep 1
235 done
236 echo ""
238 # /tmp has to be writeable
239 echo "* Checking /tmp is writeable"
240 cktmpdir
242 rm "${logfile}" >> ${logfile} 2>&1
243 echo "* Output to ${logfile}"
245 # kldload needed modules and start udevd
246 echo "* Loading modules"
247 ckloadmod ccd >> ${logfile} 2>&1
249 # check previous installations
250 ckpreinstall
252 # Unmount any prior mounts on /mnt, reverse order to unwind
253 # sub-directory mounts.
255 for mount in `df | fgrep /mnt | awk '{ print $6; }' | tail -r`
257 echo " Umounting ${mount}"
258 umount ${mount} >> ${logfile} 2>&1
259 done
261 # Prepare the disks in the list
262 for disk in ${disklist}
264 echo "* Preparing disk ${disk}"
265 prepdisk ${disk}
266 mklabel ${disk}
267 done
269 # ccdconfig operations in the disks
270 echo "* Performing CCD operations"
271 ccdops
273 # prepare the concatenated disk
274 prepdisk ccd0
275 sleep 1
276 mklabel ccd0 root
278 # Format the volumes
279 echo "* Formating ccd0"
280 newfs /dev/${bootdisk}s1a >> ${logfile} 2>&1
281 ccdexit $?
283 newfs_hammer -f -L ROOT /dev/ccd0s1d >> ${logfile} 2>&1
284 ccdexit $?
286 # Mount it
288 echo "* Mounting everything"
289 mount_hammer /dev/ccd0s1d /mnt >> ${logfile} 2>&1
290 ccdexit $?
292 mkdir /mnt/boot
294 mount /dev/${bootdisk}s1a /mnt/boot >> ${logfile} 2>&1
295 ccdexit $?
297 # Create PFS mount points for nullfs.
298 pfslist="usr usr.obj var var.crash var.tmp tmp home"
300 mkdir /mnt/pfs
302 for pfs in ${pfslist}
304 hammer pfs-master /mnt/pfs/$pfs >> ${logfile} 2>&1
305 ccdexit $?
306 done
308 # Mount /usr and /var so that you can add subdirs
309 mkdir /mnt/usr >> ${logfile} 2>&1
310 mkdir /mnt/var >> ${logfile} 2>&1
311 mount_null /mnt/pfs/usr /mnt/usr >> ${logfile} 2>&1
312 ccdexit $?
314 mount_null /mnt/pfs/var /mnt/var >> ${logfile} 2>&1
315 ccdexit $?
317 mkdir /mnt/usr/obj >> ${logfile} 2>&1
318 mkdir /mnt/var/tmp >> ${logfile} 2>&1
319 mkdir /mnt/var/crash >> ${logfile} 2>&1
321 mkdir /mnt/tmp >> ${logfile} 2>&1
322 mkdir /mnt/home >> ${logfile} 2>&1
324 mount_null /mnt/pfs/tmp /mnt/tmp >> ${logfile} 2>&1
325 ccdexit $?
327 mount_null /mnt/pfs/home /mnt/home >> ${logfile} 2>&1
328 ccdexit $?
330 mount_null /mnt/pfs/var.tmp /mnt/var/tmp >> ${logfile} 2>&1
331 ccdexit $?
333 mount_null /mnt/pfs/var.crash /mnt/var/crash >> ${logfile} 2>&1
334 ccdexit $?
336 mount_null /mnt/pfs/usr.obj /mnt/usr/obj >> ${logfile} 2>&1
337 ccdexit $?
339 chmod 1777 /mnt/tmp >> ${logfile} 2>&1
340 chmod 1777 /mnt/var/tmp >> ${logfile} 2>&1
342 # Install the system from the live CD
344 echo "* Starting file copy"
345 cpdup -vv -o / /mnt >> ${logfile} 2>&1
346 ccdexit $?
348 cpdup -vv -o /boot /mnt/boot >> ${logfile} 2>&1
349 ccdexit $?
351 cpdup -vv -o /usr /mnt/usr >> ${logfile} 2>&1
352 ccdexit $?
354 cpdup -vv -o /var /mnt/var >> ${logfile} 2>&1
355 ccdexit $?
357 cpdup -vv -i0 /etc.hdd /mnt/etc >> ${logfile} 2>&1
358 ccdexit $?
360 chflags -R nohistory /mnt/tmp >> ${logfile} 2>&1
361 chflags -R nohistory /mnt/var/tmp >> ${logfile} 2>&1
362 chflags -R nohistory /mnt/var/crash >> ${logfile} 2>&1
363 chflags -R nohistory /mnt/usr/obj >> ${logfile} 2>&1
365 echo "* Adapting fstab and loader.conf"
366 cat > /mnt/etc/fstab << EOF
367 # Device Mountpoint FStype Options Dump Pass#
368 /dev/ccd0s1d / hammer rw 1 1
369 /dev/${bootdisk}s1a /boot ufs rw 1 1
370 /dev/ccd0s1b none swap sw 0 0
371 /pfs/usr /usr null rw 0 0
372 /pfs/var /var null rw 0 0
373 /pfs/tmp /tmp null rw 0 0
374 /pfs/home /home null rw 0 0
375 /pfs/var.tmp /var/tmp null rw 0 0
376 /pfs/usr.obj /usr/obj null rw 0 0
377 /pfs/var.crash /var/crash null rw 0 0
378 proc /proc procfs rw 0 0
381 # Because root is not on the boot partition we have to tell the loader
382 # to tell the kernel where root is.
384 cat > /mnt/boot/loader.conf << EOF
385 ccd_load="YES"
386 initrd.img_load="YES"
387 initrd.img_type="md_image"
388 vfs.root.mountfrom="ufs:md0s0"
389 vfs.root.realroot="local:hammer:/dev/ccd0s1d"
392 # Setup interface, configuration, sshd
394 echo "* iface setup"
395 ifc=`route -n get default | fgrep interface | awk '{ print $2; }'`
396 ip=`ifconfig $ifc | fgrep inet | fgrep -v inet6 | awk '{ print $2; }'`
397 lip=`echo $ip | awk -F . '{ print $4; }'`
399 echo -n "ifconfig_$ifc=" >> /mnt/etc/rc.conf
400 echo '"DHCP"' >> /mnt/etc/rc.conf
401 cat >> /mnt/etc/rc.conf << EOF
402 sshd_enable="YES"
403 dntpd_enable="YES"
404 hostname="test$lip.MYDOMAIN.XXX"
405 dumpdev="/dev/ccd0s1b"
408 # Misc sysctls
410 cat >> /mnt/etc/sysctl.conf << EOF
411 #net.inet.ip.portrange.first=4000
414 # mkinitd image
415 echo "* Preparing initrd image"
416 cp /etc/defaults/mkinitrd.conf /etc/mkinitrd.conf
417 cpdup /usr/share/initrd ${contents_dir}
419 sed -i.bak '/^BIN_TOOLS/ s/\"$/ sleep\"/' /etc/mkinitrd.conf
420 sed -i.bak '/^SBIN_TOOLS/ s/\\/ccdconfig \\/' /etc/mkinitrd.conf
422 # Need to escape the directory so that sed doesn't take the var literally
423 escaped_var=$(printf '%s\n' "${contents_dir}" | sed 's/[\&/]/\\&/g')
424 sed -i.bak "s/^CONTENT_DIRS=.*/CONTENT_DIRS=\"${escaped_var}\"/" /etc/mkinitrd.conf
426 gen_rc_ccd
428 /sbin/mkinitrd -b /mnt/boot >> ${logfile} 2>&1
430 # copy installation log
431 echo "* Saving up installation log"
432 cp ${logfile} /mnt/tmp/
434 # Warn about ssh
435 echo ""
436 echo "Warning:"
437 echo "chroot now to /mnt and change root password and also edit"
438 echo "/mnt/etc/ssh/sshd_config to allow root login and authentication"
439 echo "using passwords. Alternatively, you can also just copy your"
440 echo "~/.ssh/authorized_keys file to allow login using your ssh keys."
442 echo "Installation finished successfully."
445 # take CD out and reboot