迁移home目录到独立分区


    home目录为普通用户的家目录,存放着用户的数据,时间一长数据就会变得越来越多,如文档、视频、工作记录等,容量也会随之增大,这时就需要想办法增加home目录的容量,此时就显得尤为重要,而普通分区时不能进行扩充容量的,就需要使用其他的办法——将home目录迁移到容量大的独立分区中。当然了,如果初始安装系统的时候就想到了,为home分区分区容量大的空间就不需要了,而当用户数量越来越多的时候,后续为home增大空间这还是很需要的。


一、创建分区

[root@localhost ~]# fdisk /dev/sda

Welcome to fdisk (util-linux 2.23.2).

 

Changes will remain in memory only, until you decide to write them.

Be careful before using the write command.

 

Command (m for help): n

All primary partitions are in use

Adding logical partition 6#分配分区号,之前的已经使用了,这里使用6

First sector (50579456-419430399, default 50579456):

Using default value 50579456

Last sector, +sectors or +size{K,M,G} (50579456-419430399, default 419430399): +10G                                                         #为分区指定容量,可以根据需要进行分配

Partition 6 of type Linux and of size 10 GiB is set

 

Command (m for help): w        #保存退出

The partition table has been altered!

 

Calling ioctl() to re-read partition table.

 

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.

The kernel still uses the old table. The new table will be used at

the next reboot or after you run partprobe(8) or kpartx(8)

Syncing disks.

 

[root@localhost ~]# ls /dev/sda*

/dev/sda  /dev/sda1  /dev/sda2  /dev/sda3  /dev/sda4  /dev/sda5

[root@localhost ~]# partprobe        #分区好了之后需要同步分区

[root@localhost ~]# ls /dev/sda*

/dev/sda  /dev/sda1  /dev/sda2  /dev/sda3  /dev/sda4  /dev/sda5  /dev/sda6

 

[root@localhost ~]# mkfs.ext4 /dev/sda6        #格式化分区

mke2fs 1.42.9 (28-Dec-2013)

Filesystem label=

OS type: Linux

Block size=4096 (log=2)

Fragment size=4096 (log=2)

Stride=0 blocks, Stripe width=0 blocks

655360 inodes, 2621440 blocks

131072 blocks (5.00%) reserved for the super user

First data block=0

Maximum filesystem blocks=2151677952

80 block groups

32768 blocks per group, 32768 fragments per group

8192 inodes per group

Superblock backups stored on blocks:

        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

 

Allocating group tables: done                            

Writing inode tables: done                            

Creating journal (32768 blocks): done

Writing superblocks and filesystem accounting information: done

 

[root@localhost ~]# blkid /dev/sda6        #使用blkid可以查看格式化之后的分区,也可以查看指定的分区

/dev/sda6: UUID="77481be9-726d-4830-97b5-ff0324026aa0" TYPE="ext4"

 

二、挂载分区

    先将/dev/sda6挂载到/mn/home目录下,新建一个其他的目录也可以,拷贝完数据后,再将新建分区挂载到/home目录下

 

[root@localhost ~]# mkdir /mnt/home

[root@localhost ~]# mount /dev/sda6 /mnt/home/

[root@localhost ~]# cd /home/

[root@localhost home]# ls

bash  basher  centos  cyh  gentoo  hht  jone  nologin  stud  test  testbash  tom  tom8  zhangsan

[root@localhost home]# mv * /mnt/home/        #移动home目录文件到刚挂载目录中

[root@localhost home]# cd /mnt/home/

[root@localhost home]# ls

bash    centos  gentoo  jone        nologin  test      tom   zhangsan

basher  cyh     hht     lost+found  stud     testbash  tom8

 

[root@localhost home]# cd

[root@localhost ~]# mount /dev/sda6 /home/

[root@localhost ~]# df -h

Filesystem      Size  Used Avail Use% Mounted on

/dev/sda5        20G  4.6G   16G  23% /

devtmpfs        899M     0  899M   0% /dev

tmpfs           913M   88K  913M   1% /dev/shm

tmpfs           913M  9.1M  904M   1% /run

tmpfs           913M     0  913M   0% /sys/fs/cgroup

/dev/sda1       197M  139M   59M  71% /boot

/dev/sda3       197M   11M  187M   6% /testdir

tmpfs           183M   24K  183M   1% /run/user/0

/dev/sdb1       976M  1.3M  924M   1% /mnt

/dev/sda6       9.8G   41M  9.2G   1% /home

[root@localhost ~]# umount /mnt/home/

 

三、编辑配置文件,让分区能开机自动挂载

    切记一定要将挂载条目写入到/etc/fstab配置文件中,要不然下次开机之后,普通用户就会找不到家目录,也就访问不了各自的数据了,终端提示符也会出现错误。

[root@localhost ~]# vi /etc/fstab

#

# /etc/fstab

# Created by anaconda on Thu Jul 21 11:23:38 2016

#

# Accessible filesystems, by reference, are maintained under '/dev/disk'

# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

#

UUID=80371af2-ee0e-481c-b71a-c9d25b11800a /          xfs     defaults        0 0

UUID=33974954-468b-490d-b84a-c955716d7146 /boot     xfs     defaults        0 0

UUID=346923cc-58f2-44b6-91d0-6ad8687ebd7b /testdir    xfs     defaults        0 0

UUID=981b1be0-81ec-46aa-bb2c-6d7f0d79aa46 swap      swap    defaults        0 0

UUID="77481be9-726d-4830-97b5-ff0324026aa0" /home   ext4    defaults    0   0                                                                                               

 

[root@localhost ~]# reboot        #重启测试

 

Last login: Wed Aug 31 15:24:51 2016

[root@localhost ~]# df -h

Filesystem      Size  Used Avail Use% Mounted on

/dev/sda5        20G  4.6G   16G  23% /

devtmpfs        899M     0  899M   0% /dev

tmpfs           913M     0  913M   0% /dev/shm

tmpfs           913M  9.0M  904M   1% /run

tmpfs           913M     0  913M   0% /sys/fs/cgroup

/dev/sda6       9.8G   41M  9.2G   1% /home

/dev/sda3       197M   11M  187M   6% /testdir

/dev/sda1       197M  139M   59M  71% /boot

tmpfs           183M     0  183M   0% /run/user/0

[root@localhost ~]# su - cyh

Last login: Wed Aug 31 15:54:45 CST 2016 on tty1

[cyh@localhost ~]$ pwd

/home/cyh

[cyh@localhost ~]$ touch 11

[cyh@localhost ~]$ ll 11

-rw-rw-r--. 1 cyh cyh 0 Aug 31 15:55 11