首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mac get /dev/diskNsM大小

Mac get /dev/diskNsM大小
EN

Stack Overflow用户
提问于 2011-03-28 15:20:49
回答 4查看 6.8K关注 0票数 4

如何获得以字节表示的设备大小()

在MacOSX10.6中,我使用的是:

代码语言:javascript
复制
$ 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中,输出将是

代码语言:javascript
复制
$ 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脚本解析磁盘输出,提取总大小(以字节为单位),并使用dd命令获取磁盘的最后10 Mb,因此在10.4中它不能工作.

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

EN

回答 4

Stack Overflow用户

发布于 2011-03-28 15:31:27

你能这样用吗?

代码语言:javascript
复制
df | grep /dev/disk0s2
票数 1
EN

Stack Overflow用户

发布于 2013-11-12 17:16:57

你可以根据以下情况构建一些东西..。我的Mac中有一个32 my的磁盘安装在/dev/rdisk0s4上。下面的命令显示,我可以以30 of的偏移量从其中读取1MB:

代码语言:javascript
复制
dd if=rdisk0s4 bs=1m count=1 skip=30000 2> /dev/null | wc -c
1048576

下面的命令显示了当我尝试在偏移量为40 of时从它读取1MB时得到了什么:

代码语言:javascript
复制
dd if=rdisk0s4 bs=1m count=1 skip=40000 2> /dev/null | wc -c
0

因此,您可以从大块开始,快速找到磁盘的大致结束,然后依次使用较小的块,直到达到所需的精度为止。下面是一些对我非常有用的perl:

代码语言:javascript
复制
#!/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"
票数 1
EN

Stack Overflow用户

发布于 2015-10-06 13:35:11

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

假设您的驱动器至少是127.2 GigbaGytes或~ 127.000.000.000 bytes,您将从这个命令中得到一个分区s2大小的大小,整个磁盘的工作原理完全相同。

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

我的128 my SSD严格地驱动驱动器和127175917568128035676160字节,并为EFI驱动一个减去200 my的分区。

在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_SIZE | \ pigz -9z > /path/to/backup.zz

在这里,我们假设我想要一个带有9压缩的disk0s2 z-ziped (11是max或标志--最好),向精巧的dd进度栏问好,因为它是一个从来不知道的操作;-)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5461192

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档