installer/modules/commons.py
bc583990
 import os
 import subprocess
4967065a
 import re
 
 PRE_INSTALL = "pre-install"
 POST_INSTALL = "post-install"
 
022959f9
 LOG_LEVEL_DESC = ["emerg", "alert", "crit", "err", "warning", "notice", "info", "debug"]
 LOG_FILE_NAME  = "/var/log/installer.log"
 SIGNATURE   = "Photon echo"
 LOG_EMERG   = 0
 LOG_ALERT   = 1
 LOG_CRIT    = 2
 LOG_ERROR   = 3
 LOG_WARNING = 4
 LOG_NOTICE  = 5
 LOG_INFO    = 6
 LOG_DEBUG   = 7
 
c3771c35
 default_partitions = [
                         {"mountpoint": "/", "size": 0, "filesystem": "ext4"},
f5c55576
                      ]
c3771c35
 
 def partition_compare(p1, p2):
     if 'mountpoint' in p1 and 'mountpoint' in p2:
         if len(p1['mountpoint']) == len(p2['mountpoint']):
             return cmp(p1['mountpoint'], p2['mountpoint'])
         return len(p1['mountpoint']) - len(p2['mountpoint'])
     return 0
 
 def partition_disk(disk, partitions):
bc583990
     partitions_data = {}
     partitions_data['disk'] = disk
c3771c35
     partitions_data['partitions'] = partitions
bc583990
     output = open(os.devnull, 'w')
 
     # Clear the disk
c3771c35
     process = subprocess.Popen(['sgdisk', '-o', '-g', disk], stdout = output)
bc583990
     retval = process.wait()
     if retval != 0:
c3771c35
         log(LOG_ERROR, "Failed clearing disk {0}".format(disk))
         return None
1ed13969
 
c3771c35
     # Partitioning the disk
     extensible_partition = None
     partitions_count = len(partitions)
85904234
     partition_number = 2
     # Add part size and grub flags
     grub_flag = ':ef02'
     part_size = '+2M'
     if os.path.isdir("/sys/firmware/efi"):
         grub_flag = ':ef00'
         part_size = '+3M'
 
c3771c35
     # Adding the bios partition
85904234
     partition_cmd = ['sgdisk', '-n 1::' + part_size]
c3771c35
     # Adding the known size partitions
     for partition in partitions:
         if partition['size'] == 0:
             # Can not have more than 1 extensible partition 
             if extensible_partition != None:
                 log(LOG_ERROR, "Can not have more than 1 extensible partition")
                 return None
             extensible_partition = partition
         else:
             partition_cmd.extend(['-n', '{}::+{}M'.format(partition_number, partition['size'])])
00a5007f
 
         partition['partition_number'] = partition_number
         prefix = ''
         if 'nvme' in disk:
             prefix = 'p'
         partition['path'] = disk + prefix + `partition_number`
f5c55576
         partition_number = partition_number + 1
c3771c35
 
     # Adding the last extendible partition
     if extensible_partition:
         partition_cmd.extend(['-n', `extensible_partition['partition_number']`])
 
     partition_cmd.extend(['-p', disk])
 
     # Run the partitioning command
1ed13969
     process = subprocess.Popen(partition_cmd, stdout = output)
bc583990
     retval = process.wait()
     if retval != 0:
c3771c35
         log(LOG_ERROR, "Faild partition disk, command: {0}". format(partition_cmd))
1ed13969
         return None
bc583990
 
85904234
     process = subprocess.Popen(['sgdisk', '-t1' + grub_flag, disk], stdout = output)
bc583990
     retval = process.wait()
     if retval != 0:
c3771c35
         log(LOG_ERROR, "Failed to setup grub partition")
1ed13969
         return None
bc583990
 
c3771c35
     # Format the filesystem
     for partition in partitions:
         if "mountpoint" in partition:
             if partition['mountpoint'] == '/':
                 partitions_data['root'] = partition['path']
85904234
                 partitions_data['root_partition_number'] = partition['partition_number']
c3771c35
             elif partition['mountpoint'] == '/boot':
                 partitions_data['boot'] = partition['path']
85904234
                 partitions_data['boot_partition_number'] = partition['partition_number']
c3771c35
                 partitions_data['bootdirectory'] = '/'
         if partition['filesystem'] == "swap":
             process = subprocess.Popen(['mkswap', partition['path']], stdout = output)
             retval = process.wait()
             if retval != 0:
                 log(LOG_ERROR, "Failed to create swap partition @ {}".format(partition['path']))
                 return None
         else:
             process = subprocess.Popen(['mkfs', '-t', partition['filesystem'], partition['path']], stdout = output)
             retval = process.wait()
             if retval != 0:
                 log(LOG_ERROR, "Failed to format {} partition @ {}".format(partition['filesystem'], partition['path']))
                 return None
 
     # Check if there is no root partition
     if not 'root' in partitions_data:
         log(LOG_ERROR, "There is no partition assigned to root '/'")
1ed13969
         return None
 
c3771c35
     if not 'boot' in partitions_data:
         partitions_data['boot'] = partitions_data['root']
85904234
         partitions_data['boot_partition_number'] = partitions_data['root_partition_number']
c3771c35
         partitions_data['bootdirectory'] = '/boot/'
 
     partitions.sort(lambda p1,p2: partition_compare(p1, p2))
85904234
 
bc583990
     return partitions_data
 
4967065a
 def replace_string_in_file(filename,  search_string,  replace_string):
     with open(filename, "r") as source:
         lines=source.readlines()
 
     with open(filename, "w") as destination:
         for line in lines:
022959f9
             destination.write(re.sub(search_string,  replace_string,  line))
 
 def log(type, message):
     command = 'systemd-cat echo \"<{}> {} : {}\"'.format(type, LOG_LEVEL_DESC[type], message)
     process = subprocess.Popen([command], shell=True)
     retval = process.wait()
     return retval
 
 def dump(type, filename):
     command = "journalctl -p {0} | grep --line-buffered \"{1}\" > {2}".format(LOG_LEVEL_DESC[type], SIGNATURE, filename)
     process = subprocess.Popen([command], shell=True)
     retval = process.wait()
34dfc7d7
     return retval
 
 def dump(filename):
     command = "journalctl | grep --line-buffered \"{0}\" > {1}".format(SIGNATURE, filename)
     process = subprocess.Popen([command], shell=True)
     retval = process.wait()    
     return retval