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
|
.\" Copyright, the authors of the Linux man-pages project
.\"
.\" SPDX-License-Identifier: Linux-man-pages-copyleft
.\"
.TH move_mount 2 2025-10-01 "Linux man-pages (unreleased)"
.SH NAME
move_mount \- move or attach mount object to filesystem
.SH LIBRARY
Standard C library
.RI ( libc ,\~ \-lc )
.SH SYNOPSIS
.nf
.BR "#include <fcntl.h>" " /* Definition of " AT_* " constants */"
.B #include <sys/mount.h>
.P
.BI "int move_mount(int " from_dirfd ", const char *" from_path ,
.BI " int " to_dirfd ", const char *" to_path ,
.BI " unsigned int " flags );
.fi
.SH DESCRIPTION
The
.BR move_mount ()
system call is part of
the suite of file-descriptor-based mount facilities in Linux.
.P
.BR move_mount ()
moves the mount object indicated by
.I from_dirfd
and
.I from_path
to the path indicated by
.I to_dirfd
and
.IR to_path .
The mount object being moved
can be an existing mount point in the current mount namespace,
or a detached mount object created by
.BR fsmount (2)
or
.BR open_tree (2)
with
.BR \%OPEN_TREE_CLONE .
.P
To access the source mount object
or the destination mount point,
no permissions are required on the object itself,
but if either pathname is supplied,
execute (search) permission is required
on all of the directories specified in
.I from_path
or
.IR to_path .
.P
The calling process must have the
.B \%CAP_SYS_ADMIN
capability in order to move or attach a mount object.
.P
As with "*at()" system calls,
.BR move_mount ()
uses the
.I from_dirfd
and
.I to_dirfd
arguments
in conjunction with the
.I from_path
and
.I to_path
arguments to determine the source and destination objects to operate on
(respectively), as follows:
.IP \[bu] 3
If the pathname given in
.RI * _path
is absolute, then
the corresponding
.RI * _dirfd
is ignored.
.IP \[bu]
If the pathname given in
.RI * _path
is relative and
the corresponding
.RI * _dirfd
is the special value
.BR \%AT_FDCWD ,
then
.RI * _path
is interpreted relative to
the current working directory
of the calling process (like
.BR open (2)).
.IP \[bu]
If the pathname given in
.RI * _path
is relative,
then it is interpreted relative to
the directory referred to by
the corresponding file descriptor
.RI * _dirfd
(rather than relative to
the current working directory
of the calling process,
as is done by
.BR open (2)
for a relative pathname).
In this case,
the corresponding
.RI * _dirfd
must be a directory
that was opened for reading
.RB ( O_RDONLY )
or using the
.B O_PATH
flag.
.IP \[bu]
If
.RI * _path
is an empty string,
and
.I flags
contains the appropriate
.BI \%MOVE_MOUNT_ * _EMPTY_PATH
flag,
then the corresponding file descriptor
.RI * _dirfd
is operated on directly.
In this case,
the corresponding
.RI * _dirfd
may refer to any type of file,
not just a directory.
.P
See
.BR openat (2)
for an explanation of why the
.RI * _dirfd
arguments are useful.
.P
.I flags
can be used to control aspects of the path lookup
for both the source and destination objects,
as well as other properties of the mount operation.
A value for
.I flags
is constructed by bitwise ORing
zero or more of the following constants:
.RS
.TP
.B MOVE_MOUNT_F_EMPTY_PATH
If
.I from_path
is an empty string, operate on the file referred to by
.I from_dirfd
(which may have been obtained from
.BR open (2),
.BR fsmount (2),
or
.BR open_tree (2)).
In this case,
.I from_dirfd
may refer to any type of file,
not just a directory.
If
.I from_dirfd
is
.BR \%AT_FDCWD ,
.BR move_mount ()
will operate on the current working directory
of the calling process.
.IP
This is the most common mechanism
used to attach detached mount objects
produced by
.BR fsmount (2)
and
.BR open_tree (2)
to a mount point.
.TP
.B MOVE_MOUNT_T_EMPTY_PATH
As with
.BR \%MOVE_MOUNT_F_EMPTY_PATH ,
except operating on
.I to_dirfd
and
.IR to_path .
.TP
.B MOVE_MOUNT_F_SYMLINKS
If
.I from_path
references a symbolic link,
then dereference it.
The default behaviour for
.BR move_mount ()
is to
.I not follow
symbolic links.
.TP
.B MOVE_MOUNT_T_SYMLINKS
As with
.BR \%MOVE_MOUNT_F_SYMLINKS ,
except operating on
.I to_dirfd
and
.IR to_path .
.TP
.B MOVE_MOUNT_F_NO_AUTOMOUNT
Do not automount the terminal ("basename") component of
.I \%from_path
if it is a directory that is an automount point.
This allows a mount object
that has an automount point at its root
to be moved
and prevents unintended triggering of an automount point.
This flag has no effect
if the automount point has already been mounted over.
.TP
.B MOVE_MOUNT_T_NO_AUTOMOUNT
As with
.BR \%MOVE_MOUNT_F_NO_AUTOMOUNT ,
except operating on
.I to_dirfd
and
.IR to_path .
This allows an automount point to be manually mounted over.
.TP
.BR MOVE_MOUNT_SET_GROUP " (since Linux 5.15)"
Add the attached private-propagation mount object indicated by
.I to_dirfd
and
.I to_path
into the mount propagation "peer group"
of the attached non-private-propagation mount object indicated by
.I from_dirfd
and
.IR from_path .
.IP
Unlike other
.BR move_mount ()
operations,
this operation does not move or attach any mount objects.
Instead, it only updates the metadata
of attached mount objects.
(Also, take careful note of
the argument order\[em]\c
the mount object being modified
by this operation is the one specified by
.I to_dirfd
and
.IR to_path .)
.IP
This makes it possible to first create a mount tree
consisting only of private mounts
and then configure the desired propagation layout afterwards.
(See the "SHARED SUBTREES" section of
.BR mount_namespaces (7)
for more information about mount propagation and peer groups.)
.TP
.BR MOVE_MOUNT_BENEATH " (since Linux 6.5)"
If the path indicated by
.I to_dirfd
and
.I to_path
is an existing mount object,
rather than attaching or moving the mount object
indicated by
.I from_dirfd
and
.I from_path
on top of the mount stack,
attach or move it beneath the current top mount
on the mount stack.
.IP
After using
.BR \%MOVE_MOUNT_BENEATH ,
it is possible to
.BR umount (2)
the top mount
in order to reveal the mount object
which was attached beneath it earlier.
This allows for the seamless (and atomic) replacement
of intricate mount trees,
which can further be used
to "upgrade" a mount tree with a newer version.
.IP
This operation has several restrictions:
.RS
.IP \[bu] 3
Mount objects cannot be attached beneath the filesystem root,
including cases where
the filesystem root was configured by
.BR chroot (2)
or
.BR pivot_root (2).
To mount beneath the filesystem root,
.BR pivot_root (2)
must be used.
.IP \[bu]
The target path indicated by
.I to_dirfd
and
.I to_path
must not be a detached mount object,
such as those produced by
.BR open_tree (2)
with
.B \%OPEN_TREE_CLONE
or
.BR fsmount (2).
.IP \[bu]
The current top mount
of the target path's mount stack
and its parent mount
must be in the calling process's mount namespace.
.IP \[bu]
The caller must have sufficient privileges
to unmount the top mount
of the target path's mount stack,
to prove they have privileges
to reveal the underlying mount.
.IP \[bu]
Mount propagation events triggered by this
.BR move_mount ()
operation
(as described in
.BR mount_namespaces (7))
are calculated based on the parent mount
of the current top mount
of the target path's mount stack.
.IP \[bu]
The target path's mount
cannot be an ancestor in the mount tree of
the source mount object.
.IP \[bu]
The source mount object
must not have any overmounts,
otherwise it would be possible to create "shadow mounts"
(i.e., two mounts mounted on the same parent mount at the same mount point).
.IP \[bu]
It is not possible to move a mount
beneath a top mount
if the parent mount
of the current top mount
propagates to the top mount itself.
Otherwise,
.B \%MOVE_MOUNT_BENEATH
would cause the mount object
to be propagated
to the top mount
from the parent mount,
defeating the purpose of using
.BR \%MOVE_MOUNT_BENEATH .
.IP \[bu]
It is not possible to move a mount
beneath a top mount
if the parent mount
of the current top mount
propagates to the mount object
being mounted beneath.
Otherwise, this would cause a similar propagation issue
to the previous point,
also defeating the purpose of using
.BR \%MOVE_MOUNT_BENEATH .
.RE
.RE
.P
If
.I from_dirfd
is a mount object file descriptor and
.BR move_mount ()
is operating on it directly,
.I from_dirfd
will remain associated with the mount object after
.BR move_mount ()
succeeds,
so you may repeatedly use
.I from_dirfd
with
.BR move_mount (2)
and/or "*at()" system calls
as many times as necessary.
.SH RETURN VALUE
On success,
.BR move_mount ()
returns 0.
On error, \-1 is returned, and
.I errno
is set to indicate the error.
.SH ERRORS
.TP
.B EACCES
Search permission is denied
for one of the directories
in the path prefix of one of
.I from_path
or
.IR to_path .
(See also
.BR path_resolution (7).)
.TP
.B EBADF
One of
.I from_dirfd
or
.I to_dirfd
is not a valid file descriptor.
.TP
.B EFAULT
One of
.I from_path
or
.I to_path
is NULL
or a pointer to a location
outside the calling process's accessible address space.
.TP
.B EINVAL
Invalid flag specified in
.IR flags .
.TP
.B EINVAL
The path indicated by
.I from_dirfd
and
.I from_path
is not a mount object.
.TP
.B EINVAL
The mount object type
of the source mount object and target inode
are not compatible
(i.e., the source is a file but the target is a directory, or vice-versa).
.TP
.B EINVAL
The source mount object or target path
are not in the calling process's mount namespace
(or an anonymous mount namespace of the calling process).
.TP
.B EINVAL
The source mount object's parent mount
has shared mount propagation,
and thus cannot be moved
(as described in
.BR mount_namespaces (7)).
.TP
.B EINVAL
The source mount has
.B MS_UNBINDABLE
child mounts
but the target path
resides on a mount tree with shared mount propagation,
which would otherwise cause the unbindable mounts to be propagated
(as described in
.BR mount_namespaces (7)).
.TP
.B EINVAL
.B \%MOVE_MOUNT_BENEATH
was attempted,
but one of the listed restrictions was violated.
.TP
.B ELOOP
Too many symbolic links encountered
when resolving one of
.I from_path
or
.IR to_path .
.TP
.B ENAMETOOLONG
One of
.I from_path
or
.I to_path
is longer than
.BR PATH_MAX .
.TP
.B ENOENT
A component of one of
.I from_path
or
.I to_path
does not exist.
.TP
.B ENOENT
One of
.I from_path
or
.I to_path
is an empty string,
but the corresponding
.BI MOVE_MOUNT_ * _EMPTY_PATH
flag is not specified in
.IR flags .
.TP
.B ENOTDIR
A component of the path prefix of one of
.I from_path
or
.I to_path
is not a directory,
or one of
.I from_path
or
.I to_path
is relative
and the corresponding
.I from_dirfd
or
.I to_dirfd
is a file descriptor referring to a file other than a directory.
.TP
.B ENOMEM
The kernel could not allocate sufficient memory to complete the operation.
.TP
.B EPERM
The calling process does not have the required
.B \%CAP_SYS_ADMIN
capability.
.SH STANDARDS
Linux.
.SH HISTORY
Linux 5.2.
.\" commit 2db154b3ea8e14b04fee23e3fdfd5e9d17fbc6ae
.\" commit 400913252d09f9cfb8cce33daee43167921fc343
glibc 2.36.
.SH EXAMPLES
.BR move_mount ()
can be used to move attached mounts like the following:
.P
.in +4n
.EX
move_mount(AT_FDCWD, "/a", AT_FDCWD, "/b", 0);
.EE
.in
.P
This would move the mount object mounted on
.I /a
to
.IR /b .
The above procedure is functionally equivalent to
the following mount operation
using
.BR mount (2):
.P
.in +4n
.EX
mount("/a", "/b", NULL, MS_MOVE, NULL);
.EE
.in
.P
.BR move_mount ()
can also be used in conjunction with file descriptors returned from
.BR open_tree (2)
or
.BR open (2):
.P
.in +4n
.EX
int fd = open_tree(AT_FDCWD, "/mnt", 0); /* open("/mnt", O_PATH); */
move_mount(fd, "", AT_FDCWD, "/mnt2", MOVE_MOUNT_F_EMPTY_PATH);
move_mount(fd, "", AT_FDCWD, "/mnt3", MOVE_MOUNT_F_EMPTY_PATH);
move_mount(fd, "", AT_FDCWD, "/mnt4", MOVE_MOUNT_F_EMPTY_PATH);
.EE
.in
.P
This would move the mount object mounted at
.I /mnt
to
.IR /mnt2 ,
then
.IR /mnt3 ,
and then
.IR /mnt4 .
.P
If the source mount object
indicated by
.I from_dirfd
and
.I from_path
is a detached mount object,
.BR move_mount ()
can be used to attach it to a mount point:
.P
.in +4n
.EX
int fsfd, mntfd;
\&
fsfd = fsopen("ext4", FSOPEN_CLOEXEC);
fsconfig(fsfd, FSCONFIG_SET_STRING, "source", "/dev/sda1", 0);
fsconfig(fsfd, FSCONFIG_SET_FLAG, "user_xattr", NULL, 0);
fsconfig(fsfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0);
mntfd = fsmount(fsfd, FSMOUNT_CLOEXEC, MOUNT_ATTR_NODEV);
move_mount(mntfd, "", AT_FDCWD, "/home", MOVE_MOUNT_F_EMPTY_PATH);
.EE
.in
.P
This would create a new filesystem configuration context for ext4,
configure it,
create a detached mount object,
and then attach it to
.IR /home .
The above procedure is functionally equivalent to
the following mount operation
using
.BR mount (2):
.P
.in +4n
.EX
mount("/dev/sda1", "/home", "ext4", MS_NODEV, "user_xattr");
.EE
.in
.P
The same operation also works with detached bind-mounts created with
.BR open_tree (2)
with
.BR OPEN_TREE_CLONE :
.P
.in +4n
.EX
int mntfd = open_tree(AT_FDCWD, "/home/cyphar", OPEN_TREE_CLONE);
move_mount(mntfd, "", AT_FDCWD, "/root", MOVE_MOUNT_F_EMPTY_PATH);
.EE
.in
.P
This would create a new bind-mount of
.I /home/cyphar
as a detached mount object,
and then attach it to
.IR /root .
The above procedure is functionally equivalent to
the following mount operation
using
.BR mount (2):
.P
.in +4n
.EX
mount("/home/cyphar", "/root", NULL, MS_BIND, NULL);
.EE
.in
.SH SEE ALSO
.BR fsconfig (2),
.BR fsmount (2),
.BR fsopen (2),
.BR fspick (2),
.BR mount (2),
.BR mount_setattr (2),
.BR open_tree (2),
.BR mount_namespaces (7)
|