Create a file whose size in bytes is the percentage of free space on a disk

4

I needed a command-line method of creating a file whose size in bytes is the percentage of free space on a disk. I intend to run it via cron every five minutes, but first a shell script will do.

The target platform is the BSD4.3 environment in DOMAIN/OS, so I have ksh, awk, sed and the usual BSD commands, but not GNU versions.

I can run a command lvolfs -a (not a BSD command) which lists volume free space in this format:

  # free   # total   % free         node id  entry directory
  732532   1295672       57           4D72C   /
 3787184   4165732       91           52055   //tr3
-------------------------------
 4519716   5461404    82.76%      2 volumes.

so it seems straightforward to extract the values, in this case 57 and 91, identifying them by the hexadecimal node ids, 4D72C and 52055, respectively.

I immediately thought 'dd can do it', so concentrating on node 4D72C first, I came up with:

/com/lvolfs -a | \
    awk -e '/4D72C/ { print $3 }' | \
    sed -e 's|^|dd if=/dev/rdsk of=//tr2/test/data/tr2.free bs=|' | \
    sed -e 's|$| count=1|' >//tr2/archive/dirlist

. //tr2/archive/dirlist

And dirlist looks like

dd if=/dev/rdsk of=//tr2/test/data/tr2.free bs=57 count=1

It works, and I'll need to repeat something like this for node 52055, but I wonder if dd is the only way using really basic BSD commands. Would anyone have any suggestions?


EDIT: Some more background information about the target system ... it's an HP/Apollo DN433 'Engineering Workstation', the bee's knees when it was released in the early '80s, but now about as impressive as a 486DX system. The native OS attempts to imitate Unix by providing a BSD4.3 'environment' with a reasonable set of BSD commands, but there is a temptation to mix BSD and Aegis commands (like lvolfs) as I've done here.

I have two workstations, and each controls Automatic Test Equipment. The two workstations are networked together and the network root directory // includes tr2 (node id 4D72C) and tr3 (node id 52055). You don't really want or need to know all this stuff - I'd just like to give the impression that nothing should be taken for granted. I don't want anyone to waste too much time on this stuff - that's my job, but I'd appreciate any suggestions you might have. The Korn Shell is still a bit of a mystery to me.

UPDATE: The df -a command in DOMAIN/OS gives

$ df -a
Filesystem         kbytes    used   avail capacity  Mounted on
/dev/wn96a        1295672  563904  731768    44%    //tr2
/dev/wn96a        4165732  379804 3785928     9%    //tr3
                ----------------------------------
                  5461300  943700 4517600    17%

Which, since it mentions the more familiar names tr2 and tr3, might be preferable. The only slight hurdle is that we get disk space used not free, but this can be overcome.

pavium

Posted 2010-01-29T01:32:04.330

Reputation: 5 956

Answers

1

I would use dd. You say "lvolfs -a (not a BSD command)". Do you have df? That would be a more standard and portable way to get the volume usage information.

It shouldn't be necessary to create a temporary script file to do this. You should be able to do:

dd if=/dev/rdsk of=//tr2/test/data/tr2.free bs=\
$(/com/lvolfs -a | awk -e '/4D72C/ { print $3 }') count=1

or

avail=$(df /usr | awk -e 'NR==2 { print $5 }')
avail=${avail%*%}    # strip off the percent sign
dd if=/dev/rdsk of=//tr2/test/data/tr2.free bs=$avail count=1

(depending on how your filesystems are organized and what the output of df looks like).

You could even put the second one in a loop:

for fs in / /usr /home    # list the ones you want to include
do
    avail=$(df $fs | awk -e 'NR==2 { print $5 }')
    avail=${avail%*%}     # strip off the percent sign
    # create a file on the particular filesystem/mount point
    dd if=/dev/rdsk of=//$fs/test/data/tr2.free bs=$avail count=1
done

Examine this carefully before you use it to make sure it does what you intend.

Paused until further notice.

Posted 2010-01-29T01:32:04.330

Reputation: 86 075

Thanks for the suggestion. The lvolfs command is native DOMAIN/OS and it gives a different format to df, of course. I prefer the format given by lvolfs. I don't really have to worry about portability. I'll try using df but it will have to wait until Monday when I go back to work. – pavium – 2010-01-29T09:44:52.980

Been thinking about the df command, and how closely its output resembles what we're used to. I've edited the question to add a little more background information about the peculiar system this is to work on. – pavium – 2010-01-29T10:21:31.040

Thanks Dennis, I decided to stick with lvolfs -a but I used your idea of eliminating the temporary file. I must try pushing ksh a little more on this ancient equipment. – pavium – 2010-01-31T23:53:36.180