Macos Mac OS X Bash get/dev/diskNsM大小

Macos Mac OS X Bash get/dev/diskNsM大小,macos,bash,dd,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:

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

在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
而且工作很好。但在MacOSX10.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字节)(正好是78150744512字节的块)

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


如何以另一种方式获得字节大小?

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

您可以这样使用它吗:

df | grep /dev/disk0s2

您可以基于以下内容构建一些内容。。。我在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"
#/usr/bin/perl
################################################################################
#disksize.pl
#作者:马克·塞切尔
#Perl脚本,通过尝试以不同的速度读取磁盘来确定磁盘的大小
#使用“dd”进行偏移,直到发生故障。
################################################################################
使用警告;
严格使用;
my$device=“/dev/rdisk0s4”;
我的$Debug=1;#设置为0可关闭调试消息
我的$blocksize=1024*1024;
我的$offsetinc=1024;
我的$offset=0;
我的$size=0;
而(1){
如果$Debug,则打印“测试字节偏移量:”、$offset*$blocksize、“\n”;
my$result=`sudo dd if=$device bs=$blocksize iseek=$offset count=1 2>/dev/null | wc-c`;
如果($result!=$blocksize){
#如果无法读取,请返回到以前的正确位置,并移动一半字节
$offset-=$offset;
$offsetinc/=2;
如果$Debug,则打印“读取太远-后退\n”;
如果$offsetinc<2,则为最后一个;
$offset+=$offset;
}否则{
#如果我们能够成功读取,请再移动$offsentic字节
$offset+=$offset;
$size=($offset+1)*$blocksize;
如果$Debug,则打印“迄今为止的最小大小:$size\n”;
}
}
打印“大小:$Size\n”

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

假设您的驱动器至少为
127.2千兆字节
或~
127.000.000.000字节
,您将从该命令中获得一个分区大小为
s2
的驱动器,该驱动器对整个磁盘的作用完全相同

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

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

将regex中的总计更改为空闲,您将获得所选分区的可用空闲空间。在某些奇特的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|U尺寸|\
pigz-9z>/path/to/backup.zz

这里我们假设我想要一个disk0s2z-ziped和9压缩(11是max或flag--best),
向漂亮的dd进度条问好,因为它是其中一个永远不知道运行多长时间的进度条;-)

$df/dev/disk1s2 df:/dev/disk1s2:Raw devices not supported然后使用grep仅选取该行。我使用grep和awk提取数字,但我需要准确的“40013180928”数字,而不是“500.0MB”来获取最后10Mbi的数据。意思是:
df | grep disk1s2
仅获取disk1s2行。我理解,但df不工作:df:/dev/disk1s2:不支持原始设备$diskutil info/dev/disk1s2设备标识符:disk1s2设备节点:/dev/disk1s2。。。总大小:524.2 MB(524247040字节)(正是1023920 512字节块)卷可用空间:不适用只读介质:无只读卷:不适用(无文件系统)可弹出:是整体:否内部:否$df/dev/disk1s2 df:/dev/disk1s2:原始设备不受支持盐湖城:~ivt$仅当设备已安装时。