Ejemplo n.º 1
0
def getCurrentRootPartition(disk):
    """
    Returns the current root partition

    @type  disk: string
    @param disk: path to the disk [/dev/<name>]

    @rtype: string
    @returns: current root partition or None
    """
    mountDir = mount("%s2" % disk)
    if mountDir is None:
        return None

    path = os.path.join(mountDir, "etc/yaboot.conf")
    if not os.path.exists(path):
        return None

    fd = open(path)
    content = fd.read()
    fd.close()

    rootPartition = re.findall("root = .*", content)[0].split("=")[1].strip()

    if not umount(mountDir):
        return None

    return rootPartition
Ejemplo n.º 2
0
def cpyRootfsToDisk(partition):
    """
    Copy kernel, initrd and root file system imagens to the disk

    @type  partition: string
    @param partition: new root device

    @rtype: boolean
    @returns: True if the all the files was successfully copied into partition;
              False otherwise
    """
    mountDir = mount(partition)
    if mountDir is None:
        return False

    bootDir = os.path.join(mountDir, "boot")
    if os.path.exists(bootDir):
        shutil.rmtree(bootDir, ignore_errors=True)

    shutil.copytree("/boot", bootDir)
    shutil.copy(ROOTFS_IMG, mountDir)

    if not umount(mountDir):
        return False

    return True
Ejemplo n.º 3
0
def updateYabootFile(rootdev):
    """
    Update /etc/yaboot.conf file with the new values

    @type  rootdev: string
    @param rootdev: root device

    @rtype: boolean
    @returns: True if the /etc/yaboot.conf file was successfully updated; 
              False otherwise
    """
    disk = rootdev[:-1]

    mountDir = mount("%s2" % disk)
    if mountDir is None:
        return False

    path = os.path.join(mountDir, "etc/yaboot.conf")

    initrd = glob.glob("/boot/initrd-*")[0]
    kernel = glob.glob("/boot/vmlinuz-*")[0]
    label = "powerkvm-%s" % kernel.split("-", 1)[1]

    fd = open(path)
    content = fd.read()
    fd.close()

    # get root file syste mode
    fsmode = re.findall("rootfsmode=..", content)
    if len(fsmode) > 0:
        fsmode = fsmode[0].split("=")[1]
    else:
        fsmode = "rw"

    newEntry = NEW_YABOOT_ENTRY % {"kernel": kernel, "label": label, 
                                   "initrd": initrd, "rootdev": rootdev, 
                                   "fsmode": fsmode}

    fd = open(path, "a")
    fd.write(newEntry)
    fd.close()

    # set boot-once parameter
    rc = subprocess.call("nvsetenv boot-once \"%s\" &>/dev/null" % label, shell=True)
    if rc == 1:
        return False

    if not umount(mountDir):
        return False

    return True
Ejemplo n.º 4
0
def restoreCowPartition(disk):
    """
    Restore cow partition when an error occurred during the update process

    @type  disk: string
    @param disk: path to the disk [/dev/<name>]

    @rtype: boolean
    @returns: True if the cow partition content was successfully restored; 
              False otherwise
    """
    currentRoot = getCurrentRootPartition(disk)

    mountDir = mount(currentRoot)
    if mountDir is None:
        return False

    cowDir = mount("%s5" % disk)
    if cowDir is None:
        return False

    path = os.path.join(mountDir, COW_BACKUP)
    cmd = "tar -xzp --numeric-owner -f %s -C %s" % (path, cowDir) 
    status, output = commands.getstatusoutput(cmd)
    if status != 0:
        return False

    os.remove(path)

    if (not umount(mountDir)) or (not umount(cowDir)):
        return False

    rc = subprocess.call("nvsetenv boot-once \"\" &>/dev/null", shell=True)
    if rc == 1:
        return False

    return True
Ejemplo n.º 5
0
def updateSystem(directory, partition):
    """
    Update packages in the system in directory

    @type  directory: string
    @param directory: path to the directory which contains a copy of the whole system

    @type  partition: string
    @param partition: new root device

    @rtype: boolean
    @returns: True if the system was successfully updated;
              False otherwise
    """
    kernel = glob.glob("/boot/vmlinuz-*")[0]
    version = kernel.split("-", 1)[1]

    cmd = "python %s %s %s" % (UPDATE_PKGS_SCRIPT, directory, version)
    status, output = commands.getstatusoutput(cmd)
    if status != 0:
        return False

    cleanupSystem(directory)

    mountDir = mount(partition)
    if mountDir is None:
        return False

    newrootfs = os.path.join(mountDir, NEW_ROOTFS_IMG)
    if os.path.exists(newrootfs):
        os.remove(newrootfs)

    cmd = "mksquashfs %s %s" % (directory, newrootfs)
    status, output = commands.getstatusoutput(cmd)
    if status != 0:
        return False

    shutil.rmtree(directory)

    path = os.path.dirname(directory)
    if not umount(path):
        return False

    if not umount(mountDir):
        return False

    return True