For details about the/dev/null and/dev/zero files, and solutions for accidental deletion of/dev/null and/dev/zero, and the disk I/O Testing Method Using/dev/zero, disk deletion and recovery by mistake

Source: Internet
Author: User
Tags vps

For details about the/dev/null and/dev/zero files, and solutions for accidental deletion of/dev/null and/dev/zero, and the disk I/O Testing Method Using/dev/zero, disk deletion and recovery by mistake
1. Introduction-explanation from Wikipedia/dev/null:

In Unix-like systems,/dev/null, or an empty device, is a special device file that discards all data written into it (but reports a successful write operation ), read it to get an EOF immediately.
In a programmer's line, especially a Unix line,/dev/null is called a bit bucket or a black hole ). Empty devices are usually used to discard unwanted output streams or empty files used as input streams. These operations are usually completed by redirection.

/Dev/zero:

In UNIX-like operating systems,/dev/zero is a special file. When you read it, it provides unlimited NULL characters (NULL, ascii nul, 0x00 ). One typical usage is to overwrite information with the volume stream provided by it. Another common usage is to generate a blank file of a specific size. BSD maps/dev/zero to the virtual address space through mmap to implement shared memory. Mmap can be used to map/dev/zero to a virtual memory space. The effect of this operation is equivalent to using an anonymous memory (not related to any files ). (Source:/dev/zero-Wikipedia)

The following is an example of how to extract personal information from the sky:

The/dev/null nickname is a bottomless pit. You can output any data to it. It doesn't support it!
/Dev/zero is an input device. You can use it to initialize files.
/Dev/null ------ it is an empty device, also known as a bit bucket ). Any output written to it will be discarded. If you do not want to display messages in standard output or write files, you can redirect messages to the bucket.
/Dev/zero ------ the device provides 0 infinitely, and you can use any number you need-the device provides more. It can be used to write string 0 to a device or file.

2. daily use of/dev/null regards/dev/null as a "black hole ". It is equivalent to a write-only file, and all the content written to it will be lost forever, and the attempt to read the content from it will not be able to read anything. However,/dev/null is very useful for both command lines and scripts.

We all know that cat $ filename will output the file content corresponding to filename (output to standard output)
Use cat $ filename>/dev/null
No information will be obtained, because we will redirect the file information originally displayed through the standard output to/dev/null. so what will you get?
Using cat $ filename 1>/dev/null also achieves the same effect, because the default redirection 1 is the standard output. If you are familiar with shell scripts or redirection, you should think of 2, that is, standard error output.
When we use cat $ filename, if the file corresponding to filename does not exist, the system will certainly report the error "cat: filename: No file or directory ".

What if we don't want to see the error output? We can disable the standard error: cat $ badname 2>/dev/null

We can use the following test to better understand/dev/null:

$ Cat test.txt
Just for test
$ Cat test.txt>/dev/null
$ Cat test.txt 1>/dev/null
$ Cat test2.txt
Cat: test2.txt: the file or directory does not exist.
$ Cat test2.txt>/dev/null
Cat: test2.txt: the file or directory does not exist.
$ Cat test2.txt 2>/dev/null
$

Sometimes, I don't want to see any output. I just want to see if this command is running normally, so we can disable both standard output and standard error output:

Cat $ filename 2>/dev/null

Therefore:

* If "$ filename" does not exist, no error message is prompted,
* If "$ filename" exists, the file content will not be printed to the standard output.
* Therefore, the above Code does not output any information at all. It is useful when you only want to test the exit code of the command and do not want to have any output.

Next, we use echo $? View the exit code of the previous command: 0 indicates that the command is executed normally, and 1-indicates that there is an error.
Of course, cat $ filename &>/dev/null can also achieve the same effect as cat $ filename 2>/dev/null.

$ Cat test2.txt 2>/dev/null
$ Cat test.txt 2>/dev/null
$ Echo $?
0
$ Cat test2.txt 2>/dev/null
$ Echo $?
1
$ Cat test.txt &>/dev/null
$ Echo $?
0

Sometimes, we need to delete the content of some files without deleting the file itself: (this method can be used to delete log files. In my Debian notebook, the space I have allocated to the/var disk is too small, sometimes you need to manually use this operation to clear logs)

# Cat/dev/null>/var/log/messages
#:>/Var/log/messages has the same effect, but no new processes are generated. (Because: built-in)
 
