业精于勤
而荒于嬉

Ubuntu 挂载新硬盘并把 home 目录迁移到新硬盘

1. 查看磁盘信息

ubuntu@ubuntu-server:~$ sudo lshw -C disk
  *-disk:0
       description: SCSI Disk
       product: Virtual disk
       vendor: VMware
       physical id: 0.0.0
       bus info: scsi@32:0.0.0
       logical name: /dev/sda
       version: 2.0
       size: 64GiB (68GB)
       capabilities: gpt-1.00 partitioned partitioned:gpt
       configuration: ansiversion=6 guid=5bf316b4-618f-47db-b82b-d83ef4ab0352 logicalsectorsize=512 sectorsize=512
  *-disk:1
       description: SCSI Disk
       product: Virtual disk
       vendor: VMware
       physical id: 0.1.0
       bus info: scsi@32:0.1.0
       logical name: /dev/sdb
       version: 2.0
       size: 1TiB (1099GB)
       capabilities: 7200rpm
       configuration: ansiversion=6 logicalsectorsize=512 sectorsize=512
ubuntu@ubuntu-server:~$ sudo fdisk -l | grep sd
Disk /dev/sda: 64 GiB, 68719476736 bytes, 134217728 sectors
/dev/sda1     2048      4095      2048   1M BIOS boot
/dev/sda2     4096   2101247   2097152   1G Linux filesystem
/dev/sda3  2101248 134215679 132114432  63G Linux filesystem
Disk /dev/sdb: 1 TiB, 1099511627776 bytes, 2147483648 sectors

可以看到,有两块硬盘,其中 sdb 是准备要挂载的硬盘。

2. 挂载前设置硬盘

硬盘设置命令: sudo fdisk /dev/sdb

ubuntu@ubuntu-server:~$ sudo fdisk /dev/sdb

Welcome to fdisk (util-linux 2.34).
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.
Created a new DOS disklabel with disk identifier 0x44e25654.

Command (m for help):

输入 m 查看所有的功能:

Command (m for help): m

Help:

  DOS (MBR)
   a   toggle a bootable flag
   b   edit nested BSD disklabel
   c   toggle the dos compatibility flag

  Generic
   d   delete a partition
   F   list free unpartitioned space
   l   list known partition types
   n   add a new partition(创建分区)
   p   print the partition table(查看分区状态)
   t   change a partition type
   v   verify the partition table
   i   print information about a partition

  Misc
   m   print this menu
   u   change display/entry units
   x   extra functionality (experts only)

  Script
   I   load disk layout from sfdisk script file
   O   dump disk layout to sfdisk script file

  Save & Exit
   w   write table to disk and exit
   q   quit without saving changes

  Create a new label
   g   create a new empty GPT partition table
   G   create a new empty SGI (IRIX) partition table
   o   create a new empty DOS partition table
   s   create a new empty Sun partition table

操作步骤如下:

  1. 输入 p 查看 /dev/sdb/ 分区状态。
Command (m for help): p
Disk /dev/sdb: 1 TiB, 1099511627776 bytes, 2147483648 sectors
Disk model: Virtual disk    
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x44e25654
  1. 输入 n 创建 sdb 硬盘分区,依次选择 p1Enter 。含义为:创建主分区、分区编号 1 、全部存储分一个区。
Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-2147483647, default 2048): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-2147483647, default 2147483647): 

Created a new partition 1 of type 'Linux' and of size 1024 GiB.
  1. 输入 w 保存状态。
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

3. 格式化硬盘

将硬盘 dev/sdb1 格式化 ext4 格式。

ubuntu@ubuntu-server:~$ sudo mkfs.ext4 /dev/sdb1
mke2fs 1.45.5 (07-Jan-2020)
Creating filesystem with 268435200 4k blocks and 67108864 inodes
Filesystem UUID: 628a99f4-c8b3-4083-808b-9061ad6912ed
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
        4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968, 
        102400000, 214990848

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (262144 blocks): done
Writing superblocks and filesystem accounting information: done 

