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

发布于 2024-10-27 12:27:25 字数 1980 浏览 0 评论 0原文

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

在 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 输出,提取总大小(以字节为单位)并获取磁盘的最后 10 Mb使用 dd 命令,所以在 10.4 中它不起作用...

我怎样才能获得尺寸字节另一种方式?

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 command, so in 10.4 it doesn't work...

How could I get the size in bytes another way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

他是夢罘是命 2024-11-03 12:27:25

你能像这样使用它吗:

df | grep /dev/disk0s2

Could you use it like so:

df | grep /dev/disk0s2
_畞蕅 2024-11-03 12:27:25

您可以基于以下内容构建一些东西...我在 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"

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"
小兔几 2024-11-03 12:27:25

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

假设您的驱动器至少为 127.2 GigbaGytes 或 ~ 127.000.000.000 字节,您将从该命令获得分区 s2 的大小,有效整个磁盘完全相同。

diskutil 信息磁盘0 | grep -Ei '总计.+([0-9]){10,}' | grep -Eio '[0-9]{10,}'

我的 128GB SSD 驱动器正好 128035676160 字节用于驱动器和 127175917568 和单个分区减去 200MB 的 EFI

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

例如:

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

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

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 ;-)

爱已欠费 2024-11-03 12:27:25

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文