In the following instance,/dev/null is used to delete the cookie and the cookie is no longer used.
If [-f ~ /. Netscape/cookies] # delete a soft link if it exists.
Then
Rm-f ~ /. Netscape/cookies
Fi

Ln-s/dev/null ~ /. Netscape/cookies

The cookies directory can be changed. For example, the firefox cookie directory on my computer is :~ /. Mozilla/firefox/nah4b6di. default/cookies *

3. daily use of/dev/zero
Like/dev/null,/dev/zero is also a pseudo file, but it actually produces continuous null streams (Binary zero streams instead of ASCII streams ). The output written to it will be lost, And/dev/zero is mainly used to create an empty file with a specified length for initialization, such as a temporary swap file.

For example, in my previous blog ("try to install the new version of Chrome OS Vanilla & Solve the problem encountered by the USB flash drive after installation"), I mentioned the USB flash drive system I created using dd, however, my USB flash drive has 16 GB, and after the preparation, the system disk only occupies 2.5 GB, while other spaces (nearly 12 GB) are not available. I can only use dd if =/dev/zero of =/dev/sdb bs = 4 M to reset the entire USB flash disk.

Script instance 1. Use/dev/zero to create a temporary file for swap
#! /Bin/bash
# Create an swap file. The parameter is the number of created blocks (default if no parameter is provided), and the block size is B (1 K)


ROOT_UID = 0 # the Root user's $ UID is 0.
E_WRONG_USER = 65 # Not root?


FILE =/swap
BLOCKSIZE = 1024
MINBLOCKS = 40
SUCCESS = 0

# This script must be run with the root user. If it is not the root user, the system prompts and exits.
If ["$ UID"-ne "$ ROOT_UID"]
Then
Echo; echo "You must be root to run this script."; echo
Exit $ E_WRONG_USER
Fi


Blocks =1 {1:-$ MINBLOCKS} # If the command line is not specified, set it to 40 by default.
# The above sentence is equivalent:
#----------------------------------------
# If [-n "$1"]
# Then
# Blocks = $1
# Else
# Blocks = $ MINBLOCKS
# Fi
#-----------------------------------------

If ["$ blocks"-lt $ MINBLOCKS]
Then
Blocks = $ MINBLOCKS # There must be at least 40 blocks in length. If the input parameter is smaller than 40, set the number of blocks to 40.
Fi

Echo "Creating swap file of size $ blocks (KB )."
Dd if =/dev/zero of = $ FILE bs = $ BLOCKSIZE count = $ blocks # writes zero to a FILE.

Mkswap $ FILE $ blocks # create this FILE as a swap FILE (or swap partition ).
Swapon $ FILE # activate the swap FILE.

Echo "Swap file created and activated ."
Exit $ SUCCESS

We can see the running effect:

[Root @ localhost zhaiqutianshangxing/tmp] $ vim testswap. sh
[Root @ localhost zhaiqutianshangxing/tmp] $ chmod + x testswap. sh
[Root @ localhost zhaiqutianshangxing/tmp] $ sudo./testswap. sh
[Sudo] password for zhaiqutianshangxing:
[Root @ localhost zhaiqutianshangxing/tmp] $./testswap. sh

You must be root to run this script.

[Root @ localhost zhaiqutianshangxing/tmp] $ sudo./testswap. sh
[Sudo] password for zhaiqutianshangxing:
Creating swap file of size 40 blocks (KB ).
Records 40 + 0 reads
Records 40 + 0 records
40960 bytes (41 kB) Copied, 0.000904021 seconds, 45.3 MB/second
Setting swap space version 1, size = 36 KiB
No tag, UUID = 3e59eddf-098f-454d-9507-aba55f434a8c
Swap file created and activated.

Another application about/dev/zero is to fill a specified size file with zero for a specific purpose, such as mounting a file system to loopback device) or "Safely" to delete an object.
Script instance 2. Create ramdisk
#! /Bin/bash
# Ramdisk. sh
# "Ramdisk" is a part of the system RAM memory. It can be operated as a file system.
# Advantages: fast access (including reading and writing ).
# Disadvantage: It is volatile. Data is lost when the computer is restarted or shut down.
# The system's available RAM is reduced.
#
# What is the role of ramdisk?
# Saving a large dataset on ramdisk, such as a table or dictionary can accelerate data query because searching in memory is much faster than searching on a disk.

E_NON_ROOT_USER = 70 # It must be run with root.
ROOTUSER_NAME = root

