在企业中我们可能会遇到这样的情况,就是home分区没有独立挂载现在想把home分区独立挂载出去,或者是占用空间太大,需要加一块磁盘把home分区挂载到大的磁盘空间里。这就是我们常说的迁移home分区问题,现在就跟大家谈谈如何迁移home分区
1,给服务器添加一块80G的硬盘,查看服务器磁盘信息
[root@localhost ~]# fdisk -l
Disk /dev/sda: 322.1 GB, 322122547200 bytes
255 heads, 63 sectors/track, 39162 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
DeviceBoot Start End Blocks Id System
/dev/sda1 * 1 13 104391 83 Linux
/dev/sda2 14 1318 10482412+ 83 Linux
/dev/sda3 1319 2623 10482412+ 83 Linux
/dev/sda4 2624 39162 293499517+ 5 Extended
/dev/sda5 2624 2884 2096451 82 Linux swap / Solaris
Disk /dev/sdb: 85.8 GB, 85899345920 bytes……此为刚加的那块硬盘
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk /dev/sdb doesn't contain a valid partition table
2,给sdb分区,全部给主分区1
[root@localhost ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table,nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain inmemory only,
until you decide to write them. After that, ofcourse, the previous
content won't be recoverable.
The number of cylinders for this disk is set to10443.
There is nothing wrong with that, but this is largerthan 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., oldversions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOSFDISK, OS/2 FDISK)
Warning: invalid flag 0x0000 of partition table 4will be corrected by w(rite)
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-10443, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-10443,default 10443):
Using default value 10443
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]#
3,给sdb1格式化
[root@localhost ~]# mkfs.ext3 /dev/sdb1
4,在/data/下建立user目录(用于存放home下的用户的家目录)
[root@localhost ~]# mkdir /data/user
5,把home下的所有文件移动到/data/user
[root@localhost ~]# mv /home/* /data/user/
[root@localhost ~]# ls -ld /data/user/*
drwx------ 3 frank frank 4096 02-20 20:41/date/user/frank
drwx------ 3 lili lili 4096 02-20 20:41/date/user/lili
drwx------ 3 mingfan mingfan 4096 02-20 20:41/date/user/mingfan
drwx------ 3 renlong renlong 4096 02-20 20:42 /date/user/renlong
drwx------ 3 wangwei wangwei 4096 02-20 20:42/date/user/wangwei
6,把sdb1挂载到/home/下
[root@localhost ~]# mount /dev/sdb1 /home/
[root@localhost ~]# df -h
文件系统容量已用可用已用%挂载点
/dev/sda3 9.7G 2.7G 6.6G 30% /
/dev/sda2 9.7G 151M 9.1G 2% /date
/dev/sda1 99M 12M 82M 13% /boot
tmpfs 500M 0 500M 0% /dev/shm
/dev/hdc 4.1G 4.1G 0 100% /media/RHEL_5.9 x86_64 DVD
/dev/sdb1 79G 185M 75G 1% /home……已挂载成功
7,把home下的文件全部移回去
[root@localhost ~]# mv /data/user/* /home/
[root@localhost ~]# ls -ld /home/*
drwx------ 3 frank frank 4096 02-20 20:41/home/frank
drwx------ 3 lili lili 4096 02-20 20:41/home/lili
drwx------ 2 root root 16384 02-20 20:37/home/lost+found
drwx------ 3 mingfan mingfan 4096 02-20 20:41 /home/mingfan
drwx------ 3 renlong renlong 4096 02-20 20:42 /home/renlong
drwx------ 3 wangwei wangwei 4096 02-20 20:42 /home/wangwei
8,设定开机自动挂载,否则重新开机用户将找不到用户了家目录喽
[root@localhost ~]# vim /etc/fstab
LABEL=/ / ext3 defaults 1 1
LABEL=/boot /boot ext3 defaults 1 2
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
LABEL=SWAP-sda3 swap swap defaults 0 0
/dev/sdb1……一般用UUID /home ext3 defaults 0 0
~
~
-- INSERT -- 8,76 All
9,迁移home分区已经做完,当然这个不是唯一解决迁移home分区的方法,我们可以把用户的家目录直接移到一个大的分区,然后更改/etc/passwd中用户的家目录的路径,这样也可以达到目的。当然如果用户过多修改路径就显得很麻烦了,这样用第一种方法比较便捷!