File: lvmcfg.sh

package info (click to toggle)
lvmcfg 1.36
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,028 kB
  • ctags: 23
  • sloc: sh: 562; makefile: 5
file content (759 lines) | stat: -rwxr-xr-x 16,438 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
#!/bin/sh

. /usr/share/debconf/confmodule

#
# convert the return values from "xx (yy)" => "xx"
#
convert_return() {
	local IFS=","
	RET=""

	for part in $1; do
		part=${part%% *}
		if [ -z "$RET" ]; then
			RET="$part"
		else
			RET="$RET, $part"
		fi
	done
}

#
# Convert common terms for disk sizes into something LVM understands.
#  e.g. "200 Gb" -> "200G"
#
human2lvm() {
	echo "$1" | tr -d " " | sed -e 's/\([kmgtKMGT]\)[bB]/\1/'
}

#
# return extra informations (like size, current lv) for the
# volume group
#
addinfos_pv() {
	cmdout=`pvdisplay "$1" 2>&1`

	echo "$cmdout" | grep -q 'is a new physical volume'
	if [ $? -eq 0 ]; then
		RET2=`echo "$cmdout" | sed -e 's/.*of \(.*\)$/Size: \1/'`
		RET="${RET2}"
		return
	fi

	echo "$cmdout" | grep -q '^pvdisplay'
	if [ $? -eq 0 ]; then
		RET="unknown"
		return
	fi

	RET=""
	VG=`echo "$cmdout" | grep '^[ ]*VG Name' | \
		sed -e 's/^[ ]*VG Name \+//'`
	[ "$VG" != "" ] && RET="VG: ${VG}/ "

	RET2=`echo "$cmdout" | grep '^[ ]*PV Size' | \
		sed -e 's/^[ ]*PV Size \+\(.*\)/Size: \1/' | \
		cut -d "[" -f 1 | cut -d "/" -f 1 | sed -e 's/ $//'`
	RET="${RET}${RET2}"
}

#
# return the free space of a volume group
#
getfree_vg() {
	cmdout=`vgdisplay "$1" 2>&1`
	echo "$cmdout" | grep '^[ ]*Free  PE' | sed -e 's,^.*/ ,,' | sed -e 's/ \+$//'
}

#
# return the free space of a volume group (in physical extents [PE])
#
getfreepe_vg() {
	cmdout=`vgdisplay "$1" 2>&1`
	echo "$cmdout" | grep '^[ ]*Free  PE' | sed -e 's/^.*Size \+//' | sed -e 's/ \+.*//'
}

#
# return the size of a volume group
#
getsize_vg() {
	cmdout=`vgdisplay "$1" 2>&1`
	echo "$cmdout" | grep '^[ ]*VG Size' | sed -e 's/^[ ]*VG Size \+//'
}

#
# return extra informations (like size, current lv) for the
# volume group
#
addinfos_vg() {
	cmdout=`vgdisplay "$1" 2>&1`

	RET2="Free: `getfree_vg "$1"`"
	RET="${RET2}"

	RET2="Size: `getsize_vg "$1"`"
	RET="${RET}/ ${RET2}"

	RET2=`echo "$cmdout" | grep '^[ ]*Cur LV' | \
		sed -e 's/^[ ]*Cur LV \+\(.*\)/LVs: \1/'`
	RET="${RET}/ ${RET2}"

	RET2=`echo "$cmdout" | grep '^[ ]*Cur PV' | \
		sed -e 's/^[ ]*Cur PV \+\(.*\)/PVs: \1/'`
	RET="${RET}/ ${RET2}"
}

#
# return extra informations (like size, fs type) for the
# logical volume
#
addinfos_lv() {
	cmdout=`lvdisplay "$1" 2>&1`

	RET2=`echo "$cmdout" | grep '^[ ]*LV Size' | \
		sed -e 's/^[ ]*LV Size \+\(.*\)/Size: \1/'`
	RET="${RET2}"

	cmdout=`parted $1 print | grep '^1' | \
		sed -e 's/ \+/ /g' | cut -d " " -f 4`
	[ -z "$cmdout" ] && cmdout="unknown"
	RET="${RET}/ FS: ${cmdout}"

	cmdout=`grep "^$1" /proc/mounts | cut -d " " -f2 | \
		sed -e 's,/target,,'`
	[ -z "$cmdout" ] && cmdout="n/a"
	RET="${RET}/ Mount: ${cmdout}"
}

