Write Zeros to unused space in Linux

From ETCwiki
Jump to navigationJump to search

Recently I had to backup my main linux partition. I was limited (or rather wanted to challenge myself) to using a FAT32 file system (max limit 4GB file size). This becomes a problem when my source drive (200GB) is copied block for block, including all of the unused space (which is not empty, but is filled with remnants of old files). This would mean my Linux dd backup would include all 200GB of data, both relevant and remnant files leftover. My 4GB limit max would not feel well if I asked it to backup a 200GB image file of my drive. So here is another way:

My Setup

  • Hard drive: Western Digital Blue 320gb (/dev/sdb)
  • Partitions on /dev/sdb: /dev/sdb1 (200GB, EXT4, 5GB used) , /dev/sdb6 (120GB, FAT32, 0GB used) , and a swap partition (10GB)
  • OS: Linux Mint 15 Mate Desktop x64
  • CPU: i5-2500k
  • RAM: 4GB
  • GFX: 560GTX
  • Motherboard: Gigabyte z68xp-ud3p

My Goal

  • Backup my 200GB main Linux partition (/dev/sdb1 , ext4 , about 5GB of actual data) to my storage partition (/dev/sdb6 - 120GB free, FAT32 so 4GB max file size)
  • I want to be able to do this without changing my storage drive to NTFS or ext4 for compatibility reasons
  • I want to take all of the free space on /dev/sdb1 and write ZERO's to any area without live data.

My Method

  • Write Zeros to all unused space on /dev/sdb1 (makes it way easier to compress)
  • Use dd to read from /dev/sdb1 and output to bzip2 to be zipped heavily, then output to a file on my FAT32 drive mapped to /storage


Step by Step Procedure

  1. apt-get install zerofree
  2. dd if=/dev/sdb1 conv=sync,noerror bs=64K | bzip2 -z --best > /storage/sdb1.img.bz2
    1. dd is the dump disk command
    2. if=/dev/sdb1 is the source drive (the Input File)
    3. conv=sync,noerror tells the drive to write anything instead of skipping bad blocks
    4. bs=64K blocksize of each backup block
    5. | Pipe the output to next command
    6. bzip2 A great compression program (slow but heavy compression)
    7. -z Force a zip operation
    8. --best Choose best compression (slower!)
    9. > Output the whole bzip operation to....
    10. /storage/sdb1.img.bz2 the output file, already zipped, on my FAT32 drive mounted at /storage