Module: HrrRbMount

Includes:
Constants
Defined in:
lib/hrr_rb_mount.rb,
lib/hrr_rb_mount/version.rb,
ext/hrr_rb_mount/hrr_rb_mount.c

Overview

A wrapper around mount and umount. See mount(2) and umount(2) for details.

Examples:

require "hrr_rb_mount"

flags = HrrRbMount::NOEXEC
HrrRbMount.mount "tmpfs", "/path/to/target", "tmpfs", flags, "size=1M" # => 0
HrrRbMount.mountpoint? "/path/to/target"                               # => true
HrrRbMount.umount "/path/to/target"                                    # => 0

Defined Under Namespace

Modules: Constants

Constant Summary collapse

PROC_MOUNTINFO_PATH =

The path to /proc/self/mountinfo.

"/proc/self/mountinfo"
VERSION =
"0.3.0"

Constants included from Constants

Constants::BIND, Constants::DETACH, Constants::DIRSYNC, Constants::EXPIRE, Constants::FORCE, Constants::LAZYTIME, Constants::MANDLOCK, Constants::MOVE, Constants::NOATIME, Constants::NODEV, Constants::NODIRATIME, Constants::NOEXEC, Constants::NOFOLLOW, Constants::NOSUID, Constants::PRIVATE, Constants::RDONLY, Constants::REC, Constants::RELATIME, Constants::REMOUNT, Constants::SHARED, Constants::SILENT, Constants::SLAVE, Constants::STRICTATIME, Constants::SYNCHRONOUS, Constants::UNBINDABLE

Class Method Summary collapse

Class Method Details

.bind(source, target, mountflags = 0, data = "") ⇒ Integer

A wrapper around mount –bind source target command.

Examples:

HrrRbMount.bind "source", "target" # => 0

Parameters:

  • source (String)

    The pathname referring to a device or a pathname of a directory or file, or a dummy string.

  • target (String)

    The location (a directory or file) specified by the pathname.

  • mountflags (Integer) (defaults to: 0)

    The mount operation is performed depending on the bits specified in the mountflags.

  • data (String) (defaults to: "")

    Per filesystem options.

Returns:

  • (Integer)

    0.

Raises:

  • (Errno::EXXX)

    A SystemCallError is raised when the operation failed.



63
64
65
# File 'lib/hrr_rb_mount.rb', line 63

def self.bind source, target, mountflags=0, data=""
  mount source, target, nil, BIND | mountflags, data
end

.make_private(mountpoint) ⇒ Integer

A wrapper around mount –make-private mountpoint command.

Examples:

HrrRbMount.make_private "mountpoint" # => 0

Parameters:

  • mountpoint (String)

    The location (a directory or file) specified by the pathname.

Returns:

  • (Integer)

    0.

Raises:

  • (Errno::EXXX)

    A SystemCallError is raised when the operation failed.



114
115
116
# File 'lib/hrr_rb_mount.rb', line 114

def self.make_private mountpoint
  mount "none", mountpoint, nil, PRIVATE
end

.make_rprivate(mountpoint) ⇒ Integer

A wrapper around mount –make-rprivate mountpoint command.

Examples:

HrrRbMount.make_rprivate "mountpoint" # => 0

Parameters:

  • mountpoint (String)

    The location (a directory or file) specified by the pathname.

Returns:

  • (Integer)

    0.

Raises:

  • (Errno::EXXX)

    A SystemCallError is raised when the operation failed.



162
163
164
# File 'lib/hrr_rb_mount.rb', line 162

def self.make_rprivate mountpoint
  mount "none", mountpoint, nil, REC | PRIVATE
end

.make_rshared(mountpoint) ⇒ Integer

A wrapper around mount –make-rshared mountpoint command.

Examples:

HrrRbMount.make_rshared "mountpoint" # => 0

Parameters:

  • mountpoint (String)

    The location (a directory or file) specified by the pathname.

Returns:

  • (Integer)

    0.

Raises:

  • (Errno::EXXX)

    A SystemCallError is raised when the operation failed.



138
139
140
# File 'lib/hrr_rb_mount.rb', line 138

def self.make_rshared mountpoint
  mount "none", mountpoint, nil, REC | SHARED
end

.make_rslave(mountpoint) ⇒ Integer

A wrapper around mount –make-rslave mountpoint command.

Examples:

HrrRbMount.make_rslave "mountpoint" # => 0

Parameters:

  • mountpoint (String)

    The location (a directory or file) specified by the pathname.

Returns:

  • (Integer)

    0.

