From: Brian Candler Date: 2010-10-09T21:35:32+09:00 Subject: Re: Getting the size of a block device Terry Michaels wrote: > Obviously, I could wrap a system call around a Linux utility or read a > proc file... but I am curious if there is a 'built-in' Ruby method. The ioctl is called BLKGETSIZE/BLKGETSIZE64, and a quick grep of the ruby source code doesn't find it. Maybe someone else has published an extension which wraps the API directly, but you could just use IO.ioctl directly. $ sudo blockdev --getsz /dev/sda 976773168 $ sudo strace blockdev --getsz /dev/sda 2>&1 | grep ioctl ioctl(3, BLKGETSIZE64, 0x7fff486af378) = 0 $ sudo strace -e raw=ioctl blockdev --getsz /dev/sda 2>&1 | grep ioctl ioctl(0x3, 0x80081272, 0x7fffacd73d68) = 0 $ sudo ruby -e 'v = [0].pack("Q"); File.open("/dev/sda").ioctl(0x80081272, v); v = v.unpack("Q")[0]; puts v' 500107862016 But I'm not sure that's exactly right; it gives the size in bytes rather than blocks. -- Posted via http://www.ruby-forum.com/.