34

Does anybody have a suggestion for how to move the root partition to a new drive and set up grub2 to boot on that drive? I seem to have no luck instructing grub-mkconfig what it is I want to do (e.g. chroot'int into my new root just confuses all the scripts).

Background I am running Debian Squeeze on a headless low-power NAS. My current setup is / on sda0 and /boot on sde0 (a CF card): I needed the separate /boot because sd[a-d] need to do a delayed spin-up. Now I've found an old 2.5" IDE disk to use as / including /boot to allow me to spin all the big disks down.

What I've tried Basically I went

mount -o rw /dev/sdf5 /mnt/newroot
cp -ax / /mnt/newroot
cp -ax /boot /mnt/newroot/boot

Then I tried

chroot /mnt/newroot
update-grub

But that failed with grub asking if root was mounted. Then I did a half-hearted attempt at setting up /mnt/newroot/grub/grub.cfg to find the kernel image on sdf5, followed by a grub-install --root-directory=/mnt/newroot /dev/sdf. But this just landed me a grub rescue prompt when I tried booting from sdf.

My backup plan is to just reinstall, so a bonus question (no checkmarks for this one): What do I have to do to get my lvm2 and mdadm config across? Is it all stored in the filesystems (and will it be automatically discovered), or do I need to take of it myself?

Solution (thanks to Maciej Piechotka): As Maciej points out, I need to to a proper chroot for all the grub tools to work. For reference, this is how I did it:

janus@nasguld:/mnt/newroot$ sudo cp -ax / /mnt/newroot
janus@nasguld:/mnt/newroot$ sudo cp -ax /boot /mnt/newroot

All the files are now copied (see here for a discussion of copy strategies). Fix the new etc/fstab to point to new root:

janus@nasguld:/mnt/newroot$ diff -u etc/fstab.old  etc/fstab
    -UUID=399b6a6d-c067-4caf-bb3e-85317d66cf46 /     ext3 errors=remount-ro         0 1
    -UUID=b394b614-a977-4860-bbd5-7862d2b7e02a /boot ext3 defaults                  0 2
    +UUID=b9d62595-e95c-45b1-8a46-2c0b37fcf153 /     ext3 noatime,errors=remount-ro 0 1

Finally, mount dev,sys and proc to the new root and chroot:

janus@nasguld:/mnt/newroot$ sudo mount -o bind /dev /mnt/newroot/dev
janus@nasguld:/mnt/newroot$ sudo mount -t proc none  /mnt/newroot/proc
janus@nasguld:/mnt/newroot$ sudo mount -t sysfs none /mnt/newroot/sys
janus@nasguld:/mnt/newroot$ sudo parted /dev/sdb set 5 boot on 
janus@nasguld:/mnt/newroot$ sudo chroot .

We are now chrooted to the future root exactly as it will look. According to Maciej, it should be ok to just call grub-install, but I did an update-grub first to get a look at the generated /boot/grub/grub.cfg before installing the bootloader. I am not sure it will be automatically updated?

root@nasguld:/# update-grub
root@nasguld:/# grub-install /dev/sdb
4
  • Post the contents of /etc/default/grub, and the exact transcript from update-grub. Jan 1, 2011 at 17:00
  • (thoughts after adding solution): It might actually be that the problem was that I hadn't updated /etc/fstab in the chroot (this would be consistent with update-grub complaining that "\ isn't mounted"). Were I to do this again, I would try first without bothering to mount the special file systems in the chroot.
    – Janus
    Jan 3, 2011 at 1:12
  • just a note: do not forget to unmount /mnt/newroot before reboot! May 16, 2013 at 20:31
  • Here are instructions on how to move your root partition / to new nvme drive, while keeping the /home on hdd. lucasmanual.com/blog/…
    – Lucas
    Sep 17, 2018 at 2:51

3 Answers 3

24

Mount basic filesystems and copy/modify files while chrooting like:

  • /dev (mount -o bind /dev/ /path/to/chroot/dev)
  • /proc (mount -t proc none /path/to/chroot/proc)
  • /sys (mount -t sysfs none /path/to/chroot/sys)

IIRC that worked for me while installing Grub 2 in arch and numerous times on Gentoo. Then after chroot to /path/to/chroot command was simply:

grub-install /dev/<boot_disk>

As of lvm2 (and I belive madm but I haven't used it) the configuration is stored on disk. There is configuration what should be read to discover devices. Assuming your devices are in standard locations (/dev/sd* or /dev/hd*) there should be no problem.


PS. I would not trust simple cp of live system as there are several places where it can go wrong:

  • Forgot to change /etc/fstab and other useful files
  • Files changed during access
  • Coping garbage (/tmp etc.)
4
  • Thanks. So basically, you are saying I do a sloppy chroot :) I'll give it a try with a proper chroot. cp -ax should be fine (maybe after dropping to single user runlevel) according to this old howto: tldp.org/HOWTO/Hard-Disk-Upgrade/copy.html
    – Janus
    Jan 2, 2011 at 14:52
  • Worked a charm! Thank you very much for the help.
    – Janus
    Jan 2, 2011 at 15:50
  • 1
    @Janus: I'd copy data from Live distro to be on safe side. I'm glad however it worked. Jan 2, 2011 at 20:37
  • 2
    I had to do update-grub before install-grub. Aug 8, 2013 at 17:08
4

you can install grub from live distro without chrooting:

grub-install /dev/hda --root-directory=/mnt/guest/
2
  • Thanks. I'm not sure this would work here: as I understand it, grub-install only updates the grub image files and writes the MBR: In particular, grub.cfg is not updated. As I commented above, mounting the special dirs might be overkill, but I still think the chroot is the way to update grub.cfg in a simple way?
    – Janus
    Jan 4, 2011 at 7:23
  • Yes, this didn't work for me either. You have to get update-grub to work and that one does not have --root-directory or something, no?
    – Mitar
    Jun 30, 2017 at 6:40
1

BTW if you are adding a partition (like a windows) on which you don't want grub to write a in the boot sector, but you want grub to know about it when your computer boots, you can re-scan the partitions and generate a new grub.cfg file by using the grub_mkconfig command as follows in a terminal session

cd /boot/grub

sudo cp grub.cfg ./grub.cfg.old

sudo grub_mkconfig -o ./grub.cfg

Now when you boot off of your current linux partition (that had grub booting it) it will now know about the other partition.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.