• 如何在Linux中转换一个/Home目录到分区


    当你安装Linux时,安装者已经默认选择了"受指导的"分区。当你遇到这个选项时,安装者在root(/)分区放置了home目录和所有其他系统目录。

    虽然这种设置看起来非常好,但它展现了非常大的风险。如果你的系统崩溃或者有个东西损坏了这个root分区,驻留在home目录中所有你个人的文件丢失了。

    处于此种原因,在安装过程中创建一个单独的home分区是重要的。这在操作系统重装或者遇到root分区损坏时确保了你个人文件的安全。

    如果你接受了默认选项(所有目录划到root分区下那样地划分你的硬盘),则这是对你的指南。

    在本指南中,我们把home目录移动到一个新添加磁盘上的单独分区中。为了演示目的,我们在计算机上装上了第二块磁盘、在现实世界中,这就是你想要称为home分区的SDD磁盘或者另一块HDD。

    第一步:识别新添加的磁盘

    在接入第二个磁盘前,我们只有一个硬盘驱动(/dev/sda)。

    使用df命令看一下你硬盘驱动配置:

    1. [blctrl@localhost applications]$ df -hT
    2. Filesystem Type Size Used Avail Use% Mounted on
    3. devtmpfs devtmpfs 3.7G 0 3.7G 0% /dev
    4. tmpfs tmpfs 3.7G 0 3.7G 0% /dev/shm
    5. tmpfs tmpfs 3.7G 9.9M 3.7G 1% /run
    6. tmpfs tmpfs 3.7G 0 3.7G 0% /sys/fs/cgroup
    7. /dev/mapper/rl-root xfs 90G 6.0G 84G 7% /
    8. /dev/mapper/rl-home xfs 14G 188M 14G 2% /home
    9. /dev/sda1 xfs 1014M 264M 751M 26% /boot
    10. tmpfs tmpfs 754M 64K 754M 1% /run/user/1000
    11. [blctrl@localhost applications]$ lsblk
    12. NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
    13. sda 8:0 0 111.8G 0 disk
    14. ├─sda1 8:1 0 1G 0 part /boot
    15. └─sda2 8:2 0 110.8G 0 part
    16. ├─rl-root 253:0 0 89.3G 0 lvm /
    17. ├─rl-swap 253:1 0 7.7G 0 lvm [SWAP]
    18. └─rl-home 253:2 0 13.8G 0 lvm /home

    接下来,我们接入一个250GB的SDD磁盘。这被识别为/dev/sdb。要证实这个,我们将运行lsblk命令。

    1. [blctrl@localhost applications]$ lsblk
    2. NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
    3. sda 8:0 0 111.8G 0 disk
    4. ├─sda1 8:1 0 1G 0 part /boot
    5. └─sda2 8:2 0 110.8G 0 part
    6. ├─rl-root 253:0 0 89.3G 0 lvm /
    7. ├─rl-swap 253:1 0 7.7G 0 lvm [SWAP]
    8. └─rl-home 253:2 0 13.8G 0 lvm /home
    9. sdb 8:16 0 238.5G 0 disk

    分区会根据你的设备而不同。例如,第三个磁盘将被识别为/dev/sdc,第四个磁盘被识别为/dev/sdd以此类推。

    第二步:在Linux中创建一个新分区

    我们已经向我们的系统添加了第二块磁盘,但为了使其用作home目录的单独分区,我们需要在它上创建一个分区。到此,由于他是一个新磁盘,它还没有一个分区。

    fdisk命令验证:

    1. [blctrl@localhost applications]$ sudo fdisk -l
    2. Disk /dev/sda: 111.8 GiB, 120034123776 bytes, 234441648 sectors
    3. Units: sectors of 1 * 512 = 512 bytes
    4. Sector size (logical/physical): 512 bytes / 512 bytes
    5. I/O size (minimum/optimal): 512 bytes / 512 bytes
    6. Disklabel type: dos
    7. Disk identifier: 0x000a8d44
    8. Device Boot Start End Sectors Size Id Type
    9. /dev/sda1 * 2048 2099199 2097152 1G 83 Linux
    10. /dev/sda2 2099200 234440703 232341504 110.8G 8e Linux LVM
    11. Disk /dev/sdb: 238.5 GiB, 256060514304 bytes, 500118192 sectors
    12. Units: sectors of 1 * 512 = 512 bytes
    13. Sector size (logical/physical): 512 bytes / 4096 bytes
    14. I/O size (minimum/optimal): 4096 bytes / 4096 bytes
    15. Disk /dev/mapper/rl-root: 89.3 GiB, 95860817920 bytes, 187228160 sectors
    16. Units: sectors of 1 * 512 = 512 bytes
    17. Sector size (logical/physical): 512 bytes / 512 bytes
    18. I/O size (minimum/optimal): 512 bytes / 512 bytes
    19. Disk /dev/mapper/rl-swap: 7.7 GiB, 8241807360 bytes, 16097280 sectors
    20. Units: sectors of 1 * 512 = 512 bytes
    21. Sector size (logical/physical): 512 bytes / 512 bytes
    22. I/O size (minimum/optimal): 512 bytes / 512 bytes
    23. Disk /dev/mapper/rl-home: 13.8 GiB, 14847836160 bytes, 28999680 sectors
    24. Units: sectors of 1 * 512 = 512 bytes
    25. Sector size (logical/physical): 512 bytes / 512 bytes
    26. I/O size (minimum/optimal): 512 bytes / 512 bytes

    到此,你看到新磁盘不像第一块有/dev/sda1和/dev/sda2的磁盘,它没有与自身关联的任何分区。

    现在,我们将使用以下命令创建一个分区:

    $ sudo fdisk /dev/sdb

    在提示时,按'n'创建一个新分区。接着按'p'指定一个主分区的创建,并且按'1'指定分区号。对于接下来的两次提示,输入'回车'接受指定第一和末尾 扇区的默认值。要保存所有所作的修改,按'w‘,随后这写了所作的所有修改到了这个分区。

    1. [blctrl@localhost applications]$ sudo fdisk /dev/sdb
    2. [sudo] password for blctrl:
    3. Welcome to fdisk (util-linux 2.32.1).
    4. Changes will remain in memory only, until you decide to write them.
    5. Be careful before using the write command.
    6. The old xfs signature will be removed by a write command.
    7. Device does not contain a recognized partition table.
    8. Created a new DOS disklabel with disk identifier 0xcbe5ad7a.
    9. Command (m for help): n
    10. Partition type
    11. p primary (0 primary, 0 extended, 4 free)
    12. e extended (container for logical partitions)
    13. Select (default p): p
    14. Partition number (1-4, default 1): 1
    15. First sector (2048-500118191, default 2048):
    16. Last sector, +sectors or +size{K,M,G,T,P} (2048-500118191, default 500118191):
    17. Created a new partition 1 of type 'Linux' and of size 238.5 GiB.
    18. Command (m for help): w
    19. The partition table has been altered.
    20. Calling ioctl() to re-read partition table.
    21. Syncing disks.

    要验证做的更改,再次运行这条命令:

     sudo fdisk /dev/sdb

    在提示时,按'p'对应输出。这打印出这个分区信息。我们可以从输出看到已经用Linux作为文件系统类型创建了一个新分区/dev/sdb1。我们需要在下一步中格式化成xfs文件系统类型。 

    1. [blctrl@localhost applications]$ sudo fdisk /dev/sdb
    2. Welcome to fdisk (util-linux 2.32.1).
    3. Changes will remain in memory only, until you decide to write them.
    4. Be careful before using the write command.
    5. Command (m for help): p
    6. Disk /dev/sdb: 238.5 GiB, 256060514304 bytes, 500118192 sectors
    7. Units: sectors of 1 * 512 = 512 bytes
    8. Sector size (logical/physical): 512 bytes / 4096 bytes
    9. I/O size (minimum/optimal): 4096 bytes / 4096 bytes
    10. Disklabel type: dos
    11. Disk identifier: 0xcbe5ad7a
    12. Device Boot Start End Sectors Size Id Type
    13. /dev/sdb1 2048 500118191 500116144 238.5G 83 Linux

     

    第三步 在Linux中格式化一个新分区

    下一步是使用xfs文件系统格式化/dev/sdb1分区。注意:我们格式化/dev/sdb1(分区),而不是/dev/sdb(磁盘本身)。

    1. [blctrl@localhost applications]$ sudo mkfs.xfs /dev/sdb1
    2. [sudo] password for blctrl:
    3. meta-data=/dev/sdb1 isize=512 agcount=4, agsize=15628630 blks
    4. = sectsz=4096 attr=2, projid32bit=1
    5. = crc=1 finobt=1, sparse=1, rmapbt=0
    6. = reflink=1 bigtime=0 inobtcount=0
    7. data = bsize=4096 blocks=62514518, imaxpct=25
    8. = sunit=0 swidth=0 blks
    9. naming =version 2 bsize=4096 ascii-ci=0, ftype=1
    10. log =internal log bsize=4096 blocks=30524, version=2
    11. = sectsz=4096 sunit=1 blks, lazy-count=1
    12. realtime =none extsz=4096 blocks=0, rtextents=0
    13. Discarding blocks...Done.

    第四步:在Linux中挂载新分区

    为了磁盘被系统接受,我们需要挂载它到系统的文件系统。但首先,我们将创建一个我们将挂载这个分区的挂载点。

    [blctrl@localhost applications]$ sudo mkdir -p /srv/home
    

    下一步,我们将按以下在这个挂载点挂载这个分区。这实际上使得磁盘对系统可用。

    [blctrl@localhost applications]$ sudo mount /dev/sdb1 /srv/home/

    要验证这个,按如下运行df命令。

    1. [blctrl@localhost applications]$ sudo df -Th
    2. Filesystem Type Size Used Avail Use% Mounted on
    3. devtmpfs devtmpfs 3.7G 0 3.7G 0% /dev
    4. tmpfs tmpfs 3.7G 0 3.7G 0% /dev/shm
    5. tmpfs tmpfs 3.7G 9.9M 3.7G 1% /run
    6. tmpfs tmpfs 3.7G 0 3.7G 0% /sys/fs/cgroup
    7. /dev/mapper/rl-root xfs 90G 6.0G 84G 7% /
    8. /dev/mapper/rl-home xfs 14G 188M 14G 2% /home
    9. /dev/sda1 xfs 1014M 264M 751M 26% /boot
    10. tmpfs tmpfs 754M 64K 754M 1% /run/user/1000
    11. /dev/sdb1 xfs 239G 1.7G 237G 1% /srv/home

    第五步: 把Home目录文件复制到新分区

    我们现在复制home目录的内容到位于这个磁盘上的挂载点。我们运行命令:

    [blctrl@localhost applications]$ sudo cp -aR /home/* /srv/home/

    只要确认所有内容都被复制了,我们将检查home目录的内容:

    我们可以从输出清楚地看到预计在home目录中地所有默认目录都出现了。

    1. [blctrl@localhost applications]$ ls -l /srv/home/blctrl/
    2. total 0
    3. drwxr-xr-x. 2 blctrl blctrl 6 Jul 1 10:50 Desktop
    4. drwxr-xr-x. 2 blctrl blctrl 6 Jul 1 10:50 Documents
    5. drwxr-xr-x. 2 blctrl blctrl 6 Jul 1 10:50 Downloads
    6. drwxr-xr-x. 2 blctrl blctrl 6 Jul 1 10:50 Music
    7. drwxr-xr-x. 2 blctrl blctrl 6 Jul 1 10:50 Pictures
    8. drwxr-xr-x. 2 blctrl blctrl 6 Jul 1 10:50 Public
    9. drwxr-xr-x. 2 blctrl blctrl 6 Jul 1 10:50 Templates
    10. drwxr-xr-x. 2 blctrl blctrl 6 Jul 1 10:50 Videos

    第六步:创建一个新目录并且挂载磁盘

    我们现在需要创建另一个home目录,在其上我们将挂载我们地home分区。要避免冲突,我们重命名我们的当前home目录为/home.bak。

    1. [root@localhost /]# fuser -km /home # 查到使用home的进程
    2. [root@localhost /]# kill 2220 # 杀死以上使用home的进程
    3. [root@localhost /]# umount /home # 卸载掉home
    4. [root@localhost /]# mv /home /home.bak # 重命名

    接着,我们创建一个新的home目录。

    1. [blctrl@localhost /]$ mkdir /home
    2. [blctrl@localhost /]$ ls
    3. bin boot dev etc home home.bak lib lib64 media mnt opt proc root run sbin srv sys tmp usr var

    我们将卸载/dev/sdb1文件系统并且挂载它到新创建的home目录:

    1. [root@localhost /]# umount /dev/sdb1
    2. [root@localhost /]# mount /dev/sdb1 /home

    要验证/home目录包含默认的目录,我们进入到/home下并且列出其内容:

    1. [root@localhost /]# cd /home
    2. [root@localhost home]# ls
    3. blctrl
    4. [root@localhost home]# ls -l blctrl/
    5. total 0
    6. drwxr-xr-x. 2 blctrl blctrl 6 Jul 1 10:50 Desktop
    7. drwxr-xr-x. 2 blctrl blctrl 6 Jul 1 10:50 Documents
    8. drwxr-xr-x. 2 blctrl blctrl 6 Jul 1 10:50 Downloads
    9. drwxr-xr-x. 2 blctrl blctrl 6 Jul 1 10:50 Music
    10. drwxr-xr-x. 2 blctrl blctrl 6 Jul 1 10:50 Pictures
    11. drwxr-xr-x. 2 blctrl blctrl 6 Jul 1 10:50 Public
    12. drwxr-xr-x. 2 blctrl blctrl 6 Jul 1 10:50 Templates
    13. drwxr-xr-x. 2 blctrl blctrl 6 Jul 1 10:50 Videos

    另外,我们可以使用df命令验证我们的文件系统挂载到了/home挂载点:

    1. [root@localhost home]# df -Th /dev/sdb1
    2. Filesystem Type Size Used Avail Use% Mounted on
    3. /dev/sdb1 xfs 239G 1.8G 237G 1% /home

     输出确认在我们磁盘上的dev/sdb1文件系统被挂载到了/home分区。但重启后,这将不存在。要使之永久,需要再补充一步并且那就是用文件系统信息修改/etc/fstab文件。

    第七步 在Linux上永久分区挂载

    要自动确认在每次系统启动时挂载文件系统,我们要修改/etc/fstab文件。但首先,按如下获取文件系统的UUID。

    1. [root@localhost home]# blkid /dev/sdb1
    2. /dev/sdb1: UUID="23fea399-44c5-4fba-97e1-9b18a70e9efd" BLOCK_SIZE="4096" TYPE="xfs" PARTUUID="cbe5ad7a-01"

    由于将在下一步中使用这个UUID,在文本编辑器中某处复制并且粘贴这个UUID。

    接着,打开/etc/fstab文件。

    [root@localhost home]# vim /etc/fstab

     在打开的文件末尾添加一行。用/dev/sdb1文件系统的实际UUID替换在方括号中的uid:

    UUID=[uid] /home xfs     defaults        0 1
    1. #
    2. # /etc/fstab
    3. # Created by anaconda on Fri Jul 1 02:12:26 2022
    4. #
    5. # Accessible filesystems, by reference, are maintained under '/dev/disk/'.
    6. # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
    7. #
    8. # After editing this file, run 'systemctl daemon-reload' to update systemd
    9. # units generated from this file.
    10. #
    11. /dev/mapper/rl-root / xfs defaults 0 0
    12. UUID=aaa8bd0f-cd64-4435-adc5-fc7e7ae6c27d /boot xfs defaults 0 0
    13. #/dev/mapper/rl-home /home xfs defaults 0 0
    14. /dev/mapper/rl-swap none swap defaults 0 0
    15. UUID=23fea399-44c5-4fba-97e1-9b18a70e9efd /home xfs defaults 0 1

    保存并且关闭这个文件。接着运行以下命令来挂载所有分区:

    [root@localhost home]# mount -a

    现在,在系统每次启动时,你的驱动将被挂载到/home分区。

    1. [root@localhost home]# df -h /dev/sdb1
    2. Filesystem Size Used Avail Use% Mounted on
    3. /dev/sdb1 239G 1.8G 237G 1% /home
    4. [root@localhost home]# df -h
    5. Filesystem Size Used Avail Use% Mounted on
    6. devtmpfs 3.7G 0 3.7G 0% /dev
    7. tmpfs 3.7G 0 3.7G 0% /dev/shm
    8. tmpfs 3.7G 18M 3.7G 1% /run
    9. tmpfs 3.7G 0 3.7G 0% /sys/fs/cgroup
    10. /dev/mapper/rl-root 90G 6.0G 84G 7% /
    11. /dev/sda1 1014M 264M 751M 26% /boot
    12. tmpfs 754M 0 754M 0% /run/user/1000
    13. tmpfs 754M 0 754M 0% /run/user/0
    14. /dev/sdb1 239G 1.8G 237G 1% /home

    为了避免经历这些步骤,为了把你个人数据从文件系统文件分开,在安装过程中,建议把home分区与余下的系统分区分隔开。这使得在故障时恢复你的数据变得简单。

  • 相关阅读:
    NTP网络时间服务器是如何完成它的授时服务的?
    python 画韦恩图(venn)代码(两组和三组数据),简单易学易上手
    力扣思路题:丑数
    Dubbo 原理和机制详解 (非常全面)
    解决本地nginx部署vue项目只能访问默认页面问题
    P1304 哥德巴赫猜想
    SysML理论知识
    忘记 iPhone 密码:如果忘记密码,如何解锁 iPhone
    Nanoprobes原位杂交:基因检测的演变(从 Nanogold到EnzMet)
    第二章:整数二分与浮点数二分(极限思想)
  • 原文地址:https://blog.csdn.net/yuyuyuliang00/article/details/125555057