用另一块硬盘给linux系统扩容(拯救没root密码的vps)

更多精彩请关注公众号:蓝色先驱
租了个烂服务器,Root密码无论怎么重置都是错的。写邮件给服务商不回,打电话打不通。
只有rescue模式能进去。。。
可是rescue模式我给装了Debian10好歹也能用啊。
之前试过chroot到主硬盘改密码,不行。
不得已,拆了主系统的空间给rescue模式用。
这个vps是我生日时买的。说送自己个生日礼物。今年生日,没有蛋糕没有聚会。也不敢办聚会。没了聚会也没人记得我生日。生日也没和刚在一起不久的新欢说,总觉得刚在一起不久就让人家花钱不好。这个vps便宜是真的便宜。50来块钱1年。可是面板密码是错的。重置还是错的。重装系统还是错的。联系客服总是遮遮掩掩,没有重点,客服电话打不通,只能发邮件,一天一回复,保证回的是没用的内容。仿佛我交了个笔友。近来太穷,没什么生意,又不好意思去吃软饭。小鸡也是肉啊。一鼓作气把只有rescue模式的小鸡搞好了,大不了以后就只用rescue呗。

探索

先看看硬盘df -h,有两块硬盘。没法直接全扩到根分区去。

Disk /dev/vda: 10 GiB, 10737418240 bytes, 20971520 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
Disklabel type: dos
Disk identifier: 0x4cd40c9a

Device     Boot    Start      End  Sectors  Size Id Type
/dev/vda1  *        2048 19920511 19918464  9.5G 83 Linux
/dev/vda2       19920512 20969087  1048576  512M 82 Linux swap / Solaris




Disk /dev/vdb: 1.1 GiB, 1181115904 bytes, 2306867 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
Disklabel type: dos
Disk identifier: 0xb52fd1c0

用parted分区

rescue模式用的是/dev/vdb。
下来拆分了/dev/vda,好让我能在rescue模式下舒服一点点

parted /dev/sda

(parted) print
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system     Flags
 1      1049kB  10.2GB  10.2GB  primary  ext3            boot
 2      10.2GB  10.7GB  537MB   primary  linux-swap(v1)
 

下面移除分区1,开始分区mkpart primary可以创建新的主分区。start最好与上一分区的end差一点,不然可能出错。

(parted) rm 1
(parted) print
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size   Type     File system     Flags
 2      10.2GB  10.7GB  537MB  primary  linux-swap(v1)

(parted) mkpart primary
File system type?  [ext2]? ext4
Start? 2
End? 4096
(parted) mkpart primary
File system type?  [ext2]? ext4
Start? 4097
End? 8193
(parted) mkpart primary
File system type?  [ext2]? ext4
Start? 8194
End? 9728
(parted) quit
Information: You may need to update /etc/fstab.

现在看看分区情况

rescue # fdisk -l
Disk /dev/vda: 10 GiB, 10737418240 bytes, 20971520 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
Disklabel type: dos
Disk identifier: 0x4cd40c9a

Device     Boot    Start      End Sectors  Size Id Type
/dev/vda1           4096  7999487 7995392  3.8G 83 Linux
/dev/vda2       19920512 20969087 1048576  512M 82 Linux swap / Solaris
/dev/vda3        8001536 16001023 7999488  3.8G 83 Linux
/dev/vda4       16003072 18999295 2996224  1.4G 83 Linux

Partition table entries are not in disk order.




Disk /dev/vdb: 1.1 GiB, 1181115904 bytes, 2306867 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
Disklabel type: dos
Disk identifier: 0xb52fd1c0

Device     Boot Start     End Sectors  Size Id Type
/dev/vdb1  *     2048 2304511 2302464  1.1G 83 Linux

mkfs格式化分区 -t 文件系统格式 分区

rescue # mkfs -t ext4 /dev/vda1
mke2fs 1.44.5 (15-Dec-2018)
Creating filesystem with 999424 4k blocks and 249984 inodes
Filesystem UUID: 1a34e155-0e57-45cc-9074-24a553772500
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736

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

rescue # mkfs -t ext4 /dev/vda3
mke2fs 1.44.5 (15-Dec-2018)
Creating filesystem with 999936 4k blocks and 249984 inodes
Filesystem UUID: 987ca10e-0bb0-45fb-8a27-57e0a44bb88f
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736

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

rescue # mkfs -t ext4 /dev/vda4
mke2fs 1.44.5 (15-Dec-2018)
Creating filesystem with 374528 4k blocks and 93696 inodes
Filesystem UUID: 6819294e-8b71-4af6-8620-a3b63c078953
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912

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

数据迁移及删除原空间文件

在mnt创建三个文件夹
rescue # cd /mnt
rescue # mkdir usr home var
rescue # ls
home  usr  var

#挂载分区并拷贝数据
rescue # mount /dev/vda1 /mnt/usr&&cp -a /usr/* /mnt/usr/
rescue # mount /dev/vda3 /mnt/home&&cp -a /home/* /mnt/home/
rescue # mount /dev/vda4 /mnt/var&&cp -a /var/* /mnt/var/

#删除这些数据,毕竟寸土寸金
rescue # rm -rf /usr/*
rescue # rm -rf /home/*
rescue # rm -rf /var/*

#卸载全部分区
rescue # umount /dev/vda1
rescue # umount /dev/vda3
rescue # umount /dev/vda4

检查检查是不是全卸载了

rescue # df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            109M     0  109M   0% /dev
tmpfs            25M  3.0M   22M  13% /run
/dev/vdb1       1.1G  320M  715M  31% /
tmpfs           121M     0  121M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           121M     0  121M   0% /sys/fs/cgroup
tmpfs            25M     0   25M   0% /run/user/0
/dev/vda3       3.7G   16M  3.5G   1% /mnt/var

自动挂载分区nano /etc/fstab

  1. 分区位置
  2. 挂载点
  3. 文件系统格式
  4. 默认
  5. 0 0表示不需要开机进行检查
#/dev/vda1       /               ext3    discard,errors=remount-ro 0       1
/dev/vda1       /usr               ext4    defaults 0       0
/dev/vda3       /home               ext4    defaults 0       0
/dev/vda4       /var               ext4    defaults 0       0

最后挂载mount -a

检查一下。

rescue # df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            109M     0  109M   0% /dev
tmpfs            25M  2.9M   22M  13% /run
/dev/vdb1       1.1G  988M   47M  96% /
tmpfs           121M     0  121M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           121M     0  121M   0% /sys/fs/cgroup
tmpfs            25M     0   25M   0% /run/user/0
/dev/vda1       3.7G   16M  3.5G   1% /usr
/dev/vda3       3.7G   16M  3.5G   1% /home
/dev/vda4       1.4G  4.3M  1.3G   1% /var

大功告成了,更多精彩请关注公众号:蓝色先驱

接各种网站小程序开发,如有意介绍生意,有红包,具体电话谈。
微信: hero141to

1 个赞

这操作倒是清奇


服务器资源由ZeptoVM赞助

Partners Wiki Discord