1#
2# Copyright (c) 2005 XenSource Ltd.
3#
4# This library is free software; you can redistribute it and/or
5# modify it under the terms of version 2.1 of the GNU Lesser General Public
6# License as published by the Free Software Foundation.
7#
8# This library is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11# Lesser General Public License for more details.
12#
13# You should have received a copy of the GNU Lesser General Public
14# License along with this library; If not, see <http://www.gnu.org/licenses/>.
15#
16
17
18dir=$(dirname "$0")
19. "$dir/xen-hotplug-common.sh"
20
21findCommand "$@"
22
23if [ "$command" != "add" ] &&
24   [ "$command" != "remove" ]
25then
26  log err "Invalid command: $command"
27  exit 1
28fi
29
30
31XENBUS_PATH="${XENBUS_PATH:?}"
32
33
34ebusy()
35{
36  xenstore_write "$XENBUS_PATH/hotplug-error" "$*" \
37                 "$XENBUS_PATH/hotplug-status" busy
38  log err "$@"
39  exit 1
40}
41
42
43##
44# Print the given device's major and minor numbers, written in hex and
45# separated by a colon.
46device_major_minor()
47{
48  stat -L -c %t:%T "$1"
49}
50
51
52##
53# Write physical-device = MM,mm to the store, where MM and mm are the major
54# and minor numbers of device respectively.
55#
56# @param device The device from which major and minor numbers are read, which
57#               will be written into the store.
58#
59write_dev() {
60  local mm
61
62  mm=$(device_major_minor "$1")
63
64  if [ -z $mm ]
65  then
66    fatal "Backend device does not exist"
67  fi
68
69  xenstore_write "$XENBUS_PATH/physical-device" "$mm"
70  xenstore_write "$XENBUS_PATH/physical-device-path" "$1"
71
72  success
73}
74
75
76##
77# canonicalise_mode mode
78#
79# Takes the given mode, which may be r, w, ro, rw, w!, or rw!, or variations
80# thereof, and canonicalises them to one of
81#
82#   'r': perform checks for a new read-only mount;
83#   'w': perform checks for a read-write mount; or
84#   '!': perform no checks at all.
85#
86canonicalise_mode()
87{
88  local mode="$1"
89
90  if ! expr index "$mode" 'w' >/dev/null
91  then
92    echo 'r'
93  elif ! expr index "$mode" '!' >/dev/null
94  then
95    echo 'w'
96  else
97    echo '!'
98  fi
99}
100
101
102same_vm()
103{
104  local otherdom="$1"
105  # Note that othervm can be MISSING here, because Xend will be racing with
106  # the hotplug scripts -- the entries in /local/domain can be removed by
107  # Xend before the hotplug scripts have removed the entry in
108  # /local/domain/0/backend/.  In this case, we want to pretend that the
109  # VM is the same as FRONTEND_UUID, because that way the 'sharing' will be
110  # allowed.
111  local othervm=$(xenstore_read_default "/local/domain/$otherdom/vm"         \
112                  "$FRONTEND_UUID")
113  local target=$(xenstore_read_default  "/local/domain/$FRONTEND_ID/target"   \
114                 "-1")
115  local targetvm=$(xenstore_read_default "/local/domain/$target/vm" "-1")
116  local otarget=$(xenstore_read_default  "/local/domain/$otherdom/target"   \
117                 "-1")
118  local otvm=$(xenstore_read_default  "/local/domain/$otarget/vm"   \
119                 "-1")
120  otvm=${otvm%-1}
121  othervm=${othervm%-1}
122  targetvm=${targetvm%-1}
123  local frontend_uuid=${FRONTEND_UUID%-1}
124
125  [ "$frontend_uuid" = "$othervm" -o "$targetvm" = "$othervm" -o \
126    "$frontend_uuid" = "$otvm" -o "$targetvm" = "$otvm" ]
127}
128
129