Пример #1
0
 def test_blkid(self, mock_subp):
     # we use shlex on blkid, so cover that it might output non-ascii
     out = (b'/dev/sda2: UUID="19ac97d5-6973-4193-9a09-2e6bbfa38262" '
            b'LABEL="\xc3\xb8foo" TYPE="ext4"').decode('utf-8')
     err = b''.decode()
     mock_subp.return_value = (out, err)
     block.blkid()
Пример #2
0
def mkfs(path, fstype, strict=False, label=None, uuid=None, force=False):
    """Make filesystem on block device with given path using given fstype and
       appropriate flags for filesystem family.

       Filesystem uuid and label can be passed in as kwargs. By default no
       label or uuid will be used. If a filesystem label is too long curtin
       will raise a ValueError if the strict flag is true or will truncate
       it to the maximum possible length.

       If a flag is not supported by a filesystem family mkfs will raise a
       ValueError if the strict flag is true or silently ignore it otherwise.

       Force can be specified to force the mkfs command to continue even if it
       finds old data or filesystems on the partition.
       """

    if path is None:
        raise ValueError("invalid block dev path '%s'" % path)
    if not os.path.exists(path):
        raise ValueError("'%s': no such file or directory" % path)

    fs_family = specific_to_family.get(fstype, fstype)
    mkfs_cmd = mkfs_commands.get(fstype)
    if not mkfs_cmd:
        raise ValueError("unsupported fs type '%s'" % fstype)

    if util.which(mkfs_cmd) is None:
        raise ValueError("need '%s' but it could not be found" % mkfs_cmd)

    cmd = [mkfs_cmd]

    # use device logical block size to ensure properly formated filesystems
    (logical_bsize, physical_bsize) = block.get_blockdev_sector_size(path)
    if logical_bsize > 512:
        lbs_str = ('size={}'.format(logical_bsize)
                   if fs_family == "xfs" else str(logical_bsize))
        cmd.extend(
            get_flag_mapping("sectorsize",
                             fs_family,
                             param=lbs_str,
                             strict=strict))

        if fs_family == 'fat':
            # mkfs.vfat doesn't calculate this right for non-512b sector size
            # lp:1569576 , d-i uses the same setting.
            cmd.extend(["-s", "1"])

    if force:
        cmd.extend(get_flag_mapping("force", fs_family, strict=strict))
    if label is not None:
        limit = label_length_limits.get(fs_family)
        if len(label) > limit:
            if strict:
                raise ValueError("length of fs label for '%s' exceeds max \
                                 allowed for fstype '%s'. max is '%s'" %
                                 (path, fstype, limit))
            else:
                label = label[:limit]
        cmd.extend(
            get_flag_mapping("label", fs_family, param=label, strict=strict))

    # If uuid is not specified, generate one and try to use it
    if uuid is None:
        uuid = str(uuid4())
    cmd.extend(get_flag_mapping("uuid", fs_family, param=uuid, strict=strict))

    if fs_family == "fat":
        fat_size = fstype.strip(string.ascii_letters)
        if fat_size in ["12", "16", "32"]:
            cmd.extend(
                get_flag_mapping("fatsize",
                                 fs_family,
                                 param=fat_size,
                                 strict=strict))

    cmd.append(path)
    util.subp(cmd, capture=True)

    # if fs_family does not support specifying uuid then use blkid to find it
    # if blkid is unable to then just return None for uuid
    if fs_family not in family_flag_mappings['uuid']:
        try:
            uuid = block.blkid()[path]['UUID']
        except Exception:
            pass

    # return uuid, may be none if it could not be specified and blkid could not
    # find it
    return uuid