Raises:

  • (Errno::EXXX)

    A SystemCallError is raised when the operation failed.



150
151
152
# File 'lib/hrr_rb_mount.rb', line 150

def self.make_rslave mountpoint
  mount "none", mountpoint, nil, REC | SLAVE
end

.make_runbindable(mountpoint) ⇒ Integer

A wrapper around mount –make-runbindable mountpoint command.

Examples:

HrrRbMount.make_runbindable "mountpoint" # => 0

Parameters:

  • mountpoint (String)

    The location (a directory or file) specified by the pathname.

Returns:

  • (Integer)

    0.

Raises:

  • (Errno::EXXX)

    A SystemCallError is raised when the operation failed.



174
175
176
# File 'lib/hrr_rb_mount.rb', line 174

def self.make_runbindable mountpoint
  mount "none", mountpoint, nil, REC | UNBINDABLE
end

.make_shared(mountpoint) ⇒ Integer

A wrapper around mount –make-shared mountpoint command.

Examples:

HrrRbMount.make_shared "mountpoint" # => 0

Parameters:

  • mountpoint (String)

    The location (a directory or file) specified by the pathname.

Returns:

  • (Integer)

    0.

Raises:

  • (Errno::EXXX)

    A SystemCallError is raised when the operation failed.



90
91
92
# File 'lib/hrr_rb_mount.rb', line 90

def self.make_shared mountpoint
  mount "none", mountpoint, nil, SHARED
end

.make_slave(mountpoint) ⇒ Integer

A wrapper around mount –make-slave mountpoint command.

Examples:

HrrRbMount.make_slave "mountpoint" # => 0

Parameters:

  • mountpoint (String)

    The location (a directory or file) specified by the pathname.

Returns:

  • (Integer)

    0.

Raises:

  • (Errno::EXXX)

    A SystemCallError is raised when the operation failed.



102
103
104
# File 'lib/hrr_rb_mount.rb', line 102

def self.make_slave mountpoint
  mount "none", mountpoint, nil, SLAVE
end

.make_unbindable(mountpoint) ⇒ Integer

A wrapper around mount –make-unbindable mountpoint command.

Examples:

HrrRbMount.make_unbindable "mountpoint" # => 0

Parameters:

  • mountpoint (String)

    The location (a directory or file) specified by the pathname.

Returns:

  • (Integer)

    0.

Raises:

  • (Errno::EXXX)

    A SystemCallError is raised when the operation failed.



126
127
128
# File 'lib/hrr_rb_mount.rb', line 126

def self.make_unbindable mountpoint
  mount "none", mountpoint, nil, UNBINDABLE
end

.mount(source, target, filesystemtype, mountflags = 0, data = "") ⇒ Integer

A wrapper around mount system call.

Examples:

HrrRbMount.mount "source", "target", "filesystemtype", mountflags, "data" # => 0

Returns 0.

Parameters:

  • source (String)

    The pathname referring to a device or a pathname of a directory or file, or a dummy string.

  • target (String)

    The location (a directory or file) specified by the pathname.

  • filesystemtype (String)

    The filesystem type, which is supported by the kernel.

  • mountflags (Integer) (defaults to: 0)

    The mount operation is performed depending on the bits specified in the mountflags.

  • data (String) (defaults to: "")

    Per filesystem options.

Returns:

  • (Integer)

    0.

Raises:

  • (Errno::EXXX)

    A SystemCallError is raised when the operation failed.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'ext/hrr_rb_mount/hrr_rb_mount.c', line 22

VALUE
hrr_rb_mount_mount(int argc, VALUE *argv, VALUE self)
{
  const char *source, *target, *filesystemtype, *data;
  unsigned long mountflags;

  rb_check_arity(argc, 3, 5);

  source         = StringValueCStr(argv[0]);
  target         = StringValueCStr(argv[1]);
  filesystemtype = NIL_P(argv[2]) ? NULL : StringValueCStr(argv[2]);
  mountflags     = argc < 4 ? 0 : NUM2ULONG(argv[3]);
  data           = argc < 5 ? NULL : StringValueCStr(argv[4]);

  if (mount(source, target, filesystemtype, mountflags, data) < 0)
    rb_sys_fail("mount");

  return INT2FIX(0);
}

.mountpoint?(target, follow_symlinks: true) ⇒ Boolean

Returns true if the target directory or file is a mountpoint. Otherwise, returns false.

Internally, uses /proc/self/mountinfo file to detect if the target is a mountpoint.

When the file is not available, then uses stat(2). In this case, bind mount is not able to be detected.

Examples:

