| 1 |
#!/bin/sh |
| 2 |
# Copyright © 2005-2009 Roger Leigh <[email protected]> |
| 3 |
# Copyright © 2009 Jan-Marek Glogowski <[email protected]> |
| 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 |
. "$SETUP_DATA_DIR/common-data" |
| 24 |
. "$SETUP_DATA_DIR/common-functions" |
| 25 |
. "$SETUP_DATA_DIR/common-config" |
| 26 |
|
| 27 |
# Mount a filesystem |
| 28 |
# $1: mount options |
| 29 |
# $2: mount device |
| 30 |
# $3: mount location |
| 31 |
do_mount() |
| 32 |
{ |
| 33 |
info "Mounting $2 on $3" |
| 34 |
|
| 35 |
if [ -h "$3" ]; then |
| 36 |
fatal "$3 is a symbolic link, not usable as a mountpoint" |
| 37 |
fi |
| 38 |
if [ ! -d "$3" ]; then |
| 39 |
mkdir -p "$3" |
| 40 |
fi |
| 41 |
if [ ! -d "$3" ]; then |
| 42 |
fatal "$3 does not exist, and could not be created" |
| 43 |
fi |
| 44 |
|
| 45 |
info "$MOUNT_VERBOSE $1 $2 $3" |
| 46 |
mount $MOUNT_VERBOSE $1 "$2" "$3" |
| 47 |
|
| 48 |
# Work around systemd insanity. |
| 49 |
# |
| 50 |
# Mark this mountpoint as private; some systems have / as a shared mountpoint. |
| 51 |
# As an example, assume /home/m/ch is the chroot directory. |
| 52 |
# schroot will mount -o bind /home/m/ch to /var/lib/schroot/mount/ch-123 |
| 53 |
# Afterwards, it will bind-mount /dev to /var/lib/schroot/mount/ch-123. |
| 54 |
# With shared mountpoints, that mount will also show up in the original |
| 55 |
# /home/m/ch. This is a problem once schroot mounted /home: the following |
| 56 |
# mount of /tmp will show up in /var/lib/schroot/mount/ch-123/tmp, |
| 57 |
# /home/m/ch/tmp and /home/m/ch/home/m/ch/tmp (!), which leads to failure |
| 58 |
# on unmounting. |
| 59 |
if [ "$(uname -s)" = "Linux" ]; then |
| 60 |
mount --make-private "$3" |
| 61 |
fi |
| 62 |
} |
| 63 |
|
| 64 |
# Unmount all filesystems under specified location |
| 65 |
# $1: mount base location |
| 66 |
do_umount_all() |
| 67 |
{ |
| 68 |
if [ -d "$1" ]; then |
| 69 |
# Note that flock is used here to prevent races reading |
| 70 |
# /proc/mounts, which on current (Linux 2.6.32) kernels is |
| 71 |
# racy. If other processes are mounting or unmounting |
| 72 |
# filesystems as we read it, we can miss mount entries due to |
| 73 |
# the file changing as we read it. This needs fixing in the |
| 74 |
# kernel, but an exclusive lock surrounding the |
| 75 |
# schroot-listmounts invocation is a partial fix. This |
| 76 |
# prevents racing when multiple schroot processes are running. |
| 77 |
# Note that this does not prevent the problem when programs |
| 78 |
# other than schroot mount and unmount filesystems (since they |
| 79 |
# don't create the lock). |
| 80 |
if [ -e /var/lock/schroot ]; then |
| 81 |
if [ ! -d /var/lock/schroot ]; then |
| 82 |
error "/var/lock/schroot is not a directory" |
| 83 |
exit 1 |
| 84 |
fi |
| 85 |
else |
| 86 |
mkdir -m 0700 /var/lock/schroot |
| 87 |
fi |
| 88 |
|
| 89 |
if [ "$(stat -c "%u %g %a" /var/lock/schroot)" != "0 0 700" ]; then |
| 90 |
error "/var/lock/schroot is not owned by root:root, or lacks 0700 permissions" |
| 91 |
exit 1 |
| 92 |
fi |
| 93 |
|
| 94 |
( flock 9 |
| 95 |
mounts="$("$LIBEXEC_DIR/schroot-listmounts" -m "$1")" |
| 96 |
anyfailed="" |
| 97 |
if [ "x$mounts" != 'x' ]; then |
| 98 |
echo "$mounts" | |
| 99 |
while read mountloc; do |
| 100 |
info "Unmounting $mountloc" |
| 101 |
umount "$mountloc" || anyfailed="yes" |
| 102 |
done |
| 103 |
|
| 104 |
[ -z "$anyfailed" ] || exit 1 |
| 105 |
fi |
| 106 |
) 9>"/var/lock/schroot/umount" |
| 107 |
else |
| 108 |
warn "Mount location $1 no longer exists; skipping unmount" |
| 109 |
fi |
| 110 |
} |
| 111 |
|
| 112 |
# Mount a filesystem union |
| 113 |
# $1: the mount location |
| 114 |
do_mount_fs_union() |
| 115 |
{ |
| 116 |
# Prepare mount options (branch config) for union type |
| 117 |
if [ -z "$CHROOT_UNION_MOUNT_OPTIONS" ]; then |
| 118 |
case $CHROOT_UNION_TYPE in |
| 119 |
unionfs) |
| 120 |
CHROOT_UNION_MOUNT_OPTIONS="dirs=${CHROOT_UNION_OVERLAY_DIRECTORY}=rw,${CHROOT_UNION_UNDERLAY_DIRECTORY}=ro" |
| 121 |
;; |
| 122 |
aufs) |
| 123 |
CHROOT_UNION_MOUNT_OPTIONS="br:${CHROOT_UNION_OVERLAY_DIRECTORY}:${CHROOT_UNION_UNDERLAY_DIRECTORY}=ro" |
| 124 |
;; |
| 125 |
overlayfs) |
| 126 |
CHROOT_UNION_MOUNT_OPTIONS="lowerdir=${CHROOT_UNION_UNDERLAY_DIRECTORY},upperdir=${CHROOT_UNION_OVERLAY_DIRECTORY}" |
| 127 |
;; |
| 128 |
overlay) |
| 129 |
mkdir -p ${CHROOT_UNION_OVERLAY_DIRECTORY}/upper ${CHROOT_UNION_OVERLAY_DIRECTORY}/work |
| 130 |
CHROOT_UNION_MOUNT_OPTIONS="lowerdir=${CHROOT_UNION_UNDERLAY_DIRECTORY},upperdir=${CHROOT_UNION_OVERLAY_DIRECTORY}/upper,workdir=${CHROOT_UNION_OVERLAY_DIRECTORY}/work" |
| 131 |
esac |
| 132 |
fi |
| 133 |
|
| 134 |
info "Using '$CHROOT_UNION_TYPE' for filesystem union" |
| 135 |
|
| 136 |
# Try mounting fs |
| 137 |
mount -t "$CHROOT_UNION_TYPE" -o "$CHROOT_UNION_MOUNT_OPTIONS" "$CHROOT_NAME" "$1" |
| 138 |
} |
| 139 |
|
| 140 |
if [ "$VERBOSE" = "verbose" ]; then |
| 141 |
MOUNT_VERBOSE="-v" |
| 142 |
# FSCK_VERBOSE="-V" |
| 143 |
fi |
| 144 |
|
| 145 |
if [ -e "$MOUNT_DIR" ]; then |
| 146 |
if [ ! -d "$MOUNT_DIR" ]; then |
| 147 |
error "$MOUNT_DIR is not a directory" |
| 148 |
exit 1 |
| 149 |
fi |
| 150 |
else |
| 151 |
mkdir -m 0755 -p "$MOUNT_DIR" |
| 152 |
fi |
| 153 |
|
| 154 |
if [ "$CHROOT_TYPE" = "directory" ] \ |
| 155 |
|| [ "$CHROOT_TYPE" = "file" ] \ |
| 156 |
|| [ "$CHROOT_TYPE" = "loopback" ] \ |
| 157 |
|| [ "$CHROOT_TYPE" = "block-device" ] \ |
| 158 |
|| [ "$CHROOT_TYPE" = "lvm-snapshot" ] \ |
| 159 |
|| [ "$CHROOT_TYPE" = "zfs-snapshot" ] \ |
| 160 |
|| [ "$CHROOT_TYPE" = "btrfs-snapshot" ]; then |
| 161 |
|
| 162 |
if [ "${CHROOT_UNION_TYPE:-none}" != "none" ]; then |
| 163 |
CREATE_UNION="yes" |
| 164 |
else |
| 165 |
CREATE_UNION="no" |
| 166 |
fi |
| 167 |
|
| 168 |
if [ $STAGE = "setup-start" ] || [ $STAGE = "setup-recover" ]; then |
| 169 |
|
| 170 |
case "$(uname -s)" in |
| 171 |
*FreeBSD) : |
| 172 |
BINDOPT="-t nullfs" |
| 173 |
;; |
| 174 |
*): |
| 175 |
BINDOPT="--bind" |
| 176 |
;; |
| 177 |
esac |
| 178 |
|
| 179 |
if [ "$CHROOT_TYPE" = "directory" ]; then |
| 180 |
CHROOT_MOUNT_OPTIONS="$BINDOPT $CHROOT_MOUNT_OPTIONS" |
| 181 |
CHROOT_MOUNT_DEVICE="$CHROOT_DIRECTORY" |
| 182 |
if [ ! -d "$CHROOT_DIRECTORY" ]; then |
| 183 |
fatal "Directory '$CHROOT_DIRECTORY' does not exist" |
| 184 |
fi |
| 185 |
elif [ "$CHROOT_TYPE" = "file" ]; then |
| 186 |
CHROOT_MOUNT_OPTIONS="$BINDOPT $CHROOT_MOUNT_OPTIONS" |
| 187 |
CHROOT_MOUNT_DEVICE="${CHROOT_FILE_UNPACK_DIR}/${SESSION_ID}" |
| 188 |
elif [ "$CHROOT_TYPE" = "block-device" ]; then |
| 189 |
if [ ! "$DEVTYPE" "$CHROOT_DEVICE" ]; then |
| 190 |
fatal "Device '$CHROOT_DEVICE' does not exist" |
| 191 |
fi |
| 192 |
elif [ "$CHROOT_TYPE" = "btrfs-snapshot" ]; then |
| 193 |
CHROOT_MOUNT_OPTIONS="$BINDOPT $CHROOT_MOUNT_OPTIONS" |
| 194 |
CHROOT_MOUNT_DEVICE="$CHROOT_BTRFS_SNAPSHOT_NAME" |
| 195 |
elif [ "$CHROOT_TYPE" = "loopback" ]; then |
| 196 |
if [ ! -f "$CHROOT_FILE" ]; then |
| 197 |
fatal "File '$CHROOT_FILE' does not exist" |
| 198 |
fi |
| 199 |
|
| 200 |
case "$(uname -s)" in |
| 201 |
*FreeBSD): |
| 202 |
LOOP_DEVICE="/dev/$(/sbin/mdconfig -a -t vnode -f "$CHROOT_FILE")" |
| 203 |
CHROOT_MOUNT_DEVICE="$LOOP_DEVICE" |
| 204 |
;; |
| 205 |
*): |
| 206 |
LOOP_DEVICE="$(/sbin/losetup -j "$CHROOT_FILE" | sed -e 's/:.*$//')" |
| 207 |
if [ -z "$LOOP_DEVICE" ]; then |
| 208 |
CHROOT_MOUNT_DEVICE="$CHROOT_FILE" |
| 209 |
CHROOT_MOUNT_OPTIONS="-o loop $CHROOT_MOUNT_OPTIONS" |
| 210 |
else |
| 211 |
CHROOT_MOUNT_DEVICE="$LOOP_DEVICE" |
| 212 |
fi |
| 213 |
;; |
| 214 |
esac |
| 215 |
elif [ "$CHROOT_TYPE" = "zfs-snapshot" ]; then |
| 216 |
CHROOT_MOUNT_OPTIONS="-t zfs $CHROOT_MOUNT_OPTIONS" |
| 217 |
if [ -n "$CHROOT_ZFS_CLONE_NAME" ]; then |
| 218 |
CHROOT_MOUNT_DEVICE="$CHROOT_ZFS_CLONE_NAME" |
| 219 |
fi |
| 220 |
fi |
| 221 |
|
| 222 |
if [ ! -d "$CHROOT_MOUNT_LOCATION" ]; then |
| 223 |
mkdir -p "$CHROOT_MOUNT_LOCATION" |
| 224 |
fi |
| 225 |
if [ ! -d "$CHROOT_MOUNT_LOCATION" ]; then |
| 226 |
fatal "$CHROOT_MOUNT_LOCATION does not exist, and could not be created" |
| 227 |
fi |
| 228 |
|
| 229 |
# If recovering, we want to remount all filesystems to ensure |
| 230 |
# a sane state. |
| 231 |
if [ $STAGE = "setup-recover" ]; then |
| 232 |
if [ "$CREATE_UNION" = "yes" ]; then |
| 233 |
do_umount_all "$CHROOT_UNION_UNDERLAY_DIRECTORY" |
| 234 |
fi |
| 235 |
do_umount_all "$CHROOT_MOUNT_LOCATION" |
| 236 |
fi |
| 237 |
|
| 238 |
if [ "$CREATE_UNION" = "yes" ]; then |
| 239 |
do_mount "$CHROOT_MOUNT_OPTIONS" "$CHROOT_MOUNT_DEVICE" "$CHROOT_UNION_UNDERLAY_DIRECTORY" |
| 240 |
do_mount_fs_union "$CHROOT_MOUNT_LOCATION" |
| 241 |
else |
| 242 |
do_mount "$CHROOT_MOUNT_OPTIONS" "$CHROOT_MOUNT_DEVICE" "$CHROOT_MOUNT_LOCATION" |
| 243 |
fi |
| 244 |
|
| 245 |
elif [ $STAGE = "setup-stop" ]; then |
| 246 |
|
| 247 |
do_umount_all "$CHROOT_MOUNT_LOCATION" |
| 248 |
# If CHROOT_TYPE is "file", _UNPACK_DIR is bindmounted |
| 249 |
# on _MOUNT_LOCATION, and thus due to #763932, currently |
| 250 |
# bindmounts-on-bindmounts show up on hurd-i386 as bindmounts on source |
| 251 |
# device i.e. _UNPACK_DIR. |
| 252 |
if [ "$CHROOT_TYPE" = "file" -a "$(uname -s)" = "GNU" ]; then |
| 253 |
do_umount_all "${CHROOT_FILE_UNPACK_DIR}/${SESSION_ID}" |
| 254 |
fi |
| 255 |
|
| 256 |
if [ "$CREATE_UNION" = "yes" ]; then |
| 257 |
do_umount_all "$CHROOT_UNION_UNDERLAY_DIRECTORY" |
| 258 |
fi |
| 259 |
|
| 260 |
# Purge mount location. |
| 261 |
# The contents of file chroots are purged separately, because |
| 262 |
# we might want to repack the contents. |
| 263 |
if echo "$CHROOT_MOUNT_LOCATION" | grep -q "^$MOUNT_DIR/"; then |
| 264 |
if [ -d "$CHROOT_MOUNT_LOCATION" ]; then |
| 265 |
rmdir "$CHROOT_MOUNT_LOCATION" |
| 266 |
fi |
| 267 |
fi |
| 268 |
|
| 269 |
fi |
| 270 |
|
| 271 |
fi |
| 272 |
|
| 273 |
# Mount filesystems from fstab for all chroot types |
| 274 |
if [ $STAGE = "setup-start" ] || [ $STAGE = "setup-recover" ]; then |
| 275 |
|
| 276 |
if [ -n "$SETUP_FSTAB" ]; then |
| 277 |
if [ -f "$SETUP_FSTAB" ]; then |
| 278 |
"$LIBEXEC_DIR/schroot-mount" $MOUNT_VERBOSE \ |
| 279 |
-f "$SETUP_FSTAB" -m "$CHROOT_PATH" |
| 280 |
else |
| 281 |
fatal "setup_fstab file '$SETUP_FSTAB' does not exist" |
| 282 |
fi |
| 283 |
fi |
| 284 |
|
| 285 |
fi |
| 286 |
|
| 287 |
if [ $STAGE = "setup-start" ] || [ $STAGE = "setup-recover" ]; then |
| 288 |
if [ "$(uname -s)" = "Linux" ]; then |
| 289 |
# If schroot was invoked from a terminal, we still want to be able to |
| 290 |
# access that terminal. lxc and systemd-nspawn achieve this by |
| 291 |
# binding it onto /dev/console; so can we. |
| 292 |
if stdin_tty="$(tty)"; then |
| 293 |
if [ ! -e "$CHROOT_PATH/dev/console" ]; then |
| 294 |
# We need something to mount onto. Ideally it would be |
| 295 |
# the correctly-numbered device node, c 5 1; but |
| 296 |
# systemd-nspawn's seccomp profile does not allow creating |
| 297 |
# that, so fall back to a regular empty file if necessary. |
| 298 |
if ! mknod -m700 "$CHROOT_PATH/dev/console" c 5 1 2>/dev/null; then |
| 299 |
touch "$CHROOT_PATH/dev/console" |
| 300 |
fi |
| 301 |
fi |
| 302 |
|
| 303 |
mount --bind "$stdin_tty" "$CHROOT_PATH/dev/console" |
| 304 |
fi |
| 305 |
fi |
| 306 |
fi |