Score:1

How do I migrate Ubuntu 22.04 from a 1TB to 500GB SSD?

in flag

I would like to migrate from a 1TB to 500GB SSD. I believe less than 100GB are used (Home Assistant via Docker was installed). I have seen options such as dd and Clonezilla.

I'm aware that dd does not take care of resizing the partition, but I think Clonezilla will only resize if moving to a larger SSD. So how do I shrink the 1TB SSD so that Clonezilla sees it as smaller than the 500GB SSD?

I am open to better options other than Clonezilla.

per request:

lsblk

returns:

NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
loop0    7:0    0     4K  1 loop /snap/bare/5
loop1    7:1    0  63.3M  1 loop /snap/core20/1828
loop2    7:2    0    73M  1 loop /snap/core22/583
loop3    7:3    0  63.3M  1 loop /snap/core20/1852
loop4    7:4    0 163.3M  1 loop /snap/firefox/1635
loop5    7:5    0 239.9M  1 loop /snap/firefox/2487
loop6    7:6    0 346.3M  1 loop /snap/gnome-3-38-2004/119
loop7    7:7    0 460.4M  1 loop /snap/gnome-42-2204/68
loop8    7:8    0 460.3M  1 loop 
loop9    7:9    0  91.7M  1 loop /snap/gtk-common-themes/1535
loop10   7:10   0  45.9M  1 loop /snap/snap-store/582
loop11   7:11   0  45.9M  1 loop /snap/snap-store/638
loop12   7:12   0  49.8M  1 loop /snap/snapd/18357
loop13   7:13   0  49.8M  1 loop /snap/snapd/18596
loop14   7:14   0   304K  1 loop /snap/snapd-desktop-integration/49
loop15   7:15   0   428K  1 loop /snap/snapd-desktop-integration/57
loop16   7:16   0  65.1M  1 loop /snap/sublime-text/118
loop17   7:17   0 349.7M  1 loop /snap/gnome-3-38-2004/137
loop18   7:18   0    73M  1 loop /snap/core22/607
loop19   7:19   0 460.6M  1 loop /snap/gnome-42-2204/87
sda      8:0    0 931.5G  0 disk 
├─sda1   8:1    0   512M  0 part /boot/efi
└─sda2   8:2    0   931G  0 part /var/snap/firefox/common/host-hunspell
                                 /
