- # I know this is the wrong answer, but sometimes the first
- # lvchange -a n fails for no particularly good reason, so this is
- # a pretty good workaround
- call(["/sbin/lvchange", "-a", "n", lvpath])
- rv = call(["/sbin/lvchange", "-a", "n", lvpath])
- if rv != 0:
- error()
- rv = call(["/sbin/lvchange", "-a", "ey", lvpath])
- if rv != 0:
- error()
- rv = call(["/sbin/lvremove", "--force", lvpath])
- if rv != 0:
- error()
+
+ # Rename the LV to something else so we can wipe it before reusing
+ # the space
+ while True:
+ new_lvname = "old_%s_%s" % (lvname, ''.join(random.choice(string.ascii_letters) for i in xrange(6)))
+ new_lvpath = "/dev/%s/%s" % (vg, new_lvname)
+ p = Popen(["/sbin/lvrename", lvpath, new_lvpath], stdout=PIPE, stderr=PIPE)
+ rv = p.wait()
+ if rv == 5 and 'already exists in volume group' in p.stderr.read():
+ continue
+ elif rv != 0:
+ error()
+ else:
+ break