import hashlib
import struct
import base64


def volume_label_hash( s ):
    """
    Linux volume labels are typically limited to 12 or 16 characters while the strings we want to
    use for them are longer, usually a namespaced role name with additional data at the end. This
    hash function returns a 12-character string that is reasonably representative of the input
    string.

    >>> volume_label_hash( 'hannes_spark-master__0' )
    'i0u77fnocoo'
    >>> volume_label_hash( '' )
    'PZ2FQWP48Ho'
    >>> volume_label_hash( ' ' )
    'oIf03JUELnY'
    >>> volume_label_hash( '1' )
    'yQYSos_Mpxk'
    """
    h = hashlib.md5( s )
    h = h.digest( )
    assert len( h ) == 16
    hi, lo = struct.unpack( '!QQ', h )
    h = hi ^ lo
    h = struct.pack( '!Q', h )
    assert len( h ) == 8
    h = base64.urlsafe_b64encode( h )
    assert h[ -1 ] == '='
    return h[ :-1 ]