How to determine what pool a drive is in?

Status
Not open for further replies.

NASbox

Guru
Joined
May 8, 2012
Messages
650
I am writing a script to facilitate the partial automation of backups, and I have a single drive pool that goes in a hot swap on ada3.

It is very easy to determine if a drive is plugged in diskinfo -s $backupdevice :
  • command fails - no drive mounted
  • get a serial number - drive mounted
Now my problem is to determine if it is part of a pool has been imported.

The only idea that I can see as a possibility is zpool import , but it doesn't have "script friendly options, so it's not the easiest thing to work with.
Code:
#>zpool import
   pool: BACKUP01
	 id: 5978781716520886925
  state: ONLINE
 action: The pool can be imported using its name or numeric identifier.
 config:

		BACKUP01									  ONLINE
		  gptid/8d86d52b-edf9-11e7-aadb-001517d3287a  ONLINE

This command takes awhile to return, but other than the delay which is a bit scary, is there any significant risk?
(Sorry I'm a real ZFS NOOB, so I want to make sure I'm not doing something stupid.)

If for some reason there was more than one pool unmounted, the script could run into trouble without manual intervention.

glabel status can be used to find the gptid
Code:
									  Name  Status  Components
[REMOVED]
gptid/8d7a9f48-edf9-11e7-aadb-001517d3287a	 N/A  ada3p1
gptid/8d86d52b-edf9-11e7-aadb-001517d3287a	 N/A  ada3p2
to match the two together, but again it's a lot of work and there are no "script friendly" options for glabel either.

Any "hints" or suggestions would be much appreciated.
 

joeschmuck

Old Man
Moderator
Joined
May 28, 2011
Messages
10,995
So I would think you are on the right track here...

1) You know the serial number of the drive you are installing.
2) Cross Reference the drive serial number to the drive ID (ada3p2).
3) Cross reference drive ID to the gptid searching the output of glabel status
4) Now take the output of zpool status and serch for the gptid's to see if it's attached to a pool.

I can see this not being terribly difficult to do but I guess I do not understand what you actually need the script to do, meaning what do you want to happen if it:
1) Finds a drive not mounted
2) Finds a drive not as part of a pool
3) Finds a drive as part of a pool
4) Finds multiple drives mounted or not

So what do you need the script to do? Do you want this script to run every 10 minutes and if there is a drive installed that it should import the pool and then commence the backup, and then remove the pool and stop any further operations until the drive has been physically removed? Well you can do this somewhat but depending on "ada3" is not a smart thing, you are better off using somethine else like maybe the drive serial number and hard code it that way, doing this allows you to place that drive in any location. And you could add as many drive serial numbers as you like to check for. You could even use these for unique backup material. Maybe you want one drive for only financial data and you can have a different drive for home photos and music. You would need to ensure that if the program was running that on the second instance that it would check to see if there is a first instance running and if so, then it would abort the second instance. Again, easy to do I think, just takes a little time.

You can do a lot but the main thing is to figure out exactly what is needed. Write each step down on how you want the program to act.

I could help you out with a few things if you need it but I'm not on the internet at home very much these days so it would be slow going.

My advice, look at several of the scripts here on the forums and on the FreeBSD forums, do a Google seach for scripts and these will provide you examples of how to use a function. This is how I look for something I am new to.

Lastly, plan it out (yea I said that already). When you build something, take it slow and do it one piece at a time.

Can you post any scripts you have already?
 

NASbox

Guru
Joined
May 8, 2012
Messages
650
So I would think you are on the right track here...

1) You know the serial number of the drive you are installing.
2) Cross Reference the drive serial number to the drive ID (ada3p2).
3) Cross reference drive ID to the gptid searching the output of glabel status
4) Now take the output of zpool status and serch for the gptid's to see if it's attached to a pool.

I can see this not being terribly difficult to do but I guess I do not understand what you actually need the script to do, meaning what do you want to happen if it:
1) Finds a drive not mounted
2) Finds a drive not as part of a pool
3) Finds a drive as part of a pool
4) Finds multiple drives mounted or not

So what do you need the script to do? Do you want this script to run every 10 minutes and if there is a drive installed that it should import the pool and then commence the backup, and then remove the pool and stop any further operations until the drive has been physically removed? Well you can do this somewhat but depending on "ada3" is not a smart thing, you are better off using somethine else like maybe the drive serial number and hard code it that way, doing this allows you to place that drive in any location. And you could add as many drive serial numbers as you like to check for. You could even use these for unique backup material. Maybe you want one drive for only financial data and you can have a different drive for home photos and music. You would need to ensure that if the program was running that on the second instance that it would check to see if there is a first instance running and if so, then it would abort the second instance. Again, easy to do I think, just takes a little time.

