Mac OS X Bash get / dev / diskNsM size

How can I get the device size in bytes ?

On Mac OS X 10.6, I use the following:

$ 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 works fine. But on 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 isn’t something like (40013180928 bytes) (exactly 78150744 512-byte blocks)

My bash script analyzes the output of diskutil, extracts the Total Size in bytes and captures the last 10 Mb of the disk using the dd , so in 10.4 this does not work ...

How can I get the size in bytes in another way?

+4
source share
4 answers

Could you use it like this:

 df | grep /dev/disk0s2 
+1
source

You can create something based on the following ... I have a 32 gigabyte drive installed on / dev / rdisk 0s4 on my Mac. The following command shows that I can read 1 MB from it with an offset of 30 GB:

 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 to read 1 MB from it with an offset of 40 GB:

 dd if=rdisk0s4 bs=1m count=1 skip=40000 2> /dev/null | wc -c 0 

So, you can start with large chunks to quickly find the approximate end of the disk, and then step back with smaller chunks until you have the necessary precision. Here are a few perl that work well for me:

 #!/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
source

next command diskutil info disk0s2 | grep -Ei 'Total.+([0-9]){10,}' | grep -Eio '[0-9]{10,}' diskutil info disk0s2 | grep -Ei 'Total.+([0-9]){10,}' | grep -Eio '[0-9]{10,}' diskutil info disk0s2 | grep -Ei 'Total.+([0-9]){10,}' | grep -Eio '[0-9]{10,}' (assuming you have disk0s2) returns the size of the / partion disk in bytes.

Assuming your drive is at least 127.2 GigbaGytes or ~ 127.000.000.000 bytes , you will get one s2 partition size from this command, which will work the same way for the entire drive.

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

my 128GB SSDs for hard drives are 128035676160 , and 127175917568 and one partition minus 200 MB for EFI

Change Total in regular expression Free , and you will get available free space for the selected section. Use size in some backup scripts pv + dd + pigz ;-)

eg:

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 disk0s2 z-ziped with 9 compression (11 - max or the --best flag). Say hello to the excellent progress indicator, since one of them is never know-how ,-)

+1
source

It is possible that df adheres to some standard in different versions of Mac OS.

0
source

Source: https://habr.com/ru/post/1345566/


All Articles