Yes, you are absolutely right. The command you provided only zeroed out the first 4096 bytes (which is 4 KiB) of the swap device. Let's break down why:
-
dd command: This command is a powerful tool for copying and converting data.
-
if=/dev/zero: This specifies the input source as /dev/zero, which provides an endless stream of zero bytes.
-
of=$ZRAM_DEV: This specifies the output file as the device represented by the variable $ZRAM_DEV (your swap device).
-
bs=4096: This sets the block size to 4096 bytes. dd reads and writes data in blocks of this size.
-
count=1: This tells dd to copy only one block. Since the block size is 4096 bytes, it copies only 4096 bytes.
-
status=none: Suppresses progress output from dd.
Why only 4096 bytes and not the whole space?
The count=1 parameter is the key. It explicitly limited the amount of data dd wrote to the device. Without count=1, dd would continue writing until it reached the end of the input (which is infinite in the case of /dev/zero) or until the output device was full.
Why might this command be used like this?
There are a few possibilities, but they're all somewhat related:
-
Quick Initialization: The author might only be trying to quickly initialize the swap device with a minimal amount of data. Perhaps the later swap initialization process (e.g., mkswap or the system enabling the swap) overwrites the rest of the device anyway. Zeroing only the beginning might be enough to satisfy some minimal requirement or prevent some specific error.
-
Placeholder: The script may have been intended to write more data but it was either modified incorrectly or left as a placeholder.
-
Misunderstanding: The person who wrote the script may have mistakenly believed that writing just a small amount of data to the beginning was sufficient.
-
Security (Debatable): Even though it's a tiny fraction of the total space, zeroing the start of the swap space could be a weak attempt to clear some initial metadata or prevent certain types of data leakage if the swap isn't being fully used. However, this is a very minimal measure and wouldn't provide significant security.
To properly "zero out" the entire swap space, you'd need to remove the count=1 parameter, or specify a much larger count:
# Zero out the ENTIRE device contents
sudo dd if=/dev/zero of=$ZRAM_DEV bs=4096 status=none
Warning: The command above will zero the entire swap device, which will take time depending on the device's size and speed. The operation can be slow. It is better to use mkswap command.