Expanding and Migrating a Linux System

Problem Environment: System: Linux-Debian, Boot Method: BIOS MBR. Initially, only 50GB was selected as the system disk during installation. After six months, storage is insufficient, and the system needs to be expanded to 100GB.

Root Terminal Command Line Operations

Execute the <span>fdisk -l</span> command to view the disk partition information in the system.

root@Debian:~# fdisk -l
Disk /dev/sda: 50 GiB, 53687091200 bytes, 132120576 sectors
Disk model: QEMU HARDDISK   
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: 0x34510bc9

Device     Boot     Start       End   Sectors  Size Id Type
/dev/sda1  *         2048 102856703 102854656   49G 83 Linux
/dev/sda2       102858750 104855551   1996802  975M  5 Extended
/dev/sda5       102858752 104855551   1996800  975M 82 Linux swap / Solaris

root@Debian:~# df -h /
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1      48G  45G  894M  99%  /

It can be seen that the Disklabel type is dos, indicating MBR partition format. The partition type /dev/sda1 is the system partition, with a size of 49GB, the Extended partition is 975MB, and the swap virtual memory partition is 975MB.According to the <span>df -h /</span> command, the total capacity is 48G, with 45G used, leaving only 894M available.

Path Type Size
/dev/sda1 <span>Linux</span> 49GB
/dev/sda2 <span>Extended</span> 975MB
/dev/sda3 <span>swap</span> 975MB

Adding a New Hard Disk (No Formatting or Partitioning Done)

View the newly added disk by executing the <span>fdisk -l</span> command to check the disk partition information in the system.A new disk can be seen: <span>/dev/sdb: 100 GiB</span>, which is the new disk I added.

root@Debian:~# fdisk -l
Disk /dev/sda: 50 GiB, 53687091200 bytes, 132120576 sectors
Disk model: QEMU HARDDISK   
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: 0x34510bc9

Device     Boot     Start       End   Sectors  Size Id Type
/dev/sda1  *         2048 102856703 102854656   49G 83 Linux
/dev/sda2       102858750 104855551   1996802  975M  5 Extended
/dev/sda5       102858752 104855551   1996800  975M 82 Linux swap / Solaris


Disk /dev/sdb: 100 GiB, 107374182400 bytes, 209715200 sectors
Disk model: QEMU HARDDISK   
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

1. Create Partition Format

# Set the partition table type of the disk /dev/sdb to traditional MBR (MS-DOS) format
# Note: This will clear all existing partitions on the disk
parted /dev/sdb mklabel msdos

# Create the first primary partition:
# - File system type is ext4 (actually needs to be formatted with mkfs)
# - Start from 1MiB (modern disk optimization alignment)
# - Allocate 99GiB of space (approximately reserving 1GiB for subsequent partitions)
parted /dev/sdb mkpart primary ext4 1MiB 99GiB

# Mark the first partition as "bootable" (set boot flag)
# Suitable for traditional BIOS/MBR boot method
parted /dev/sdb set 1 boot on

# Create an extended partition:
# - Occupy the remaining disk space (from 99GiB to 100%)
# Note: MBR format supports a maximum of 4 primary partitions, need to use an extended partition to create more logical partitions
parted /dev/sdb mkpart extended 99GiB 100%

# Create logical partitions within the extended partition:
# - Type set to linux-swap (actually needs to be initialized with mkswap)
# - Occupy all remaining space (from 99GiB to the end of the disk)
parted /dev/sdb mkpart logical linux-swap 99GiB 100%

<span>All partitions have been created, and the newly established partitions can be seen as follows:</span>

root@Debian:~# fdisk /dev/sdb -l
Disk /dev/sda: 100 GiB, 107374182400 bytes, 209715200 sectors
Disk model: QEMU HARDDISK   
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: 0x7a57e592

Device     Boot     Start       End   Sectors  Size Id Type
/dev/sdb1  *         2048 207618047 207616000   99G 83 Linux
/dev/sdb2       207618048 209715199   2097152    1G  f W95 Ext'd (LBA)
/dev/sdb5       207618049 209715199   2097151 1024M 82 Linux swap / Solaris

2. Format Partitions

# Format the /dev/sdb1 partition to ext4 format
mkfs.ext4 /dev/sdb1 

# Format the /dev/sdb5 partition to swap format (virtual memory)
mkswap /dev/sdb5

3. Mount New Partition and Write System Files

Mount the new disk and write system files. This process may take some time. During this time, you can open another terminal and execute the <span>df -h /dev/sdb</span> command to check how much storage has been written.

# Create a path for mounting the new disk's /dev/sdb1 system directory
mkdir -p /mnt/newroot

# Mount the new disk's /dev/sdb1 system directory to /mnt/newroot directory
mount /dev/sdb1 /mnt/newroot

# Use rsync to write system files to /mnt/newroot/, excluding certain folders
rsync -aAXv --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} / /mnt/newroot/

4. Prepare chroot Environment

After the system files are written, mount the partitions and enter the chroot environment.

mount --bind /dev /mnt/newroot/dev
mount -t proc proc /mnt/newroot/proc
mount -t sysfs sys /mnt/newroot/sys
mount -t devpts devpts /mnt/newroot/dev/pts

5. Update fstab File

# Enter the system environment
chroot /mnt/newroot /bin/bash

# Comment out unnecessary partitions or directly clear the mount configuration information
vim /etc/fstab
echo &gt; /etc/fstab 

# Write the system partition mount information
blkid | grep '/dev/sda1' | awk -F'UUID="' '{print $2}' | awk -F'"' '{print "UUID=" $1 " / ext4    errors=remount-ro 0       1"}' &gt;&gt; /etc/fstab

# Write swap partition mount information
blkid | grep '/dev/sdb5' | awk -F'UUID="' '{print $2}' | awk -F'"' '{print "UUID=" $1 " swap swap defaults 0 0"}' &gt;&gt; /etc/fstab
vim /etc/fstab  # Manually verify and adjust

6. Reinstall Bootloader

grub-install /dev/sdb
update-grub

7. Exit chroot System Environment and Clean Up

exit
umount -l /mnt/newroot/dev{/pts,}
umount /mnt/newroot/proc
umount /mnt/newroot/sys
umount /mnt/newroot

8. Shutdown the System

poweroff -h

<span>Note: After shutdown, remove the original system disk and boot with the new disk, or select the new disk in the BIOS boot options to enter the 100GB new disk system.</span> Test file writing on boot: echo 1>1.txt. If it prompts read-only, it is often due to UUID errors. Check UUID: blkid -s UUID. To mount read-write: mount -o remount,rw /dev/sda1 / . Use vim to edit /etc/fstab to correct UUID and reboot.

Check After Boot

It can be seen that the expansion was successful, and the system has moved to the new disk.

root@Debian:~# fdisk -l
Disk /dev/sda: 100 GiB, 107374182400 bytes, 209715200 sectors
Disk model: QEMU HARDDISK   
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: 0x7a57e592

Device     Boot     Start       End   Sectors  Size Id Type
/dev/sda1  *         2048 207618047 207616000   99G 83 Linux
/dev/sda2       207618048 209715199   2097152    1G  f W95 Ext'd (LBA)
/dev/sda5       207618049 209715199   2097151 1024M 82 Linux swap / Solaris

# Check remaining space
root@Debian:~# df -h /
Filesystem        Size  Used Avail Use% Mounted on
/dev/sda1      97G  46G   47G 50%  /

If there are any omissions or other notes, feel free to leave a comment to supplement.

Leave a Comment