dd “data duplicator” -
Will be used for copying and converting data.
Below are the some of the tasks
1) Backup and restore the entire hard disk or
partition.
2) Wipe/delete content of a disk so that it will be
empty for some one to use it.
3) Backing Up and Restoring MBR (Master Boot Record)
4) Convert lower case to upper case and vice versa
5) It can also be used by Linux kernel make files to
make boot images.
SYNTAX
dd if=<source file name> of=<target file
name> [Options]
1) Backup and restore the entire
hard disk or partition.
Clone one hard disk to another hard disk. This is
useful when we are building many machines with same configuration. We no need
to install OS on all the machines. Just install OS and required software on
machine then clone with below example.
dd if=/dev/sda
of=/dev/sdb
EXAMPLE-2:
To create a disk image.
# dd if=/dev/sda of=/tmp/sdadisk.img
# dd if=/dev/sda of=/tmp/sdadisk.img
The above creates the image of a harddisk /dev/hda. Backing up a disk to an image will be faster than copying the exact data. Also, disk image make the restoration much more easier.
EXAMPLE-3:
To create a compressed disk image.
# dd if=/dev/sda | gzip >/tmp/sdadisk.img.gz
EXAMPLE-4:
To restore hard disk image.
# dd if=hdadisk.img of=/dev/hdb
The image file hdadisk.img file, is the image of a /dev/hda, so the above command will restore the image of /dev/hda to /dev/hdb.
2) Deleting the Data from Disk
a) Wipe/delete content of a disk so that it will be
empty for some one to use it.
# dd if=/dev/zero of=/dev/sdb
b) What to hide your ass by deleting your personal
data. Many people think if we do rm -rf /<your data> will do the needful.
But we can recover those deletion by using disk recovery tools like Photorec or
some forensic tools. But if you want some not to recover your data you have to
write random data on your partition where you data resides.
dd if=/dev/random of=/dev/sdb
3. Backing up and restoring MBR
As you are aware MBR makes up the first 512 bytes of
the disk, in which 466 bytes about boot loader info. The additional space will
be used to store the partition table for that drive. If MBR gets corrupted, we will not be able to
boot into Linux.
a. Backing up MBR
Because the MBR makes up the first 512 bytes of the disk, we just need to copy that block size
# dd if=/dev/sda of=/tmp/sdambr.img bs=512 count=1
With the count=1 and bs=512, only 512 bytes will be copied which corr
b. Backing up the boot data of MBR excluding the
partition table
The MBR 512 bytes data is located at the first sector
of the hard disk. It consists of 446 bytes bootstrap, 64 bytes partition table
and 2 bytes signature. It means that we can exclude the partition table and
bytes signature while backing up the MBR with conserving only a block size
equal to the bootstrap size.
# dd if=/dev/sda of=/tmp/sdambr2.img bs=446 count=1
c. Restoring MBR from MBR image
You can restore your MBR as shown on the previous commands with
# dd if=/tmp/sdambr.img of=/dev/sda
4. Converting case of a file
dd command can be also used for an amazing thing. It
can convert all text (alphabets) in a file to upper or lower case and vice
versa. For the example below, we will have a file for the tests.
# cat file10
test dd convert
a. Converting a file to uppercase
Because our text file example is on lowercase, we will
convert it to uppercase
# dd if=~/file10 of=~/file20 conv=ucase
The command will create the new file indicated. See that now conv option takes ucase value. Let's check the result
# cat file20
TEST DD CONVERT
b. Converting a file to lowercase
Now we will do the reverse operation which will
convert to lowercase
# dd if=~/file20 of=~/file30 conv=lcase
See that we use lcase of conv option to convert from upper case to lower case.
# cat file30
test dd convert
Note: dd command does not convert the file names, only
its content
2. Creating virtual filesystem and backup images of CD
or DVDs as iso files
You can need to create a virtual filesystem on Linux
for some reasons as creating a virtual machine on your Linux host. You can also
need to create a backup iso image of a CD or DVD
a. Creating a virtual filesystem
A virtual filesystem is a filesystem that exists in a
file, which in turn exists on a physical disk. You can need it to create for
example an additional swap or loop device or a virtual machine. We need
/dev/zero which is a file used to create a file with no data but with required
size (a file with all zero’s). In other words, this will create a data file
with all zeros in the file which will give the size to a file.
# dd if=/dev/zero of=/file bs=1024K count=500
500+0 records in
500+0 records out
524288000 bytes (524 MB) copied, 1.21755 s, 431 MB/s
The option count refers to the number of input blocks
to be copied. Combined with block size value, it indicates the total size to
copy. For example bs=1024k and count=500 give a size=1024K*500 =524288000 bytes
=524MB
Now let's check the size of our file
# ls -lh /file
-rw-r--r-- 1 root root 500M May 17 18:57 /file
You can see that we have our virtual filesystem
created with the size indicated. You can now use it to create loop device or a
virtual disk or anything else.
b. Modify the first 512 bytes of a file with null data
If during the operation you indicate an existing
output file, you will lose its data. For some reasons, you can need to replace
a block size of the output file.
#dd if=/dev/zero of=file1 bs=512 count=1 conv=notrunc
The notrunc option refers to do not truncate the file, only replace the first 512 bytes, if it exists. Otherwise, you will get a 512 byte file
c. Creating a backup iso image of CD or DVD
You may wonder why not just copy the contents of your
CD to a directory. How would you handle the boot sector of a CD? You can’t find
that as a file on the device because it’s just the first sector. Because dd
copies sector by sector, on the other hand, it will copy that information as
well.
# dd if=/dev/cdrom of=/mycd.iso
You need to know that you have to use the -o loop option, which allows you to mount a file like any normal device. So, to mount /mycd.iso on the /mnt/cd directory, do as below
No comments:
Post a Comment