Merge branch 'vendor/GDTOA'
[dragonfly.git] / share / examples / rconfig / hammer.sh
1 #!/bin/csh
2 #
3 # This will format a new machine with a BOOT+HAMMER setup and install
4 # the live CD.  You would boot the live CD, dhclient your network up,
5 # then run 'rconfig :hammer', assuming you have a rconfig server on the
6 # LAN.  Alternately fetch the script from a known location and just run it.
7 #
8 # ad6s1a will be setup as a small UFS /boot.  ad6s1d will be setup as
9 # HAMMER with all remaining disk space.  Pseudo file-systems will be
10 # created for /var, /usr, etc (giving them separate inode spaces and
11 # backup domains).
12 #
13 # WARNING: HAMMER filesystems (and pseudo-filesystems) must be
14 # occassionally pruned and reblocked.  'man hammer' for more information.
15 #
16 # $DragonFly: src/share/examples/rconfig/hammer.sh,v 1.4 2008/10/21 14:02:48 swildner Exp $
17
18 set disk = "ad6"
19
20 # For safety this only runs on a CD-booted machine
21 #
22 df / | awk '{ print $1; }' | fgrep cd
23 if ( $status > 0 ) then
24     echo "This program formats your disk and you didn't run it from"
25     echo "A CD boot!"
26     exit 1
27 endif
28
29 echo "This program formats disk ${disk}!  Hit ^C now or its gone."
30 foreach i ( 10 9 8 7 6 5 4 3 2 1 )
31     echo -n " $i"
32     sleep 1
33 end
34 echo ""
35
36 # Unmount any prior mounts on /mnt, reverse order to unwind
37 # sub-directory mounts.
38 #
39 foreach i ( `df | fgrep /mnt | awk '{ print $6; }' | tail -r` )
40     echo "UMOUNT $i"
41     umount $i
42 end
43
44 # Set our disk here
45 #
46 sleep 1
47 set echo
48
49 # Format and label the disk.  
50 #
51 #       'a' small UFS boot
52 #       'd' HAMMER filesystem
53 #
54 #       Use PFSs for backup domain separation
55 #
56 dd if=/dev/zero of=/dev/${disk} bs=32k count=16
57 fdisk -IB ${disk}
58 disklabel -r -w ${disk}s1 auto
59 disklabel -B ${disk}s1
60 disklabel ${disk}s1 > /tmp/label
61 cat >> /tmp/label << EOF
62   a: 256m 32 4.2BSD
63   b: 2g * swap
64   d: * * HAMMER
65 EOF
66 disklabel -R ${disk}s1 /tmp/label
67
68 newfs /dev/${disk}s1a
69 newfs_hammer -L ROOT /dev/${disk}s1d
70
71 # Mount it
72 #
73 mount_hammer /dev/${disk}s1d /mnt
74 mkdir /mnt/boot
75 mount /dev/${disk}s1a /mnt/boot
76
77 # Create PFS mount points for nullfs.
78 #
79 # Do the mounts manually so we can install the system, setup
80 # the fstab later on.
81 mkdir /mnt/pfs
82
83 hammer pfs-master /mnt/pfs/usr
84 hammer pfs-master /mnt/pfs/usr.obj
85 hammer pfs-master /mnt/pfs/var
86 hammer pfs-master /mnt/pfs/var.crash
87 hammer pfs-master /mnt/pfs/var.tmp
88 hammer pfs-master /mnt/pfs/tmp
89 hammer pfs-master /mnt/pfs/home
90
91 mkdir /mnt/usr
92 mkdir /mnt/var
93 mkdir /mnt/tmp
94 mkdir /mnt/home
95
96 mount_null /mnt/pfs/usr /mnt/usr
97 mount_null /mnt/pfs/var /mnt/var
98 mount_null /mnt/pfs/tmp /mnt/tmp
99 mount_null /mnt/pfs/home /mnt/home
100
101 mkdir /mnt/usr/obj
102 mkdir /mnt/var/tmp
103 mkdir /mnt/var/crash
104
105 mount_null /mnt/pfs/var.tmp /mnt/var/tmp
106 mount_null /mnt/pfs/var.crash /mnt/var/crash
107 mount_null /mnt/pfs/usr.obj /mnt/usr/obj
108
109 chmod 1777 /mnt/tmp
110 chmod 1777 /mnt/var/tmp
111
112 chflags nohistory /mnt/tmp
113 chflags nohistory /mnt/var/tmp
114 chflags nohistory /mnt/var/crash
115 chflags nohistory /mnt/usr/obj
116
117 # Install the system from the live CD
118 #
119 cpdup -o / /mnt
120 cpdup -o /var /mnt/var
121 cpdup -o /dev /mnt/dev
122 cpdup -i0 /etc.hdd /mnt/etc
123
124 # Create some directories to be used for NFS mounts later on.
125 # Edit as desired.
126 #
127 foreach i ( /proc /usr/doc /usr/src /repository /ftp /archive )
128     if ( ! -d /mnt$i ) then
129         mkdir /mnt$i
130     endif
131 end
132
133 cat > /mnt/etc/fstab << EOF
134 # Device                Mountpoint      FStype  Options         Dump    Pass#
135 /dev/${disk}s1d         /               hammer  rw              1       1
136 /dev/${disk}s1a         /boot           ufs     rw              1       1
137 /dev/${disk}s1b         none            swap    sw              0       0
138 /pfs/usr                /usr            null    rw              0       0
139 /pfs/var                /var            null    rw              0       0
140 /pfs/tmp                /tmp            null    rw              0       0
141 /pfs/home               /home           null    rw              0       0
142 /pfs/var.tmp            /var/tmp        null    rw              0       0
143 /pfs/usr.obj            /usr/obj        null    rw              0       0
144 /pfs/var.crash          /var/crash      null    rw              0       0
145 proc                    /proc           procfs  rw              0       0
146 # misc NFS mounts to get your test box access to 'stuff'
147 #crater:/repository     /repository     nfs     ro,intr,bg      0       0
148 #crater:/usr/doc        /usr/doc        nfs     ro,intr,bg      0       0
149 #crater:/ftp            /ftp            nfs     ro,intr,bg      0       0
150 #crater:/sources/HEAD   /usr/src        nfs     ro,intr,bg      0       0
151 #pkgbox:/archive        /archive        nfs     ro,intr,bg      0       0
152 EOF
153
154 # Since /boot is a separate partition we need to adjust kern.bootfile
155 # to make savecore work properly upon boot.
156 #
157 cat >> /mnt/etc/sysctl.conf << EOF
158 kern.bootfile=/boot/kernel
159 EOF
160
161 # Because root is not on the boot partition we have to tell the loader
162 # to tell the kernel where root is.
163 #
164 cat > /mnt/boot/loader.conf << EOF
165 vfs.root.mountfrom="hammer:${disk}s1d"
166 EOF
167
168 # Setup interface, configuration, sshd
169 #
170 set ifc = `route -n get default | fgrep interface | awk '{ print $2; }'`
171 set ip = `ifconfig $ifc | fgrep inet | fgrep -v inet6 | awk '{ print $2; }'`
172 set lip = `echo $ip | awk -F . '{ print $4; }'`
173
174 echo -n "ifconfig_$ifc=" >> /mnt/etc/rc.conf
175 echo '"DHCP"' >> /mnt/etc/rc.conf
176 cat >> /mnt/etc/rc.conf << EOF
177 sshd_enable="YES"
178 dntpd_enable="YES"
179 hostname="test$lip.MYDOMAIN.XXX"
180 dumpdev="/dev/${disk}s1b"
181 EOF
182
183 # Misc sysctls
184 #
185 cat >> /mnt/etc/sysctl.conf << EOF
186 #net.inet.ip.portrange.first=4000
187 EOF
188
189 # adjust work directory for pkgsrc in case we want
190 # to mount /usr/pkgsrc read-only.
191 #
192 cat >> /mnt/usr/pkg/etc/mk.conf << EOF
193 .ifdef BSD_PKG_MK       # begin pkgsrc settings
194 WRKOBJDIR=              /usr/obj/pkgsrc
195 .endif                  # end pkgsrc settings
196 EOF
197
198 # Allow sshd root logins via dsa key only
199 #
200 fgrep 'PermitRootLogin without-password' /mnt/etc/ssh/sshd_config >& /dev/null
201 if ( $?status ) then
202     echo "PermitRootLogin without-password" >> /mnt/etc/ssh/sshd_config
203 endif
204
205 # additional loader.conf stuff
206 #cat >> /mnt/boot/loader.conf << EOF
207 #if_nfe_load="YES"
208 #EOF
209
210 # Get sshd working - auto install my key so I can login.
211 #
212 #mkdir -p /mnt/root/.ssh
213 #cat > /mnt/root/.ssh/authorized_keys << EOF
214 #ssh-dss ...
215 #EOF
216
217 if ( ! -f /mnt/etc/ssh/ssh_host_dsa_key ) then
218     cd /mnt/etc/ssh
219     ssh-keygen -t dsa -f ssh_host_dsa_key -N ""
220 endif
221
222 # take CD out and reboot
223