Using dd to replace a solid state drive, restore a partition and grow its ext4 file system

Written by - 0 comments

Published on February 27th 2021 - Listed in Linux

A solid state drive (SSD) started to die and even though this drive was purely used for using virtual machines, it killed the host (running Linux Mint). Check out the post "How a dying SSD of a Windows virtual machine killed the physical Linux host" for more information.

Anyway, the defect SSD, an ADATA S510 120GB, was replaced by a Samsung 860 EVO 1TB drive. To transfer the existing files from the dying SSD (read operations still seemed to work), I decided to use dd.

ck@linux ~ $ sudo dd if=/dev/sdc1 of=/root/sdc1.out bs=4096 status=progress

This created a ~112 GB file /root/sdc1.out containing the full partition 1 of this drive.

ck@linux ~ $ sudo du -h /root/sdc1.out
112G    /root/sdc1.out

After I received the Samsung SSD, I removed the ADATA drive and installed the Samsung drive instead. The Samsung drive uses the same SATA connector as the ADATA drive before; therefore no surprise, Linux detected this drive as /dev/sdc.

First I created a partition /dev/sdc1 with the full available size of the drive:

linux ~ # fdisk /dev/sdc

Welcome to fdisk (util-linux 2.27.1).
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 0xdd4f6591.

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): [enter]
First sector (2048-1953525167, default 2048): [enter]
Last sector, +sectors or +size{K,M,G,T,P} (2048-1953525167, default 1953525167):

Created a new partition 1 of type 'Linux' and of size 931.5 GiB.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

Time to restore the dd output to the new partition:

ck@linux ~ $ sudo dd if=/root/sdc1.out of=/dev/sdc1 bs=4096 status=progress
119872147456 bytes (120 GB, 112 GiB) copied, 800 s, 150 MB/s    
29304950+0 records in
29304950+0 records out
120033075200 bytes (120 GB, 112 GiB) copied, 807.718 s, 149 MB/s

Now there's one important thing to keep in mind. The previous drive had a size of 120 GB. The partition 1 (sdc1) was around 112 GB. So was the ext4 file system. Now that the whole partition was copied using dd, the file system inside this "disk dump" still contains the old size.

To increase the file system, a file system check followed by resize2fs is necessary:

linux ~ # e2fsck -f /dev/sdc1
e2fsck 1.42.13 (17-May-2015)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/sdc1: 36/7331840 files (0.0% non-contiguous), 12796834/29304950 blocks

linux ~ # resize2fs /dev/sdc1
resize2fs 1.42.13 (17-May-2015)
Resizing the filesystem on /dev/sdc1 to 244190390 (4k) blocks.
The filesystem on /dev/sdc1 is now 244190390 (4k) blocks long.

That's it! The partition can now be mounted as ext4 and the original files from the ADATA drive are still available:

linux ~ # mount /dev/sdc1 /mnt

linux ~ # ll /mnt
total 32
drwxrwxrwx  5 root  root   4096 Jan 20 13:01 ./
drwxr-xr-x 25 root  root   4096 Feb  4 17:27 ../
drwx------  2 root  root  16384 Jan  4 11:17 lost+found/
drwx------  5 ckadm ckadm  4096 Jan 20 13:01 .Trash-1000/
drwxrwxr-x  6 ckadm ckadm  4096 Jan 20 13:23 Windows10/

linux ~ # df -h /mnt
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdc1       917G   47G  870G   6% /mnt

Add a comment

Show form to leave a comment

Comments (newest first)

No comments yet.