如何获得以字节表示的设备大小()
在MacOSX10.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字节)这样的东西(确切地说是78150744 512个字节块)。
我的bash脚本解析磁盘输出,提取总大小(以字节为单位),并使用dd命令获取磁盘的最后10 Mb,因此在10.4中它不能工作.
如何以字节为单位以另一种方式获得大小?
发布于 2011-03-28 15:31:27
你能这样用吗?
df | grep /dev/disk0s2发布于 2013-11-12 17:16:57
你可以根据以下情况构建一些东西..。我的Mac中有一个32 my的磁盘安装在/dev/rdisk0s4上。下面的命令显示,我可以以30 of的偏移量从其中读取1MB:
dd if=rdisk0s4 bs=1m count=1 skip=30000 2> /dev/null | wc -c
1048576下面的命令显示了当我尝试在偏移量为40 of时从它读取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"发布于 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严格地驱动驱动器和127175917568的128035676160字节,并为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进度栏问好,因为它是一个从来不知道的操作;-)
https://stackoverflow.com/questions/5461192
复制相似问题