LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Solaris / OpenSolaris (http://www.linuxquestions.org/questions/solaris-opensolaris-20/)
-   -   Diff Between /dev/zero & /dev/null (http://www.linuxquestions.org/questions/solaris-opensolaris-20/diff-between-dev-zero-and-dev-null-565736/)

rajaniyer123 06-30-2007 11:42 PM

Diff Between /dev/zero & /dev/null
 
Hi

As I would like to know the difference between

cp /dev/zero /var/tmp/testfile
cp /dev/null /var/tmp/testfile

with ref of the starving the system of its resources


Thanks
Rajan

IBall 07-01-2007 12:41 AM

(From Wikipedia)
/dev/zero provides as many ASCII null characters as read from it. The characters returned are 0x00, not the letter 0. Any writes to /dev/zero are successful, with no other side effects.

/dev/null can not be read from - any reads will return an EOF.

Now, why would you want to starve the system of its resources? This sounds suspiciously like hacking to me, which is not acceptable at LQ.

--Ian

jschiwal 07-01-2007 12:54 AM

/dev/null is a sink. It is commonly used to redirect stderr to. In a cron script, you can't write to the console so you would redirect both stdout and stderr to /dev/null.

/dev/zero is a source of 0 bytes. You can use it when creating a zeroed out file, which can then be used to create a filesystem on:
dd if=/dev/zero of=imagefile bs=512 count=$((2*1024*64))
/sbin/mkfs.ext3 imagefile

Now you have an image file you can mount using the loopback device:
sudo mount -t ext3 image.img mountdir/ -o loop
sudo chown username:username mountdir
sudo chmod 007 mountdir

----

Sorry Iball, you posted your message first, and I was too dense to notice the potential hacking aspect of the question.

jlliagre 07-01-2007 03:46 AM

Quote:

Originally Posted by jschiwal
/dev/zero is a source of 0 bytes. You can use it when creating a zeroed out file, which can then be used to create a filesystem on:
dd if=/dev/zero of=imagefile bs=512 count=$((2*1024*64))
/sbin/mkfs.ext3 imagefile

Now you have an image file you can mount using the loopback device:
sudo mount -t ext3 image.img mountdir/ -o loop
sudo chown username:username mountdir
sudo chmod 007 mountdir

Please note that while a good example of /dev/zero use, these commands are more or less Linux specific.
Here is what I would do to achieve the same result under Solaris
Code:

mkfile -n $((2*1024*64*512)) imagefile
device=$(lofiadm -a $PWD/imagefile)
rdevice=$(echo $device | sed 's/lofi/rlofi/')
yes | newfs $rdevice
mountdir=/mnt/image
mkdir $mountdir
mount $device $mountdir
chown username:groupname $mountdir
chmod 0700 $mountdir

Or with ZFS, the quite simpler:
Code:

mkfile -n $((2*1024*64*512)) imagefile
zpool create imagefile

About the resource starving question, "cp /dev/zero ..." will eventually fill up the target filesystem unless quotas are in place.
OTOH "cp /dev/null ..." would create an empty file. No big deal.

jschiwal 07-02-2007 06:09 AM

Quote:

Originally Posted by jlliagre
Please note that while a good example of /dev/zero use, these commands are more or less Linux specific.
Here is what I would do to achieve the same result under Solaris

Thanks for that. I used the rss feed in firefox and failed to notice the forum I was in.


All times are GMT -5. The time now is 06:22 PM.