Detailed Explanation of Disk Performance Testing with the dd Command in Linux

1. Common Parameters of the dd Command

1.1 Basic Input/Output Parameters

  • if=filename: Specifies the input file path;

  • of=filename: Specifies the output file path;

  • ibs=bytes/obs=bytes: Sets the input/output block size (in bytes) for a single operation, where bs can cover both (commonly used);

1.2 Block Control Parameters

  • bs=size: Sets the read/write block size uniformly (e.g., 1M, 4k), directly affecting I/O efficiency;

  • count=N: Limits the number of blocks to copy, determining the total data volume in conjunction with bs (e.g., bs=1M count=100 generates a 100MB file);

  • skip=N/seek=N: Skips the first N blocks of the input/output file respectively (calculated with ibs/obs), not commonly used.

1.3 I/O Mode Flags

  • iflag=FLAGS/oflag=FLAGS: Controls read/write behavior, common flags include:

    • direct: Bypasses the system cache to operate directly on the disk, enhancing the authenticity of performance testing;

    • sync: Ensures that each operation synchronously writes to the physical disk, ensuring data persistence;

    • dsync: Similar to sync but only synchronizes data (not metadata);

    • nonblock: Non-blocking I/O mode.

1.4 Data Conversion and Progress Display

  • conv=keyword: Specifies the data conversion method, such as:

    • ascii/ebcdic: Character encoding conversion;

    • ucase/lcase: Case conversion;

    • noerror: Does not stop on errors.

  • status=progress: Displays real-time transfer progress and rate.

2. Typical Application Scenarios and Parameter Combinations

2.1 Disk Performance Testing:

dd if=/dev/zero of=/your/test/filepath/testfile bs=1G count=10 oflag=direct,sync

Description:

  • if=/dev/zero: Specifies the input source as the zero-value device file in the Linux system, which generates empty characters (ASCII NUL) as the data source indefinitely;

  • bs=1G: Sets the block size for each read/write operation to 1GB (acceptable units include K/M/G, default is bytes). A larger block size can improve I/O efficiency but needs to balance memory usage;

  • count=10: Limits the copy to 10 blocks, resulting in a total data volume of bs×count=10GB. If not specified, it will continue writing until manually terminated.

  • oflag=direct,sync: Excludes cache interference.

2.2 Disk Cloning:

dd if=/dev/sda of=/dev/sdb bs=4M conv=noerror,sync

Description: conv=noerror prevents interruption due to bad blocks, and sync ensures data integrity.

2.3 Creating an Empty File:

dd if=/dev/zero of=/tmp/empty_file bs=1M count=100

Uses /dev/zero to generate an empty_file of specified size 100M in the /tmp directory.

Leave a Comment