# get all available physical volumes
enum_pvs() {
	# Use partconf's find-partitions to find partitions
	# marked as lvm volumes.
	/usr/lib/partconf/find-partitions --ignore-fstype 2>/dev/null | \
		grep "[[:space:]]LVM[[:space:]]" | cut -f1
}
	

# get all unused available physical volumes
# 	in this case all partitions with 0x8e,
#	or all other non-lvm devices from /proc/partitions
#
get_pvs() {
	PARTITIONS=""
	for i in $(enum_pvs); do
		# skip already assigned
		found=no
		for pv in $(vgdisplay -v | grep "[ ]*PV Name" | sed -e "s/ \+PV Name \+//"); do
			[ "$(readlink -f "$pv")" = "$(readlink -f "$i")" ] && found=yes
		done
		[ "$found" = "yes" ] && continue

		addinfos_pv "$i"
		i=`printf "%-15s (%s)" "$i" "$RET"`
		
		if [ -z "$PARTITIONS" ]; then
			PARTITIONS="$i"
		else
			PARTITIONS="${PARTITIONS},$i"
		fi
	done
}

#
# list all activated volume groups
#
list_vgs() {
	echo `vgdisplay | grep '^[ ]*VG Name' | sed -e 's/.*[[:space:]]\+\(.*\)$/\1/' | sort`
}

#
# check if any VGs have free space
#
any_free_vgs() {
	for i in $(list_vgs); do
		[ "$(getfree_vg "$i")" = "0" ] || return 0
	done
	return 1
}

#
# get all activated volume groups
#
get_vgs() {
	arg="$1"
	GROUPS=""
	for i in $(list_vgs); do
		if [ "$arg" = "free" -a "$(getfree_vg "$i")" = "0" ]; then
			continue
		fi
		if [ "$arg" = "with-lv" ]; then
			get_vglvs "$i"
			[ "$LVS" = "" ] && continue
		fi
		addinfos_vg "$i"
		i=`printf "%-15s (%s)" "$i" "$RET"`
		
		if [ -z "$GROUPS" ]; then
			GROUPS="$i"
		else
			GROUPS="$GROUPS, $i"
		fi
	done
}

#
# get all physical volumes from a volume group
#
get_vgpvs() {
	PARTITIONS=""
	for i in `vgdisplay -v $1 | grep '^[ ]*PV Name' | 
		sed -e 's,.*/dev,/dev,' | cut -d " " -f 1 | sort`; do

		addinfos_pv "$i"
		i=`printf "%-15s (%s)" "$i" "$RET"`
		
		if [ -z "$PARTITIONS" ]; then
			PARTITIONS="$i"
		else
			PARTITIONS="$PARTITIONS, $i"
		fi
	done
}

#
# get all logical volumes from a volume group
#
get_vglvs() {
	local i
	LVS=""
	for i in `vgdisplay -v $1 | grep '^[ ]*LV Name' | 
		sed -e 's,.*/\(.*\),\1,' | sort`; do

		addinfos_lv "/dev/$1/$i"
		i=`printf "%-15s (%s)" "$i" "$RET"`
		
		if [ -z "$LVS" ]; then
			LVS="$i"
		else
			LVS="$LVS, $i"
		fi
	done
}

