开发者

Mac OS X Bash get /dev/diskNsM size

开发者 https://www.devze.com 2023-02-20 12:35 出处:网络
How could I get device size in bytes? In Mac OS X 10.6 I am using this: $ diskutil information /dev/disk0s2

How could I get device size in bytes?

In Mac OS X 10.6 I am using this:

$ diskutil information /dev/disk0s2
   Device Identifier:        disk0s2
   Device Node:              /dev/disk0s2
   Part Of Whole:            disk0
   Device / Media Name:      macOSX106

   Volume Name:              macOSX106
   Escaped with Unicode:     macOSX106

   Mounted:                  Yes
   Mount Point:              /
   Escaped with Unicode:     /

   File System:              Journaled HFS+
   Type:                     hfs
   Name:                     Mac OS Extended (Journaled)
   Journal:                  Journal size 8192 KB at offset 0x12d000
   Owners:                   Enabled

   Partition Type:           Apple_HFS
   Bootable:                 Is bootable
   Media Type:               Generic
   Protocol:                 SATA
   SMART Status:             Verified
   Volume UUID:              E2D5E93F-2CCC-3506-8075-79FD232DC63C

   Total Size:               40.0 GB (40013180928 Bytes) (exactly 78150744 512-Byte-Blocks)
   Volume Free Space:        4.4 GB (4424929280 Bytes) (exactly 8642440 512-Byte-Blocks)

   Read-Only Media:          No
   Read-Only Volume:         No
   Ejectable:                No

   Whole:                    No
   Internal:                 Yes

and it's work fine. But in Mac OS X 10.4 the output will be

$ diskutil info disk0s2
   Device Node:        /dev/disk1s2
   Device Identifier:  disk1s2
   Mount Point:        
   Volume Name:        

   Partition Type:     Apple_HFS
   Bootable:           Not bootable
   Media Type:         Generic
   Protocol:           SATA
   SMART Status:       Not Supported

   Total Size:         500.0 MB
   Free Space:         0.0 B

   Read Only:          No
   Ejectable:          Yes

and there is no something like (40013180928 Bytes) (exactly 78150744 512-Byte-Blocks)

My bash script parses the diskutil output, extract Total Size in bytes and grab last 10 Mb of the disk with the dd开发者_JAVA技巧 command, so in 10.4 it doesn't work...

How could I get the size in bytes another way?


Could you use it like so:

df | grep /dev/disk0s2


You can build something based on the following... I have a 32GB disk installed at /dev/rdisk0s4 in my Mac. The following command shows I can read 1MB from it at an offset of 30GB:

dd if=rdisk0s4 bs=1m count=1 skip=30000 2> /dev/null | wc -c
1048576

The following command shows what I get when I try and read 1MB from it at offset of 40GB:

dd if=rdisk0s4 bs=1m count=1 skip=40000 2> /dev/null | wc -c
0

So, you could start with large chunks to quickly find the approximate end of the disk and then back off with successively smaller chunks till you have the accuracy you need. Here is some perl that works pretty well for me:

#!/usr/bin/perl
################################################################################
# disksize.pl
# Author: Mark Setchell
# Perl script to determine size of a disk by trying to read from it at various
# offsets using "dd" until failure.
################################################################################
use warnings;
use strict;

my $device="/dev/rdisk0s4";
my $Debug=1;    # Set to 0 to turn off debug messages

my $blocksize=1024*1024;
my $offsetinc=1024;
my $offset=0;
my $size=0;

while(1){
    print "Testing byte offset:",$offset*$blocksize,"\n" if $Debug;
    my $result=`sudo dd if=$device bs=$blocksize iseek=$offset count=1 2>/dev/null | wc -c`;
    if($result!=$blocksize){
       # If unable to read, step back to previous good position and move on half as many bytes
       $offset -= $offsetinc;
       $offsetinc /= 2;
       print "Read too far - stepping back\n" if $Debug;
       last if $offsetinc < 2;
       $offset += $offsetinc;
    } else {
       # If we were able to read successfully, move on another $offsetinc bytes
       $offset += $offsetinc;   
       $size = ($offset+1)*$blocksize;
       print "Minimum size so far: $size\n" if $Debug;
    }
}
print "Size: $size\n"


the following command diskutil info disk0s2 | grep -Ei 'Total.+([0-9]){10,}' | grep -Eio '[0-9]{10,}' (assuming you have a disk0s2) returns the size of disk/partion in bytes.

Assuming that your drive is at least 127.2 GigbaGytes or ~ 127.000.000.000 bytes you will get one the size of partition s2 from this command, works exactly the same for the entire disk.

diskutil info disk0 | grep -Ei 'Total.+([0-9]){10,}' | grep -Eio '[0-9]{10,}'

my 128GB SSD drives exectly 128035676160 bytes for the drive and 127175917568 and a single partition minus 200MB for EFI

Change the Total in the regex for Free and you will get the available free space for chosen partition. Use the size in some fancy pv + dd + pigz backup scenarios ;-)

for example:

DISK0S2_SIZE=`diskutil info disk0s2 | \ grep -Ei 'Total.+([0-9]){10,}' | \ grep -Eio '[0-9]{10,}'` | \ sudo dd if=/dev/rdisk0s2 bs=1m | \ pv -s $DISK0S2_SIZE | \ pigz -9z > /path/to/backup.zz

Here we assume that I want a disk0s2 z-ziped with 9 compression (11 is max or flag --best), Say hello to the nifty dd progress-bar since it's one of them never-know-how-long operations ;-)


It might be that df adheres to some standard across different Mac OS versions.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号