Linux NTFS: Difference between revisions

From miki
Jump to navigation Jump to search
Line 70: Line 70:


== NTFSClone ==
== NTFSClone ==
=== Backup a fresh Windows PC ===
Here the procedure I follow anytime I get a new laptop installed with Windows. The goal of this procedure:
* Make a complete backup of all Windows partitions<br/>So that it can be reinstalled even in case of a complete disk crash
* Reduce the size of Windows partitions<br/>So that to install Linux alongside

This procedure assumes there is only one disk (<tt>/dev/sda</tt>), and that we boot from a Linux USB stick.

# Backup the MBR (Master Boot Record), and table of partitions:
<source lang="bash">
sudo dd if=/dev/sda of=mbr-mypc-sda-20111201.mbr bs=512 count=2048 # Backup first 2MB to make sure we get MBR + any extension
sudo sfdisk -l -uS /dev/sda > sfdisk-mypc-sda-20111201.txt
sudo sfdisk -d /dev/sda > sfdisk-mypc-sda-20111201.dump # We can restore with sudo sfdisk /dev/sda < sfdisk-mypc-sda-20111201.dump
</source>
# Backup the Windows partition(s) as is.<br/>This is just for safety in order to make sure we can recover in case the procedure fails:
<source lang="bash">
sudo ntfsclone --save-image -o - /dev/sda1 | gzip -c > ntfsclone-mypc-sda1-20111201.img.gz
sudo ntfsclone --save-image -o - /dev/sda2 | gzip -c > ntfsclone-mypc-sda2-20111201.img.gz # In case there are other partition
</source>
# Delete the ''Windows page file'' and ''hibernation file'' (or better yet, reboot in Windows, and ''disable virtual memory'' and ''hibernation'')
# Shrink the partition
# Backup the partition again + MBR
# Delete everything
# Restore the backup (shrunk) + MBR
# Resize the partition
# Reboot, restore windows hibernation and page file

* Save the session log
* Write a description of the pc (brand, etc), short description of the partitions


=== Backup an NTFS partition ===
=== Backup an NTFS partition ===
Backup an NTFS partition to an image file
Backup an NTFS partition to an image file

Revision as of 23:03, 1 December 2011

This page is part of the Linux Disk Management pages.

Create

# 1) Create partition using sfdisk
# 2) Format the partition
mkntfs -Q -v -L "Data" /dev/sda3

Mounting

References

Mounting an NTFS partition using file /etc/fstab

  • First get the UUID of the partition to mount
sudo blkid
  • Edit /etc/fstab accordingly:
UUID=XXXXXXXXXXXXXXXXXXXXX /media/windows ntfs defaults,umask=007,gid=46 0 1
Note that gid=46 refers to plugdev group.
  • Now the partition can be mounted with:
sudo mount /media/windows

Mounting a NTFS partition using command mount

  • To mount an NTFS partition /dev/sda1 to mount point /media/windows:
sudo mkdir /media/windows
sudo chgrp plugdev /media/windows
sudo mount -t ntfs -o defaults,umask=007,gid=46 /dev/sda1 /media/windows

NTFSFix

Use ntfsfix to fix errors on the NTFS partition (from package ntfsprogs). Say your partition is on /dev/sda1:

sudo ntfsfix /dev/sda1

This fix errors like:

$MFTMirr does not match $MFT (record 1).

NTFSResize

Change the size of an existing NTFS partition

$ fdisk -l /dev/sda                               # List information on partitions on /dev/sda
$ ntfsresize --info /dev/sda1
$ ntfsresize --no-action --size 20152M /dev/sda1  # Testing
$ ntfsresize --no-action --size 20151M /dev/sda1  # Testing
$ ntfsresize --size 20151M /dev/sda1
$ ntfsresize --size 20000M /dev/sda1

Resize an NTFS partition

The easiest is to use the graphical tool gparted. Otherwise here is the equivalent command sequence (shrink partition /dev/sda2 to 35000MB):

sudo sfdisk ...                                                       #Calibrate /dev/sda2 (get partition info)
sudo ntfsresize -i -f -v /dev/sda2                                    #Check file system
sudo ntfsresize --force /dev/sda2 --size 35000MB --no-action          #Shrink file system, test first...
sudo ntfsresize --force /dev/sda2 --size 35000MB                      #... do it
sudo sfdisk ...                                                       #Shrink MBR partition
sudo ntfsresize -i -f -v /dev/sda2                                    #Check file system
sudo ntfsresize --force /dev/sda2 --no-action                         #Grow file system to fill partition, test first...
sudo ntfsresize --force /dev/sda2                                     #... do it

NTFSClone

Backup a fresh Windows PC

Here the procedure I follow anytime I get a new laptop installed with Windows. The goal of this procedure:

  • Make a complete backup of all Windows partitions
    So that it can be reinstalled even in case of a complete disk crash
  • Reduce the size of Windows partitions
    So that to install Linux alongside

This procedure assumes there is only one disk (/dev/sda), and that we boot from a Linux USB stick.

  1. Backup the MBR (Master Boot Record), and table of partitions:
sudo dd if=/dev/sda of=mbr-mypc-sda-20111201.mbr bs=512 count=2048     # Backup first 2MB to make sure we get MBR + any extension
sudo sfdisk -l -uS /dev/sda > sfdisk-mypc-sda-20111201.txt
sudo sfdisk -d /dev/sda > sfdisk-mypc-sda-20111201.dump                # We can restore with sudo sfdisk /dev/sda < sfdisk-mypc-sda-20111201.dump
  1. Backup the Windows partition(s) as is.
    This is just for safety in order to make sure we can recover in case the procedure fails:
sudo ntfsclone --save-image -o - /dev/sda1 | gzip -c > ntfsclone-mypc-sda1-20111201.img.gz
sudo ntfsclone --save-image -o - /dev/sda2 | gzip -c > ntfsclone-mypc-sda2-20111201.img.gz   # In case there are other partition
  1. Delete the Windows page file and hibernation file (or better yet, reboot in Windows, and disable virtual memory and hibernation)
  2. Shrink the partition
  3. Backup the partition again + MBR
  4. Delete everything
  5. Restore the backup (shrunk) + MBR
  6. Resize the partition
  7. Reboot, restore windows hibernation and page file
  • Save the session log
  • Write a description of the pc (brand, etc), short description of the partitions


Backup an NTFS partition

Backup an NTFS partition to an image file

Before doing the backup
  • Shrink the partition to its minimum size (maybe shrinking filesystem is enough)
  • Delete Windows page file if necessary (Or better, disable virtual memory in Windows)
sudo ntfsclone --save-image -o - /dev/sda1 | gzip -c > backup-20090908.img.gz
After the backup
  • Grow the partition to its full size (ntfsresize --force /dev/sda2)
  • If needed, enable virtual memory in Windows

Restore an NTFS partition

Restore an NTFS partition from an image file

gzip -d -c backup-20090908.img.gz | sudo ntfsclone --restore-image --overwrite /dev/sda1 -

Mount an NTFSClone image

Convert an image file to a file, and mount it

gzip -d -c backup-20090908.img.gz | sudo ntfsclone --restore-image --overwrite backup.img -
mount -t ntfs -o loop backup.ntfs /mnt/ntfsclone

! bug ! If you get an error Failed to read last sector (...): Invalid argument, it is probably because the size of the image volume created by ntfsclone does not match the Image device size (the last cluster is incomplete). To fix this bug, simply pad the file with zeroes as necessary. Let's assume the following output for ntfsclone:

...
Cluster size           : 4096 bytes
Image volume size      : 48057282560 bytes (48058 MB)
Image device size      : 48057286656 bytes
...

However the file generated by ntfsclone is smaller than the original device size:

-rw------- 1 root     root     48057283072 2010-03-26 00:07 e6500.ntfs

We see that 3584 bytes are missing. Simply pad the file:

dd if=/dev/zero bs=3584 count=1 >> e6500ntfs
mount -t ntfs -o loop backup.ntfs /mnt/ntfsclone

We can also use sudo truncate (extremely fast):

sudo truncate --size=48057286656 e6500.ntfs

Resizing an NTFSClone image

ntfsclone will fail if the original device size is bigger than the destination partition. To fix this, first resize the ntfsclone image by using a loop devices.

gzip -d -c backup.img.gz | sudo ntfsclone --restore-image --overwrite backup.img -
sudo losetup /dev/loop0 backup.img
sudo ntfsresize --no-action --size 20000M /dev/loop0       # Testing
                                                           # If this fail with "Failed to read last sector", increase a bit the size
                                                           # of 'backup.img' using sudo truncate
sudo ntfsresize --size 20000M /dev/loop0
sudo truncate --size=20000M backup.img                     # For more safety, takes actual size of volume + cluster size
sudo ntfsfix /dev/loop0                                    # Fix any error on the volume
sudo losetup -c /dev/loop0                                 # Sync loop device with new file size
sudo ntfsclone --save-image -o - /dev/loop0 | gzip -c > backup-resized.img.gz
sudo losetup -d /dev/loop0

When truncating the file, check first the actual volume size (by calling ntfsclone), and use that size (that should be rounded to cluster size) plus another cluster size.

Windows 7

Since Vista / Windows 7, Windows will use MBR Disk Signature to identify the hard drive and assigns it the proper drive letter. If this signature changes, Windows will see the disk as a new drive and assign it a new drive letter (typically D:). Since Windows will not find the boot drive (should be C:), boot will fail with an error 0xc00000e:

0xc00000e : Can't run WINLOAD.EXE

Workaround:

  1. Boot with the Windows7 DVD
  2. Choose the Repair option
  3. Reboot the repaired Windows7
  4. Remove all entries in the Registry HKLM\System\MountedDevices\ except Default
  5. Reboot with CloneZilla to make a new image
  6. Restore the new image: You should now be able to boot Windows7

Also better save the MBR for later reuse if needed with

sudo dd if=/dev/sda of=sda.mbr bs=512 count=63

Reference: