# what does it mean "to poke a disk"?



## fluca1978 (Apr 3, 2012)

This could sound trivial, but in a book (guess which one) I found a sample script that, before mounting a USB stick, was poking the drive using a command like the following:

```
sleep 2
/bin/true > /dev/da1
```

Now, I thought that redirecting stdout to a whole drive was a bad idea, but after having a look at the very simple code of true(1) I understand it does nothing with its stdout so nothing is really happening. Therefore, as I understand the above, the trick is to "simulate" a write to the disk with nothing to write just to wait that the device is ready, If that is true, than the initial sleep(1) should not be required, since the redirection will "wait" until the drive is ready.
Moreover, is it possible to poke the drive using another command like

```
test -s /dev/da1
```
or in this case the command returns immediately because the node exists?


----------



## wblock@ (Apr 3, 2012)

A write to a disk device forces geom(4) to "retaste" it and update from it.  That updates all the entries in /dev for labels and such.

The sleep is to avoid wasting system resources on a job that really only needs to check every few seconds.

test(1) is not a replacement because reads do not trigger retasting.


----------



## fluca1978 (Apr 3, 2012)

wblock@ said:
			
		

> A write to a disk device forces geom(4) to "retaste" it and update from it.  That updates all the entries in /dev for labels and such.



And in this case the write is a fake one, otherwise it will destroy the disk, right? I mean, I cannot do a:


```
echo "hello disk" > /dev/da1
```

or can I?



			
				wblock@ said:
			
		

> The sleep is to avoid wasting system resources on a job that really only needs to check every few seconds.



Not sure I get it: the sleep avoids to taste a device that is already being tasted or do it twice?


----------



## Zare (Apr 3, 2012)

true(1)() exits with EXIT_SUCCESS (0) status, and writes nothing to stdout. So in your example, shell will open a handle to /dev/da1, and nothing will be written to block device. The handle opening will trigger geom(4)() to refresh the device. In your other example, you will actually write "hello world" to block device, refreshing GEOM and corrupting sector zero of /dev/da1.



> Not sure I get it: the sleep avoids to taste a device that is already being tasted or do it twice?



Every access to /dev/ devices will result in data copy between user and kernel address space. User<->kernel transitions are heavy on CPU instructions. sleep(1)() will limit that to once every (n) seconds, in a looped script. In your case, it's probably used to allow kernel mechanisms to settle down after USB drive has been inserted, especially if you're running it automatically via devd(8)().


----------



## wblock@ (Apr 3, 2012)

That's the magic of true(1), that it only changes the exit status.  Anything else that doesn't write anything to stdout could be used.

The sleep could be used as Zare says above, because some USB things in particular take some time to appear.  But it could also be used to avoid a busy wait.  We'd have to see more of that script to see which.


----------