#
# this is the VG main-menu
#
vg_mainmenu() {
	while [ 1 ]; do
		db_set lvmcfg/vgmenu "false"
		db_input critical lvmcfg/vgmenu
		db_go
		db_get lvmcfg/vgmenu

		log-output -t lvmcfg vgscan
		case "$RET" in
		Create*)
			vg_create
			;;
		Delete*)
			vg_delete
			;;
		Extend*)
			vg_extend
			;;
		Reduce*)
			vg_reduce
			;;
		*)
			break
			;;
		esac

		# If we have only created VGs so far, then check if any PVs
		# are still free; if not, leave the menu and default to
		# creating a LV.
		if [ "$FIRST" = "yes" ]; then
			if [ "$VGRET" = "create" ]; then
				get_pvs
				if [ -z "$PARTITIONS" ]; then
					OVERRIDE=no
					db_set lvmcfg/mainmenu "Modify logical volumes (LV)"
					break
				fi
			else
				FIRST=no
			fi
		fi
	done
}

#
# create a new volume group
#
vg_create() {
	# searching for available partitions (0x8e)
	get_pvs
	if [ -z "$PARTITIONS" ]; then
		db_set lvmcfg/nopartitions "false"
		db_input high lvmcfg/nopartitions
		db_go
		return
	fi

	db_subst lvmcfg/vgcreate_parts PARTITIONS $PARTITIONS
	db_set lvmcfg/vgcreate_parts "false"
	db_input critical lvmcfg/vgcreate_parts
	db_go
	db_get lvmcfg/vgcreate_parts
	[ "$RET" = "false" ] && return

	convert_return "$RET"
	PARTITIONS="$RET"

	if [ -z "$RET" ]; then
		db_set lvmcfg/vgcreate_nosel "false"
		db_input high lvmcfg/vgcreate_nosel
		db_go
		return
	fi

	db_set lvmcfg/vgcreate_name ""
	db_input critical lvmcfg/vgcreate_name
	db_go
	[ $? -eq 30 ] && return
	db_get lvmcfg/vgcreate_name
	NAME="$RET"

	# check whether a name for the vg has been given
	if [ "$NAME" = "" ]; then
		db_set lvmcfg/vgcreate_nonamegiven "false"
		db_input high lvmcfg/vgcreate_nonamegiven
		db_go
		return
	fi

	# check, if the vg name is already in use
	vgdisplay -v | grep '^[ ]*VG Name' | sed -e 's/ *VG Name *//' | grep -q "^$NAME$"
	if [ $? -eq 0 ]; then
		db_set lvmcfg/vgcreate_nameused "false"
		db_input high lvmcfg/vgcreate_nameused
		db_go
		return
	fi

	# check if the vg name overlaps with an existing device
	# FIXME: d-i uses devfs while Debian doesn't, so ideally we
	# would also check against a regular /dev tree.
	if [ -e "/dev/$NAME" ]; then
		db_set lvmcfg/vgcreate_devnameused "false"
		db_input high lvmcfg/vgcreate_devnameused
		db_go
		return
	fi

	for p in `echo "$PARTITIONS" | sed -e 's/,/ /g'`; do
		log-output -t lvmcfg pvcreate -ff -y $p
	done

	log-output -t lvmcfg vgcreate "$NAME" `echo "$PARTITIONS" | sed -e 's/,/ /g'`
}

#
# delete a volume group
#
vg_delete() {
	get_vgs
	if [ -z "$GROUPS" ]; then
		db_set lvmcfg/vgdelete_novg "false"
		db_input high lvmcfg/vgdelete_novg
		db_go
		return
	fi

	db_subst lvmcfg/vgdelete_names GROUPS "${GROUPS}, Leave"
	db_set lvmcfg/vgdelete_names "false"
	db_input critical lvmcfg/vgdelete_names
	db_go
	db_get lvmcfg/vgdelete_names
	[ "$RET" = "Leave" -o "$RET" = "false" ] && return
	VG=`echo "$RET" | cut -d " " -f1`

	# confirm message
	db_subst lvmcfg/vgdelete_confirm VG $VG
	db_set lvmcfg/vgdelete_confirm "false"
	db_input critical lvmcfg/vgdelete_confirm
	db_go
	db_get lvmcfg/vgdelete_confirm
	[ "$RET" != "true" ] && return

	log-output -t lvmcfg vgchange -a n $VG && \
		log-output -t lvmcfg vgremove $VG
	[ $? -eq 0 ] && return

	# reactivate if deleting was failed
	log-output -t lvmcfg vgchange -a y $VG
	
	db_set lvmcfg/vgdelete_error "false"
	db_input high lvmcfg/vgdelete_error
	db_go
}

