microsoft_windows:clone_disk_using_linux
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
— | microsoft_windows:clone_disk_using_linux [2019/01/29 02:05] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Clone an NTFS disk using Unix ====== | ||
+ | Unix based rescue disks can be used to efficiently move an existing NTFS file system to a new disk using the common utility // | ||
+ | * Much faster as ntfsclone only copies used space on the source disk | ||
+ | * Target disk can be any size equal to or greater than the sum of the source partitions | ||
+ | * " | ||
+ | |||
+ | **Note**: With all these procedures, it is best to start with a clean file system. Run a scan disk (from inside Windows, or using //chkdsk C: /f /r// before you do anything, if possible. | ||
+ | |||
+ | **Note**: After any of these procedures, the NTFS partitions should be marked as //dirty//, meaning Windows will automatically perform a chkdsk on them. Do **not** bypass this; let Windows perform a disk scan and attempt to fix errors. If by some chance this is not initiated automatically, | ||
+ | |||
+ | ===== Tools Needed ===== | ||
+ | |||
+ | You will need the following tools, generally available on all Linux distributions. | ||
+ | * **dd** Device Duplicate, available on all Unix based systems as far as I know | ||
+ | * **sfdisk** disk partitioner, | ||
+ | * **ntfsclone** efficiently copy ntfs file system from one source partition to a target partition. Available on Linux and most Unix systems in the package ntfsprogs | ||
+ | * **gparted** // | ||
+ | * **kpartx** // | ||
+ | * **pbzip2** // | ||
+ | |||
+ | All of these tools can be found on several " | ||
+ | |||
+ | ===== Procedure ===== | ||
+ | ==== using ntfs-clone ==== | ||
+ | - // | ||
+ | - Copy MBR from source to target (may not be needed, but I do it for safety) | ||
+ | - Clone partition table from source to target | ||
+ | - Copy file system from each source partition to target partition. | ||
+ | - // | ||
+ | |||
+ | ==== using dd to duplicate partition ==== | ||
+ | - // | ||
+ | - use the dd command to copy the partition to a file, optionally using a compressor to make it smaller | ||
+ | |||
+ | |||
+ | ===== Examples ===== | ||
+ | |||
+ | ==== Direct Disk Clone ==== | ||
+ | Following example assumes your old disk is /dev/sda, with two partitions, /dev/sda1 and /dev/sda2. We have a new disk, /dev/sdb, which is the same size or larger than the sum of space used by /dev/sda1 and /dev/sda2. | ||
+ | |||
+ | **Be Very Careful** forgetting which disk is the target and which is the source can result in loss of data. Make backups, and read each command carefully before executing. This is especially important with the command ntfsclone, which is counter-intuitive. | ||
+ | |||
+ | <code bash> | ||
+ | # copy MBR, first 446 bytes in disk | ||
+ | dd if=/dev/sda of=/dev/sdb bs=446 count=1 | ||
+ | # copy the partition table to the new device. | ||
+ | # New device MUST be same size or larger than original one | ||
+ | sfdisk -d /dev/sda | sfdisk sdb | ||
+ | # optional, set DMA on both drives to speed access | ||
+ | hdparm -d 1 /dev/sda | ||
+ | hdparm -d 1 /dev/sdb | ||
+ | # use ntfsclone to copy from /dev/sda1 to /dev/sdb1 in rescue mode | ||
+ | ntfsclone --rescue --force --overwrite /dev/sdb1 /dev/sda1 | ||
+ | # copy second partition | ||
+ | ntfsclone --rescue --force --overwrite /dev/sdb2 /dev/sda2 | ||
+ | </ | ||
+ | |||
+ | * The first command simply copies the first 446 bytes of the source to the target disk; this is the Master Boot Record, and it might be set up already, but doesn' | ||
+ | * The //sfdisk// command is only one way of copying a partition table. You can do the same thing using fdisk -l /dev/sda, copying the information, | ||
+ | * The //hdparm// commands simply ensure both drives can use Direct Memory Access, which greatly speeds reading and writing. Optional, but this can significantly reduce the time to make the partition copies. | ||
+ | * Finally, the two // | ||
+ | * The other two parameters to // | ||
+ | |||
+ | ==== Save to a compressed file ==== | ||
+ | |||
+ | ntfsclone is also very good for making an efficient backup of an NTFS partition. Save the output to a file instead of a partition; it will create an output file only large enough to store the data on the partition. Adding compression will further reduce storage requirements | ||
+ | |||
+ | <code bash> | ||
+ | ntfsclone --output - / | ||
+ | </ | ||
+ | |||
+ | Note that pbzip2 uses a nice threaded setup that will max out all of your processor cores, resulting in an amazing space reduction. In one test, a 500G partition image with 172G used space, including all programs and the operating system, was compressed into a 25 gigabyte file. Since ntfsclone "zero fills" unallocated sectors, compression gives you fantastic results. | ||
+ | |||
+ | ===== Disk Images ===== | ||
+ | |||
+ | Disk images can be a single file or an LVM2 logical partition or something that is not a physical device. In this case, you need to work with the partitions embedded in the image. | ||
+ | |||
+ | There are some older ways of directly accessing the partitions, but if you have the command '' | ||
+ | |||
+ | <code bash> | ||
+ | # display what will happen first | ||
+ | kpartx -lv / | ||
+ | # now, actually add the partitions to /dev/mapper | ||
+ | kpartx -av / | ||
+ | ######## do your work ########## | ||
+ | # unmap the partitions | ||
+ | kpartx -dv / | ||
+ | </ | ||
+ | |||
+ | In this case, kpartx -av will create the mapping and show you the resulting paths to the partitions. They are generally the same as the device itself, with the partition number at the end. You can now work with the partitions as if you were working with a disk partition; cloning and restoring. | ||
+ | |||
+ | Prior to making the copy, it is a good idea (though not a requirement) to zero out all of the unused parts of the hard drive. The following assumes you have an lvm block that is used by a Windows machine (Windows 7 to whatever) named ' | ||
+ | |||
+ | <code bash> | ||
+ | kpartx -lv / | ||
+ | kpartx -av / | ||
+ | ntfs-3g / | ||
+ | # there are two files in the root directory of the disk which are for hibernation and swap space | ||
+ | # they can be safely removed if you did not hibernate the disk | ||
+ | rm / | ||
+ | rm / | ||
+ | rm -fR / | ||
+ | mkdir / | ||
+ | df -h # see how much free space we have, use that number in the following command | ||
+ | # in the following command, change 20 to the amount of space you want to zero out | ||
+ | for i in {01..20} ; do echo Loop $i ; dd if=/ | ||
+ | # remove the files created | ||
+ | rm -fR / | ||
+ | # unmount the partition | ||
+ | umount /mnt | ||
+ | # delete the partition mapping | ||
+ | kpartx -dv / | ||
+ | dd if=/ | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Resizing ===== | ||
+ | |||
+ | I use [[http:// | ||
+ | |||
+ | That being said, here is how to manually resize without resorting to // | ||
+ | |||
+ | <code bash> | ||
+ | # manually reduce /dev/sda2 to 100G. | ||
+ | # /dev/sda2 has less than 100G of data on it. | ||
+ | ntfsresize --size 100G /dev/sda2 | ||
+ | fdisk /dev/sda2 | ||
+ | # in fdisk, use the " | ||
+ | # then delete /dev/sda2 | ||
+ | # Add /dev/sda2 starting point | ||
+ | # and extending it to 100G | ||
+ | </ | ||
+ | |||
+ | The opposite, enlarging a partition, is done just backwards from reducing | ||
+ | |||
+ | <code bash> | ||
+ | fdisk /dev/sda | ||
+ | # use the ' | ||
+ | # delete /dev/sda2 | ||
+ | # recreate it from the same starting point | ||
+ | # and extending it out to whatever size you want | ||
+ | ntfsresize /dev/sdb | ||
+ | # with no size parameters, ntfsresize will grow | ||
+ | # to the size of the partition | ||
+ | </ | ||
+ | |||
+ | While there are no known issues with either ntfsresize or fdisk, there is always the chance you'll mess up and trash something you didn't want to, so it is definitely good to make a backup. | ||
+ | |||
+ | You'll see why I like gparted! The authors have done a great job bringing together the ntfs-progs toolbox and partition tools. Makes it less likely to mess up. | ||
+ | |||
+ | ===== References ===== | ||
+ | * [[https:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// |
microsoft_windows/clone_disk_using_linux.txt · Last modified: 2019/01/29 02:05 by 127.0.0.1