MOUNTPT =/mnt/ramdisk
SIZE = 2000 #2 K blocks (which can be modified as appropriate)
BLOCKSIZE = 1024 # each block has a size of 1 K (1024 bytes)
DEVICE =/dev/ram0 # first ram DEVICE

Username = 'id-nu'
If ["$ username "! = "$ ROOTUSER_NAME"]
Then
Echo "Must be root to run" "'basename $0 '""."
Exit $ E_NON_ROOT_USER
Fi

If [! -D "$ MOUNTPT"] # test whether the mount point already exists,
Then # + If the script has been run for several times, the directory will not be created.
Mkdir $ MOUNTPT # + is already created.
Fi

Dd if =/dev/zero of = $ DEVICE count = $ SIZE bs = $ BLOCKSIZE # Fill the content of the ram device with zero.
# Why?
Mke2fs $ DEVICE # create an ext2 File System on the ram device.
Mount $ DEVICE $ MOUNTPT # mount the DEVICE.
Chmod 777 $ MOUNTPT # allows common users to access this ramdisk, but it can only be carried by the root user.

Echo "$ MOUNTPT" "now available for use ."
# Now ramdisk can be used to access files even for common users.
# Note: ramdisk is easy to lose, so the content in ramdisk disappears when the computer system is restarted or shut down.
#
# Run this script to create a ramdisk again after restart.
# Only reload/mnt/ramdisk and no other steps will work correctly.


# If it is improved, this script can be placed in/etc/rc. d/rc. local so that a ramdisk can be automatically set up when the system starts. This is suitable for database servers with high speed requirements.
Exit 0
The running result is as follows:


[Root @ localhost zhaiqutianshangxing/tmp] $ vim ramdisk. sh
[Root @ localhost zhaiqutianshangxing/tmp] $ chmod + x ramdisk. sh
[Root @ localhost zhaiqutianshangxing/tmp] $./ramdisk. sh
Must be root to run ramdisk. sh.
[Root @ localhost zhaiqutianshangxing/tmp] $ sudo./ramdisk. sh
Recorded 2000 + 0 reads
Records 2000 + 0 writes
2048000 bytes (2.0 MB) Copied, 0.0113732 seconds, 180 MB/second
Mke2fs 1.42.8 (20-Jun-2013)
Discarding device blocks: complete
File System tag =
OS type: Linux
Block size = 1024 (log = 0)
Part size = 1024 (log = 0)
Stride = 0 blocks, Stripe width = 0 blocks
16384 inodes, 65536 blocks
3276 blocks (5.00%) reserved for the super user
First data block = 1
Maximum filesystem blocks = 67108864
8 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
8193,245 77, 40961,573 45


Allocating group tables: complete
Writing to inode table: complete
Writing superblocks and filesystem accounting information: complete
/Mnt/ramdisk now available for use.
Finally, it is worth mentioning that the ELF binary file uses/dev/zero.


When I executed the command during disk I/O testing today, I suddenly found that the/dev/zero file was lost inexplicably. I was prompted that the file could not be found. The error message is as follows:
Win the stars, a person who loves Internet art! Email: happy.yin@qq.com
[Root @ localhost zhaiqutianshangxing] # dd if =/dev/zero of = test bs = 64 k count = 4 k oflag = dsync
Dd: Opening "/dev/zero": No file or directory
The solution for accidentally deleting the/dev/zero file is as follows: [root @ localhost zhaiqutianshangxing] # mknod/dev/zero c 1 5
[Root @ localhost zhaiqutianshangxing] # chmod 666/dev/zero
After the repair is completed, run the command to test whether the disk is available and check whether the disk can be used normally.
[Root @ localhost sq808sq] # dd if =/dev/zero of = test bs = 64 k count = 4 k oflag = dsync
Recorded 4096 + 0 reads
Records 4096 + 0 writes
268435456 bytes (268 MB) Copied, 105.086 seconds, 2.6 MB/second
At this point, the restoration and reconstruction of the/dev/zero file has been completed (this file cannot be directly copied and used from the system disk or other systems after the system special file is lost, and can only be repaired through reconstruction, the same is true for the/dev/null file below)
The solution for deleting the/dev/null file by mistake is as follows: [root @ localhost zhaiqutianshangxing] # mknod/dev/null c 1 3
[Root @ localhost zhaiqutianshangxing] # chmod 666/dev/null
Then run the command to view the/dev/null file, as shown below. The/dev/null file has been repaired successfully.
[Root @ localhost zhaiqutianshangxing] # ls-la/dev/null
Crw-rw-. 1 root 1, December 12 13:13/dev/null


