def send_telemetry(self): logger.info( 'Uploading telemetry data to {0}'.format(TELEMETRY_ENDPOINT_PATH)) conf = Configuration.Configuration() conf.LoadTrainsConfig() manifest = conf.SystemManifest() headers = { 'X-iXSystems-Project': Configuration.Avatar(), 'X-iXSystems-Version': manifest.Version(), 'X-iXSystems-HostID': self.hostuuid, 'X-iXSystems-Train': conf.CurrentTrain() } manifest = { 'host_uuid': self.hostuuid, 'cpu_type': sysctl.sysctlbyname("hw.model"), 'cpu_clock': sysctl.sysctlbyname("hw.clockrate"), 'cpu_cores': sysctl.sysctlbyname("hw.ncpu"), 'hypervisor': sysctl.sysctlbyname("kern.vm_guest"), 'mem_size': sysctl.sysctlbyname("hw.physmem") } files = { name: open(os.path.join(TELEMETRY_STAGING_PATH, name), 'rb') for name in os.listdir(TELEMETRY_STAGING_PATH) } files['manifest'] = (None, json.dumps(manifest), 'application/json') try: requests.post(TELEMETRY_ENDPOINT_PATH, headers=headers, files=files) except BaseException as err: logger.error('Cannot send telemerty data: {0}'.format(str(err)))
def init_textdumps(): system('/sbin/ddb', 'script', 'kdb.enter.break=watchdog 38; textdump set; capture on') system('/sbin/ddb', 'script', 'kdb.enter.sysctl=watchdog 38; textdump set; capture on') system( '/sbin/ddb', 'script', 'kdb.enter.default=watchdog 38; textdump set; capture on; show allpcpu; bt; ps; alltrace; textdump dump; reset' ) sysctl.sysctlbyname('debug.ddb.textdump.pending', new=1) sysctl.sysctlbyname('debug.debugger_on_panic', new=1)
def sysctl(self, name): """ Tiny wrapper for sysctl module for compatibility """ sysc = sysctl.sysctlbyname(name) if sysc is not None: return sysc raise ValueError(name)
def send_telemetry(self): logger.info('Uploading telemetry data to {0}'.format(TELEMETRY_ENDPOINT_PATH)) conf = Configuration.Configuration() conf.LoadTrainsConfig() manifest = conf.SystemManifest() headers = { 'X-iXSystems-Project': Configuration.Avatar(), 'X-iXSystems-Version': manifest.Version(), 'X-iXSystems-HostID': self.hostuuid, 'X-iXSystems-Train': conf.CurrentTrain() } manifest = { 'host_uuid': self.hostuuid, 'cpu_type': sysctl.sysctlbyname("hw.model"), 'cpu_clock': sysctl.sysctlbyname("hw.clockrate"), 'cpu_cores': sysctl.sysctlbyname("hw.ncpu"), 'hypervisor': sysctl.sysctlbyname("kern.vm_guest"), 'mem_size': sysctl.sysctlbyname("hw.physmem") } files = { name: open(os.path.join(TELEMETRY_STAGING_PATH, name), 'rb') for name in os.listdir(TELEMETRY_STAGING_PATH) } files['manifest'] = (None, json.dumps(manifest), 'application/json') try: requests.post(TELEMETRY_ENDPOINT_PATH, headers=headers, files=files) except BaseException as err: logger.error('Cannot send telemerty data: {0}'.format(str(err))) # Remove the collected files shutil.rmtree(TELEMETRY_STAGING_PATH, ignore_errors=True) self.last_send = datetime.datetime.now()
def validate_system(): """ At this point, all this does is check memory size -- Corral needs 8gbytes of ram. (Which we'll check against 7gbytes, for Reasons.) It should potentially do more. """ gByte = 1024 * 1024 * 1024 min_memsize = 7 * gByte try: sys_memsize = sysctlbyname("hw.physmem") except: LogIt("Could not determine system memory size") return False if sys_memsize <= min_memsize: LogIt("System memory size ({}) is lss than minimum ({})".format( sys_memsize, min_memsize)) return False return True
def SaveSerialSettings(mount_point): # See if we booted via serial port, and, if so, update the sqlite database. # I don't like that this uses sqlite3 directly, but there is currently no # wrapping command (so I am told) to handle it. This of course will cause # terrible problems if the database changes. dbfile = "/data/freenas-v1.db" try: serial_boot = sysctl.sysctlbyname("debug.boothowto") except: # Couldn't find it, so just ignore it return if (serial_boot & 0x1000) == 0: return import sqlite3 (port, baud) = SerialConsole() try: db = sqlite3.connect(mount_point + dbfile) db.text_factor = str db.row_factor = sqlite3.Row cursor = db.cursor() sql = "UPDATE system_advanced SET adv_serial = ?" parms = (1,) if br: sql += ", adv_serialspeed = ?" parms += (br,) if port: sql += ", adv_serialport = ?" parms += (port,) LogIt("SaveSerialSettings: sql = {}, parms = {}".format(sql, parms)) cursor.execute(sql, parms) cursor.commit() cursor.close() except: LogIt("Could not save serial port settings", exc_info=True) raise
def validate_system(): """ At this point, all this does is check memory size -- Corral needs 8gbytes of ram. (Which we'll check against 7gbytes, for Reasons.) It should potentially do more. """ gByte = 1024 * 1024 * 1024 min_memsize = 7 * gByte try: sys_memsize = sysctlbyname("hw.physmem") except: LogIt("Could not determine system memory size") raise ValidationError(code=ValidationCode.MemoryTooSmall, message="Could not get memory size") if sys_memsize <= min_memsize: LogIt("System memory size ({}) is lss than minimum ({})".format( sys_memsize, min_memsize)) raise ValidationError( code=ValidationCode.MemoryTooSmall, message="System memory {} is less than minimum {}".format( sys_memsize, min_memsize)) return
def _init(dispatcher, plugin): def on_service_started(args): if args['name'] == 'devd': # devd is running, kick in DevdEventSource plugin.register_event_source('system.device', DevdEventSource) plugin.unregister_event_handler('service.started', on_service_started) plugin.register_schema_definition( 'DiskDevice', { 'type': 'object', 'properties': { 'name': { 'type': 'string' }, 'mediasize': { 'type': 'integer' }, 'description': { 'type': 'string' } } }) plugin.register_schema_definition( 'NetworkDevice', { 'type': 'object', 'properties': { 'name': { 'type': 'string' }, 'description': { 'type': 'string' } } }) plugin.register_schema_definition( 'CpuDevice', { 'type': 'object', 'properties': { 'name': { 'type': 'string' }, 'description': { 'type': 'string' } } }) plugin.register_schema_definition( 'SerialPortDevice', { 'type': 'object', 'properties': { 'name': { 'type': 'string' }, 'description': { 'type': 'string' }, 'drivername': { 'type': 'string' }, 'location': { 'type': 'string' }, 'start': { 'type': 'string' }, 'size': { 'type': 'string' }, } }) plugin.register_schema_definition( 'UsbDevice', { 'type': 'object', 'properties': { 'bus': { 'type': 'integer' }, 'address': { 'type': 'integer' }, 'manufacturer': { 'type': 'string' }, 'product': { 'type': 'string' }, 'class': { 'type': 'integer' }, 'vid': { 'type': 'integer' }, 'pid': { 'type': 'integer' } } }) plugin.register_event_source('system.device', DevdEventSource) plugin.register_provider('system.device', DeviceInfoProvider) plugin.register_provider('system.dmi', DMIDataProvider) # Set kern.hostuuid to the correct thing dmi = dispatcher.call_sync('system.dmi.get') hostuuid = q.get(dmi, 'System Information.UUID') if hostuuid: hostuuid = hostuuid.lower() logger.info('Hardware system UUID: {0}'.format(hostuuid)) if not hostuuid or any( fnmatch.fnmatch(hostuuid, p) for p in HOSTUUID_BLACKLIST): # Bad uuid. Check for a saved one hostuuid = dispatcher.configstore.get('system.hostuuid') if not hostuuid: # No SMBIOS hostuuid and no saved one hostuuid = str(uuid.uuid4()) hostid = int.from_bytes(hashlib.md5(hostuuid.encode('ascii')).digest()[:4], byteorder='little') dispatcher.configstore.set('system.hostuuid', hostuuid) sysctl.sysctlbyname('kern.hostuuid', new=hostuuid) sysctl.sysctlbyname('kern.hostid', new=hostid)
def select_frontend(self): if sysctl.sysctlbyname('debug.bootverbose') == 1: return VerboseFrontend(self) return CursesFrontend(self)
def _init(dispatcher, plugin): def on_service_started(args): if args['name'] == 'devd': # devd is running, kick in DevdEventSource plugin.register_event_source('system.device', DevdEventSource) plugin.unregister_event_handler( 'service.started', on_service_started) plugin.register_schema_definition('disk-device', { 'type': 'object', 'properties': { 'name': {'type': 'string'}, 'mediasize': {'type': 'integer'}, 'description': {'type': 'string'} } }) plugin.register_schema_definition('network-device', { 'type': 'object', 'properties': { 'name': {'type': 'string'}, 'description': {'type': 'string'} } }) plugin.register_schema_definition('cpu-device', { 'type': 'object', 'properties': { 'name': {'type': 'string'}, 'description': {'type': 'string'} } }) plugin.register_schema_definition('serial-port-device', { 'type': 'object', 'properties': { 'name': {'type': 'string'}, 'description': {'type': 'string'}, 'drivername': {'type': 'string'}, 'location': {'type': 'string'}, 'start': {'type': 'string'}, 'size': {'type': 'string'}, } }) plugin.register_schema_definition('usb-device', { 'type': 'object', 'properties': { 'bus': {'type': 'integer'}, 'address': {'type': 'integer'}, 'manufacturer': {'type': 'string'}, 'product': {'type': 'string'}, 'class': {'type': 'integer'}, 'vid': {'type': 'integer'}, 'pid': {'type': 'integer'} } }) if os.path.exists('/var/run/devd.pipe'): plugin.register_event_source('system.device', DevdEventSource) else: plugin.register_event_handler( 'service.started', on_service_started) plugin.register_provider('system.device', DeviceInfoProvider) plugin.register_provider('system.dmi', DMIDataProvider) # Set kern.hostuuid to the correct thing dmi = dispatcher.call_sync('system.dmi.get') hostuuid = q.get(dmi, 'System Information.UUID') if hostuuid: hostuuid = hostuuid.lower() logger.info('Hardware system UUID: {0}'.format(hostuuid)) if not hostuuid or any(fnmatch.fnmatch(hostuuid, p) for p in HOSTUUID_BLACKLIST): # Bad uuid. Check for a saved one hostuuid = dispatcher.configstore.get('system.hostuuid') if not hostuuid: # No SMBIOS hostuuid and no saved one hostuuid = str(uuid.uuid4()) hostid = int.from_bytes(hashlib.md5(hostuuid.encode('ascii')).digest()[:4], byteorder='little') dispatcher.configstore.set('system.hostuuid', hostuuid) sysctl.sysctlbyname('kern.hostuuid', new=hostuuid) sysctl.sysctlbyname('kern.hostid', new=hostid)
def __init__(self): self.observer = None self.lock = RLock() self.hostuuid = sysctl.sysctlbyname('kern.hostuuid')
def __init__(self): self.observer = None self.lock = RLock() self.hostuuid = sysctl.sysctlbyname('kern.hostuuid')[:-1]
def get_sysctl(name): return sysctl.sysctlbyname(name)
def init_textdumps(): system('/sbin/ddb', 'script', 'kdb.enter.break=watchdog 38; textdump set; capture on') system('/sbin/ddb', 'script', 'kdb.enter.sysctl=watchdog 38; textdump set; capture on') system('/sbin/ddb', 'script', 'kdb.enter.default=watchdog 38; textdump set; capture on; show allpcpu; bt; ps; alltrace; textdump dump; reset') sysctl.sysctlbyname('debug.ddb.textdump.pending', new=1) sysctl.sysctlbyname('debug.debugger_on_panic', new=1)
def __init__(self): self.observer = None self.last_send = datetime.datetime.now() self.lock = RLock() self.hostuuid = sysctl.sysctlbyname('kern.hostuuid')[:-1]
def InstallGrub(chroot, disks, bename, efi=False): # Tell beadm to activate the dataset, and make the grub # configuration. # To do that, we need to change some file. os.environ["PATH"] = os.environ["PATH"] + ":/usr/local/bin:/usr/local/sbin" grub_files = ["{}/usr/local/sbin/beadm".format(chroot), "{}/conf/base/etc/local/grub.d/10_ktrueos".format(chroot)] backup_data = {} for data_file in grub_files: LogIt("Backing up {}".format(data_file)) with open(data_file, "r") as f: backup_data[data_file] = [x.rstrip() for x in f] with open(data_file, "w") as f: for line in backup_data[data_file]: if line.startswith("ROOTFS="): LogIt("Setting {} -> {}".format(line, "ROOTFS={}".format(bename))) print("ROOTFS={}".format(bename), file=f) else: print(line, file=f) x = "{}/etc/local".format(chroot) cleanit = None if os.path.islink(x): # If /usr/local/etc is a symlink to /etc/local, we need to chagne it try: cleanit = os.readlink("{}/etc/local".format(chroot)) LogIt("Getting rid of {}/etc/local".format(chroot)) os.remove("{}/etc/local".format(chroot)) except: pass if not os.path.exists(x): try: os.symlink("/conf/base/etc/local", x) except: pass os.environ["GRUB_TERMINAL_OUTPUT"] = "console serial" if efi: with open("{}/conf/base/etc/local/default/grub".format(chroot), "r") as f: lines = [x.rstrip() for x in f] with open("{}/conf/base/etc/local/default/grub".format(chroot), "w") as f: LogIt("Editing default/grub") for line in lines: LogIt("\t{}".format(line)) if "GRUB_TERMINAL_OUTPUT=console" in line: line = line.replace("GRUB_TERMINAL_OUTPUT=console", "GRUB_TERMINAL_OUTPUT=gfxterm") LogIt("\t\t-> {}".format(line)) print(line, file=f) for disk_name in disks: LogIt("InstallGrub: disk={}".format(disk_name)) disk = Utils.Disk(disk_name) if disk is None: LogIt("Cannot find disk info for {}".format(disk_name)) raise InstallationError("Cannot find information for {}".format(disk_name)) if efi: sysctl.sysctlbyname("kern.geom.debugflags", old=False, new=16) sysctl.sysctlbyname("kern.geom.label.disk_ident.enable", old=False, new=0) try: RunCommand("/sbin/glabel", "label", "efibsd", "/dev/{}p1".format(disk.name)) except RunCommandException as e: LogIt("glabel got {}".format(str(e))) try: os.makedirs("{}/boot/efi".format(chroot), 0o755) except: pass LogIt("Attempting to mount /dev/{}p1 on {}/boot/efi".format(disk.name, chroot)) bsd.nmount(source="/dev/{}p1".format(disk.name), fspath="{}/boot/efi".format(chroot), fstype="msdosfs") LogIt("Attempting to run grub-install in chrooted environment") RunCommand("/usr/local/sbin/grub-install", "--efi-directory=/boot/efi", "--removable", "--target=x86_64-efi", "/dev/{}".format(disk.name), chroot=chroot) LogIt("Attempting to unmount {}/boot/efi".format(chroot)) bsd.unmount("{}/boot/efi".format(chroot)) else: RunCommand("/usr/local/sbin/grub-install", "--modules=zfs part_gpt", "/dev/{}".format(disk.name), chroot=chroot) RunCommand("/usr/local/sbin/beadm", "activate", os.path.basename(bename), chroot=chroot) RunCommand("/usr/local/sbin/grub-mkconfig", "-o", "/boot/grub/grub.cfg", chroot=chroot) # Now put the grub files back to what they should be for name, data in backup_data.items(): LogIt("Restoring {}".format(name)) with open(name, "w") as f: for line in data: print(line, file=f) if cleanit: try: p = "{}/etc/local".format(chroot) os.remove(p) os.symlink(cleanit, p) except BaseException as e: LogIt("Got exception {} while trying to clean /etc/local fixup".format(str(e)))
# The big potential conflict between the config file and the # command-line arguments are disks. If no disks were specified, then # we have to depend on the paramters in the config file. if args.disks: install_config['disks'] = args.disks if not install_config.get("disks", None): min_disk_size = install_config.get("minDiskSize", 0) max_disk_size = install_config.get("maxDiskSize", 0) diskCount = install_config.get("diskCount", 1) mirror = install_config.get("mirror", False) force_mirror = install_config.get("forceMirror", False) if force_mirror and diskCount < 2: diskCount = 2 # Let's try searching for some disks then system_disks = sysctlbyname("kern.disks") if ord(system_disks[-1]) == 0: system_disks = system_disks[:-1] possible_disks = [] for disk_name in system_disks.split(): try: validate_disk(disk_name) disk = Utils.Disk(disk_name) if min_disk_size and disk.size < min_disk_size: LogIt("Disk {} is too small".format(disk_name)) comtinue if max_disk_size and disk.size > max_disk_size: LogIt("Disk {} is too large".format(disk_name)) possible_disks.append(disk_name) except ValidationError as e: LogIt(e.message)