Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a Linux system with only one partition /dev/sda1 on /dev/sda disk with / mount point. Is it possible to create partitions after installation?

e.g. create /dev/sda2 with the mount point /home/

How can I achieve it from command line? How is it physically implemented? (If I later remove /dev/sda1 will it remove my /home/ directory because it's a subdirectory of /? I'd like this partition to survive and I could install new system someday.

share|improve this question
    
If you do this much, I suggest using LVM, which does make this a little more pleasant. –  Faheem Mitha Dec 8 '11 at 21:12

2 Answers 2

up vote 7 down vote accepted

To create a new partition in an already partitioned system you should start with a livecd, then use gparted to shrink the sda1 partition (that I assume take all the space on the disk, excluded the swap), then create a new partition in the free space created.

It can be of course done on the command line, but GParted take care of many details that it is difficult to track.

After create the partition, you should move the content of your current home on the new partition, then modify /etc/fstab to mount the new partition on /home.

Here's one way to actually move the files from sda1 to sda2. All this is done as root:

  1. Take a backup. Have you tried restoring your backups? They're not backups until you do. And make sure that they're stored on a different machine (that isn't mounted), or on an external drive that is currently detached. The point is, rm -Rf / shouldn't delete them. Nor should mkfs on the wrong partition.
  2. Read all the steps and make sure you fully understand them before starting. Except for take a backup. You should do that immediately if you haven't already.
  3. Make a filesystem on sda2. Something like mkfs.ext4 /dev/sda2 (assuming you're fine with ext4, which is probably what you're currently using on sda1).
  4. Mount sda2 to somewhere that isn't /home. For example, you could mkdir /mnt/new_home and then mount -text4 /dev/sda2 /mnt/new_home
  5. Make sure no users are logged in—otherwise they may be trying to change their files as you do this.
  6. Copy the files. First, confirm that /home doesn't have anything hidden (only dot entries should be . and ..) by doing ls -l /home. If it does, you need to copy the separately (* will not match them, and .* doesn't do what you want): cp -a /home/* /mnt/new_home/
  7. Stop now if there are any errors copying.
  8. Unmount sda2: umount /mnt/new_home. Do it now to make sure you don't accidentally nuke the wrong one.
  9. This is the point of no return. rm -Rf /home/*. Absolutely, positively, do not try /home/.* here. Also, make sure not to accidentally add a space in that path. You should be at least a little afraid wile typing this command, it is a scary command, and you should tripple-check you've entered it correctly (otherwise, you'll be needing that backup).
  10. If there are any dot-files or folders in /home, remove them by name. Remember, you must not ever run rm -Rf .*. That'd match .., which means parent directory, ultimately leading to complete filesystem destruction (though some versions of rm may refuse to do this, you shouldn't depend on that).
  11. Edit /etc/fstab and add an entry for your new sda2 partition, like this: /dev/sda2 /home ext4 relatime,user_xattr,errors=remount-ro 0 2. You may use different flags if you like, those are the ones I use. Also, that pass number (2) assumes that you've only got two filesystems on sda, which seems very likely since this is sda2.
  12. Finally, mount your new home mount /home. This should run without errors, and your files should be there. If you get errors, check your fstab line. If you get a warning about files under the mount point being hidden, you didn't delete everything in the last few steps: unmount /home and delete the remainder. If files are missing, see the first step.
share|improve this answer
    
If you would know please write what happens after removing /dev/sda1. I'm afraid because /home/ is subdirectory of / –  xralf Dec 8 '11 at 20:24
    
@xralf: You don't remove sda1, you resize it. But of course, you should have backups! –  derobert Dec 8 '11 at 22:22
    
@derobert I meant "after resizing" when I already have /dev/sda1 and /dev/sda2 and I decide to remove /dev/sda1 If my /home/ directory will be lost or if it will be still on /dev/sda2 –  xralf Dec 9 '11 at 8:15
    
@xralf: You copy your home directory to the new filesystem you create on /dev/sda2... I'll edit the post to add instructions. –  derobert Dec 9 '11 at 16:34
1  
I have proposed an edit to your answer to add in the steps for actually moving the files, since @xralf sounds like he/she needs some guidance on that. –  derobert Dec 9 '11 at 16:51

Each partition is a separate region of the disk.

If you've left some unpartitioned space, you can create a new partition there with fdisk, cfisk, parted or any other partition manager. Otherwise you'll need to shrink your existing filesystem to make room for the new one. Today's filesystems do not support shrinking while mounted), so you'll need to do that while booting from a live CD/USB (such as the Parted live CD).

After you've created a separate partition for /home:

  1. Create a filesystem on it. This operation may be combined with the creation of the partition with some tools.
  2. Mount the new filesystem manually to a location that is an empty directory, e.g. mount /dev/sda2 /media/new_home.
  3. Move your files from /home to the new filesystem:

    mv /home/* /media/new_home
    

    You should do that while not logged into your account: either from a root login prompt in text mode, or from a live CD still.

  4. Unmount the new filesystem: umount /media/new_home
  5. Edit the file /etc/fstab to add an entry for the new partition. Something like (to go below the entry for /):

    /dev/sda2  /home  ext4  errors=remount_ro,acl  0 2
    

    Instead of /dev/sda2 which could change if you add a second disk, you should use a UUID or a label. See What happens to partition labels after removing a partition?

  6. If on a live CD, reboot. Otherwise, run mount /home.

What mount points exist on a typical Linux system? may be useful background.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.