1 | #!/bin/sh |
---|
2 | # Copyright © 2005-2009 Roger Leigh <rleigh@debian.org> |
---|
3 | # Copyright © 2009 Jan-Marek Glogowski <glogow@fbihome.de> |
---|
4 | # |
---|
5 | # schroot is free software: you can redistribute it and/or modify it |
---|
6 | # under the terms of the GNU General Public License as published by |
---|
7 | # the Free Software Foundation, either version 3 of the License, or |
---|
8 | # (at your option) any later version. |
---|
9 | # |
---|
10 | # schroot is distributed in the hope that it will be useful, but |
---|
11 | # WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
13 | # General Public License for more details. |
---|
14 | # |
---|
15 | # You should have received a copy of the GNU General Public License |
---|
16 | # along with this program. If not, see |
---|
17 | # <http://www.gnu.org/licenses/>. |
---|
18 | # |
---|
19 | ##################################################################### |
---|
20 | |
---|
21 | set -e |
---|
22 | |
---|
23 | if [ -f "$CHROOT_SCRIPT_CONFIG" ]; then |
---|
24 | . "$CHROOT_SCRIPT_CONFIG" |
---|
25 | elif [ "$2" = "ok" ]; then |
---|
26 | echo "script-config file '$CHROOT_SCRIPT_CONFIG' does not exist" |
---|
27 | exit 1 |
---|
28 | fi |
---|
29 | |
---|
30 | # Mount a filesystem |
---|
31 | # $1: mount options |
---|
32 | # $2: mount device |
---|
33 | # $3: mount location |
---|
34 | do_mount() |
---|
35 | { |
---|
36 | if [ "$AUTH_VERBOSITY" = "verbose" ]; then |
---|
37 | echo "Mounting $2 on $3" |
---|
38 | fi |
---|
39 | |
---|
40 | if [ ! -d "$3" ]; then |
---|
41 | mkdir -p "$3" |
---|
42 | fi |
---|
43 | if [ ! -d "$3" ]; then |
---|
44 | echo "$3 does not exist, and could not be created" |
---|
45 | exit 1 |
---|
46 | fi |
---|
47 | |
---|
48 | mount $VERBOSE $1 "$2" "$3" |
---|
49 | } |
---|
50 | |
---|
51 | # Unmount all filesystems under specified location |
---|
52 | # $1: mount base location |
---|
53 | do_umount_all() |
---|
54 | { |
---|
55 | if [ -d "$1" ]; then |
---|
56 | mounts="$("$LIBEXEC_DIR/schroot-listmounts" -m "$1")" |
---|
57 | if [ "x$mounts" != 'x' ]; then |
---|
58 | echo "$mounts" | |
---|
59 | while read mountloc; do |
---|
60 | if [ "$AUTH_VERBOSITY" = "verbose" ]; then |
---|
61 | echo "Unmounting $mountloc" |
---|
62 | fi |
---|
63 | umount "$mountloc" || exit 1 |
---|
64 | done || exit 1 |
---|
65 | fi |
---|
66 | fi |
---|
67 | } |
---|
68 | |
---|
69 | # Mount a filesystem union |
---|
70 | # $1: the mount location |
---|
71 | do_mount_fs_union() |
---|
72 | { |
---|
73 | # Prepare mount options (branch config) for union type |
---|
74 | if [ -z "$CHROOT_UNION_MOUNT_OPTIONS" ]; then |
---|
75 | case $CHROOT_UNION_TYPE in |
---|
76 | unionfs) |
---|
77 | CHROOT_UNION_MOUNT_OPTIONS="dirs=${CHROOT_UNION_OVERLAY_DIRECTORY}=rw,${CHROOT_UNION_UNDERLAY_DIRECTORY}=ro" |
---|
78 | ;; |
---|
79 | aufs) |
---|
80 | CHROOT_UNION_MOUNT_OPTIONS="br:${CHROOT_UNION_OVERLAY_DIRECTORY}:${CHROOT_UNION_UNDERLAY_DIRECTORY}=ro" |
---|
81 | ;; |
---|
82 | esac |
---|
83 | fi |
---|
84 | |
---|
85 | if [ "$AUTH_VERBOSITY" = "verbose" ]; then |
---|
86 | echo "Using '$CHROOT_UNION_TYPE' for filesystem union" |
---|
87 | fi |
---|
88 | |
---|
89 | # Try mounting fs |
---|
90 | mount -t "$CHROOT_UNION_TYPE" -o "$CHROOT_UNION_MOUNT_OPTIONS" "$CHROOT_NAME" "$1" |
---|
91 | } |
---|
92 | |
---|
93 | if [ "$AUTH_VERBOSITY" = "verbose" ]; then |
---|
94 | VERBOSE="-v" |
---|
95 | # FSCK_VERBOSE="-V" |
---|
96 | fi |
---|
97 | |
---|
98 | if [ "$CHROOT_TYPE" = "directory" ] || [ "$CHROOT_TYPE" = "file" ] || [ "$CHROOT_TYPE" = "file" ] || [ "$CHROOT_TYPE" = "loopback" ] || [ "$CHROOT_TYPE" = "block-device" ] || [ "$CHROOT_TYPE" = "lvm-snapshot" ]; then |
---|
99 | |
---|
100 | if [ "${CHROOT_UNION_TYPE:-none}" != "none" ]; then |
---|
101 | CREATE_UNION="yes" |
---|
102 | else |
---|
103 | CREATE_UNION="no" |
---|
104 | fi |
---|
105 | |
---|
106 | if [ "$CHROOT_TYPE" = "directory" ]; then |
---|
107 | CHROOT_MOUNT_OPTIONS="--bind" |
---|
108 | CHROOT_MOUNT_DEVICE="$CHROOT_DIRECTORY" |
---|
109 | elif [ "$CHROOT_TYPE" = "file" ]; then |
---|
110 | UNPACK_LOCATION="${UNPACK_DIR}/${SESSION_ID}" |
---|
111 | CHROOT_MOUNT_OPTIONS="--bind" |
---|
112 | CHROOT_MOUNT_DEVICE="${CHROOT_FILE_UNPACK_DIR}/${SESSION_ID}" |
---|
113 | |
---|
114 | elif [ "$CHROOT_TYPE" = "loopback" ]; then |
---|
115 | LOOP_DEVICE="$(/sbin/losetup -j "$CHROOT_FILE" | sed -e 's/:.*$//')" |
---|
116 | if [ -z "$LOOP_DEVICE" ]; then |
---|
117 | CHROOT_MOUNT_DEVICE="$CHROOT_FILE" |
---|
118 | CHROOT_MOUNT_OPTIONS="${CHROOT_MOUNT_OPTIONS},loop" |
---|
119 | else |
---|
120 | CHROOT_MOUNT_DEVICE="$LOOP_DEVICE" |
---|
121 | CHROOT_MOUNT_OPTIONS="" |
---|
122 | fi |
---|
123 | fi |
---|
124 | |
---|
125 | if [ $1 = "setup-start" ] || [ $1 = "setup-recover" ]; then |
---|
126 | |
---|
127 | # fsck doesn't like being run non-interactively |
---|
128 | #/sbin/fsck $FSCK_VERBOSE -n "$CHROOT_MOUNT_DEVICE" |
---|
129 | |
---|
130 | if [ ! -d "$CHROOT_MOUNT_LOCATION" ]; then |
---|
131 | mkdir -p "$CHROOT_MOUNT_LOCATION" |
---|
132 | fi |
---|
133 | if [ ! -d "$CHROOT_MOUNT_LOCATION" ]; then |
---|
134 | echo "$CHROOT_MOUNT_LOCATION does not exist, and could not be created" |
---|
135 | exit 1 |
---|
136 | fi |
---|
137 | |
---|
138 | # If recovering, we want to remount all filesystems to ensure |
---|
139 | # a sane state. |
---|
140 | if [ $1 = "setup-recover" ]; then |
---|
141 | if [ "$CREATE_UNION" = "yes" ]; then |
---|
142 | do_umount_all "$CHROOT_UNION_UNDERLAY_DIRECTORY" |
---|
143 | fi |
---|
144 | do_umount_all "$CHROOT_MOUNT_LOCATION" |
---|
145 | fi |
---|
146 | |
---|
147 | if [ "$CREATE_UNION" = "yes" ]; then |
---|
148 | do_mount "$CHROOT_MOUNT_OPTIONS" "$CHROOT_MOUNT_DEVICE" "$CHROOT_UNION_UNDERLAY_DIRECTORY" |
---|
149 | do_mount_fs_union "$CHROOT_MOUNT_LOCATION" |
---|
150 | else |
---|
151 | do_mount "$CHROOT_MOUNT_OPTIONS" "$CHROOT_MOUNT_DEVICE" "$CHROOT_MOUNT_LOCATION" |
---|
152 | fi |
---|
153 | |
---|
154 | if [ -n "$FSTAB" ]; then |
---|
155 | if [ -f "$FSTAB" ]; then |
---|
156 | "$LIBEXEC_DIR/schroot-mount" $VERBOSE \ |
---|
157 | -f "$FSTAB" -m "$CHROOT_PATH" |
---|
158 | else |
---|
159 | echo "fstab file '$FSTAB' does not exist" |
---|
160 | exit 1 |
---|
161 | fi |
---|
162 | fi |
---|
163 | |
---|
164 | elif [ $1 = "setup-stop" ]; then |
---|
165 | |
---|
166 | do_umount_all "$CHROOT_MOUNT_LOCATION" |
---|
167 | if [ "$CREATE_UNION" = "yes" ]; then |
---|
168 | do_umount_all "$CHROOT_UNION_UNDERLAY_DIRECTORY" |
---|
169 | fi |
---|
170 | |
---|
171 | # Purge mount location. |
---|
172 | # The contents of file chroots are purged separately, because |
---|
173 | # we might want to repack the contents. |
---|
174 | if [ "$CHROOT_TYPE" != "file" ]; then |
---|
175 | if echo "$CHROOT_MOUNT_LOCATION" | grep -q "^$MOUNT_DIR/"; then |
---|
176 | if [ -d "$CHROOT_MOUNT_LOCATION" ]; then |
---|
177 | rmdir "$CHROOT_MOUNT_LOCATION" |
---|
178 | fi |
---|
179 | fi |
---|
180 | fi |
---|
181 | |
---|
182 | fi |
---|
183 | |
---|
184 | fi |
---|
185 | |
---|