#
# allow expanding of existing volume groups
#
vg_extend() {
	# searching for available partitions (0x8e)
	get_pvs
	if [ -z "$PARTITIONS" ]; then
		db_set lvmcfg/nopartitions "false"
		db_input high lvmcfg/nopartitions
		db_go
		return
	fi

	get_vgs
	if [ -z "$GROUPS" ]; then
		db_set lvmcfg/vgextend_novg "false"
		db_input high lvmcfg/vgextend_novg
		db_go
		return
	fi

	db_subst lvmcfg/vgextend_names GROUPS "${GROUPS}, Leave"
	db_set lvmcfg/vgextend_names "false"
	db_input critical lvmcfg/vgextend_names
	db_go
	db_get lvmcfg/vgextend_names
	[ "$RET" = "Leave" -o "$RET" = "false" ] && return
	VG=`echo "$RET" | cut -d " " -f1`

	db_subst lvmcfg/vgextend_parts PARTITIONS $PARTITIONS
	db_set lvmcfg/vgextend_parts "false"
	db_input critical lvmcfg/vgextend_parts
	db_go
	db_get lvmcfg/vgextend_parts

	convert_return "$RET"
	PARTITIONS="$RET"

	if [ -z "$RET" ]; then
		db_set lvmcfg/vgextend_nosel "false"
		db_input high lvmcfg/vgextend_nosel
		db_go
		return
	fi

	for p in `echo "$PARTITIONS" | sed -e 's/,/ /g'`; do
		log-output -t lvmcfg pvcreate -ff -y $p && \
			log-output -t lvmcfg vgextend $VG $p
		if [ $? -ne 0 ]; then	# on error
			db_subst lvmcfg/vgextend_error PARTITION $p
			db_subst lvmcfg/vgextend_error VG $VG
			db_set lvmcfg/vgextend_error "false"
			db_input high lvmcfg/vgextend_error
			db_go
			return
		fi
	done
}

#
# allow reducing the volume groups
#
vg_reduce() {
	get_vgs
	if [ -z "$GROUPS" ]; then
		db_set lvmcfg/vgreduce_novg "false"
		db_input high lvmcfg/vgreduce_novg
		db_go
		return
	fi

	db_subst lvmcfg/vgreduce_names GROUPS "${GROUPS}, Leave"
	db_set lvmcfg/vgreduce_names "false"
	db_input critical lvmcfg/vgreduce_names
	db_go
	db_get lvmcfg/vgreduce_names
	[ "$RET" = "Leave" -o "$RET" = "false" ] && return
	VG=`echo "$RET" | cut -d " " -f1`

	# check, if the vg has more then one pv's
	set -- `vgdisplay $VG | grep '^[ ]*Cur PV'`
	if [ $3 -le 1 ]; then
		db_subst lvmcfg/vgreduce_minpv VG $VG
		db_set lvmcfg/vgreduce_minpv "false"
		db_input high lvmcfg/vgreduce_minpv
		db_go
		return
	fi

	get_vgpvs "$VG"
	db_subst lvmcfg/vgreduce_parts PARTITIONS $PARTITIONS
	db_set lvmcfg/vgreduce_parts "false"
	db_input critical lvmcfg/vgreduce_parts
	db_go
	db_get lvmcfg/vgreduce_parts
	PARTITIONS=`echo "$RET" | cut -d " " -f1`

	log-output -t lvmcfg vgreduce $VG $PARTITIONS
	if [ $? -ne 0 ]; then
		db_subst lvmcfg/vgreduce_error VG $VG
		db_subst lvmcfg/vgreduce_error PARTITION $PARTITIONS
		db_set lvmcfg/vgreduce_error "false"
		db_input high lvmcfg/vgreduce_error
		db_go
		return
	fi
}

