For review: NAND out of space patch.

Chris Ball cjb at laptop.org
Mon Jul 21 22:21:33 EDT 2008


Hi,

Here's a small Python script that acts as a final fail-safe in the event
that the datastore is full and we can't boot because of it, by deleting
datastore files largest-first until we cross a threshold of how much
free space is "enough".  It could be incorporated into the Python init
process.  (See #7591 for more detail.)

Caveats:
   * Deleting a file from the datastore doesn't delete its entry in the
     index.  Resuming a Journal entry with no corresponding file usually
     produces a blank document in the activity being resumed.
   * This doesn't try anything outside of the datastore, such as the
     excellent suggestion of identifying unnecessary large files in the
     build that could be deleted.  We should of course try that first.

Please review.

- Chris.

#!/usr/bin/env python
#
# failsafe.py: If the NAND doesn't have enough free space, delete datastore objects 
#              until it does.  This doesn't modify the datastore's index.
# Author:      Chris Ball <cjb at laptop.org>

import os, sys, statvfs, subprocess

THRESHOLD = 1024 * 50 # 50MB
PATH = "/home/olpc/.sugar/default/datastore/store/*-*"

def main():
    # First, check to see whether we have enough free space.
    if find_freespace() < THRESHOLD:
        print "Not enough disk space."
        lines = os.popen("du -s %s" % PATH).readlines()
        filesizes = [line.split('\t') for line in lines]
        for file in filesizes:
           file[0] = int(file[0])     # size
           file[1] = file[1].rstrip() # path
        filesizes.sort()
        filelist = [file[1] for file in filesizes]
        
        while find_freespace() < THRESHOLD and len(filelist) > 0:
            delete_file(filelist.pop())

def find_freespace():
    # Determine free space on /.
    stat = os.statvfs("/")
    freebytes  = stat[statvfs.F_BSIZE] * stat[statvfs.F_BAVAIL]
    freekbytes = freebytes / 1024
    return freekbytes

def delete_file(file):
    # Delete a single file. 
    print "Deleting " + file
    try:
        os.remove(file)
    except os.error:
        print "Couldn't delete " + file
        
def reboot():
    os.popen("reboot")

main()
reboot()


-- 
Chris Ball   <cjb at laptop.org>



More information about the Devel mailing list