4

如何以字节为单位获取设备大小?

在 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 Bytes) (确切地说是 78150744 512-Byte-Blocks)

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

我怎样才能以另一种方式获得以字节为单位的大小?

4

4 回答 4

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"
于 2013-11-12T17:16:57.430 回答
1

你能像这样使用它吗:

df | grep /dev/disk0s2
于 2011-03-28T15:31:27.213 回答
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的字节数和127175917568EFI 的单个分区减去 200MB

将正则表达式中的Total更改为Free,您将获得所选分区的可用空间。在一些花哨的 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

在这里,我们假设我想要一个具有 9 压缩率的disk0s2 z-ziped(11 是最大或标志 --best),向漂亮的 dd 进度条问好,因为它是其中一个永远不知道多长时间的操作;-)

于 2015-10-06T13:35:11.800 回答
0

这可能是df在不同的 Mac OS 版本中遵守某些标准。

于 2011-03-28T15:26:18.440 回答