From cb1bc47977b7062997c9b47e0a21eea5f41878f3 Mon Sep 17 00:00:00 2001 From: Artem Bityutskiy Date: Fri, 16 Nov 2012 12:31:56 +0200 Subject: [PATCH] BmapCopy.py: make blkdev optimization work for partitions as wall If we are writing to something like /dev/sdc1 instead of /dev/sdc, we should go one level up in the sysfs hierarchy to access the block device configuration files like 'scheduler'. Change-Id: I845a145197aa15513a288214f0b58c7a1da3b8cf Signed-off-by: Artem Bityutskiy --- TODO | 2 -- bmaptools/BmapCopy.py | 37 +++++++++++++++++-------------------- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/TODO b/TODO index 873c48e..cda0771 100644 --- a/TODO +++ b/TODO @@ -2,5 +2,3 @@ better. Currently we are calling it for each block. 2. In the block device optimization code - remember the original settings and reconstruct them after writing. -3. The block device optimizations will not work if it was /dev/sdc1 instead of - /dev/sdc - fix this. diff --git a/bmaptools/BmapCopy.py b/bmaptools/BmapCopy.py index b122810..d3996d1 100644 --- a/bmaptools/BmapCopy.py +++ b/bmaptools/BmapCopy.py @@ -500,35 +500,32 @@ class BmapBdevCopy(BmapCopy): sysfs_base = "/sys/dev/block/%s:%s/" \ % (os.major(st_rdev), os.minor(st_rdev)) + # Check if the 'quque' sub-directory exists. If yes, then our block + # device is entire disk. Otherwise, it is a partition, in which case we + # need to go one level up in the sysfs hierarchy. + try: + if not os.path.exists(sysfs_base + "queue"): + sysfs_base = sysfs_base + "../" + except OSError: + # Something happened when we tried to access the base directory. + # No problem, this is just an optimization. + pass + # Switch to the 'noop' I/O scheduler scheduler_path = sysfs_base + "queue/scheduler" try: - f_scheduler = open(scheduler_path, "w") - except IOError: - # If we can't find the file, no problem, this stuff is just an - # optimization. - f_scheduler = None - - if f_scheduler: - try: + with open(scheduler_path, "w") as f_scheduler: f_scheduler.write("noop") - except IOError: - pass - f_scheduler.close() + except IOError: + pass # Limit the write buffering ratio_path = sysfs_base + "bdi/max_ratio" try: - f_ratio = open(ratio_path, "w") - except IOError: - f_ratio = None - - if f_ratio: - try: + with open(ratio_path, "w") as f_ratio: f_ratio.write("1") - except IOError: - pass - f_ratio.close() + except IOError: + pass def copy(self, sync = True, verify = True): """ The same as in the base class but tunes the block device for better -- 2.7.4