#
# this is the LV main-menu
#
lv_mainmenu() {
	FIRST=yes
	while [ 1 ]; do
		db_set lvmcfg/lvmenu "false"
		db_input critical lvmcfg/lvmenu
		db_go
		db_get lvmcfg/lvmenu

		log-output -t lvmcfg vgscan
		case "$RET" in
		Create*)
			lv_create
			;;
		Delete*)
			lv_delete
			;;
		Extend*)
			lv_expand
			;;
		Reduce*)
			lv_reduce
			;;
		*)
			break
			;;
		esac

		# Leave the menu and default to "Leave" if there is no
		# free space anymore after setting up LVs.
		if [ "$FIRST" = "yes" ]; then
			if [ "$LVRET" = "create" ]; then
				if ! $(any_free_vgs) ; then
					OVERRIDE=no
					db_set lvmcfg/mainmenu "Leave"
					break
				fi
			else
				FIRST=no
			fi
		fi
	done
}

#
# create a new logical volume
#
lv_create() {
	get_vgs
	if [ -z "$GROUPS" ]; then
		db_set lvmcfg/lvcreate_novg "false"
		db_input high lvmcfg/lvcreate_novg
		db_go
		return
	fi
	get_vgs "free"
	if [ -z "$GROUPS" ]; then
		db_set lvmcfg/lvcreate_nofreevg "false"
		db_input high lvmcfg/lvcreate_nofreevg
		db_go
		return
	fi

	db_set lvmcfg/lvcreate_name ""
	db_input critical lvmcfg/lvcreate_name
	db_go
	[ $? -eq 30 ] && return
	db_get lvmcfg/lvcreate_name
	NAME="$RET"

	# check whether a name for the lv has been given
	if [ "$NAME" = "" ]; then
		db_set lvmcfg/lvcreate_nonamegiven "false"
		db_input high lvmcfg/lvcreate_nonamegiven
		db_go
		return
	fi

	db_subst lvmcfg/lvcreate_vgnames GROUPS "${GROUPS}, Leave"
	db_set lvmcfg/lvcreate_vgnames "false"
	db_input critical lvmcfg/lvcreate_vgnames
	db_go
	db_get lvmcfg/lvcreate_vgnames
	[ "$RET" = "Leave" -o "$RET" = "false" ] && return
	VG=`echo "$RET" | cut -d " " -f1`

	# make sure, the name isn't already in use
	vgdisplay -v $VG | grep '^[ ]*LV Name' | grep -q "${VG}/${NAME}$"
	if [ $? -eq 0 ]; then
		db_subst lvmcfg/lvcreate_exists LV "$NAME"
		db_subst lvmcfg/lvcreate_exists VG $VG
		db_set lvmcfg/lvcreate_exists "false"
		db_input high lvmcfg/lvcreate_exists
		db_go
		return
	fi

	MAX_SIZE=$(human2lvm "$(getfree_vg "$VG")")
	db_set lvmcfg/lvcreate_size "$MAX_SIZE"
	db_fset lvmcfg/lvcreate_size seen false
	db_input critical lvmcfg/lvcreate_size
	db_go
	db_get lvmcfg/lvcreate_size
	SIZE=$(human2lvm "$RET")
	[ -z "$RET" ] && return

	# If the maximum free space should be used for the new LV, use the
	# number of physical extents (PEs) because the size given by LVM
	# might not be accurate, resulting in an error because the VG is
	# not big enough (see #250594).
	if [ "$MAX_SIZE" = "$SIZE" ]; then
		log-output -t lvmcfg lvcreate -l$(getfreepe_vg "$VG") -n "$NAME" $VG
	else
		log-output -t lvmcfg lvcreate -L${SIZE} -n "$NAME" $VG
	fi

	if [ $? -ne 0 ]; then
		db_subst lvmcfg/lvcreate_error VG $VG
		db_subst lvmcfg/lvcreate_error LV $NAME
		db_subst lvmcfg/lvcreate_error SIZE $SIZE
		db_set lvmcfg/lvcreate_error "false"
		db_input high lvmcfg/lvcreate_error
		db_go
		return
	fi
}


