|
From: Benjamin D. <ben...@py...> - 2009-05-18 14:01:50
|
2009/5/18 Stefan Sonnenberg-Carstens <ste...@py...>:
>> As it's my first attempt at Python the code is rather scrappy and I'd
>> rather keep it under wraps for now.
> Everyone started somewhere :-)
True... :) Well, popen doesn't seem to have helped much after all...
Please don't be too harsh! :) Here's the thread function:
# drivedetails is an array in the format [device, serial_number] and
threads is a count of active threads.
def eraseThread(lock, offset):
global drivedetails
global threads
# Basic behaviour - try locking drive, try secure erase.
# Unlock drive. If secure erase failed, try wipe
# If wipe failed, stop
# Enable smart
# Fill drive
# Check smart status.
driveErased = 0
# print "Thread to erase " + drivedetails[offset][0]
# Fl.lock()
window.statusBox.buffer().append(drivedetails[offset][0] + ": trying
secure erase\n")
# Fl.unlock()
# May fail for drive already locked
# commands.getstatusoutput('hdparm --user-master u --security-set-pass
PASSWORD /dev/' + drivedetails[offset][0])
executing = subprocess.Popen ([ "/sbin/hdparm", "--user-master", "u",
"--security-set-pass", "PASSWORD", "/dev/" + drivedetails[offset][0]
], -1, None, None, None, None, None, False, False, None, None, False)
# executing.wait()
while (executing.poll() == None):
time.sleep(1)
# Attempt secure erase
# status = commands.getstatusoutput('hdparm --user-master u
--security-erase-enhanced PASSWORD /dev/' + drivedetails[offset][0])
executing = subprocess.Popen ([ "/sbin/hdparm", "--user-master", "u",
"--security-erase-enhanced", "PASSWORD", "/dev/" +
drivedetails[offset][0] ], -1, None, None, None, None, None, False,
False, None, None, False)
executing.wait()
if (executing.returncode == 0):
driveErased = 1
# Ensure drive left in predictable state
# commands.getstatusoutput('hdparm --user-master u --security-disable
PASSWORD /dev/' + drivedetails[offset][0])
executing = subprocess.Popen ([ "/sbin/hdparm", "--user-master", "u",
"--security-disable", "PASSWORD", "/dev/" + drivedetails[offset][0] ],
-1, None, None, None, None, None, False, False, None, None, False)
executing.wait()
if (driveErased == 0):
# Fl.lock()
window.statusBox.buffer().append(drivedetails[offset][0] + ": trying
overwrite\n")
# Fl.unlock()
# status = commands.getstatusoutput('nice -n 19 wipe -fkQ 2 /dev/' +
drivedetails[offset][0])
# status = subprocess.call([ "wipe", "-fkQ 2", "/dev/" +
drivedetails[offset][0] ])
executing = subprocess.Popen ([ "/usr/bin/wipe", "-fkQ", "2",
"/dev/" + drivedetails[offset][0] ], -1, None, None, None, None, None,
False, False, None, None, False)
# executing.wait()
if (executing.returncode == 0):
# Fl.lock()
window.statusBox.buffer().append(drivedetails[offset][0] + ":
enabling SMART and filling drive\n")
# Fl.unlock()
# commands.getstatusoutput('smartctl --smart=on /dev/' +
drivedetails[offset][0])
# commands.getstatusoutput('dcfldd sizeprobe=of textpattern="STRING
TO FILL DRIVE * " of=/dev/' + drivedetails[offset][0])
# health = commands.getstatusoutput('smartctl --health /dev/' +
drivedetails[offset][0])
executing = subprocess.Popen ([ "/usr/sbin/smartctl", "--smart=on",
"/dev/" + drivedetails[offset][0] ], -1, None, None, None, None, None,
False, False, None, None, False)
executing.wait()
# executing = subprocess.Popen ([ "/usr/bin/dcfldd", "sizeprobe=of",
"textpattern='STRING TO FILL DRIVE * '" , "of=/dev/" +
drivedetails[offset][0] ], -1, None, None, None, None, None, False,
False, None, None, False)
# executing.wait()
executing = subprocess.Popen ([ "/usr/sbin/smartctl", "--health",
"/dev/" + drivedetails[offset][0] ], -1, None, None, None, None, None,
False, False, None, None, False)
executing.wait()
# Fl.lock()
window.statusBox.buffer().append(drivedetails[offset][0] + ": complete\n")
if (executing.returncode == 0):
window.messagesBox.buffer().append(drivedetails[offset][0] + ":
erased successfully\n")
else:
window.messagesBox.buffer().append(drivedetails[offset][0] + ":
REPLACE DRIVE - FAILED SMART\n")
else:
window.messagesBox.buffer().append(drivedetails[offset][0] + ":
CHECK DRIVE - FAILED ERASE\n")
# Fl.unlock()
lock.acquire()
threads -= 1
lock.release()
# *** The erase button callback:
def erase_cb(widget, data):
global dance
global drivedetails
global threads
if window.driveList.nchecked() > 0:
window.messagesBox.buffer().text("")
window.statusBox.buffer().text("")
window.progressBar.activate()
window.messagesBox.activate()
window.statusBox.activate()
window.driveList.deactivate()
window.eraseButton.deactivate()
window.menu.deactivate()
threadsLck = thread.allocate_lock()
dance = 1
for loop in range(window.driveList.nitems()):
if (window.driveList.checked(loop+1) == 1):
if (thread.start_new_thread (eraseThread, (threadsLck, loop))):
threadsLck.acquire()
threads += 1
threadsLck.release()
while (threads != 0):
# time.sleep (1)
pass
# All threads done
dance = 0
window.driveList.check_none()
window.progressBar.deactivate()
window.driveList.activate()
window.eraseButton.deactivate()
window.menu.activate()
# *** The timer function:
def showNextImage_cb():
global image
global dance
global progress
global progressDirection
# Only change image if dance enabled - ie erase in progress
# Callback constantly called. Better behaviour would be to disable when
# not needed. However, system likely to be idle when not needed, so...
if dance:
image=image + 1
if image > 29:
image = 0
window.progress.image(penguin_image[image])
progress = progress + progressDirection
if (progress < 2) or (progress > 499):
progressDirection = progressDirection * -1
window.progressBar.value(progress)
window.progressBar.redraw()
window.progress.redraw()
Fl.repeat_timeout(0.02, showNextImage_cb)
# *** ... and main function:
# main
window = new_view()
drivelist()
window.show(1, sys.argv)
# Register timer for animated image
Fl.add_timeout(0.02, showNextImage_cb)
Fl.run()
I experimented with, while (executing.poll() == None): print "." in
the thread function and the terminal filled with dots, however the GUI
failed to update.
Take care,
Ben
|