From owner-freebsd-hackers@FreeBSD.ORG Fri Jun 23 01:55:29 2006 Return-Path: X-Original-To: freebsd-hackers@freebsd.org Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 77A4A16A608 for ; Fri, 23 Jun 2006 01:55:29 +0000 (UTC) (envelope-from anderson@centtech.com) Received: from mh2.centtech.com (moat3.centtech.com [207.200.51.50]) by mx1.FreeBSD.org (Postfix) with ESMTP id 74080440BB for ; Fri, 23 Jun 2006 01:33:45 +0000 (GMT) (envelope-from anderson@centtech.com) Received: from [192.168.42.22] (andersonbox2.centtech.com [192.168.42.22]) by mh2.centtech.com (8.13.1/8.13.1) with ESMTP id k5N1XX5W033057; Thu, 22 Jun 2006 20:33:33 -0500 (CDT) (envelope-from anderson@centtech.com) Message-ID: <449B44F9.70006@centtech.com> Date: Thu, 22 Jun 2006 20:33:45 -0500 From: Eric Anderson User-Agent: Thunderbird 1.5.0.4 (X11/20060612) MIME-Version: 1.0 To: Dan Nelson References: <1151008839.2360.30.camel@LatitudeFC5.network> <17563.3550.496244.953904@bhuda.mired.org> <20060622220127.GE74589@dan.emsphone.com> In-Reply-To: <20060622220127.GE74589@dan.emsphone.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: ClamAV 0.87.1/1561/Thu Jun 22 10:40:00 2006 on mh2.centtech.com X-Virus-Status: Clean Cc: freebsd-hackers@freebsd.org, Mike Meyer , Andrew Subject: Re: Coding question: finding the size of a block device X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 23 Jun 2006 01:55:29 -0000 Dan Nelson wrote: > In the last episode (Jun 22), Mike Meyer said: >> In <1151008839.2360.30.camel@LatitudeFC5.network>, Andrew typed: >>> So I guess my question is: is there a POSIX compatible function that >>> will allow me to check the size of a given block device? >> I'd be surprised - POSIX doesn't seem to deal with block devices at all. >> >> Checking the sources to df, it uses statfs to get the >> information. Linux appears to have it as well, so it may be portable. > > statfs only works on mounted filesystems, not arbitrary block devices. > > /usr/sbin/diskinfo uses ioctl(fd, DIOCGMEDIASIZE, &mediasize), where > mediasize is an off_t. > Maybe something like this? ----------- #include #include #include #include #include #include #include #include int main(int argc, char **argv) { int devfd, error; if (argc != 2) { fprintf(stderr, "Need to specify device\n"); exit(1); } devfd = open(argv[1], O_RDONLY); if (devfd < 0) { fprintf(stderr, "Failed to open device\n"); exit(1); } error = getdiskinfo(devfd); return (0); } int getdiskinfo (int fd) { int error; off_t mediasize; u_int sectorsize; error = ioctl(fd, DIOCGMEDIASIZE, &mediasize); if (error) printf("ioctl(DIOCGMEDIASIZE) failed, probably not a disk."); error = ioctl(fd, DIOCGSECTORSIZE, §orsize); if (error) printf("DIOCGSECTORSIZE failed, probably not a disk."); printf("Sector size: %d \n", sectorsize); printf("Media size: %jd\n", (intmax_t)mediasize); printf("Media: %jd sectors\n", (intmax_t)mediasize/sectorsize); return(error); } --------------- -- ------------------------------------------------------------------------ Eric Anderson Sr. Systems Administrator Centaur Technology Anything that works is better than anything that doesn't. ------------------------------------------------------------------------