Mac OS X Bash获取/ dev / diskNsM大小

时间:2011-03-28 15:20:49

标签: macos bash dd

如何以字节来获取设备尺寸?

在Mac OS X 10.6中,我使用的是:

$ 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

它工作正常。但在Mac OS X 10.4中,输出将是

$ 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

并且没有类似的东西(40013180928字节)(正好是78150744 512字节块)

我的bash脚本解析diskutil输出,以字节为单位提取Total Size并使用dd命令获取磁盘的最后10 Mb,因此在10.4中它不起作用...

我怎么能以另一种方式获得字节大小?

4 个答案:

答案 0 :(得分:1)

你可以像这样使用它:

df | grep /dev/disk0s2

答案 1 :(得分:1)

您可以根据以下内容构建一些内容...我的Mac上的/ dev / rdisk0s4上安装了32GB磁盘。以下命令显示我可以从30GB的偏移量读取1MB:

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

以下命令显示了当我尝试从偏移量为40GB读取1MB时获得的内容:

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

因此,您可以从大块开始快速找到磁盘的大致末端,然后以相继较小的块退回,直到您获得所需的准确度。这是一些非常适合我的perl:

#!/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"

答案 2 :(得分:1)

以下命令diskutil info disk0s2 | grep -Ei 'Total.+([0-9]){10,}' | grep -Eio '[0-9]{10,}'(假设您有一个disk0s2)以字节为单位返回磁盘/分区的大小。

假设您的驱动器至少为127.2 GigbaGytes或〜127.000.000.000 bytes,您将从此命令获得一个大小为s2的驱动器,对整个磁盘的工作方式完全相同。

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

我的128GB SSD驱动器驱动器 128035676160 字节为 127175917568 ,单个分区为EFI减去200MB

更改免费的正则​​表达式中的总计,您将获得所选分区的可用空间。在一些花哨的pv + dd + pigz备份场景中使用大小;-)

例如:

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

这里我们假设我想要一个 disk0s2 z-ziped 9压缩(11是max或flag --best), 向漂亮的dd进度条问好,因为它是其中一个从不知道如何操作的东西; - )

答案 3 :(得分:0)

可能df在不同的Mac OS版本中遵循某种标准。