3 # This script takes in account a disk list to make a lvm(8) stripe
4 # which will contain a hammer filesystem and the swap partition.
6 # Be aware that this is just to provide _basic_ installation
7 # functionalities and it may need actions from the sysadmin to
8 # properly install and get a consistent installed system.
12 export LVM_SUPPRESS_FD_WARNINGS
=1
16 logfile
="/tmp/hammer_lvm_stripe.log"
20 # Display an error and exit
27 echo 1>&2 "$0: ERROR: $*"
34 # Checks whether /tmp is writeable.
39 mktemp
"/tmp/XXX" >> ${logfile} 2>&1
42 err
1 "/tmp directory must be writeable"
49 # Checks whether lv nodes exist which would may
50 # indicate an installation already succeeded or
51 # a previous failed one.
56 # Try to activate the volume first
57 vgchange
-a y main
>> ${logfile} 2>&1
59 if [ -c /dev
/mapper
/main-lv_root
-o -c /dev
/mapper
/main-lv_swap
]; then
60 echo " ERROR: Either a previous installation failed or your installation"
61 echo " is already done. If the former, please follow instructions below."
70 # ckstatus status progname
72 # Checks exit status of programs and if it fails
73 # it exists with a message
80 if [ ${st} -ne 0 ]; then
81 err
1 "Failed to run $2. Check ${logfile}"
88 # Checks if a module is loaded, if not
89 # it tries to load it. In case of failure
96 if ! kldstat
-q -m ${mod}; then
97 kldload
$1 >> ${logfile} 2>&1
100 err
1 "Could not load ${mod}"
108 # Clears the first sectors of the disk
109 # and then prepares the disk for disklabel.
110 # It also installs bootblocks in the first
117 # Hey don't shoot yourself in the foot
118 if [ ! "$disk" = "" ]; then
119 mount | fgrep
${disk} >> ${logfile} 2>&1
121 if [ $?
-ne 1 ]; then
122 err
1 "${disk} is already being used, aborting"
126 if [ "${bootdisk}" = "" ]; then
130 dd if=/dev
/zero of
=/dev
/${disk} bs
=32k count
=16 >> ${logfile} 2>&1
133 fdisk
-IB ${disk} >> ${logfile} 2>&1
136 disklabel
-r -w ${disk}s1
>> ${logfile} 2>&1
137 ckstatus $?
"disklabel"
139 if [ ! "${bootdisk}" = "" ]; then
140 disklabel
-B ${disk}s1
>> ${logfile} 2>&1
141 ckstatus $?
"disklabel"
148 # Create same labels for every disk
149 # except for the disk that will contain
156 disklabel
${disk}s1
> /tmp
/label
157 ckstatus $?
"disklabel"
159 if [ "${disk}" = "${bootdisk}" ]; then
160 cat >> /tmp
/label
<<EOF
165 cat >> /tmp
/label
<<EOF
171 disklabel
-R ${disk}s1
/tmp
/label
>> ${logfile} 2>&1
172 ckstatus $?
"disklabel"
184 if [ $# -ne 0 ]; then
185 if [ ${status} -eq 0 ]; then
190 echo " There was an error during or after LVM operations for this"
191 echo " installation and those operations done cannot be reverted"
192 echo " by the script itself. Please revert them manually:"
194 echo " Basically you need to perform these steps:"
195 echo " 1. Remove all logical volumes in VG main (lv_swap, lv_root)"
196 echo " 2. Remove main volume group"
197 echo " 3. Remove all the physical volumes. PVs are all in [disk]1d"
198 echo " For example, if you have 2 disks, you need to remove:"
199 echo " /sbin/pvremove /dev/da0s1d"
200 echo " /sbin/pvremove /dev/da1s1d"
213 vgchange
-a n main
>> ${logfile} 2>&1
215 lvremove
--force lv_swap main
>> ${logfile} 2>&1
216 if [ $?
-ne 0 ]; then
220 lvremove
--force lv_root main
>> ${logfile} 2>&1
221 if [ $?
-ne 0 ]; then
225 vgremove
--force main
>> ${logfile} 2>&1
226 if [ $?
-ne 0 ]; then
230 for disk
in ${disklist}
232 pvremove
/dev
/${disk}s1d
>> ${logfile} 2>&1
239 # Create physical volumes, volume group 'main' and
240 # the ROOT hammer filesystem where everything will
241 # be installed. swap is also created as a log. volume.
248 for disk
in ${disklist}
250 pvcreate
/dev
/${disk}s1d
>> ${logfile} 2>&1
251 if [ $?
-ne 0 ]; then
255 lst
="${tmp} /dev/${disk}s1d"
262 # Do a full scan in the hope the lvm
264 pvscan
>> ${logfile} 2>&1
266 vgcreate main
${lst} >> ${logfile} 2>&1
267 if [ $?
-ne 0 ]; then
271 # We need to sync and wait for some secs to settle
272 # Otherwise /dev/main won't appear
273 echo " Settling LVM operations (5 secs)"
276 vgscan
>> ${logfile} 2>&1
278 lvcreate
-Z n
-n lv_swap
-L ${memtotal} main
>> ${logfile} 2>&1
279 if [ $?
-ne 0 ]; then
283 # We sync in every lvm operation
285 lvscan
>> ${logfile} 2>&1
288 lvcreate
-Z n
-n lv_root
-l 100%FREE main
>> ${logfile} 2>&1
289 if [ $?
-ne 0 ]; then
295 echo "ALL DATA IN DISKS ${disklist} WILL BE LOST!"
296 echo "Press ^C to abort."
297 for n
in 10 9 8 7 6 5 4 3 2 1
304 # /tmp has to be writeable
305 echo "* Checking /tmp is writeable"
308 rm "${logfile}" >> ${logfile} 2>&1
309 echo "* Output to ${logfile}"
312 tmp
=`sysctl hw.physmem | cut -w -f 2`
313 memtotal
=`expr ${tmp} / 1024 / 1024`
315 # kldload needed modules and start udevd
316 echo "* Loading modules"
317 ckloadmod dm_target_striped
>> ${logfile} 2>&1
318 ckloadmod dm_target_linear
>> ${logfile} 2>&1
319 /sbin
/udevd
>> ${logfile} 2>&1
321 # check previous installations
324 # Unmount any prior mounts on /mnt, reverse order to unwind
325 # sub-directory mounts.
327 for mount
in `df | fgrep /mnt | awk '{ print $6; }' | tail -r`
329 echo " Umounting ${mount}"
330 umount
${mount} >> ${logfile} 2>&1
333 # Prepare the disks in the list
334 for disk
in ${disklist}
336 echo "* Preparing disk ${disk}"
339 pvscan
>> ${logfile} 2>&1
342 # lvm(8) operations in the disks
343 echo "* Performing LVM operations"
347 echo "* Formatting ${bootdisk} and LVs lv_root"
348 newfs
/dev
/${bootdisk}s1a
>> ${logfile} 2>&1
351 newfs_hammer
-f -L ROOT
/dev
/main
/lv_root
>> ${logfile} 2>&1
356 echo "* Mounting everything"
357 mount_hammer
/dev
/main
/lv_root
/mnt
>> ${logfile} 2>&1
362 mount
/dev
/${bootdisk}s1a
/mnt
/boot
>> ${logfile} 2>&1
365 # Create PFS mount points for nullfs.
366 pfslist
="usr usr.obj var var.crash var.tmp tmp home"
370 for pfs
in ${pfslist}
372 hammer pfs-master
/mnt
/pfs
/$pfs >> ${logfile} 2>&1
376 # Mount /usr and /var so that you can add subdirs
377 mkdir
/mnt
/usr
>> ${logfile} 2>&1
378 mkdir
/mnt
/var
>> ${logfile} 2>&1
379 mount_null
/mnt
/pfs
/usr
/mnt
/usr
>> ${logfile} 2>&1
382 mount_null
/mnt
/pfs
/var
/mnt
/var
>> ${logfile} 2>&1
385 mkdir
/mnt
/usr
/obj
>> ${logfile} 2>&1
386 mkdir
/mnt
/var
/tmp
>> ${logfile} 2>&1
387 mkdir
/mnt
/var
/crash
>> ${logfile} 2>&1
389 mkdir
/mnt
/tmp
>> ${logfile} 2>&1
390 mkdir
/mnt
/home
>> ${logfile} 2>&1
392 mount_null
/mnt
/pfs
/tmp
/mnt
/tmp
>> ${logfile} 2>&1
395 mount_null
/mnt
/pfs
/home
/mnt
/home
>> ${logfile} 2>&1
398 mount_null
/mnt
/pfs
/var.tmp
/mnt
/var
/tmp
>> ${logfile} 2>&1
401 mount_null
/mnt
/pfs
/var.crash
/mnt
/var
/crash
>> ${logfile} 2>&1
404 mount_null
/mnt
/pfs
/usr.obj
/mnt
/usr
/obj
>> ${logfile} 2>&1
407 chmod 1777 /mnt
/tmp
>> ${logfile} 2>&1
408 chmod 1777 /mnt
/var
/tmp
>> ${logfile} 2>&1
410 # Install the system from the live CD
412 echo "* Starting file copy"
413 cpdup
-vv -o / /mnt
>> ${logfile} 2>&1
416 cpdup
-vv -o /boot
/mnt
/boot
>> ${logfile} 2>&1
419 cpdup
-vv -o /usr
/mnt
/usr
>> ${logfile} 2>&1
422 cpdup
-vv -o /var
/mnt
/var
>> ${logfile} 2>&1
425 cpdup
-vv -i0 /etc.hdd
/mnt
/etc
>> ${logfile} 2>&1
428 chflags
-R nohistory
/mnt
/tmp
>> ${logfile} 2>&1
429 chflags
-R nohistory
/mnt
/var
/tmp
>> ${logfile} 2>&1
430 chflags
-R nohistory
/mnt
/var
/crash
>> ${logfile} 2>&1
431 chflags
-R nohistory
/mnt
/usr
/obj
>> ${logfile} 2>&1
433 echo "* Adapting fstab and loader.conf"
434 cat > /mnt
/etc
/fstab
<< EOF
435 # Device Mountpoint FStype Options Dump Pass#
436 /dev/main/lv_root / hammer rw 1 1
437 /dev/${bootdisk}s1a /boot ufs rw 1 1
438 /dev/main/lv_swap none swap sw 0 0
439 /pfs/usr /usr null rw 0 0
440 /pfs/var /var null rw 0 0
441 /pfs/tmp /tmp null rw 0 0
442 /pfs/home /home null rw 0 0
443 /pfs/var.tmp /var/tmp null rw 0 0
444 /pfs/usr.obj /usr/obj null rw 0 0
445 /pfs/var.crash /var/crash null rw 0 0
446 proc /proc procfs rw 0 0
449 # Because root is not on the boot partition we have to tell the loader
450 # to tell the kernel where root is.
452 cat > /mnt
/boot
/loader.conf
<< EOF
453 dm_target_striped_load="YES"
454 dm_target_linear_load="YES"
455 initrd.img_load="YES"
456 initrd.img_type="md_image"
457 vfs.root.mountfrom="ufs:md0s0"
458 vfs.root.realroot="local:hammer:/dev/main/lv_root"
461 # Setup interface, configuration, sshd
464 ifc
=`route -n get default | fgrep interface | awk '{ print $2; }'`
465 ip
=`ifconfig $ifc | fgrep inet | fgrep -v inet6 | awk '{ print $2; }'`
466 lip
=`echo $ip | awk -F . '{ print $4; }'`
468 echo -n "ifconfig_$ifc=" >> /mnt
/etc
/rc.conf
469 echo '"DHCP"' >> /mnt
/etc
/rc.conf
470 cat >> /mnt
/etc
/rc.conf
<< EOF
473 hostname="test$lip.MYDOMAIN.XXX"
474 dumpdev="/dev/main/lv_swap"
479 cat >> /mnt
/etc
/sysctl.conf
<< EOF
480 #net.inet.ip.portrange.first=4000
484 echo "* Preparing initrd image"
485 /sbin
/mkinitrd
-b /mnt
/boot
>> ${logfile} 2>&1
490 echo "chroot now to /mnt and change root password and also edit"
491 echo "/mnt/etc/ssh/sshd_config to allow root login and authentication"
492 echo "using passwords. Alternatively, you can also just copy your"
493 echo "~/.ssh/authorized_keys file to allow login using your ssh keys."
495 echo "Installation finished successfully."
498 # take CD out and reboot