#
# create a new logical volume
#
lv_delete() {
	get_vgs "with-lv"
	if [ -z "$GROUPS" ]; then
		db_set lvmcfg/lvdelete_novg "false"
		db_input high lvmcfg/lvdelete_novg
		db_go
		return
	fi

	db_subst lvmcfg/lvdelete_vgnames GROUPS "${GROUPS}, Leave"
	db_set lvmcfg/lvdelete_vgnames "false"
	db_input critical lvmcfg/lvdelete_vgnames
	db_go
	db_get lvmcfg/lvdelete_vgnames
	[ "$RET" = "Leave" -o "$RET" = "false" ] && return
	VG=`echo "$RET" | cut -d " " -f1`

	get_vglvs "$VG"
	if [ -z "$LVS" ]; then
		db_set lvmcfg/lvdelete_nolv "false"
		db_input high lvmcfg/lvdelete_nolv
		db_go
		return
	fi

	db_subst lvmcfg/lvdelete_lvnames VG "$VG"
	db_subst lvmcfg/lvdelete_lvnames LVS "${LVS}, Leave"
	db_set lvmcfg/lvdelete_lvnames "false"
	db_input critical lvmcfg/lvdelete_lvnames
	db_go
	db_get lvmcfg/lvdelete_lvnames
	[ "$RET" = "Leave" -o "$RET" = "false" ] && return
	LV=`echo "$RET" | cut -d " " -f1`

	log-output -t lvmcfg lvremove -f /dev/${VG}/${LV}
	if [ $? -ne 0 ]; then
		db_subst lvmcfg/lvdelete_error VG $VG
		db_subst lvmcfg/lvdelete_error LV $LV
		db_set lvmcfg/lvdelete_error "false"
		db_input high lvmcfg/lvdelete_error
		db_go
		return
	fi
}


#
# *** main stuff ***
#

# load required kernel modules
depmod -a 1>/dev/null 2>&1
modprobe dm-mod >/dev/null 2>&1
modprobe lvm-mod >/dev/null 2>&1

# make sure that lvm is available
if ! grep -q "[0-9] device-mapper$" /proc/misc ; then
	db_set lvmcfg/nolvm "false"
	db_input high lvmcfg/nolvm
	db_go
	exit 0
fi

# scan for logical volumes
log-output -t lvmcfg pvscan
log-output -t lvmcfg vgscan

# try to activate old volume groups
set -- `vgdisplay -v | grep 'NOT \(active\|available\)' | wc -l`
if [ $1 -gt 0 -a ! -f /var/cache/lvmcfg/first ]; then
	db_subst lvmcfg/activevg COUNT $1
	db_set lvmcfg/activevg "false"
	db_input critical lvmcfg/activevg
	db_go
	db_get lvmcfg/activevg
	[ "$RET" = "true" ] && log-output -t lvmcfg vgchange -a y
fi
# ask only the first time
mkdir -p /var/cache/lvmcfg && touch /var/cache/lvmcfg/first

FIRST=yes
# The following variable determines whether the lvmcfg/mainmenu is set
# to false.  At various places in this script, OVERRIDE=no is set in
# order to set a default.  This is useful to have LV as default when
# all VGs have been set up, and Leave the default after all LVs have
# been allocated.
OVERRIDE=yes
# main-loop
while [ 1 ]; do
	[ "$OVERRIDE" = "yes" ] && db_set lvmcfg/mainmenu "false"
	db_input critical lvmcfg/mainmenu
	db_go
	db_get lvmcfg/mainmenu
	MAINRET=`echo "$RET" | sed -e 's,.*(\(.*\)).*,\1,'`

	OVERRIDE=yes
	case "$MAINRET" in
	"PV")	# currently unused
		#pv_mainmenu
		;;
	"VG")
		vg_mainmenu
		;;
	"LV")
		lv_mainmenu
		;;
	*)
		break	# finished
		;;
	esac
done

# install lvm tools in /target if needed
set -- `vgdisplay -v | grep -i "Logical volume ---" | wc -l`
[ $1 -gt 0 ] && apt-install lvm2

exit 0