xref: /plan9-contrib/sys/lib/dist/pc/inst/fmtventi (revision 3cdeec5c5169e8d928b070689be6770201eac7a6)
1#!/bin/rc
2
3# desc: initialize disks for a venti server
4# prereq: mountdist
5
6switch($1){
7case checkready checkdone
8	if(! ~ $fstype fossil+venti){
9		fmtventi=notdone
10		export fmtventi
11		exit
12	}
13	if(! test -e /bin/venti/fmtarenas || ! test -e /bin/venti/fmtisect || ! test -e /bin/venti/fmtindex || ! test -e /bin/venti/fmtbloom){
14		bind -a /n/dist/386/bin/venti /bin/venti
15		if(! test -e /bin/venti/fmtarenas || ! test -e /bin/venti/fmtisect || ! test -e /bin/venti/fmtindex || ! test -e /bin/venti/fmtbloom){
16			fmtventi=notdone
17			export fmtventi
18			exit
19		}
20	}
21	ff=`{ls /dev/sd*/arenas* /dev/fs/arenas* >[2]/dev/null}
22	if(~ $#ff 0){
23		fmtventi=notdone
24		export fmtventi
25		exit
26	}
27	gg=()
28	for(f in $ff)
29		if(isventi $f)
30			gg=($gg $f)
31	if(~ $#gg 0){
32		fmtventi=ready
33		export fmtventi
34		exit
35	}
36
37	ventiarena=$gg(1)
38	export ventiarena
39
40	fmtventi=done
41	export fmtventi
42	exit
43
44case go
45	ff=`{ls /dev/sd*/arenas* /dev/fs/arenas* >[2]/dev/null}
46	if(~ $#ff 0){
47		echo 'You need to create a partition or partitions to hold the Venti arenas.'
48		echo 'The arena partition names must begin with "arenas".'
49		echo
50		fmtventi=notdone
51		export fmtventi
52		exit
53	}
54	default=(-d $"ff)
55	if(! ~ $#ventiarena 0){
56		default=(-d $"ventiarena)
57	}
58	echo You have the following Venti arena partitions.
59	ls -l $ff
60	echo
61
62	prompt $default 'Venti arena partitions to use'
63	aa=`{echo $rd}
64	bad=no
65	for(a in $aa){
66		if(! ~ $a $ff){
67			echo 'Bad venti arena partition' $a
68			fmtventi=notdone
69			export fmtventi
70			exit
71		}
72	}
73	ventiarena=$aa
74	export ventiarena
75
76	gg=`{ls /dev/sd*/isect* /dev/fs/isect* >[2]/dev/null}
77	if(~ $#gg 0){
78		echo 'You need to create a partition or partitions to hold the Venti indices.'
79		echo 'The index partition names must begin with "isect".'
80		echo
81		fmtventi=notdone
82		export fmtventi
83		exit
84	}
85	default=(-d $"gg)
86	if(! ~ $#ventiindex 0){
87		default=(-d $"ventiindex)
88	}
89
90	echo You have the following Venti index partitions.
91	ls -l $gg
92	echo
93
94	prompt $default 'Venti index partitions to use'
95	aa=`{echo $rd}
96	bad=no
97	for(a in $aa){
98		if(! ~ $a $gg){
99			echo 'Bad venti index partition' $a
100			fmtventi=notdone
101			export fmtventi
102			exit
103		}
104	}
105	ventiindex=$aa
106	export ventiindex
107
108	hh=`{ls /dev/sd*/bloom* /dev/fs/bloom* >[2]/dev/null}
109	if(~ $#hh 0){
110		echo 'You need to create a partition or partitions to hold the Venti bloom.'
111		echo 'The bloom partition names must begin with "bloom".'
112		echo
113		fmtventi=notdone
114		export fmtventi
115		exit
116	}
117	default=(-d $"hh)
118	if(! ~ $#ventibloom 0){
119		default=(-d $"ventibloom)
120	}
121
122	echo You have the following Venti bloom partitions.
123	ls -l $hh
124	echo
125
126	prompt $default 'Venti bloom partitions to use'
127	aa=`{echo $rd}
128	bad=no
129	for(a in $aa){
130		if(! ~ $a $hh){
131			echo 'Bad venti bloom partition' $a
132			fmtventi=notdone
133			export fmtventi
134			exit
135		}
136	}
137	ventibloom=$aa
138	export ventibloom
139
140	n=-1
141	fmta=()
142	for(a in $ventiarena){
143		do=yes
144		n=`{hoc -e 1+$n}
145		if(isventiarenas $a){
146			echo File $a is already formatted as a Venti arenas partition.
147			prompt -d no 'Reformat '$a yes no
148			do=$rd
149		}
150		if(~ $do yes)
151			fmta=($fmta arenas$n:$a)
152	}
153
154	n=-1
155	fmti=()
156	for(a in $ventiindex){
157		do=yes
158		n=`{hoc -e 1+$n}
159		if(isventiisect $a){
160			echo File $a is already formatted as a Venti index section.
161			prompt -d no 'Reformat '$a yes no
162			do=$rd
163		}
164		if(~ $do yes)
165			fmti=($fmti isect$n:$a)
166	}
167
168	n=-1
169	fmtb=()
170	for(a in $ventibloom){
171		do=yes
172		n=`{hoc -e 1+$n}
173		if(isventibloom $a){
174			echo File $a is already formatted as a Venti bloom section.
175			prompt -d no 'Reformat '$a yes no
176			do=$rd
177		}
178		if(~ $do yes)
179			fmtb=($fmtb bloom$n:$a)
180	}
181
182	echo Formatting Venti arenas and indices (this takes a while).
183	# do each disk in parallel
184	echo good >/tmp/fmt
185	dd=()
186	for(a in $fmta $fmti $fmtb){
187		d=`{echo $a | sed 's!.*:(/.*/).*!\1!'}
188		if(! ~ $d $dd)
189			dd=($dd $d)
190	}
191	for(d in $dd){
192		{
193			for(a in $fmta){
194				i=`{echo $a | sed 's!(.*):(/.*/)(.*)!\1 \2 \2\3!'}
195				if(~ $i(2) $d){
196					echo $i(3) ...
197					venti/fmtarenas $i(1) $i(3) || echo bad >/tmp/fmt
198					echo done with $i(3)
199				}
200			}
201			for(a in $fmti){
202				i=`{echo $a | sed 's!(.*):(/.*/)(.*)!\1 \2 \2\3!'}
203				if(~ $i(2) $d){
204					echo $i(3) ...
205					venti/fmtisect $i(1) $i(3) || echo bad >/tmp/fmt
206					echo done with $i(3)
207				}
208			}
209			for(a in $fmtb){
210				i=`{echo $a | sed 's!(.*):(/.*/)(.*)!\1 \2 \2\3!'}
211				pgsize=`{grep pagesize /dev/swap | sed 's/^([0-9]+) .*$/\1/'}
212				userused=`{grep user /dev/swap | sed 's/^([0-9]+)\/([0-9]+) .*$/\1/'}
213				userpgs=`{grep user /dev/swap | sed 's/^([0-9]+)\/([0-9]+) .*$/\2/'}
214				userfree=`{hoc -e '('$userpgs-$userused')'*$pgsize}
215				bloomsize=`{hoc -e 2'^(int(log('$userfree/1024/1024*20/100*1/3')/log(2)))'}
216				partsize=`{ls -l $i(3) | awk '{print int($6/1024/1024)}' | hoc}
217				if(test $bloomsize -gt $partsize)
218					bloomsize=$partsize
219				if(test $bloomsize -lt 1)
220					bloomsize=1
221				if(~ $i(2) $d){
222					echo $i(3) ...
223					venti/fmtbloom -s $bloomsize^m $i(3) || echo bad >/tmp/fmt
224					echo done with $i(3)
225				}
226			}
227		} &
228	}
229	wait
230	if(~ bad `{cat /tmp/fmt}){
231		echo There were errors formatting the indices and arenas.
232		fmtventi=ready
233		export fmtventi
234		exit errors
235	}
236
237	echo Done formatting Venti arenas and indices.
238
239	v=$ventiarena(1)
240	echo Storing Venti config on $v...
241	{
242		echo index main
243		for(i in $ventiindex)
244			echo isect $i
245		for(b in $ventibloom)
246			echo bloom $b
247		for(a in $ventiarena)
248			echo arenas $a
249	} | venti/conf -w $v
250
251	echo Initializing index...
252	venti/fmtindex $v
253
254	echo Done with Venti!
255}
256
257