~How to make a disk image ~~intro While one can simply copy files off of a disk, copying them onto new storage media to make a backup of its contents, sometimes it is necessary to make a copy of the whole disk as-is. A 1:1 copy of the disk, preserving all of its "block level" (below the file system) quirks and characteristics is called a "disk image." The following are two ways to make a disk image ~~ What you may need - External hard drive with enough space to fit the disk you want to copy - A Mac or Linux Laptop - dd or ddrescue software tools - a bootable usb thumbdrive with a copy of your favourite linux distro or BSD on it *(see lore 02_bootmedia.md for how to make one)* - a boot floppy *(see lore 02_bootmedia.md for more info)* ~~ How to do it while the disk is still attached to its host machine 1. If the disk is still attached to its host machine, check whether the host still works. If it can still boot, you're in luck! You can move onto step 2. If not, remove the hard drive from the computer and move to "How to do it when the disk is out of the machine." 2. Boot into the machine's BIOS *(see lore 02_bootmedia.md)* and check to see what kinds of media the computer can boot from. If it can boot from a usb prepare your bootable usb thumb drive. If it cannot boot from usb, you might have to either make a bootable CD or use your boot floppy to daisychain the usb drive *(see lore 02_bootmedia.md)*. 3. Use whichever boot media works to boot into your favourite linux distro *(see lore 02_bootmedia.md for how to do this)*. Make sure the distro you've chosen works on the host machine. You'll be using the command line so the distro you choose does not need any bells and whistles included like a desktop environment. I usually choose something like Devuan, Void, FreeBSD, or NetBSD since they're all very lightweight and come with all the tools we need. **note: maybe make a boot disk image for us** 4. Once you've booted in, make two folders as the root user: mkdir /mnt/disk mkdir /mnt/backup 5. Plug in your external hard drive and mount it to `/mnt/backup` *(see lore 03_mountingdisks.md)*. 6. Now we need to figure out where the target disk actually is, that is, what its named. We can do this by checking the boot log by running dmesg as root. If you're working without a desktop environment, you might need to run it like this: `dmesg | less`. This will give you the ability to scroll through the text. Look for places in the log where the computer identifies hard drives. You'll know which drive is your target since it is ont the same size as your bootable usb or external drive. 7. Once you have identified it, do a directory listing as follows to see how many partitions there are on the disk: `ls /dev`. If, for example, your disk is called "sda" and has three partitions, they will be sda1, sda2, and sda3. You can mount any of the partitions to `/mnt/disk` if you want to check what the contents are. Remember to unmount them when you are done. 8. Now we can make the disk image. Do it as follows: `dd if=/dev/sdx of=/mnt/backup/mydisk.img`. Add the partition number to the input file if you just want to image one partition. 9. Enjoy your freshly baked disk image! ~~ How to do it when the disk is out of the machine 1. First, identify what kind of drive you're working with. Most newer drives use a SATA interface. Older drives might be IDE or SCSI. Most old apple drives are SCSI and are often proprietary any may require a specific kind of adapter. 2. Plug the drive into your laptop with the requisite adaptor, eg. SATA/IDE/SCSI to usb. 3. Now we have to identify where the drive is. Run dmesg as root and look at the bottom to see where the drive appears in the device file system, `/dev`. It should be named something like "sdx". You can do a directory listing of /dev if you want to check how many partitions are on the disk. 4. Make a couple folders in your /mnt directory where you can mount your target drive to check if its readable and an external hard drive to hold the disk image if you don't want to fill up disk space on your laptop. Do it like this: mkdir /mnt/disk mkdir /mnt/backup 5. You can now mount the target hard drive to `/mnt/disk` if you want to check the files it holds. Remember to unmount it before you make the disk image. If, for example, your disk is called "sda" and has three partitions, they will be sda1, sda2, and sda3. 6. Mount the external hard drive if you're using it to `/mnt/backup` 7. Now we can make the disk image as follows: `dd if=/dev/sdx of=/mnt/backup/mydisk.img`. Add the partition number to the input file if you just want to image one partition. ~~ Things of note - You can make a compressed disk image to save space by "piping" the output of dd to a compression program. If you do this, you need to decompress the disk image before you access it or use an compressed filesystem mounting utility like archivemount. Do it like this (an example with bzip2): `dd if-/dev/sdx | bzip2 --best > /mnt/backup/mydisk.bz2` - Different archiving programs are better at compressing the disk image or take less time to do the compression. Some good ones to use are gzip, bzip2, and 7zip. You can also use tar to collect multiple disk images into a single file.