4. 迁移旧的 home 目录至新硬盘

此部分内容需使用 root 用户登录,因为涉及 home目录删除,所有非 root 用户都会失效。

  1. 创建临时目录
ubuntu@ubuntu-server:~$ sudo mkdir /mnt/tmp
sudo mount /dev/sdb1 /mnt/tmp
  1. 同步 home 目录
ubuntu@ubuntu-server:~$ sudo rsync -avx /home/ /mnt/tmp/
sending incremental file list
./
ubuntu/
ubuntu/.bash_history
ubuntu/.bash_logout
ubuntu/.bashrc
ubuntu/.profile
ubuntu/.sudo_as_admin_successful
ubuntu/.viminfo
ubuntu/.cache/
ubuntu/.cache/motd.legal-displayed
ubuntu/.config/
ubuntu/.config/procps/

sent 7,119 bytes  received 172 bytes  14,582.00 bytes/sec
total size is 6,427  speedup is 0.88
  1. 删除旧 home 目录。
ubuntu@ubuntu-server:~$ sudo rm -rf /home/*
ubuntu@ubuntu-server:~$ umount -l /home
umount: /home: not mounted.
ubuntu@ubuntu-server:~$ sudo mount /dev/sdb1 /home
  1. 重新挂载新硬盘
ubuntu@ubuntu-server:~$ sudo mount /dev/sdb1 /home
  1. 设置系统启动挂载需要得到硬盘的信息
sudo blkid
/dev/sda2: UUID="b6ae5aaa-dbe2-4673-b9e3-27ec0f326e0d" TYPE="ext4" PARTUUID="9f4f2834-85de-4490-a1c6-05e4fc4d4296"
/dev/sda3: UUID="P85cYp-5N3k-csb1-RMwe-TO0T-TCPF-XLmVeP" TYPE="LVM2_member" PARTUUID="75051217-927c-4904-99ff-aec3f1e1dea2"
/dev/mapper/ubuntu--vg-ubuntu--lv: UUID="14d44915-9b8e-4c43-bd82-9950b3fcc265" TYPE="ext4"

记录获取到的 UUIDTYPE ,在下面会用到。

接着编辑 /etc/fstab 文件,把以下代码添加到最后。

/dev/disk/by-uuid/628a99f4-c8b3-4083-808b-9061ad6912ed /home ext4 defaults 0 2

5. 结束

运行 df-h 即可看到最新挂载在 home 目录的硬盘设备。

ubuntu@ubuntu-server:~$ df -h
Filesystem                         Size  Used Avail Use% Mounted on
udev                               7.8G     0  7.8G   0% /dev
tmpfs                              1.6G  1.2M  1.6G   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv   31G  6.5G   23G  23% /
tmpfs                              7.9G     0  7.9G   0% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
tmpfs                              7.9G     0  7.9G   0% /sys/fs/cgroup
/dev/sda2                          976M  105M  805M  12% /boot
/dev/loop0                          56M   56M     0 100% /snap/core18/1944
/dev/loop1                          70M   70M     0 100% /snap/lxd/19188
/dev/loop2                          32M   32M     0 100% /snap/snapd/10707
tmpfs                              1.6G     0  1.6G   0% /run/user/1000
/dev/loop3                          33M   33M     0 100% /snap/snapd/11588
/dev/loop4                          56M   56M     0 100% /snap/core18/1997
/dev/loop5                          68M   68M     0 100% /snap/lxd/20326
/dev/sdb1                         1007G   77M  956G   1% /home

其中, dev/sdb1 就是新添加的 /home 硬盘。

赞(0)
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
文章名称:《Ubuntu 挂载新硬盘并把 home 目录迁移到新硬盘》
文章链接:https://ithuang.net/ubuntu-home-new-disk.html
本站资源仅供个人学习交流,本人版权所有。欢迎转载,转载请注明作者及来源链接,谢谢。