linux用户家目录无损迁移到独立硬盘
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux用户家目录无损迁移到独立硬盘相关的知识,希望对你有一定的参考价值。
学完磁盘管理之后,不知道同学们有没有想过一个问题,之前安装操作系统的时候,用户家目录没有给单独的分区,而如今用户家目录里面的数据越来越大,已经占用了服务器磁盘的大量空间,如果任其发展的话,很有可能会因为家目录的数据占用完服务器的磁盘空间导致系统崩溃。做为名运维人员来讲,我们不可能坐视这种情况的发生吧。那该怎么办了,于是,很多人在想,我们能再用一个新的硬盘创建一个更大的分区给挂载到当前的系统home目录下吗。到底行不行,就让我们一起来验证一下吧。当然要想做此迁移操作,必须提前通知所有用户注销登录。否则用户在使用的过程中执行家目录的迁移操作肯定是会出错的。如果关于这一点做为运维人员的你都想不到的话,那就可以直接出局了。
第一步:装载一块新的硬盘到服务器上,并给它分区格式化。在这里,我用一块20G的新硬盘,20G分成一个区,并格式化
环境展示:
用户家目录没有单独分区,并且用户家目录下,已经产生了多个用户的数据
[[email protected] ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 200M 0 part /boot
├─sda2 8:2 0 5G 0 part /
└─sda3 8:3 0 5G 0 part /test
sr0 11:0 1 7.2G 0 rom
[[email protected] ~]# ll /home
total 12
drwx------. 5 hacker test 4096 Aug 27 19:42 hacker
drwx------. 5 share test 4096 Aug 27 19:43 share
drwx------. 6 test test 4096 Aug 26 15:31 test
[[email protected] ~]#
添加一块20G硬盘,不用重启,直接开始划分分区
[[email protected] ~]# echo "- - -" > /sys/class/scsi_host/host0/scan
[[email protected] ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 200M 0 part /boot
├─sda2 8:2 0 5G 0 part /
└─sda3 8:3 0 5G 0 part /test
sdb 8:16 0 20G 0 disk
sr0 11:0 1 7.2G 0 rom
[[email protected] ~]# fdisk /dev/sdb
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.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x0b8592c8.
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0b8592c8
Device Boot Start End Blocks Id System
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-41943039, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039):
Using default value 41943039
Partition 1 of type Linux and of size 20 GiB is set
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0b8592c8
Device Boot Start End Blocks Id System
/dev/sdb1 2048 41943039 20970496 83 Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[[email protected] ~]#
分完区之后,一定要记得通知内核重读一下磁盘分区表,否则格式化会提示找不到分区的
[[email protected] ~]# partprobe /dev/sdb
[[email protected] ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 200M 0 part /boot
├─sda2 8:2 0 5G 0 part /
└─sda3 8:3 0 5G 0 part /test
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 20G 0 part
sr0 11:0 1 7.2G 0 rom
[[email protected] ~]# mkfs.ext4 -L homedir /dev/sdb1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=homedir
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1310720 inodes, 5242624 blocks
262131 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2153775104
160 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, 2654208,
4096000
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
[[email protected] ~]#
第二步:全部用户都注销登录之后,可以直接将home目录重命名为home1,并新建home目录,将刚才格式化好的sdb1挂载到新建的home目录下,并将home1目录下所有用户的文件移回home目录,并删除home1目录,其实现在用户的数据也就移动到了sdb1分区上了。
[[email protected] ~]# mv /home /home1
[[email protected] ~]# mkdir /home
[[email protected] ~]# mount /dev/sdb1 /home
[[email protected] ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 200M 0 part /boot
├─sda2 8:2 0 5G 0 part /
└─sda3 8:3 0 5G 0 part /test
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 20G 0 part /home
sr0 11:0 1 7.2G 0 rom
[[email protected] ~]# ll /home
total 16
drwx------. 2 root root 16384 Aug 27 20:03 lost+found
[[email protected] ~]# mv /home1/* /home
[[email protected] ~]# ll /home
total 28
drwx------. 5 hacker test 4096 Aug 27 19:42 hacker
drwx------. 2 root root 16384 Aug 27 20:03 lost+found
drwx------. 5 share test 4096 Aug 27 19:43 share
drwx------. 6 test test 4096 Aug 26 15:31 test
[[email protected] ~]# ll /home1
total 0
[[email protected] ~]# rm -rf /home1
[[email protected] ~]#
第三步:让原有用户登录系统验证家目录是否正常,在这里,我就直接使用su - username登录式切换进行验证
[[email protected] home]# su - test
Last login: Sat Aug 27 20:31:39 CST 2016 on pts/0
[[email protected] ~]$ pwd
/home/test
[[email protected] ~]$ exit
logout
[[email protected] home]# su - share
Last login: Sat Aug 27 20:31:57 CST 2016 on pts/0
[[email protected] ~]$ pwd
/home/share
[[email protected] ~]$ exit
logout
[[email protected] home]# su - hacker
Last login: Sat Aug 27 20:32:41 CST 2016 on pts/0
[[email protected] ~]$ pwd
/home/hacker
[[email protected] ~]$ exit
logout
[[email protected] home]#
第四步:将home目录的挂载信息写入fstab配置文件,否则当前的挂载一但重启就会失效,用户的家目录也就找不到了
使用blkid /dev/sdb1查询sdb1分区的UUID,使用UUID写入fstab文件,会比较精确
[[email protected] home]# blkid /dev/sdb1
/dev/sdb1: LABEL="homedir" UUID="248d9c56-7795-4747-a251-0737240cb30a" TYPE="ext4"
[[email protected] home]#
/etc/fstab文件新添一行,添加格式如下(注意:UUID尽量采用复制,以防输入错误导致系统挂载不成功)
UUID="248d9c56-7795-4747-a251-0737240cb30a" /home ext4 defaults 0 0
至此,如果你是按照我的教程严格操作每一步的话,是不会出错的。原有没有单独分区的家目录,现在已经成功迁移到了20G的新硬盘分区上。那当然这个迁移后所给的硬盘大小,可以根据你的实际需求随意设定。
本文出自 “爱情防火墙” 博客,请务必保留此出处http://183530300.blog.51cto.com/894387/1843400
以上是关于linux用户家目录无损迁移到独立硬盘的主要内容,如果未能解决你的问题,请参考以下文章