This one took me forever to figure out, but it's pretty simple in the end.
Media manufacturers list capacities in MB and GB.
These units have been redefined in recent years to be 1000^2 and 1000^3. They used to be 1024^2 and 1024^3. These are now called MiB and GiB respectively.
Linux still does things in 1024 byte (1 KiB) blocks, so you have to convert between the two. The units command will do the math for you.
Below is a code sample from one of my bash scripts that sets the media size in blocks based on the media type I have set in a variable. The actual sizes are the result of division and then integer truncation along with a little wiggle room, so the real media is slightly larger.
Since these values (in my scripts) will be passed to dump or partimage, they are further divided so that no backup chunk will be greater than 2GiB which makes mkisofs happy when creating an image to burn to media. (I put 2 chunks on a DVD and 4 chunks on a DVD/DL.)
Joe
Code:
## Set media sizes - adjust if your media capacities differ
## mkisofs refuses to write a file bigger than 4GiB
## and one of the standards (somewhere) says individual files
## Can't be larger than 2GiB
## So split them by 2 and 4 for DVD and DVDDL
## But now they changed KB,MB, GB to x 1000 (decimal)
## and KiB, Mib, Gib are x 1024 (binary)
## God bless marketing ....
## And, the blinking capacity specs are in GB, MB
## So those have to be translated back into KiB
## 8.5GB = 8,500,000,000B
## divided by 1024
## then divided by 4
case "${MEDIA}" in
DVD)
SIZE="2294921"; ## 1/2 Size
HSIZE="4.7GB"; ## Human-readable size
;;
DVDDL)
SIZE="2075195"; ## 1/4 Size
HSIZE="8.5GB";
;;
CD)
SIZE="683593"; ## Full Size
HSIZE="700MB";
;;
*)
echo "ERROR ${USAGE}"
exit 1;
;;
esac