sdb      8:16   1     0B  0 disk 
sdc      8:32   1     0B  0 disk
ar flag
My preferred method is to backup your data. Make a list of installed applications. Do a clean and me install of Ubuntu. Restore your data. Install other apps as needed. I am voting to close this question as it seeks opinions.
cn flag
The better option would be the one YOU are comfortable with. I myself would always re-install. I have my system re-installed in 15 minutes with the post-install things I need to do done too so I doubt there is a quicker method for me. And I agree: it is asking for opinions
gatorback avatar
in flag
Thank you for replying: I'm always interested in alternative ideas.
oldfred avatar
cn flag
Are both drives eventually going to be connected at same time? You cannot have duplicate UUIDs or GUIDs if gpt which it should be, so any clone method will not work. I vote for new install & restore from your normal backup which then verifies you do have good backups.
RonJohn avatar
cn flag
Shrink the existing partition, *then* clone it?
RonJohn avatar
cn flag
@oldfred you can boot using disk labels, too. (Well, you could back in the day. Don't know about now.) fstab takes them, too.
oldfred avatar
cn flag
Yes you can boot by label, but clone will also have duplicate labels.
C.S.Cameron avatar
cn flag
You can copy your partitions with drag and drop in GParted, then reinstall GRUB.
gatorback avatar
in flag
@oldfred The plan is to retire the 1TB SSD.
mpboden avatar
do flag
Please provide the output of `lsblk` to your original post.
gatorback avatar
in flag
@mpboden `lsblk` is posted. Please consider posting your own answer.
mpboden avatar
do flag
Now that we know your drive is not configured with LVM, you can follow the answer from @YuriSucupira But if you're more comfortable using Clonezilla, then you can use that instead of the `dd` commands. It's basically the same thing, but Clonezilla is considered a bit safer if you're not comfortable with `dd`, because it walks you through the process and has some checks and balances before the final commit. ---- So...boot into Live CD, use GParted to resize filesystem and partition. Then boot into Clonezilla and clone drive.
mpboden avatar
do flag
But whatever you do, I would make a full backup of your first drive before doing anything. You can use Clonezilla to make an image backup of your 1TB drive and save it to an external USB drive or something. That way, if you mess something up, you can restore the image and start over. If you can, practice all this on a Virtual Machine first.
mpboden avatar
do flag
Now having said all that, depending on how many applications you've installed and customization you've done, it may be easiest to follow the advice from @user68186 . Simply reinstall Ubuntu on the new hard drive, reinstall all your applications, configure as needed, and then copy your data from the old 1TB drive to the new.
gatorback avatar
in flag
Beginning to look like a reinstall is simpler as suggested by the majority of community. That being said, I would still like to understand the answer to the original question. Thank you for taking the time to respond.
Score:2
cn flag
  1. Use e.g. a DVD or a USB flash drive to boot a Ubuntu Live CD.

  2. Once the Live CD has booted, use it to umount and then perform a filesystem check on your hard disk's root Ubuntu partition. For instance, if your hard disk is /dev/sda and Ubuntu's root filesystem structure is ext3 located at /dev/sda2, then run:

    sudo umount /dev/sda2 ; sudo fsck.ext3 /dev/sda2
    
  3. Now that your root partition is umounted and filesystem-checked, install gparted on such Live CD (if it's not installed, yet):

    sudo apt install gparted -y
    
  4. Run gparted:

    pkexec gparted & sleep 30 ; exit
    
  5. Once gparted's window is open, right-click on e.g. /dev/sda2, select the Resize/Move option, set the new size (i.e. 500GB) and confirm.

  6. Check the current size of your resized partition and how much of its space is used and how much is available:

    See the size in Megabytes:

    df -B 1M /dev/sda2
    

    ...or see the size in Gigabytes:

    df -B 1G /dev/sda2
    
  7. If the final used space is smaller than your SSD's 500GB size, then you can now connect your SSD to your computer (I'm assuming that you can connect such SSD to the USB port through e.g. a docking station or to a e-SATA port, or directly to a motherboard's SATA port) and then create the same partition table and partitions on such SSD. Use gparted to see the partition table type of your hard disk and the filesystem of each partition.

    Example: if your hard disk is /dev/sda and your SSD is /dev/sdb, your hard disk has a GPT partition table and 3 partitions (/dev/sda1 as the boot partition, /dev/sda2 as the Ubuntu Linux root partition and /dev/sda3 as the swap partition), then take note of such information (plus what is the first and last sector of each partition) and then use e.g. fdisk to create the same partition table and partitions at your SSD. Finally, use gdisk (installation command: sudo apt install gdisk -y) to clone the data from the original (source) GPT partition table and write it on the SSD:

    sudo sgdisk /dev/sda -R /dev/sdb
    

    ...and then use dd to clone each partition from your hard disk to the SSD,:

    sudo dd if=/dev/sda1 of=/dev/sdb1 bs=1M status=progress
    sudo dd if=/dev/sda2 of=/dev/sdb2 bs=1M status=progress       
    sudo dd if=/dev/sda3 of=/dev/sdb3 bs=1M status=progress
    
  8. Turn the computer off, unplug the hard disk and the Live CD and then try to boot from the SSD.

PS: if you can't boot the SSD, it's likely that GPT data is located at the end of the block device. In this case, run sudo gdisk /dev/sdb and then type w and press Enter to write the GPT partition table to the SSD again.

oldfred avatar
cn flag
If drive is gpt, you should not use dd on partition. Drives with gpt partition tables have GUID in primary partition table, backup partition table & each partition. If you attempt to clone partition, you also then have to update GUIDs and move backup partition table to end of drive. It can technically be done, but easier just to install to new gpt partitioned drive.
Yuri Sucupira avatar
cn flag
@oldfred You're right, I forgot to explain how to clone the GPT partition table. I updated my answer. Thanks.
mpboden avatar
do flag
If the current installation is using LVM, then this won’t work. A resize of the file system and partitions can still be done, but it’s a bit more involved. So until the OP provides more info on their partition scheme, it’s difficult to provide accurate answers to their question.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.