个人工具

UbuntuHelp:Partitioning/Home/Moving

来自Ubuntu中文

跳转至: 导航, 搜索

The Advantages of This Guide

This guide was written simply because there were flaws in the other guides and it was worth recording these notes for future reference. Community Documentation is an excellent way to help others at the same time. This guide:

  • Aims to keep the system usable and the data all safe.
  • If the system unexpectedly loses power or goes into hibernate mode or anything at any point through this guide then it should still boot-up & work just fine. That's the main aim anyway.
  • Prepares Partitions and fstab first then only tweaks fstab at the end
  • People test and gain familiarity with the tools before the final crunch-test, another advantage of this guide. Some other guides use mount commands, then move the /home & then set-up fstab. However, if you mess up your fstab at that crunch-point then when you reboot your system will lose track of your /home files! Setting up fstab first and using "sudo mount -a" to mount the partitions checks the modified fstab works at a safe point in the process.
  • Use rsync to move the files
  • Rsync is designed for backups of /home, and much more. It is able to maintain other characteristics of the file, like permissions, ownership, and timestamps. There was a lot of debate about which command to copy your files to the new /home partition which stemmed from a time when cp was not able to do it properly (apparently it skipped files?). Cp was not designed to be a powerful backup tool, rsync was. The fix, then, was to use a combination of find and cpio (See section 8.3.5).

The Guide

Setting up /home on a separate partition is beneficial because your settings, files, and desktop will be maintained if you upgrade, (re)install Ubuntu or another distro. This works because /home has a sub-folder for each user's settings and files which contain all the data & settings of that user. Also, fresh installs for linux typically like to wipe whatever partition they are being installed to so either the data & settings need to be backed-up elsewhere or else avoid the fuss each time by having /home on a different partition.

Setup Partitions

This is beyond the scope of this page. Try here if you need help. Memorize or write down the location of the partition, something like /sda3. Its been suggested to use either ext2, ext3 or ext4 rather than vfat or ntfs. Using vfat is not supported and may fail, since vfat does not support permissions. Ntfs has an advantage of being journalised, like ext3 & ext4 but it can also be read easily by Windows. It is a lot less stable than ext3 or ext4 so it is really a question of stability compared with easy access for Windows. If you don't dual-boot with Windows or have plenty of hard-drive space then staying with very much safer ext3 or 4 is the obvious choice.

Find the uuid of the Partition

The uuid reference to all partitions get to a | command-line to try this

sudo blkid

Now you just need to note down (copy&paste into a text-file) the uuid of the partition that you have set-up ready to be the new /home partition. Some older releases of Ubuntu might not have the "blkid" command so this could be used instead

sudo vol_id -u <partition>

for example

sudo vol_id -u /dev/sda3

Setup Fstab

These commands should; 1. Create a backup of fstab. Replace "!DateToday" with today's date so you can find the right back-up easily if you do need to use it later. 2. Open the standard text-editor to edit fstab. Note that Kubuntu uses "kate" & Xubuntu uses "mousepad" instead of "gedit" but you could use any text-editor you have installed if you prefer.

sudo cp /etc/fstab /etc/fstab.DateToday
gksu gedit /etc/fstab

and add these lines into it

# (identifier)  (location, eg sda5)   (format, eg ext3 or ext4)      (some settings) 
UUID=????????   /media/home    ext3          nodev,nosuid       0       2 

Replace???????? with the UUID number of the intended /home partition. The Fstab location will be a temporary one - we are to mount the temporary location and copy the existing home to it. (/media/home is assumed in the rest of the guide). If /media/home does not exist yet, then you should create it with the command:

sudo mkdir /media/home

Now, mount the partition with:

sudo mount -a

Copy /home to the New Partition

sudo rsync -axS --exclude='/*/.gvfs' /home/. /media/home/.

The --exclude='/*/.gvfs' prevents rsync from complaining about not being able to copy .gvfs, but I believe it optional. Even if rsync complains, it will copy everything else anyway. (See here for discussion on this)

Check Copying Worked

The data in /home now exists in 2 places but we need to check that it looks about right. If it looks approximately the same then it is probably completely right.

A Sneaky Safety Manoeuvre

So you now have 2 copies of your /home folder. The new one on the new partition and the old one still in the same partition it was always in. The problem is how to delete the right one!! Since we are still using the old /home 'just' rename your current /home folder:

cd /
sudo mv /home /old_home

Now re-create a new blank /home directory just in case it has all gone wrong!

cd /
sudo mkdir /home/user

Now if things have gone wrong then the system will boot into what appears to be a fresh install with all your data missing. In fact, of course, there are still 2 copies of it all :)

Make the Switch

We now need to modify the fstab again to point to the new /home and mount it properly. So again on a command-line

gksu gedit /etc/fstab

and now edit the lines you added earlier to delete the /media part. This should make /media/home into /home as follows

# (identifier)  (location, eg sda5)   (format, eg ext3 or ext4)      (some settings) 
UUID=????????   /home    ext3          nodev,nosuid       0       2

and finally, remount the partition with:

sudo mount -a

Does it all still look fine? Can you still access your files & folders from the "Places" menu? Reboot to ensure that currently running programs use the /home that is on the new partition.

After a reboot

If everything is working, great, you can delete /old_home. If things aren't working, you can undo by moving your /old_home back to /home, and delete the line you added to fstab.

Technical Notes and Resources

Rsync was chosen over cp and find|cpio because it seemed to maintain permissions. http://ubuntu.wordpress.com/2006/01/29/move-home-to-its-own-partition/ http://ubuntuforums.org/showthread.php?t=46866