File: vsys

package info (click to toggle)
ibcs 971113-3
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 1,108 kB
  • ctags: 2,061
  • sloc: ansic: 14,910; makefile: 280; sh: 203; asm: 47; perl: 18; pascal: 2
file content (184 lines) | stat: -rw-r--r-- 3,532 bytes parent folder | download | duplicates (4)
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
#!/bin/sh

VSYSROOT=/OS


function create_vsys()
{
	sys="$1"

	# Create the virtual system directory and mount point
	mkdir -p "$VSYSROOT/$sys/..." > /dev/null 2>&1

	# Move to the virtual system directory
	cd "$VSYSROOT/$sys"

	# Mount the remote filesystem tree.
	showmount --no-headers -e "$sys" | while read fs
	do
		set -- $fs
		mount "$sys:$1" "...$1"
	done

	# Map names from the mount point to the virtual system directory.
	for i in .../*
	do
		# Don't map ..., tmp, dev or usr
		if [ "$i" = ".../..." -o "$i" = ".../tmp" -o "$i" = ".../dev" -o "$i" = ".../usr" ]; then
			continue
		else
			rm -f `basename "$i"`
			ln -s "$i" `basename "$i"`
		fi
	done

	# Map /usr too. We have to do this since /usr/tmp needs to be local.
	rm -f usr > /dev/null 2>&1
	mkdir usr > /dev/null 2>&1
	cd usr
	for i in ../.../usr/*
	do
		# Don't map tmp
		if [ "$i" = "../.../usr/tmp" ]; then
			continue
		else
			rm -f `basename "$i"`
			ln -s "$i" `basename "$i"`
		fi
	done
	cd ..

	# Make the local directories
	rm -f dev > /dev/null 2>&1
	mkdir tmp usr/tmp dev > /dev/null 2>&1
	chmod a=rwx,+t tmp usr/tmp
	cp -dpR /dev . > /dev/null 2>&1

	# And connect the access program for the virtual system
	cd $VSYSROOT/bin
	ln -sf bin "$sys"
}


function destroy_vsys()
{
	vsys="$1"

	# If it's a symlink just get rid of it and the associated program.
	if [ -L "$VSYSROOT/$vsys" ]; then
		rm -f $VSYSROOT/$vsys
		rm -f $VSYSROOT/bin/$vsys
	fi

	# If it isn't a directory or it hasn't got a ... subdirectory
	# it isn't a virtual system.
	if [ ! -d "$VSYSROOT/$vsys" -o ! -d "$VSYSROOT/$vsys/..." ]; then
		echo "$vsys is not a virtual system"
		exit 1
	fi

	# Unmount anything mounted for this virtual system.
	awk '{ print $2 }' /etc/mtab | grep "^$VSYSROOT/$vsys/..." | sort -r | while read fs
	do
		umount "$fs"
	done

	# Remove the virtual system tree. Avoid removing anything below
	# anything starting with . (specifically ... in case anything
	# has failed to unmount - we don't symlink dor files in the
	# root directory anyway).
	rm -rf $VSYSROOT/$vsys/*
	rmdir $VSYSROOT/$vsys/...
	rmdir $VSYSROOT/$vsys

	# Remove the associated program.
	rm -f $VSYSROOT/bin/$vsys
}


function alias_vsys()
{
	alias="$1"
	vsys="$2"

	cd $VSYSROOT

	# If the alias already exists as a link get rid of it.
	if [ -L "$alias" ]; then
		rm -f "$alias"
	fi

	# If the alias already exists or the virtual system doesn't
	# exist we can't do this.
	if [ -e "$alias" ]; then
		echo "$alias already exists"
	elif [ ! -d "$vsys" -a ! -L "$vsys" ]; then
		echo "Virtual system $vsys does not exist"
	else
		ln -s "$vsys" "$alias"
		cd bin
		rm -f "$alias"
		ln bin "$alias"
	fi
}


function usage()
{
	echo "usage: mkvsys [ -r vsysroot ]      Set the vsys root (default /OS)"
	echo "              [ -a alias real ]    Add an alias for the given system"
	echo "              [ -c system ]        Create the specified virtual system"
	echo "              [ -d system ]        Destroy the specified virtual system or alias"
}


if [ $# -eq 0 ]; then
	usage
	exit 1
fi

while [ $# -gt 0 ]
do
	case "$1" in
		-r)
			if [ $# -eq 1 ]; then
				usage
				exit 1
			fi
			VSYSROOT="$2"
			shift ; shift
			;;

		-c)
			if [ $# -eq 1 ]; then
				usage
				exit 1
			fi
			create_vsys "$2"
			shift ; shift
			;;

		-d)
			if [ $# -eq 1 ]; then
				usage
				exit 1
			fi
			destroy_vsys "$2"
			shift ; shift
			;;

		-a)
			if [ $# -eq 1 ]; then
				usage
				exit 1
			fi
			alias_vsys "$2" "$3"
			shift ; shift ; shift
			;;

		*)
			usage
			exit 1
			;;
	esac
done