def on_init(self, *args, **kwargs): bus.on("before_hello", self.on_before_hello) bus.on("before_host_init", self.on_before_host_init) bus.on("before_restart", self.on_before_restart) bus.on("before_reboot_finish", self.on_before_reboot_finish) try: system(('ntpdate', '-u', '0.amazon.pool.ntp.org')) except: pass msg_service = bus.messaging_service producer = msg_service.get_producer() producer.on("before_send", self.on_before_message_send) if not os_dist.windows_family and not __node__.get('hostname'): # Set the hostname to this instance's public hostname try: hostname_as_pubdns = int(__ec2__['hostname_as_pubdns']) except: hostname_as_pubdns = True if hostname_as_pubdns: pub_hostname = self._platform.get_public_hostname() self._logger.debug('Setting hostname to %s' % pub_hostname) system2("hostname " + pub_hostname, shell=True) if disttool.is_ubuntu(): # Ubuntu cloud-init scripts may disable root ssh login for path in ('/etc/ec2-init/ec2-config.cfg', '/etc/cloud/cloud.cfg'): if os.path.exists(path): c = None with open(path, 'r') as fp: c = fp.read() c = re.sub(re.compile(r'^disable_root[^:=]*([:=]).*', re.M), r'disable_root\1 0', c) with open(path, 'w') as fp: fp.write(c) if not linux.os.windows_family: # Add server ssh public key to authorized_keys ssh_key = self._platform.get_ssh_pub_key() if ssh_key: add_authorized_key(ssh_key) # Mount ephemeral devices # Seen on eucalyptus: # - fstab contains invalid fstype and `mount -a` fails if self._platform.name == 'eucalyptus': mtab = mount.mounts() fstab = mount.fstab() for device in self._platform.instance_store_devices: if os.path.exists(device) and device in fstab and device not in mtab: entry = fstab[device] try: mount.mount(device, entry.mpoint, '-o', entry.options) except: self._logger.warn(sys.exc_info()[1]) else: if not os_dist.windows_family: system2('mount -a', shell=True, raise_exc=False)
def _format_image(self): LOG.info("Formatting image") vol_entry = [v for v in self._mtab if v.device.startswith('/dev')][0] if vol_entry.device == '/dev/root' and not os.path.exists(vol_entry.device): vol_entry = [v for v in mount.mounts('/etc/mtab') if v.device.startswith('/dev')][0] fs = filesystem(vol_entry.fstype) # create filesystem fs.mkfs(self.devname) # set EXT3/4 options if fs.type.startswith('ext'): # max mounts before check (-1 = disable) system2(('/sbin/tune2fs', '-c', '1', self.devname)) # time based (3m = 3 month) system2(('/sbin/tune2fs', '-i', '3m', self.devname)) # set label label = fs.get_label(vol_entry.device) if label: fs.set_label(self.devname, label) LOG.debug('Image %s formatted', self.devname)
def _format_image(self): LOG.info("Formatting image") vol_entry = [v for v in self._mtab if v.device.startswith('/dev')][0] if vol_entry.device == '/dev/root' and not os.path.exists( vol_entry.device): vol_entry = [ v for v in mount.mounts('/etc/mtab') if v.device.startswith('/dev') ][0] fs = Storage.lookup_filesystem(vol_entry.fstype) # create filesystem fs.mkfs(self.devname) # set EXT3/4 options if fs.name.startswith('ext'): # max mounts before check (-1 = disable) system2(('/sbin/tune2fs', '-c', '1', self.devname)) # time based (3m = 3 month) system2(('/sbin/tune2fs', '-i', '3m', self.devname)) # set label label = fs.get_label(vol_entry.device) if label: fs.set_label(self.devname, label) LOG.debug('Image %s formatted', self.devname)
def mounted_to(self): try: self._check(fstype=False, device=True) except: return False try: return mod_mount.mounts()[self.device].mpoint except KeyError: return False
def mounts(self): skip_mpoint_re = re.compile(r'/(sys|proc|dev|selinux)') skip_fstype = ('tmpfs', 'devfs') ret = {} for m in mount.mounts(): if not (skip_mpoint_re.search(m.mpoint) or m.fstype in skip_fstype): entry = m._asdict() entry.update(self.statvfs([m.mpoint])[m.mpoint]) ret[m.mpoint] = entry return ret
def mounted_to(self): try: self._check(fstype=False, device=True) except: return False try: # mounts() resolve symlinks in MountEntry (e.g. /dev/group/lvol becomes /dev/md-N) # we need to do the same to prevent KeyError for mounted device device = os.path.realpath(self.device) return mod_mount.mounts()[device].mpoint except KeyError: return False
def make_volume(self, config, mpoint, mount_vol=False): config['type'] = 'ebs' LOG.debug('Creating ebs volume') fstype = None for v in mount.mounts('/etc/mtab'): if v.device.startswith('/dev'): fstype = v.fstype break volume = create_volume(config, fstype=filesystem(fstype)) volume.mpoint = mpoint volume.ensure(mount=True, mkfs=True) if not mount_vol: volume.umount() LOG.debug('Volume created %s' % volume.device) return volume
def statvfs(self, mpoints=None): """ :return: Information about available mounted file systems (total size \ free space). Request:: { "mpoints": [ "/mnt/dbstorage", "/media/mpoint", "/non/existing/mpoint" ] } Response:: { "/mnt/dbstorage": { "total" : 10000, "free" : 5000 }, "/media/mpoint": { "total" : 20000, "free" : 1000 }, "/non/existing/mpoint" : null } """ if not isinstance(mpoints, list): raise Exception('Argument "mpoints" should be a list of strings, ' 'not %s' % type(mpoints)) res = dict() mounts = mount.mounts() for mpoint in mpoints: try: assert mpoint in mounts mpoint_stat = os.statvfs(mpoint) res[mpoint] = dict() res[mpoint]['total'] = (mpoint_stat.f_bsize * mpoint_stat.f_blocks) / 1024 # Kb res[mpoint]['free'] = (mpoint_stat.f_bsize * mpoint_stat.f_bavail) / 1024 # Kb except: res[mpoint] = None return res
def __init__(self, volume, path=None, excludes=None): self._mtab = mount.mounts() self._volume = volume self.mpoint = '/mnt/img-mnt' self.path = path # Create rsync excludes list self.excludes = set(self.SPECIAL_DIRS) # Add special dirs self.excludes.update(excludes or ()) # Add user input self.excludes.add(self.mpoint) # Add image mount point if self.path: self.excludes.add(self.path) # Add image path # Add all mounted filesystems, except bundle volume self._excluded_mpoints = list(entry.mpoint for entry in self._mtab.list_entries() if entry.mpoint.startswith(self._volume) and entry.mpoint != self._volume) self.excludes.update(self._excluded_mpoints)
def statvfs(self, mpoints=None): if not isinstance(mpoints, list): raise Exception('Argument "mpoints" should be a list of strings, ' 'not %s' % type(mpoints)) res = dict() mounts = mount.mounts() for mpoint in mpoints: try: assert mpoint in mounts mpoint_stat = os.statvfs(mpoint) res[mpoint] = dict() res[mpoint]['total'] = (mpoint_stat.f_bsize * mpoint_stat.f_blocks) / 1024 res[mpoint]['free'] = (mpoint_stat.f_bsize * mpoint_stat.f_bavail) / 1024 except: res[mpoint] = None return res
def mounted_to(self): self._check() try: return mod_mount.mounts()[self.device].mpoint except KeyError: return False
def _device_mpoint(self, device): try: return mount.mounts()[device].mpoint except KeyError: return False
def on_init(self, *args, **kwargs): bus.on("before_hello", self.on_before_hello) bus.on("before_host_init", self.on_before_host_init) bus.on("before_restart", self.on_before_restart) bus.on("before_reboot_finish", self.on_before_reboot_finish) try: system(('ntpdate', '-u', '0.amazon.pool.ntp.org')) except: pass msg_service = bus.messaging_service producer = msg_service.get_producer() producer.on("before_send", self.on_before_message_send) if not os_dist.windows_family and not __node__.get('hostname'): # Set the hostname to this instance's public hostname try: hostname_as_pubdns = int(__ec2__['hostname_as_pubdns']) except: hostname_as_pubdns = True if hostname_as_pubdns: pub_hostname = self._platform.get_public_hostname() self._logger.debug('Setting hostname to %s' % pub_hostname) system2("hostname " + pub_hostname, shell=True) if disttool.is_ubuntu(): # Ubuntu cloud-init scripts may disable root ssh login for path in ('/etc/ec2-init/ec2-config.cfg', '/etc/cloud/cloud.cfg'): if os.path.exists(path): c = None with open(path, 'r') as fp: c = fp.read() c = re.sub( re.compile(r'^disable_root[^:=]*([:=]).*', re.M), r'disable_root\1 0', c) with open(path, 'w') as fp: fp.write(c) if not linux.os.windows_family: # Add server ssh public key to authorized_keys ssh_key = self._platform.get_ssh_pub_key() if ssh_key: add_authorized_key(ssh_key) # Mount ephemeral devices # Seen on eucalyptus: # - fstab contains invalid fstype and `mount -a` fails if self._platform.name == 'eucalyptus': mtab = mount.mounts() fstab = mount.fstab() for device in self._platform.instance_store_devices: if os.path.exists( device) and device in fstab and device not in mtab: entry = fstab[device] try: mount.mount(device, entry.mpoint, '-o', entry.options) except: self._logger.warn(sys.exc_info()[1]) else: if not os_dist.windows_family: system2('mount -a', shell=True, raise_exc=False)
pass class Win32VolumeNotFound(StorageError): def __init__(self, letter): self.letter = letter def __str__(self): return 'Windows volume with drive letter "{}" not found'.format(self.letter) class VolumeNotExistsError(StorageError): def __str__(self): return ('Volume not found: {0}. Most likely it was deleted. ' 'You can check "Regenerate storage if missing volumes" in UI ' 'to create clean storage volume with the same settings').format(self.args[0]) class OperationError(StorageError): pass RHEL_DEVICE_ORDERING_BUG = False if linux.os['release'] and linux.os['family'] == 'RedHat': # Check that system is affected by devices ordering bug # https://bugzilla.redhat.com/show_bug.cgi?id=729340 from scalarizr.linux import mount try: entry = mount.mounts()['/dev/xvde1'] RHEL_DEVICE_ORDERING_BUG = entry.mpoint == '/' except KeyError: pass
except: exc_info = sys.exc_info() LOG.warn("Failed to delete snapshot %s(%s): %s", snap.id, snap.type, exc_info[1], exc_info=exc_info) raise StorageError( "Failed to create one or more snapshots. " "Successfuly created snapshots were deleted to rollback. " "See log for detailed report about each failed snapshot" ) return tuple(r[2] for r in results) class StorageError(linux.LinuxError): pass class OperationError(StorageError): pass RHEL_DEVICE_ORDERING_BUG = False if linux.os["release"] and linux.os["family"] == "RedHat": # Check that system is affected by devices ordering bug # https://bugzilla.redhat.com/show_bug.cgi?id=729340 from scalarizr.linux import mount try: entry = mount.mounts()["/dev/xvde1"] RHEL_DEVICE_ORDERING_BUG = entry.mpoint == "/" except KeyError: pass
def on_init(self, *args, **kwargs): bus.on("before_hello", self.on_before_hello) bus.on("before_host_init", self.on_before_host_init) bus.on("before_restart", self.on_before_restart) msg_service = bus.messaging_service producer = msg_service.get_producer() producer.on("before_send", self.on_before_message_send) # Set the hostname to this instance's public hostname try: hostname_as_pubdns = int(__ec2__['hostname_as_pubdns']) except: hostname_as_pubdns = True if hostname_as_pubdns: pub_hostname = self._platform.get_public_hostname() self._logger.debug('Setting hostname to %s' % pub_hostname) system2("hostname " + pub_hostname, shell=True) if disttool.is_ubuntu(): # Ubuntu cloud-init scripts may disable root ssh login for path in ('/etc/ec2-init/ec2-config.cfg', '/etc/cloud/cloud.cfg'): if os.path.exists(path): c = None with open(path, 'r') as fp: c = fp.read() c = re.sub(re.compile(r'^disable_root[^:=]*([:=]).*', re.M), r'disable_root\1 0', c) with open(path, 'w') as fp: fp.write(c) # Add server ssh public key to authorized_keys authorized_keys_path = "/root/.ssh/authorized_keys" if os.path.exists(authorized_keys_path): c = None with open(authorized_keys_path, 'r') as fp: c = fp.read() ssh_key = self._platform.get_ssh_pub_key() idx = c.find(ssh_key) if idx == -1: if c and c[-1] != '\n': c += '\n' c += ssh_key + "\n" self._logger.debug("Add server ssh public key to authorized_keys") elif idx > 0 and c[idx-1] != '\n': c = c[0:idx] + '\n' + c[idx:] self._logger.warn('Adding new-line character before server SSH key in authorized_keys file') with open(authorized_keys_path, 'w') as fp: fp.write(c) # Mount ephemeral devices # Seen on eucalyptus: # - fstab contains invalid fstype and `mount -a` fails if self._platform.name == 'eucalyptus': mtab = mount.mounts() fstab = mount.fstab() for device in self._platform.instance_store_devices: if os.path.exists(device) and device in fstab and device not in mtab: entry = fstab[device] try: mount.mount(device, entry.mpoint, '-o', entry.options) except: self._logger.warn(sys.exc_info()[1]) else: system2('mount -a', shell=True, raise_exc=False)
'See log for detailed report about each failed snapshot') return tuple(r[2] for r in results) class StorageError(linux.LinuxError): pass class NoOpError(StorageError): pass class VolumeNotExistsError(StorageError): def __str__(self): return ('Volume not found: {0}. Most likely it was deleted. ' 'You can check "Regenerate storage if missing volumes" in UI ' 'to create clean storage volume with the same settings').format(self.args[0]) class OperationError(StorageError): pass RHEL_DEVICE_ORDERING_BUG = False if linux.os['release'] and linux.os['family'] == 'RedHat': # Check that system is affected by devices ordering bug # https://bugzilla.redhat.com/show_bug.cgi?id=729340 from scalarizr.linux import mount try: entry = mount.mounts()['/dev/xvde1'] RHEL_DEVICE_ORDERING_BUG = entry.mpoint == '/' except KeyError: pass
def devname_not_empty(f): def d(*args, **kwargs): if not args[1].devname: raise StorageError('Device name is empty.') return f(*args, **kwargs) return d RHEL_DEVICE_ORDERING_BUG = False if linux.os.redhat_family: # Check that system is affected by devices ordering bug # https://bugzilla.redhat.com/show_bug.cgi?id=729340 rootfs = [ entry for entry in mount.mounts() if entry.mpoint == '/' and entry.device.startswith('/dev') ][0] RHEL_DEVICE_ORDERING_BUG = rootfs.device.startswith('/dev/xvde') def get_system_devname(devname): if '/xvd' in devname: return devname ret = devname.replace('/sd', '/xvd') if os.path.exists( '/dev/xvda1') or RHEL_DEVICE_ORDERING_BUG else devname if RHEL_DEVICE_ORDERING_BUG: ret = ret[0:8] + chr(ord(ret[8]) + 4) + ret[9:] return ret
def on_init(self, *args, **kwargs): bus.on("before_hello", self.on_before_hello) bus.on("before_host_init", self.on_before_host_init) bus.on("before_restart", self.on_before_restart) msg_service = bus.messaging_service producer = msg_service.get_producer() producer.on("before_send", self.on_before_message_send) # Set the hostname to this instance's public hostname try: hostname_as_pubdns = int(__ec2__['hostname_as_pubdns']) except: hostname_as_pubdns = True if hostname_as_pubdns: pub_hostname = self._platform.get_public_hostname() self._logger.debug('Setting hostname to %s' % pub_hostname) system2("hostname " + pub_hostname, shell=True) if disttool.is_ubuntu(): # Ubuntu cloud-init scripts may disable root ssh login for path in ('/etc/ec2-init/ec2-config.cfg', '/etc/cloud/cloud.cfg'): if os.path.exists(path): c = None with open(path, 'r') as fp: c = fp.read() c = re.sub( re.compile(r'^disable_root[^:=]*([:=]).*', re.M), r'disable_root\1 0', c) with open(path, 'w') as fp: fp.write(c) # Add server ssh public key to authorized_keys authorized_keys_path = "/root/.ssh/authorized_keys" if os.path.exists(authorized_keys_path): c = None with open(authorized_keys_path, 'r') as fp: c = fp.read() ssh_key = self._platform.get_ssh_pub_key() idx = c.find(ssh_key) if idx == -1: if c and c[-1] != '\n': c += '\n' c += ssh_key + "\n" self._logger.debug( "Add server ssh public key to authorized_keys") elif idx > 0 and c[idx - 1] != '\n': c = c[0:idx] + '\n' + c[idx:] self._logger.warn( 'Adding new-line character before server SSH key in authorized_keys file' ) with open(authorized_keys_path, 'w') as fp: fp.write(c) # Mount ephemeral devices # Seen on eucalyptus: # - fstab contains invalid fstype and `mount -a` fails if self._platform.name == 'eucalyptus': mtab = mount.mounts() fstab = mount.fstab() for device in self._platform.instance_store_devices: if os.path.exists( device) and device in fstab and device not in mtab: entry = fstab[device] try: mount.mount(device, entry.mpoint, '-o', entry.options) except: self._logger.warn(sys.exc_info()[1]) else: system2('mount -a', shell=True, raise_exc=False)
return d def devname_not_empty(f): def d(*args, **kwargs): if not args[1].devname: raise StorageError('Device name is empty.') return f(*args, **kwargs) return d RHEL_DEVICE_ORDERING_BUG = False if linux.os.redhat_family: # Check that system is affected by devices ordering bug # https://bugzilla.redhat.com/show_bug.cgi?id=729340 rootfs = [entry for entry in mount.mounts() if entry.mpoint == '/' and entry.device.startswith('/dev')][0] RHEL_DEVICE_ORDERING_BUG = rootfs.device.startswith('/dev/xvde') def get_system_devname(devname): if '/xvd' in devname: return devname ret = devname.replace('/sd', '/xvd') if os.path.exists('/dev/xvda1') or RHEL_DEVICE_ORDERING_BUG else devname if RHEL_DEVICE_ORDERING_BUG: ret = ret[0:8] + chr(ord(ret[8])+4) + ret[9:] return ret def get_cloud_devname(devname): if '/sd' in devname: return devname ret = devname
def _device_mpoint(device): try: return mount.mounts()[device].mpoint except KeyError: return False