You are not logged in.

#1 2017-04-22 19:28:34

timemaster
Member
Registered: 2012-09-26
Posts: 26

rmdisk

Hi there,
I have a hard disk hot swap device on my computer and use it from time to time. When I need to disconnect a disk, I either used the manual way or a script rmdisk made by Tommy Butler in perl. That script was good for some years, but it was lacking.
So I have made a python script to property disconnect a disk based on it's original script. I am putting it here in case it could be useful to someone else.
Cheers.

here is how to use it

# rmdisk /dev/sdh
Êtes-vous sûr que vous voulez déconnecter ce disque ?

/dev/sdh
/dev/disk/by-path/pci-0000:00:1f.2-ata-6
/dev/disk/by-id/wwn-0x50014ee659e071f1
/dev/disk/by-id/ata-WDC_WD10JFCX-68N6GN0_WD-WX91A34T1732

Confirmer [Yes/No/y/n]:

You will have to translate it in your favorite language, here all otuput are in french.

#!/usr/bin/python

import sys
import os
from pathlib import Path


def main():

	if (len(sys.argv) != 2):
		print ("Vous devez spécifier le périphérique à débrancher comme premier paramètre")
		return
		
	if os.geteuid() != 0:
		print ("Vous devez rouler ce programme sous le compte root. Réessayer avec root ou sudo")
		return
		
	#print (sys.argv[1])
	devfile = Path(sys.argv[1])
	#print(devfile)
	
	#if not devfile.exists() or devfile.is_file():
	if not devfile.exists() or not devfile.is_block_device() :
		#is_file doit être à false, car le périphérique dans /dev n'est pas considéré comme file
		print ("Le périphérique spécifié n'existe pas ou n'est pas un disque")
		return
		
	#if the user specified a link like /dev/disk/by-id/WDC-xxx, resolve that to the /dev/sd*
	devfile = devfile.resolve()

	#check if the device is a disk by checking if there is an entry with it's name
	#need to check if the directory device exists, as this will skip partitions. This software only support whole disk.
	
	sysdir = Path("/sys/class/block/" + devfile.name + "/device/")
	
	if not sysdir.is_dir() :
		print ("Le périphérique n'est pas un disque")
		return
		
	print ("Êtes-vous sûr que vous voulez déconnecter ce disque ?\n")
	print (devfile) 	
		
	tmpfile = Path()
	tmpfile2 = Path()
	for root, directories, filenames in os.walk("/dev/disk/"):
		for filename in filenames: 
			tmpfile = Path(os.path.join(root,filename))
			#convert simlink to the destination path /dev/sd*
			tmpfile2 = tmpfile.resolve()
			
			#print (tmpfile)
			
			if tmpfile2 == devfile:
				#show the link to the user
				print (tmpfile)

	print ("\n")
	
	struser = input("Confirmer [Yes/No/y/n]:")
	if (struser.upper() == "Y" or struser.upper() == "YES"):
		
		tmpfile = Path(str(sysdir) + "/state")
		#print (tmpfile)
		strState = tmpfile.read_text()
		#print (strState)
		if (strState == "online\n" or strState == "running\n"):
			tmpfile.write_text("offline\n")
		
		#disconnect the drive, will delete the /sys/block/ entry for that block device
		tmpfile = Path(str(sysdir) + "/delete")
		tmpfile.write_text ("1")
		
		print ("Disque déconnecté")
		
		
	else:
		print ("Annulation")
	

if __name__ == "__main__":
		main()

Last edited by timemaster (2017-04-22 19:52:12)

Offline

#2 2017-04-22 20:29:11

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: rmdisk

Moving to Community Contributions...


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

Board footer

Powered by FluxBB