Mac OS X Bash 获取 /dev/diskNsM 大小
如何获取设备大小(以字节为单位)?
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你能像这样使用它吗:
Could you use it like so:
您可以基于以下内容构建一些东西...我在 Mac 中的 /dev/rdisk0s4 安装了一个 32GB 磁盘。以下命令显示我可以在 30GB 的偏移量处从中读取 1MB:
以下命令显示当我尝试在 40GB 的偏移量处从中读取 1MB 时得到的结果:
因此,您可以从大块开始,以快速找到大致的结尾磁盘,然后逐渐减小块,直到达到所需的精度。这里有一些对我来说非常有效的 perl:
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:
The following command shows what I get when I try and read 1MB from it at offset of 40GB:
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:
以下命令
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 partitions2
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 and127175917568
and a single partition minus 200MB for EFIChange 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 ;-)
df
可能在不同的 Mac OS 版本中遵循某些标准。It might be that
df
adheres to some standard across different Mac OS versions.