HrrRbMount.mountpoint? "/proc"   # => true
HrrRbMount.mountpoint? "/proc/1" # => false

Parameters:

  • target (String)

    The target path to be checked.

  • follow_symlinks (Boolean) (defaults to: true)

    Specifies whether to follow symlinks.

Returns:

  • (Boolean)

    true or false

Raises:

  • (Errno::EXXX)

    A SystemCallError is raised when the operation failed.



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/hrr_rb_mount.rb', line 193

def self.mountpoint? target, follow_symlinks: true
  return false if (! follow_symlinks) && File.symlink?(target)
  begin
    if File.exist? PROC_MOUNTINFO_PATH
      tgt_abs_path = File.realpath(target)
      File.foreach(PROC_MOUNTINFO_PATH){ |line|
        break true if line.split(" ")[4] == tgt_abs_path
      } or false
    else
      parent = File.join(target, "..")
      st, pst = File.stat(target), File.stat(parent)
      st.dev != pst.dev || st.ino == pst.ino
    end
  rescue Errno::ENOENT, Errno::ENOTDIR
    false
  end
end

.move(source, target, mountflags = 0, data = "") ⇒ Integer

A wrapper around mount –move source target command.

Examples:

HrrRbMount.move "source", "target" # => 0

Parameters:

  • source (String)

    The pathname referring to a device or a pathname of a directory or file, or a dummy string.

  • target (String)

    The location (a directory or file) specified by the pathname.

  • mountflags (Integer) (defaults to: 0)

    The umount operation is performed depending on the bits specified in the mountflags.

  • data (String) (defaults to: "")

    Per filesystem options.

Returns:

  • (Integer)

    0.

Raises:

  • (Errno::EXXX)

    A SystemCallError is raised when the operation failed.



48
49
50
# File 'lib/hrr_rb_mount.rb', line 48

def self.move source, target, mountflags=0, data=""
  mount source, target, nil, MOVE | mountflags, data
end

.rbind(source, target, mountflags = 0, data = "") ⇒ Integer

A wrapper around mount –rbind source target command.

Examples:

HrrRbMount.rbind "source", "target" # => 0

Parameters:

  • source (String)

    The pathname referring to a device or a pathname of a directory or file, or a dummy string.

  • target (String)

    The location (a directory or file) specified by the pathname.

  • mountflags (Integer) (defaults to: 0)

    The mount operation is performed depending on the bits specified in the mountflags.

  • data (String) (defaults to: "")

    Per filesystem options.

Returns:

  • (Integer)

    0.

Raises:

  • (Errno::EXXX)

    A SystemCallError is raised when the operation failed.



78
79
80
# File 'lib/hrr_rb_mount.rb', line 78

def self.rbind source, target, mountflags=0, data=""
  mount source, target, nil, BIND | REC | mountflags, data
end

.remount(mountpoint, mountflags = 0, data = "") ⇒ Integer

A wrapper around mount –remount mountpoint command.

Examples:

HrrRbMount.remount "mountpoint" # => 0

Parameters:

  • mountpoint (String)

    The location (a directory or file) specified by the pathname.

  • mountflags (Integer) (defaults to: 0)

    The umount operation is performed depending on the bits specified in the mountflags.

  • data (String) (defaults to: "")

    Per filesystem options.

Returns:

  • (Integer)

    0.

Raises:

  • (Errno::EXXX)

    A SystemCallError is raised when the operation failed.



33
34
35
# File 'lib/hrr_rb_mount.rb', line 33

def self.remount mountpoint, mountflags=0, data=""
  mount "none", mountpoint, nil, REMOUNT | mountflags, data
end

.umount(target, flags = 0) ⇒ Integer

A wrapper around umount system call.

Examples:

HrrRbMount.umount "target", flags # => 0

Returns 0.

Parameters:

  • target (String)

    The location (a directory or file) specified by the pathname.

  • flags (Integer) (defaults to: 0)

    The umount operation is performed depending on the bits specified in the flags.

Returns:

  • (Integer)

    0.

Raises:

  • (Errno::EXXX)

    A SystemCallError is raised when the operation failed.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'ext/hrr_rb_mount/hrr_rb_mount.c', line 54

VALUE
hrr_rb_mount_umount(int argc, VALUE *argv, VALUE self)
{
  const char *target;
  int flags;

  rb_check_arity(argc, 1, 2);

  target = StringValueCStr(argv[0]);
  flags  = argc < 2 ? 0 : NUM2INT(argv[1]);

  if (umount2(target, flags) < 0)
    rb_sys_fail("umount");

  return INT2FIX(0);
}