4

You can find plenty of info online about using dd to clone OS drives.

Don't listen! It's MUCH faster to use dump and restore because you only copy the data, rather than copying all the blocks (empty or not).

Part 1: Know where your source and destination drives are in the device list

lsscsi | grep sd*

will show you a list of scsi devices and their associated letters. If you are lucky enough to be working with a hot-swapable box, then you can simply run that command before and then after you insert the drive--the newest device to show up is, of course, the drive you just inserted.

Part 2: Prepare to dump

  1. Safety tip: assign variables for the destination and source devices. (Also, if you are doing this more than once, variablization allows you to reuse the commands.)

SOURCE=/dev/sdx DEST=/dev/sdy

  1. A note on the source drive (the one you are copying from). If you are copying the current OS drive, it will (duh) already be mounted. If you are copying a different drive, it actually doesn't need to be mounted.
  2. Copy the partition table of your source drive out to a file: sfdisk -d $SOURCE > part_table

  3. Copy the part_table already stored in a file sfdisk --force $DEST < part_table

  4. Zero out the boot sector: dd if=/dev/zero of=${DEST}1 bs=512 count=1

  5. Make your filesystem (one partition at a time): mkfs -t ext4 ${DEST}1 mkswap ${DEST}2

  6. Take a look: parted $DEST --script print

  7. Copy the label of all non-swap partitions. Example: tune2fs -L "/" /${DEST}1

Part 3: The dump | restore moment

  1. Make a directory to mount the destination device of the dump | restore. (As mentioned above, source device need not be mounted.) mkdir -p /mnt/${DEST}1
  2. Mount the destination device:mount -t ext4 ${DEST}1 /mnt/${DEST}1

  3. cd into the mount point:cd /mnt/${DEST}1

  4. Dump and restore: dump -a0f - /dev/${SOURCE}1 | restore -rf - (dump flags: a=autosize; 0 (zero)=start at block 0; f = file, - = stdout; restore flags: r=rebuild; f=file; - = stdout)

  5. The dump | restore should take just a few minutes.

Part 4: use grub to install a boot loader onto the cloned drive

  1. Assuming you are copying an OS drive (i.e., a drive off which you boot a box), you need to install a boot loader.

  2. Grub identifies disks as hd#, starting from 0 (NOT 1). The correspondence is easy: /dev/sda = hd0, /dev/sdb = hd1, etc.

     [root@drive-toaster /]# grub
    
     grub> root (hd1,0)  # use correct number for your disk!  
     root (hd1,0)   
     Filesystem type is ext2fs, partition type 0x83
    
     grub> setup (hd1) # use correct number for your disk!  
     setup (hd1)   
     Checking if "/boot/grub/stage1" exists... yes
     Checking if "/boot/grub/stage2" exists... yes
     Checking if "/boot/grub/e2fs_stage1_5" exists... yes
     Running "embed /boot/grub/e2fs_stage1_5 (hd1)"...  27 sectors are     embedded.
     succeeded
     Running "install /boot/grub/stage1 (hd1) (hd1)1+27 p
     (hd1,0)/boot/grub/stage2 /boot/grub/grub.conf"... succeeded
     Done.
    
     grub> quit
    
2
  • 1
    This should really be on super user.
    – fuz
    Sep 17, 2017 at 11:37
  • This is good (for another site) but it needs to be in a Q&A format.
    – halfer
    Aug 25, 2019 at 11:20

0

Browse other questions tagged or ask your own question.