#!/usr/bin/python import os, os.path def st_dev_to_devicename(st_dev): '''Try to convert st_dev to a devicename by walking /sys/block. Yes, this will only work for block devices. Yes, that's intentional.''' (major,minor) = (os.major(st_dev),os.minor(st_dev)) for root, dirs, files in os.walk('/sys/block',topdown=True): if root == '/sys/block': continue if 'dev' in files: dev_f = open(root+'/dev') dev = dev_f.readline().strip() dev_f.close() else: continue if not dev.startswith('%s:' % major): del dirs[:] elif dev == "%s:%s" % (major,minor): return os.path.basename(root) return None fn = 'images/stage2.img' bootpath='/boot/upgrade' somepath = bootpath + '/' + os.path.basename(fn) st_dev = os.stat(somepath).st_dev bootdev = st_dev_to_devicename(st_dev) mtab = open("/etc/mtab") mountpoint = '' for line in mtab: (dev,mp,rest) = line.split(' ',2) if dev.endswith(bootdev): mountpoint = mp if mountpoint: print '(%s, %s)' % (bootdev,somepath.replace(mountpoint,''))