Linux User home Directory Lossless migration to standalone hard drive

Source: Internet
Author: User
Tags uuid

After the disk management, do not know that students have not thought of a problem, before the installation of the operating system, the user home directory does not give a separate partition, and now the user home directory data is growing, has taken up a lot of server disk space, if let its development, It is likely that the system crashes because the home directory data occupies the server's disk space. As a famous ops person, we can't just sit there and let this happen. What to do, so, a lot of people are thinking, we can use a new hard disk to create a larger partition to mount to the current system home directory. In the end, let's check it out. Of course, to do this migration, all users must be notified in advance to log off. Otherwise, the user performing the migration of the home directory during the use of the process must be an error. If you don't think about this as an OPS person, then you can go straight out.


The first step: Load a new hard disk onto the server and format it partition. Here, I use a 20G new hard drive, 20G into a zone, and format

Environment Show:

The user home directory does not have separate partitions, and the user home directory has generated data for multiple users

[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 19:42 Hacker

DRWX------. 5 Share test 4096 19:43 share

DRWX------. 6 test test 4096 15:31 test

[Email protected] ~]#


Add a 20G hard drive without rebooting to start partitioning partitions directly


[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'll remain in memory only, until the 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 * MB = bytes

Sector size (logical/physical): bytes/512 bytes

I/O size (minimum/optimal): 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 GiB is set


Command (M for help): P


disk/dev/sdb:21.5 GB, 21474836480 bytes, 41943040 sectors

Units = sectors of 1 * MB = bytes

Sector size (logical/physical): bytes/512 bytes

I/O size (minimum/optimal): bytes/512 bytes

Disk label Type:dos

Disk IDENTIFIER:0X0B8592C8


Device Boot Start End Blocks Id System

/DEV/SDB1 2048 41943039 20970496-up Linux


Command (M for help): W

The partition table has been altered!


Calling IOCTL () to re-read partition table.

Syncing disks.

[Email protected] ~]#


After dividing the area, be sure to notify the kernel to reread the disk partition table, otherwise the formatting will indicate that the partition is not found


[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

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] ~]#


The second step: after all users log off, you can rename the home directory to home1, and create a new home directory, just the formatted SDB1 to the new home directory, and the home1 directory to move all the user's files back to the home directory, and delete the home1 directory, In fact, the user's data is now moved to the SDB1 partition.

[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 20:03 lost+found

[Email protected] ~]# mv/home1/*/Home

[Email protected] ~]# Ll/home

Total 28

DRWX------. 5 Hacker test 4096 19:42 Hacker

DRWX------. 2 root root 16384 20:03 lost+found

DRWX------. 5 Share test 4096 19:43 share

DRWX------. 6 test test 4096 15:31 test

[Email protected] ~]# ll/home1

Total 0

[Email protected] ~]# rm-rf/home1

[Email protected] ~]#

The third step: let the original user login system to verify the home directory is normal, here, I will directly use the su-username login switch to verify

[Email protected] home]# su-test

Last Login:sat 20:31:39 CST in pts/0

[Email protected] ~]$ pwd

/home/test

[[Email protected] ~]$ exit

Logout

[Email protected] home]# Su-share

Last Login:sat 20:31:57 CST in pts/0

[Email protected] ~]$ pwd

/home/share

[[Email protected] ~]$ exit

Logout

[Email protected] home]# Su-hacker

Last Login:sat 20:32:41 CST in pts/0

[Email protected] ~]$ pwd

/home/hacker

[[Email protected] ~]$ exit

Logout

[Email protected] home]#

Fourth step: The home directory mount information is written to the Fstab configuration file, or the current mount one but the restart will expire, the user's home directory will not be found

Using BLKID/DEV/SDB1 to query the UUID of the SDB1 partition, writing the Fstab file with the UUID will be more accurate

[Email protected] home]# BLKID/DEV/SDB1

/dev/sdb1:label= "Homedir" uuid= "248d9c56-7795-4747-a251-0737240cb30a" type= "Ext4"

[Email protected] home]#

add a new line to the/etc/fstab file with the following format (note: The UUID tries to replicate as much as possible, in case the system mount is unsuccessful due to input errors )

Uuid= "248d9c56-7795-4747-a251-0737240cb30a"/home ext4 defaults 0 0


At this point, if you follow my tutorial strictly every step of the word, there is no error. The original home directory, which has no separate partitions, is now successfully migrated to the 20G new hard disk partition. Of course, the size of the drive after the migration can be set according to your actual needs.

This article is from the "Love Firewall" blog, be sure to keep this source http://183530300.blog.51cto.com/894387/1843400

Linux User home Directory Lossless migration to standalone hard drive

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.