예제 #1
0
def is_mounted(volname, mpoint, mserver, mclient, mtype, user='******'):
    """Check if mount exist.

    Args:
        volname (str): Name of the volume
        mpoint (str): Mountpoint dir
        mserver (str): Server to which it is mounted to
        mclient (str): Client from which it is mounted.
        mtype (str): Mount type (glusterfs|nfs|smb|cifs)

    Kwargs:
        user (str): Super user of the node mclient

    Returns:
        bool: True if mounted and False otherwise.
    """
    # python will error on missing arg, so just checking for empty args here
    if not volname or not mpoint or not mserver or not mclient or not mtype:
        g.log.error("Missing arguments for mount.")
        return False

    if mtype == "smb":
        if mpoint == "*":
            return False
        else:
            cmd = powershell("net use %s" % mpoint)
            ret, out, err = g.run(mclient, cmd, user)
            if ret != 0:
                return False
            else:
                expected_output = ("Remote name       \\\%s\gluster-%s" %
                                   (mserver, volname))
                if expected_output in out:
                    return True
                else:
                    return False
    else:
        ret, _, _ = g.run(
            mclient, "mount | grep %s | grep %s | grep \"%s\"" %
            (volname, mpoint, mserver), user)
        if ret == 0:
            g.log.debug("Volume %s is mounted at %s:%s" %
                        (volname, mclient, mpoint))
            return True
        else:
            g.log.error("Volume %s is not mounted at %s:%s" %
                        (volname, mclient, mpoint))
            return False
예제 #2
0
def umount_volume(mclient, mpoint, mtype='', user='******'):
    """Unmounts the mountpoint.

    Args:
        mclient (str): Client from which it has to be mounted.
        mpoint (str): Mountpoint dir.

    Kwargs:
        mtype (str): Mounttype. Defaults to ''.
        user (str): Super user of the node mclient. Defaults to 'root'

    Returns:
        tuple: Tuple containing three elements (ret, out, err) as returned by
            umount command execution.
    """
    if mtype == "smb":
        cmd = "net use %s /d /Y" % mpoint
        cmd = powershell(cmd)
    else:
        cmd = ("umount %s || umount -f %s || umount -l %s" %
               (mpoint, mpoint, mpoint))
    return g.run(mclient, cmd, user=user)
예제 #3
0
def mount_volume(volname,
                 mtype,
                 mpoint,
                 mserver,
                 mclient,
                 options='',
                 smbuser=None,
                 smbpasswd=None,
                 user='******'):
    """Mount the gluster volume with specified options.

    Args:
        volname (str): Name of the volume to mount.
        mtype (str): Protocol to be used to mount.
        mpoint (str): Mountpoint dir.
        mserver (str): Server to mount.
        mclient (str): Client from which it has to be mounted.

    Kwargs:
        option (str): Options for the mount command.
        smbuser (str): SMB USERNAME. Used with mtype = 'cifs'
        smbpasswd (str): SMB PASSWD. Used with mtype = 'cifs'
        user (str): Super user of the node mclient


    Returns:
        tuple: Tuple containing three elements (ret, out, err).
            (0, '', '') if already mounted.
            (1, '', '') if setup_samba_service fails in case of smb.
            (ret, out, err) of mount commnd execution otherwise.
    """
    if is_mounted(volname, mpoint, mserver, mclient, mtype, user):
        g.log.debug("Volume %s is already mounted at %s" % (volname, mpoint))
        return (0, '', '')

    if options != '':
        options = "-o %s" % options

    if mtype == 'smb':
        if smbuser is None or smbpasswd is None:
            g.log.error("smbuser and smbpasswd to be passed as parameters "
                        "for cifs mounts")
            return (1, '', '')

        mcmd = ("net use %s \\\\%s\\gluster-%s " % (mpoint, mserver, volname) +
                " /user:%s " % (smbuser) + '"' + smbpasswd + '"')

        mcmd = powershell(mcmd)

        ret, out, err = g.run(mclient, mcmd, user=user)
        if ret != 0:
            g.log.error("net use command failed on windows client %s "
                        "failed: %s" % (mclient, err))
            return (ret, out, err)

        if out.startswith('Drive'):
            drv_ltr = out.split(' ')[1]
            g.log.info("Samba share mount success on windows client %s. "
                       "Share is : %s" % (mclient, drv_ltr))
            return (ret, drv_ltr, err)

        g.log.error("net use command successful but error in mount of samba "
                    " share for windows client %s for reason %s" %
                    (mclient, err))
        return (1, out, err)

    if mtype == 'nfs':
        if not options:
            options = "-o vers=4.1"

        elif options and 'vers' not in options:
            options = options + ",vers=4.1"

    if mserver:
        mcmd = ("mount -t %s %s %s:/%s %s" %
                (mtype, options, mserver, volname, mpoint))
    else:
        mcmd = ("mount -t %s %s %s %s" % (mtype, options, volname, mpoint))

    if mtype == 'cifs':
        if smbuser is None or smbpasswd is None:
            g.log.error("smbuser and smbpasswd to be passed as parameters "
                        "for cifs mounts")
            return (1, '', '')

        # Check if client is running rhel. If so add specific options
        cifs_options = ""
        cmd = "cat /etc/redhat-release | grep Santiago"
        ret, _, _ = g.run(mclient, cmd, user=user)
        if not ret:
            cifs_options = "sec=ntlmssp"
        mcmd = ("mount -t cifs -o username=%s,password=%s,%s "
                "\\\\\\\\%s\\\\gluster-%s %s" %
                (smbuser, smbpasswd, cifs_options, mserver, volname, mpoint))

    # Create mount dir
    _, _, _ = g.run(mclient,
                    "test -d %s || mkdir -p %s" % (mpoint, mpoint),
                    user=user)

    # Create mount
    return g.run(mclient, mcmd, user=user)