Mac OS X Bash 获取 /dev/diskNsM 大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5461192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mac OS X Bash get /dev/diskNsM size
提问by
How could I get device size in bytes?
如何以字节为单位获取设备大小?
In Mac OS X 10.6 I am using this:
在 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
and it's work fine. But in Mac OS X 10.4 the output will be
它工作正常。但在 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
and there is no something like (40013180928 Bytes) (exactly 78150744 512-Byte-Blocks)
并且没有像 (40013180928 Bytes) 这样的东西(正好是 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 ddcommand, so in 10.4 it doesn't work...
我的 bash 脚本解析 diskutil 输出,以字节为单位提取总大小并使用dd命令获取磁盘的最后 10 Mb ,因此在 10.4 中它不起作用......
How could I get the size in bytes another way?
我怎样才能以另一种方式获得以字节为单位的大小?
回答by jpea
Could you use it like so:
你能不能像这样使用它:
df | grep /dev/disk0s2
回答by Mark Setchell
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:
您可以基于以下内容构建一些内容...我在 Mac 的 /dev/rdisk0s4 处安装了一个 32GB 磁盘。以下命令显示我可以在 30GB 的偏移量处从中读取 1MB:
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:
以下命令显示了当我尝试以 40GB 的偏移量从中读取 1MB 时得到的结果:
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:
因此,您可以从大块开始以快速找到磁盘的大致末端,然后依次使用较小的块后退,直到获得所需的准确性。这是一些对我来说效果很好的 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"
回答by Inoperable
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.
以下命令diskutil info disk0s2 | grep -Ei 'Total.+([0-9]){10,}' | grep -Eio '[0-9]{10,}'(假设您有一个 disk0s2)以字节为单位返回磁盘/分区的大小。
Assuming that your drive is at least 127.2 GigbaGytesor ~ 127.000.000.000 bytesyou will get one the size of partition s2from this command, works exactly the same for the entire disk.
假设您的驱动器至少是127.2 GigbaGytes或 ~127.000.000.000 bytes您s2将从该命令中获得一个分区大小,对整个磁盘的工作方式完全相同。
diskutil info disk0 | grep -Ei 'Total.+([0-9]){10,}' | grep -Eio '[0-9]{10,}'
diskutil info disk0 | grep -Ei 'Total.+([0-9]){10,}' | grep -Eio '[0-9]{10,}'
my 128GB SSD drives exectly 128035676160bytes for the drive and 127175917568and a single partition minus 200MB for EFI
我的 128GB SSD 驱动器正好128035676160是该驱动器的字节数,127175917568以及一个单独的分区减去 200MB 的 EFI
Change the Totalin the regex for Freeand you will get the available free space for chosen partition. Use the size in some fancy pv + dd + pigz backup scenarios ;-)
将正则表达式中的Total更改为Free,您将获得所选分区的可用空间。在一些花哨的 pv + dd + pigz 备份场景中使用大小 ;-)
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
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 disk0s2z-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 ;-)
在这里,我们假设我想要一个带有 9 压缩的disk0s2z 压缩(11 是最大值或标志 --best),向漂亮的 dd 进度条问好,因为它是其中一个永远不知道多长时间的操作;-)
回答by mvds
It might be that dfadheres to some standard across different Mac OS versions.
它可能会df遵守不同 Mac OS 版本的某些标准。