You can do a lot but the main thing is to figure out exactly what is needed. Write each step down on how you want the program to act.

I could help you out with a few things if you need it but I'm not on the internet at home very much these days so it would be slow going.

My advice, look at several of the scripts here on the forums and on the FreeBSD forums, do a Google seach for scripts and these will provide you examples of how to use a function. This is how I look for something I am new to.

Lastly, plan it out (yea I said that already). When you build something, take it slow and do it one piece at a time.

Can you post any scripts you have already?
Thanks for taking the time to reply. When I didn't get any other quick answers, I figured that I needed to just go with the idea that I had.

Script is for an automated backup routine to replicate snapshots to a single drive pool in a removable backup cartridge. Load the drive, type backup, and answer a couple of questions to sanity check the process, and then let the script do the rest. I've now moved on to the really hard part which is snapshot management and replication.

I could have taken the easy way and just hard coded the parameters, but I prefer to automate if I can do it without too much trouble. All I'm starting with is the device name. I get the serial, and in the process check if there is a device spinning.

I got lucky and found that zpool import with no arguments tells me if there are any pools spinning waiting to be imported. The output is exactly the same as zpool status. It turns out that one sed command will flatten out this output so that a simple grep will show which pool gptid is in. These snippets are safe to run at the command line (must be root).

Code:
   unmountedpools=$(zpool import)
   s=$(echo $unmountedpools | sed -e 's/pool: /\\npool:/g')
   backuppool=$(echo -e "$s" | grep "$gptid" | cut -d' ' -f 1 | cut -d':' -f 2)


Code:
   s=$(echo $zpstat | sed -e 's/pool: /\\npool:/g')
   backuppool=$(echo -e "$s" | grep "$gptid" | cut -d' ' -f 1 | cut -d':' -f 2)


Here's important sections of the code which as far as I can tell is working properly.
Code:
backupdevice="ada3"								  # Device ID of removable cartridge
#-------------------------------------------------------------------------------------------
#  Determine if a backup cartridge is installed
#-------------------------------------------------------------------------------------------
diskserial=$(diskinfo -s $backupdevice)
mountstatus=$?
if [ "$mountstatus" -ne "0" ];then
   echo -e "Removable backup device $backupdevice is not installed - backup aborted.\n"
   exit 0
fi
#-------------------------------------------------------------------------------------------
   #################################################################################
   #  Pool Not Mounted - Attempt to find and mount
   #################################################################################
   echo "Removable Device $backupdevice ($gptid) loaded and spinning"
   echo "Searching for a pool to mount with labeL $gptid"
   unmountedpools=$(zpool import)
   s=$(echo $unmountedpools | sed -e 's/pool: /\\npool:/g')
   backuppool=$(echo -e "$s" | grep "$gptid" | cut -d' ' -f 1 | cut -d':' -f 2)

   if [ -z "$backuppool" ];then
	  echo "No pool name found for gptid = $gptid - Backup aborted"
	  exit 1
   else
	  printdiskinfo
	  read -p "Found pool $backuppool - do you want to mount it? (y/Y to mount - other = abort)? " -n 1 -r -s
	  echo
	  if [[ $REPLY =~ ^[Yy]$ ]];then
		 zpool import $backuppool
		 rc="$?"
		 if [ "$rc" -ne "0" ];then
			echo "Attempt to mount pool $backuppool failed with RC = $rc - Backup aborted."
			exit 1
		 fi
		 echo Pool $backuppool successfully mounted - ready to perform backup to $backupdevice
	  else
		 echo "Backup aborted by user."
		 exit 2
	  fi
   fi
else
   #################################################################################
   # Pool Mounted - Attempt to find pool name
   #################################################################################
   s=$(echo $zpstat | sed -e 's/pool: /\\npool:/g')
   backuppool=$(echo -e "$s" | grep "$gptid" | cut -d' ' -f 1 | cut -d':' -f 2)
   if [ "$backuppool" = "" ];then
	  echo "Unable to find a pool with name $backuppool and label $gptid - Backup aborted"
	  exit 1
   fi
   echo "Removable Device $backupdevice is mounted and ready."
   printdiskinfo
fi
#-------------------------------------------------------------------------------------------


I've now moved on to this problem:
https://forums.freenas.org/index.php?threads/help-with-incremental-snapshots.60706/

Thanks again.
 
Last edited:
Status
Not open for further replies.
Top