Test disk IO using/dev/zero:

[Root @ localhost zhaiqutianshangxing] # dd if =/dev/zero of = test bs = 64 k count = 4 k oflag = dsync
Recorded 4096 + 0 reads
Records 4096 + 0 writes
268435456 bytes (268 MB) Copied, 104.896 seconds, 2.6 MB/second
[Root @ localhost zhaiqutianshangxing] # dd if =/dev/zero of = test bs = 8 k count = 256 k conv = fdatasync
Recorded 262144 + 0 reads
Records 262144 + 0 writes
2147483648 bytes (2.1 GB) Copied, 28.9346 seconds, 74.2 MB/second

For more details about the/dev/zero disk I/O testing instructions:

During a normal test, the test may not be performed on the other side, but the average value may be obtained many times. After the normal redirection is ineffective, google redirects the test results to a file in the following way:

Dd if =/dev/zero of =/var/test bs = 8 k count = 1000000 2> info

In this way, the test result will be included in the info file.

1. dd if =/dev/zero of = test bs = 64 k count = 16 k
This is not accurate because the data is not actually written to the disk at the end of the command.

2. dd if =/dev/zero of = test bs = 64 k count = 16 k conv = fsync
This is fairly accurate. The data has been written to the disk.

3. dd if =/dev/zero of = test bs = 64 k count = 4 k oflag = dsync
This can be regarded as a simulation of database insert operations, so it is very slow

Next let's take a look at the VPS disk Performance
Dd if =/dev/zero of = test bs = 64 k count = 16 k
1073741824 bytes (1.1 GB) copied, 2.625317 seconds, 358 MB/s
The result obtained by the above method seems to be very fast.
Dd if =/dev/zero of = test bs = 64 k count = 16 k conv = fsync
1073741824 bytes (1.1 GB) copied, 12.5891 seconds, 77.1 MB/s
The execution is a little slow, but this result is of reference value.
Dd if =/dev/zero of = test bs = 64 k count = 2 k oflag = dsync
134217728 bytes (134 MB) copied, 176.151 seconds, 755 kB/s
This is the real strength of VPS. It is 20 Mb/s in 84 vps tests.

-Dsync can be used as a simulation of database insert operations. Read a piece of data in/dev/zone and write it to the hard disk immediately.
-Fsync also writes data to the disk, but is written to the hard disk after being cached.

How can I test the disk read/write speed?
Dd bs = 1 M count = 128 if =/dev/zero of = test
Dd bs = 1 M count = 128 if =/dev/zero of = test; sync
Dd bs = 1 M count = 128 if =/dev/zero of = test conv = fdatasync
Dd bs = 1 M count = 128 if =/dev/zero of = test oflag = dsync
The difference is that the write cache Processing Method in the memory.
Dd bs = 1 M count = 128 if =/dev/zero of = test
No parameters are added. The default dd method does not include the "sync" command. That is to say, before the dd command is completed, the system did not actually write the file to the disk. Therefore, the above command simply reads the mb data into the memory buffer (write cache [write cache]). So what you get is a super fast speed. In fact, dd only gives you the read speed, and the system does not actually write data to the disk until dd is complete, but you cannot see this speed. So if this speed is fast, don't be happy first. Haha
Dd bs = 1 M count = 128 if =/dev/zero of = test; sync
It is exactly the same as in previous 1. Only two independent commands are separated by semicolons. When the sync command is ready to write data to the disk, the previous dd command has displayed the incorrect "write speed" value on the screen. So you still cannot get the real write speed.
Dd bs = 1 M count = 128 if =/dev/zero of = test conv = fdatasync
After this parameter is added, the dd command will actually execute a "sync" operation until the end, so what you get at this time is the time required to read the M data to the memory and write it to the disk. The calculated time is more realistic.
Dd bs = 1 M count = 128 if =/dev/zero of = test oflag = dsync
After this parameter is added, dd performs synchronous write operations every time it is executed. That is to say, this command writes the 1 M to the disk every time it reads the 1 M, and then reads the following 1 M, repeating 128 times. This may be the slowest way, because write cache is basically not used ).
We recommend that you use dd bs = 1 M count = 128 if =/dev/zero of = test conv = fdatasync
Because this method is the closest to the actual operation of the computer, the data tested has the most reference value.

Win the stars, a person who loves Internet art! Email: happy.yin@qq.com


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.