I have a data collection system that collects data to hot swappable disks. Is there a way I can determine the type of media a file will be written to? Let me explain: I have a diskless boot system. The Linux OS and all come from over the network. So, unless I mount a disk, anything written anywhere will be to RAM. If I mount a disk, the file will be written to the disk and not RAM. How can I determine that it is a disk and not RAM? The disks are mounted via udev. The mount point exists as a directory whether there is a disk mounted over it or not, so I cannot use that to see. I want to know if there is a disk mounted where I am writing, or somewhere above it. As I read this question I too think it sounds odd. But I really would like to know. Maybe statvfs()? If the size of the media is greater than some test limit, (RAM in the machine perhaps) then it must be some mounted thing? Is there a better way? -- Roger Oberholtzer OPQ Systems AB Ramböll Sverige AB Kapellgränd 7 P.O. Box 4205 SE-102 65 Stockholm, Sweden Tel: Int +46 8-615 60 20 Fax: Int +46 8-31 42 23
Hi Roger, I use this script for backing up data to an USB-disk. If the disk is not mounted, I will get a message. If the disk is mounted but I have not yet created a directory with todays date, I will get the same message. Maybe you can adapt the idea for your need? Regards, Göran ============= #!/usr/bin/perl -w use strict; my $datum = `date +"%Y-%m-%d"`; chomp ($datum); my $target = "/media/usb-lacie/" . $datum; if (! -d $target) { printf ("\nCan not find directory %s\n\n", $target); print "1) Make sure that the USB-drive is mounted.\n"; print "2) Make a directory with the current date.\n"; print "3) Run this script again...\n\n"; } else { $target .= "/."; system ("cp -a /etc /home /root /var /usr/local '$target'") == 0 or die "copy failed!"; } On Friday 11 August 2006 17.21, Roger Oberholtzer wrote:
I have a data collection system that collects data to hot swappable disks. Is there a way I can determine the type of media a file will be written to? Let me explain:
I have a diskless boot system. The Linux OS and all come from over the network. So, unless I mount a disk, anything written anywhere will be to RAM. If I mount a disk, the file will be written to the disk and not RAM. How can I determine that it is a disk and not RAM? The disks are mounted via udev. The mount point exists as a directory whether there is a disk mounted over it or not, so I cannot use that to see. I want to know if there is a disk mounted where I am writing, or somewhere above it.
As I read this question I too think it sounds odd. But I really would like to know. Maybe statvfs()? If the size of the media is greater than some test limit, (RAM in the machine perhaps) then it must be some mounted thing? Is there a better way?
-- Roger Oberholtzer
OPQ Systems AB Ramböll Sverige AB Kapellgränd 7 P.O. Box 4205 SE-102 65 Stockholm, Sweden
Tel: Int +46 8-615 60 20 Fax: Int +46 8-31 42 23
On Sat, 2006-08-12 at 22:32 +0200, gsan.suseprogramming@mailnull.com wrote:
Hi Roger,
I use this script for backing up data to an USB-disk.
If the disk is not mounted, I will get a message.
If the disk is mounted but I have not yet created a directory with todays date, I will get the same message.
Maybe you can adapt the idea for your need?
Regards, Göran
=============
#!/usr/bin/perl -w
use strict;
my $datum = `date +"%Y-%m-%d"`; chomp ($datum);
my $target = "/media/usb-lacie/" . $datum;
if (! -d $target) { printf ("\nCan not find directory %s\n\n", $target); print "1) Make sure that the USB-drive is mounted.\n"; print "2) Make a directory with the current date.\n"; print "3) Run this script again...\n\n"; } else { $target .= "/.";
system ("cp -a /etc /home /root /var /usr/local '$target'") == 0 or die "copy failed!"; }
This assumes that the mount point will not exist until the media is inserted. This is not always the case. I am interested in the case where the mount point exists even when the media is not mounted. Like in the old days of fstab. BTW, I am hoping to do this from a C program. Preferably without calling a script. Unless the script is better than sliced bread.
On Friday 11 August 2006 17.21, Roger Oberholtzer wrote:
I have a data collection system that collects data to hot swappable disks. Is there a way I can determine the type of media a file will be written to? Let me explain:
I have a diskless boot system. The Linux OS and all come from over the network. So, unless I mount a disk, anything written anywhere will be to RAM. If I mount a disk, the file will be written to the disk and not RAM. How can I determine that it is a disk and not RAM? The disks are mounted via udev. The mount point exists as a directory whether there is a disk mounted over it or not, so I cannot use that to see. I want to know if there is a disk mounted where I am writing, or somewhere above it.
As I read this question I too think it sounds odd. But I really would like to know. Maybe statvfs()? If the size of the media is greater than some test limit, (RAM in the machine perhaps) then it must be some mounted thing? Is there a better way?
-- Roger Oberholtzer
OPQ Systems AB Ramböll Sverige AB Kapellgränd 7 P.O. Box 4205 SE-102 65 Stockholm, Sweden
Tel: Int +46 8-615 60 20 Fax: Int +46 8-31 42 23
-- Roger Oberholtzer OPQ Systems AB Ramböll Sverige AB Kapellgränd 7 P.O. Box 4205 SE-102 65 Stockholm, Sweden Tel: Int +46 8-615 60 20 Fax: Int +46 8-31 42 23
Am Montag, 14. August 2006 08:27 schrieb Roger Oberholtzer: [...]
This assumes that the mount point will not exist until the media is inserted. This is not always the case. I am interested in the case where the mount point exists even when the media is not mounted. Like in the old days of fstab. BTW, I am hoping to do this from a C program. Preferably without calling a script. Unless the script is better than sliced bread. [...]
Try the statvfs() call (demo program without any checks or validations): #include <stdio.h> #include <sys/statvfs.h> #include <string.h> int main(int argc, char **argv) { char *p = NULL; struct statvfs st; if (argc == 2) p = strdup(argv[1]); else p = strdup("."); statvfs(p, &st); printf("%ld\n", st.f_fsid); free(p); exit(0); } You can check the f_fsid field of the mount point against his parent. If the path is a simple directory, you will get equal f_fsid for both of them, otherwise the mount point will have its own f_fsid. HTH Jan
On Monday 14 August 2006 1:45 pm, Jan Trippler wrote:
Try the statvfs() call (demo program without any checks or validations):
#include <stdio.h> #include <sys/statvfs.h> #include <string.h>
int main(int argc, char **argv) { char *p = NULL; struct statvfs st; if (argc == 2) p = strdup(argv[1]); else p = strdup("."); statvfs(p, &st); printf("%ld\n", st.f_fsid); free(p); exit(0); }
Just a quick code nit. Why do you do a strdup (argv[1]) and strdup(".") where a simple pointer assignment works fine here: if (argc == 2) p = argv[1]; else p = "."; There are many cases where a strdup may be very necessary, especially when the string is going to be modified. Also in the case of 'p = "."' you are simply pointing p to a constant string. Also note that on many systems, malloc will return a pointer to a much larger block of memory than you asked for. In the case of ".", you are simply asking for 2 bytes, but malloc (called by strdup) will probably give you at least 16 bytes. (I have not looked at the efficiency of the GNU malloc for a while. -- Jerry Feldman <gaf@blu.org> Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
Am Montag, 14. August 2006 20:05 schrieb Jerry Feldman: [...]
Just a quick code nit. Why do you do a strdup (argv[1]) and strdup(".") where a simple pointer assignment works fine here: if (argc == 2) p = argv[1]; else p = "."; [...]
Yes, you're right. Sometimes one forgets the simple way ;-) Jan
On Monday 14 August 2006 2:21 pm, Jan Trippler wrote:
Am Montag, 14. August 2006 20:05 schrieb Jerry Feldman: [...]
Just a quick code nit. Why do you do a strdup (argv[1]) and strdup(".") where a simple pointer assignment works fine here: if (argc == 2) p = argv[1]; else p = ".";
[...]
Yes, you're right. Sometimes one forgets the simple way ;-) True. The bottom line is that you presented statvfs(2) which appears to somewhat solve the OP's problem.
-- Jerry Feldman <gaf@blu.org> Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
On Mon, 2006-08-14 at 19:45 +0200, Jan Trippler wrote:
Am Montag, 14. August 2006 08:27 schrieb Roger Oberholtzer: [...]
This assumes that the mount point will not exist until the media is inserted. This is not always the case. I am interested in the case where the mount point exists even when the media is not mounted. Like in the old days of fstab. BTW, I am hoping to do this from a C program. Preferably without calling a script. Unless the script is better than sliced bread. [...]
Try the statvfs() call (demo program without any checks or validations):
#include <stdio.h> #include <sys/statvfs.h> #include <string.h>
int main(int argc, char **argv) { char *p = NULL; struct statvfs st; if (argc == 2) p = strdup(argv[1]); else p = strdup("."); statvfs(p, &st); printf("%ld\n", st.f_fsid); free(p); exit(0); }
You can check the f_fsid field of the mount point against his parent. If the path is a simple directory, you will get equal f_fsid for both of them, otherwise the mount point will have its own f_fsid.
I was already using statvfs to track disk usage as data is streamed. In fact, I was originally going to use statfs for this as statfs has a f_type field that could help. But the statfs man page said it was better to use statvfs as statfs is depreciated in LSB, so I did. Where is there a description of f_fsid that describes this behavior? I will of course google. But maybe you have already found a good description. The info in the statfs man page for f_fsid is rather sketchy on the values themselves. -- Roger Oberholtzer OPQ Systems AB Ramböll Sverige AB Kapellgränd 7 P.O. Box 4205 SE-102 65 Stockholm, Sweden Tel: Int +46 8-615 60 20 Fax: Int +46 8-31 42 23
Am Dienstag, 15. August 2006 08:31 schrieb Roger Oberholtzer: [...]
Where is there a description of f_fsid that describes this behavior? I will of course google. But maybe you have already found a good description. The info in the statfs man page for f_fsid is rather sketchy on the values themselves.
I don't know a description additional to the man page. I assume the f_fsid field is a system wide unique identifier but don't know how it is calculated. I wonder if an all systems the id 0 is for the root file system but even don't know for sure. Jan
participants (4)
-
gsan.suseprogramming@mailnull.com
-
Jan Trippler
-
Jerry Feldman
-
Roger Oberholtzer