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

あなたはそれをそのように使うことができますか?

df | grep /dev/disk0s2
于 2011-03-28T15:31:27.213 に答える
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 に答える
0

df異なる Mac OS バージョン間で何らかの標準に準拠している可能性があります。

于 2011-03-28T15:26:18.440 に答える