3

I have a machine with one drive, and 2 OSs installed in 2 partitions, one windows and one Ubuntu 16.04. Grub is installed.

I just got a new physical drive. After I assemble it in the machine, I would like to:

  • Copy the exact same Ubuntu that I have in one partition of the current drive to the new drive

  • Have a grub in place that allows me to choose to boot from windows, from the original Ubuntu, or from the new Ubuntu in the new drive

I have done research and I found a very similar thread (link here). In that thread, this is one of the solutions suggested:

  • Create an ext4 partition and a swap partition on the new drive.

  • Boot from LiveUSB.

  • Mount the old Ubuntu partition to some directory, mount the new one to some other directory.

  • Copy all files from the old one to the new one using cp -a command

  • Install grub to the new drive.

  • Update /etc/fstab with new UUIDs

Given that I already have a grub in the current drive, and that I will copy the Ubuntu which is there exactly the same as it is to the new drive, do I need to install it on the new drive?

Thanks

Joe
  • 367
  • I think you don't need to install grub again. Just run sudo update-grub once the new OS is ready. – user68186 May 12 '18 at 23:38
  • 1
    Basically, I agree with the 2nd answer, but not sure whether a boot-repair is necessary. If you were restoring an image back onto the same drive, then you should not need a new grub, everything should be the same as before. But using a different drive to restore an image as in this case may make a different UUID for your '/' root partition, which would then require a corresponding change in grub.cfg and fstab files. – Paul Benson May 13 '18 at 06:38

2 Answers2

2

Grub access it's config files before drives/filesystems are mounted. Unless you duplicate the original drive, you will likely get a grub command prompt.

You will need to boot into the new OS and update grub. I have done the following in the same situation:

  • copy your files to the new drive, and edit the new /etc/fstab to reflect the new UUID's.
  • Update grub so that you get a grub entry for the new drive in the old grub.
  • boot into the new drive. Reinstall grub, update grub, and update initramfs. Be sure to target the new drive with the grub install.
  • remove the old drive, and you should be good with the new drive.
  • once booted with the new drive only, update grub again to remove the entry for the old drive.
ravery
  • 6,874
  • What exactly would I need to update in initramfs? – Joe Jul 04 '18 at 22:57
  • 1
    updating initramfs, creates the system map and the boot image. Since both are accessed before drives are mounted, they reference files by physical location, and have to be created by the system not copied from another drive – ravery Jul 04 '18 at 23:06
  • Thanks ravery. How do I do that? Do I run update-initframfs after booting with the new drive? – Joe Jul 05 '18 at 00:00
  • I am trying the approach you described, but I am having dune trouble. What I did: ceated 3 partitions in new drive, 1 for /, 1 for /home, 1 for swap. All the partitions are bigger than the corresponding in the old drive. Then cloned the partitions for / and /home using the dd command in a live session. Updated fstab. Only updated the swap partition UUID given the other 2 partitions had the same UUID than the initial drive (were cloned). Turned off computer. Physically disconnected the old drive. Switched on computer. Gets stuck immediately after motherboard screen. What I am doing wrong? – Joe Jul 05 '18 at 00:39
  • if you are cloning, then clone the entire drive not individual partitions, updating initramfs will not be needed – ravery Jul 05 '18 at 01:07
  • I can't clone the entire drive bc the new drive is smaller than the original one and the original one has both windows and Linux, and I only want to move the Linux part to the new drive. Should I use cp -a to copy the files instead of cloning? – Joe Jul 05 '18 at 01:11
  • no, the problem is the boot files are moving, unless you clone the drive. – ravery Jul 05 '18 at 01:15
  • Ok, I see. Is there any realistic option to do what I am trying to do given I can't clone the drive? – Joe Jul 05 '18 at 01:19
1

First of all, cp -a is not appropriate in this situation. What you want to do is CLONE the partition. Here we will use the 'dd' tool but there are others available with GUI etc if you prefer eg Clonezilla.

Connect both drives to your computer. Make sure the new drive is the same size or larger than the partition you want to copy. It does not need to be formatted or anything, just leave it raw.

Boot a live DVD/USB and run:

sudo blkid

This will list all the drives and partitions on your computer. Make a careful note of the partition you want to copy, eg. /dev/sda1 and the new blank drive eg. /dev/sdb (no number).

Use dd to clone your partition to the new drive - be very careful, this can destroy your data if you type the wrong command! 'if' is the (old) partition you want to copy. 'of' is the new (blank) drive. Don't get these mixed up! So for example:

sudo dd if=/dev/sda1 of=/dev/sdb status=progress

You can add bs=4096 if you want it to go faster, but I prefer not to. Now go away and do something else because it will take a while.

When it's done (you are still in the live session at this point) you can add a swap partition to the new drive if you want to, using gparted, then install/run Boot Repair as below - this will install a nice fresh Grub onto the drive of your choice (overwriting any existing one) and find any OS on any drive connected:

sudo add-apt-repository ppa:yannubuntu/boot-repair
sudo apt-get update
sudo apt-get install -y boot-repair && boot-repair

Reboot and enjoy. Note: you will want to make sure your BIOS is booting the drive with the fresh Grub on it.

~~~~~~~~~~

UPDATE: Sorry for the late reply. So yes, if you haven't already done it by now, what you said before is what you want to do. Create an empty ext4 partition of about 20Gb on the new drive, to install the new Ubuntu system. Clone the /home partition from your old drive onto the new one (make sure you have enough space), then finally add the swap partition. You will then be ready to install the system on the new drive from your USB session - select the 'something else' option during install, choose to install / on the empty 20Gb partition WITH formatting, select the new cloned partition for /home WITHOUT formatting and place Grub on the new drive (see the dropdown selection at the bottom, the new drive should already be selected by default) and that should do the trick.

Bear in mind that when you're done, Grub should show Ubuntu x2 and Windows x1 so make sure you're booting the correct one.

  • I took a look at my current system carefully, and the situation is more complex than initially described. In the current drive, I have 4 partitions. 1 Windows, 1 swap, 2 for Ubuntu. In 1 of those, I have the OS itself, with "/" as the mount point. In the other, I have the data, with "/home" as mount point. I would like to have in the new drive both the OS and the data. Should I do the following? i) boot a live USB, ii) create 3 partitions in the new drive, 1 for the OS (/), 1 for the data (/home), and 1 for the swap, iii) clone the OS as described using dd, iv) clone the data using dd – Joe May 13 '18 at 08:38
  • Having separate / and /home partitions is actually the best way to do it. Let me hazard a guess that you want to run your system on the new drive (it's a SSD right?) but you want to keep all your data and app settings, and your Windows system on the old drive? Tell me if this is the case and then I'll give you the answer you're really looking for. – JimDeadlock May 13 '18 at 16:39
  • Mostly yes. I want to keep in my old drive windows (I believe that if I move it to another drive it will not work bc of license), and the exact Ubuntu and data as I have. Then in the new drive again the same exact Ubuntu and data (I know it is duplicated). The new drive is a nvme ssd. The workloads I have in my current Ubuntu read and write huge files constantly, so I hope the new drive will help. But the system I have in place is so complex to set up that I want to duplicate everything 1st, and ensure it works in the new drive. If sthg goes wrong, I still have the system in the current drive – Joe May 13 '18 at 23:25