Martin Wilck | 0ed1b64 | 2018-04-25 17:21:49 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # SPDX-License-Identifier: LGPL-2.1+ |
| 3 | # |
| 4 | # sd-script.py: create LOTS of sd device entries in fake sysfs |
| 5 | # |
| 6 | # (C) 2018 Martin Wilck, SUSE Linux GmbH |
| 7 | # |
| 8 | # Run after sys-script.py |
| 9 | # Usage: sd-script.py <directory> <num> |
| 10 | # <num> is the number of device nodes (disks + partititions) |
| 11 | # to create in addition to what sys-script.py already did. |
| 12 | # The script can be run several times in a row if <num> is increased, |
| 13 | # adding yet more device entries. |
| 14 | # Tested up to 1000 entries, more are possible. |
| 15 | # Note that sys-script.py already creates 10 sd device nodes |
| 16 | # (sda+sdb and partitions). This script starts with sdc. |
| 17 | |
| 18 | import re |
| 19 | import os |
| 20 | import errno |
| 21 | import sys |
| 22 | |
| 23 | def d(path, mode): |
| 24 | os.mkdir(path, mode) |
| 25 | |
| 26 | def l(path, src): |
| 27 | os.symlink(src, path) |
| 28 | |
| 29 | def f(path, mode, contents): |
| 30 | with open(path, "wb") as f: |
| 31 | f.write(contents) |
| 32 | os.chmod(path, mode) |
| 33 | |
| 34 | class SD(object): |
| 35 | |
| 36 | sd_major = [8] + list(range(65, 72)) + list(range(128, 136)) |
| 37 | _name_re = re.compile(r'sd(?P<f>[a-z]*)$') |
| 38 | |
| 39 | def _init_from_name(self, name): |
| 40 | mt = self._name_re.match(name) |
| 41 | if mt is None: |
| 42 | raise RuntimeError("invalid name %s" % name) |
| 43 | nm = mt.group("f") |
| 44 | base = 1 |
| 45 | ls = nm[-1] |
| 46 | nm = nm[:-1] |
| 47 | n = base * (ord(ls)-ord('a')) |
| 48 | while len(nm) > 0: |
| 49 | ls = nm[-1] |
| 50 | nm = nm[:-1] |
| 51 | base *= 26 |
| 52 | n += base * (1 + ord(ls)-ord('a')) |
| 53 | self._num = n |
| 54 | |
| 55 | def _init_from_dev(self, dev): |
| 56 | maj, min = dev.split(":") |
| 57 | maj = self.sd_major.index(int(maj, 10)) |
| 58 | min = int(min, 10) |
| 59 | num = int(min / 16) |
| 60 | self._num = 16*maj + num%16 + 256*int(num/16) |
| 61 | |
| 62 | @staticmethod |
| 63 | def _disk_num(a, b): |
| 64 | n = ord(a)-ord('a') |
| 65 | if b != '': |
| 66 | n = 26 * (n+1) + ord(b)-ord('a') |
| 67 | return n |
| 68 | |
| 69 | @staticmethod |
| 70 | def _get_major(n): |
| 71 | return SD.sd_major[(n%256)//16] |
| 72 | @staticmethod |
| 73 | def _get_minor(n): |
| 74 | return 16 * (n % 16 + 16 * n//256) |
| 75 | |
| 76 | @staticmethod |
| 77 | def _get_name(n): |
| 78 | # see sd_format_disk_name() (sd.c) |
| 79 | s = chr(n % 26 + ord('a')) |
| 80 | n = n // 26 - 1 |
| 81 | while n >= 0: |
| 82 | s = chr(n % 26 + ord('a')) + s |
| 83 | n = n // 26 - 1 |
| 84 | return "sd" + s |
| 85 | |
| 86 | @staticmethod |
| 87 | def _get_dev_t(n): |
| 88 | maj = SD._get_major(n) |
| 89 | min = SD._get_minor(n) |
| 90 | return (maj << 20) + min |
| 91 | |
| 92 | def __init__(self, arg): |
| 93 | if type(arg) is type(0): |
| 94 | self._num = arg |
| 95 | elif arg.startswith("sd"): |
| 96 | self._init_from_name(arg) |
| 97 | else: |
| 98 | self._init_from_dev(arg) |
| 99 | |
| 100 | def __cmp__(self, other): |
| 101 | return cmp(self._num, other._num) |
| 102 | |
| 103 | def __hash__(self): |
| 104 | return hash(self._num) |
| 105 | |
| 106 | def __str__(self): |
| 107 | return "%s/%s" % ( |
| 108 | self.devstr(), |
| 109 | self._get_name(self._num)) |
| 110 | |
| 111 | def major(self): |
| 112 | return self._get_major(self._num) |
| 113 | |
| 114 | def minor(self): |
| 115 | return self._get_minor(self._num) |
| 116 | |
| 117 | def devstr(self): |
| 118 | return "%d:%d" % (self._get_major(self._num), |
| 119 | self._get_minor(self._num)) |
| 120 | |
| 121 | def namestr(self): |
| 122 | return self._get_name(self._num) |
| 123 | |
| 124 | def longstr(self): |
| 125 | return "%d\t%s\t%s\t%08x" % (self._num, |
| 126 | self.devstr(), |
| 127 | self.namestr(), |
| 128 | self._get_dev_t(self._num)) |
| 129 | |
| 130 | class MySD(SD): |
| 131 | def subst(self, first_sg): |
| 132 | disk = { |
| 133 | "lun": self._num, |
| 134 | "major": self.major(), |
| 135 | "devnode": self.namestr(), |
| 136 | "disk_minor": self.minor(), |
| 137 | "sg_minor": first_sg + self._num, |
| 138 | } |
| 139 | return disk |
| 140 | |
| 141 | disk_template = r"""\ |
| 142 | l('sys/bus/scsi/drivers/sd/7:0:0:{lun}', '../../../../devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}') |
| 143 | l('sys/bus/scsi/devices/7:0:0:{lun}', '../../../devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}') |
| 144 | l('sys/dev/char/254:{sg_minor}', '../../devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/bsg/7:0:0:{lun}') |
| 145 | l('sys/dev/char/21:{sg_minor}', '../../devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/scsi_generic/sg{sg_minor}') |
| 146 | l('sys/class/scsi_disk/7:0:0:{lun}', '../../devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/scsi_disk/7:0:0:{lun}') |
| 147 | l('sys/class/scsi_generic/sg{sg_minor}', '../../devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/scsi_generic/sg{sg_minor}') |
| 148 | l('sys/class/bsg/7:0:0:{lun}', '../../devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/bsg/7:0:0:{lun}') |
| 149 | l('sys/class/scsi_device/7:0:0:{lun}', '../../devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/scsi_device/7:0:0:{lun}') |
| 150 | d('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}', 0o755) |
| 151 | l('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/generic', 'scsi_generic/sg{sg_minor}') |
| 152 | l('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/subsystem', '../../../../../../../../../bus/scsi') |
| 153 | l('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/driver', '../../../../../../../../../bus/scsi/drivers/sd') |
| 154 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/iodone_cnt', 0o644, b'0xc3\n') |
| 155 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/device_blocked', 0o644, b'0\n') |
| 156 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/max_sectors', 0o644, b'240\n') |
| 157 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/modalias', 0o644, b'scsi:t-0x00\n') |
| 158 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/scsi_level', 0o644, b'3\n') |
| 159 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/queue_depth', 0o644, b'1\n') |
| 160 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/rev', 0o644, b'1.00\n') |
| 161 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/type', 0o644, b'0\n') |
| 162 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/iocounterbits', 0o644, b'32\n') |
| 163 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/vendor', 0o644, b'Generic \n') |
| 164 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/state', 0o644, b'running\n') |
| 165 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/queue_type', 0o644, b'none\n') |
| 166 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/iorequest_cnt', 0o644, b'0xc3\n') |
| 167 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/evt_media_change', 0o644, b'0\n') |
| 168 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/model', 0o644, b'USB Flash Drive \n') |
| 169 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/ioerr_cnt', 0o644, b'0x2\n') |
| 170 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/uevent', 0o644, b'''DEVTYPE=scsi_device |
| 171 | DRIVER=sd |
| 172 | MODALIAS=scsi:t-0x00 |
| 173 | ''') |
| 174 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/timeout', 0o644, b'60\n') |
| 175 | d('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/scsi_disk', 0o755) |
| 176 | d('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/scsi_disk/7:0:0:{lun}', 0o755) |
| 177 | l('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/scsi_disk/7:0:0:{lun}/subsystem', '../../../../../../../../../../../class/scsi_disk') |
| 178 | l('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/scsi_disk/7:0:0:{lun}/device', '../../../7:0:0:{lun}') |
| 179 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/scsi_disk/7:0:0:{lun}/app_tag_own', 0o644, b'0\n') |
| 180 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/scsi_disk/7:0:0:{lun}/FUA', 0o644, b'0\n') |
| 181 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/scsi_disk/7:0:0:{lun}/cache_type', 0o644, b'write through\n') |
| 182 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/scsi_disk/7:0:0:{lun}/protection_type', 0o644, b'0\n') |
| 183 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/scsi_disk/7:0:0:{lun}/manage_start_stop', 0o644, b'0\n') |
| 184 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/scsi_disk/7:0:0:{lun}/allow_restart', 0o644, b'1\n') |
| 185 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/scsi_disk/7:0:0:{lun}/uevent', 0o644, b'') |
| 186 | d('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/scsi_disk/7:0:0:{lun}/power', 0o755) |
| 187 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/scsi_disk/7:0:0:{lun}/power/wakeup', 0o644, b'\n') |
| 188 | d('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/power', 0o755) |
| 189 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/power/wakeup', 0o644, b'\n') |
| 190 | d('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/scsi_generic', 0o755) |
| 191 | d('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/scsi_generic/sg{sg_minor}', 0o755) |
| 192 | l('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/scsi_generic/sg{sg_minor}/subsystem', '../../../../../../../../../../../class/scsi_generic') |
| 193 | l('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/scsi_generic/sg{sg_minor}/device', '../../../7:0:0:{lun}') |
| 194 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/scsi_generic/sg{sg_minor}/dev', 0o644, b'21:{sg_minor}\n') |
| 195 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/scsi_generic/sg{sg_minor}/uevent', 0o644, b'''MAJOR=21 |
| 196 | MINOR={sg_minor} |
| 197 | ''') |
| 198 | d('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/scsi_generic/sg{sg_minor}/power', 0o755) |
| 199 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/scsi_generic/sg{sg_minor}/power/wakeup', 0o644, b'\n') |
| 200 | d('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/bsg', 0o755) |
| 201 | d('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/bsg/7:0:0:{lun}', 0o755) |
| 202 | l('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/bsg/7:0:0:{lun}/subsystem', '../../../../../../../../../../../class/bsg') |
| 203 | l('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/bsg/7:0:0:{lun}/device', '../../../7:0:0:{lun}') |
| 204 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/bsg/7:0:0:{lun}/dev', 0o644, b'254:{sg_minor}\n') |
| 205 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/bsg/7:0:0:{lun}/uevent', 0o644, b'''MAJOR=254 |
| 206 | MINOR={sg_minor} |
| 207 | ''') |
| 208 | d('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/bsg/7:0:0:{lun}/power', 0o755) |
| 209 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/bsg/7:0:0:{lun}/power/wakeup', 0o644, b'\n') |
| 210 | d('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block', 0o755) |
| 211 | d('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/scsi_device', 0o755) |
| 212 | d('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/scsi_device/7:0:0:{lun}', 0o755) |
| 213 | l('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/scsi_device/7:0:0:{lun}/subsystem', '../../../../../../../../../../../class/scsi_device') |
| 214 | l('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/scsi_device/7:0:0:{lun}/device', '../../../7:0:0:{lun}') |
| 215 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/scsi_device/7:0:0:{lun}/uevent', 0o644, b'') |
| 216 | d('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/scsi_device/7:0:0:{lun}/power', 0o755) |
| 217 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/scsi_device/7:0:0:{lun}/power/wakeup', 0o644, b'\n') |
| 218 | l('sys/dev/block/{major}:{disk_minor}', '../../devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}') |
| 219 | l('sys/class/block/{devnode}', '../../devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}') |
| 220 | l('sys/block/{devnode}', '../devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}') |
| 221 | d('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}', 0o755) |
| 222 | l('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}/subsystem', '../../../../../../../../../../../class/block') |
| 223 | l('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}/bdi', '../../../../../../../../../../virtual/bdi/{major}:{disk_minor}') |
| 224 | l('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}/device', '../../../7:0:0:{lun}') |
| 225 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}/capability', 0o644, b'13\n') |
| 226 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}/ro', 0o644, b'0\n') |
| 227 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}/make-it-fail', 0o644, b'0\n') |
| 228 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}/size', 0o644, b'257024\n') |
| 229 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}/dev', 0o644, b'{major}:{disk_minor}\n') |
| 230 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}/range', 0o644, b'16\n') |
| 231 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}/removable', 0o644, b'1\n') |
| 232 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}/stat', 0o644, b' 117 409 2103 272 0 0 0 0 0 194 272\n') |
| 233 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}/uevent', 0o644, b'''MAJOR={major} |
| 234 | MINOR={disk_minor} |
| 235 | DEVTYPE=disk |
| 236 | DEVNAME={devnode} |
| 237 | ''') |
| 238 | d('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}/queue', 0o755) |
| 239 | l('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}/queue/bsg', '../../../bsg/7:0:0:{lun}') |
| 240 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}/queue/nr_requests', 0o644, b'128\n') |
| 241 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}/queue/nomerges', 0o644, b'0\n') |
| 242 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}/queue/scheduler', 0o644, b'noop anticipatory deadline [cfq] \n') |
| 243 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}/queue/hw_sector_size', 0o644, b'512\n') |
| 244 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}/queue/max_hw_sectors_kb', 0o644, b'120\n') |
| 245 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}/queue/read_ahead_kb', 0o644, b'128\n') |
| 246 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}/queue/max_sectors_kb', 0o644, b'120\n') |
| 247 | d('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}/queue/iosched', 0o755) |
| 248 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}/queue/iosched/slice_async_rq', 0o644, b'2\n') |
| 249 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}/queue/iosched/back_seek_max', 0o644, b'16384\n') |
| 250 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}/queue/iosched/slice_sync', 0o644, b'100\n') |
| 251 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}/queue/iosched/slice_async', 0o644, b'40\n') |
| 252 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}/queue/iosched/fifo_expire_sync', 0o644, b'125\n') |
| 253 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}/queue/iosched/slice_idle', 0o644, b'8\n') |
| 254 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}/queue/iosched/back_seek_penalty', 0o644, b'2\n') |
| 255 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}/queue/iosched/fifo_expire_async', 0o644, b'250\n') |
| 256 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}/queue/iosched/quantum', 0o644, b'4\n') |
| 257 | d('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}/power', 0o755) |
| 258 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}/power/wakeup', 0o644, b'\n') |
| 259 | """ |
| 260 | |
| 261 | part_template = r"""\ |
| 262 | l('sys/dev/block/{major}:{part_minor}', '../../devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}/{devnode}{part_num}') |
| 263 | l('sys/class/block/{devnode}{part_num}', '../../devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}/{devnode}{part_num}') |
| 264 | d('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}/{devnode}{part_num}', 0o755) |
| 265 | l('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}/{devnode}{part_num}/subsystem', '../../../../../../../../../../../../class/block') |
| 266 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}/{devnode}{part_num}/start', 0o644, b'32\n') |
| 267 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}/{devnode}{part_num}/make-it-fail', 0o644, b'0\n') |
| 268 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}/{devnode}{part_num}/size', 0o644, b'256992\n') |
| 269 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}/{devnode}{part_num}/dev', 0o644, b'{major}:{part_minor}\n') |
| 270 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}/{devnode}{part_num}/stat', 0o644, b' 109 392 1903 246 0 0 0 0 0 169 246\n') |
| 271 | f('sys/devices/pci0000:00/0000:00:1d.7/usb5/5-1/5-1:1.0/host7/target7:0:0/7:0:0:{lun}/block/{devnode}/{devnode}{part_num}/uevent', 0o644, b'''MAJOR={major} |
| 272 | MINOR={part_minor} |
| 273 | DEVTYPE=partition |
| 274 | DEVNAME={devnode}{part_num} |
| 275 | ''') |
| 276 | """ |
| 277 | |
| 278 | if len(sys.argv) != 3: |
| 279 | exit("Usage: {} <target dir> <number>".format(sys.argv[0])) |
| 280 | |
| 281 | if not os.path.isdir(sys.argv[1]): |
| 282 | exit("Target dir {} not found".format(sys.argv[1])) |
| 283 | |
| 284 | def create_part_sysfs(disk, sd, prt): |
| 285 | part = disk |
| 286 | part.update ({ |
| 287 | "part_num": prt, |
| 288 | "part_minor": disk["disk_minor"] + prt, |
| 289 | }) |
| 290 | |
| 291 | try: |
| 292 | exec(part_template.format(**part)) |
| 293 | except OSError: |
| 294 | si = sys.exc_info()[1] |
| 295 | if (si.errno == errno.EEXIST): |
| 296 | print("sysfs structures for %s%d exist" % (sd.namestr(), prt)) |
| 297 | else: |
| 298 | print("error for %s%d: %s" % (sd.namestr(), prt, si[1])) |
| 299 | raise |
| 300 | else: |
| 301 | print("sysfs structures for %s%d created" % (sd.namestr(), prt)) |
| 302 | |
| 303 | def create_disk_sysfs(dsk, first_sg, n): |
| 304 | sd = MySD(dsk) |
| 305 | disk = sd.subst(first_sg) |
| 306 | |
| 307 | try: |
| 308 | exec(disk_template.format(**disk)) |
| 309 | except OSError: |
| 310 | si = sys.exc_info()[1] |
| 311 | if (si.errno == errno.EEXIST): |
| 312 | print("sysfs structures for %s exist" % sd.namestr()) |
| 313 | elif (si.errno == errno.ENOENT): |
| 314 | print("error for %s: %s - have you run sys-script py first?" % |
| 315 | (sd.namestr(), si.strerror)) |
| 316 | return -1 |
| 317 | else: |
| 318 | print("error for %s: %s" % (sd.namestr(), si.strerror)) |
| 319 | raise |
| 320 | else: |
| 321 | print("sysfs structures for %s created" % sd.namestr()) |
| 322 | |
| 323 | n += 1 |
| 324 | if n >= last: |
| 325 | return n |
| 326 | |
| 327 | for prt in range(1, 16): |
| 328 | create_part_sysfs(disk, sd, prt) |
| 329 | n += 1 |
| 330 | if n >= last: |
| 331 | return n |
| 332 | |
| 333 | return n |
| 334 | |
| 335 | os.chdir(sys.argv[1]) |
| 336 | n = 0 |
| 337 | last = int(sys.argv[2]) |
| 338 | first_sg = 2 |
| 339 | for dsk in range(2, 1000): |
| 340 | n = create_disk_sysfs(dsk, first_sg, n) |
| 341 | if n >= last or n == -1: |
| 342 | break |