Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am setting up an automated installer-system to install customized Ubuntu 12 and other operating systems onto disks, which will be removed and then permanently installed into new computers.

The problem is generating grub configuration -- if I run grub-mkconfig on the installer system, then it will put the UUID of the installer system's disk in rather than the target disk. Is there a way to tell grub-mkconfig which disk I want to use as root? If I chroot to the new system will it use whatever UUID that filesystem seems to be on? Or do I have to resort to sed to replace the search commands in grub.cfg?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

Yes, chroot will be fine, however, be sure that the system you chroot from fit to the system that you chroot into.

chroot with the correct mounts, correspond to relocate the boot system (Kernel) to a new file system without rebooting. - The chroot are used for moving OS to a new disk without rebooting, but also to install a Linux system from a LiveCD, and similarly.

1) Mount your normal system partition. X is the drive letter. Y is the partition number:
sudo mount /dev/sdXY /mnt

2) Only if you have a separate boot partition (where sdXY is the /boot partition designation):
sudo mount /dev/sdXY /mnt/boot

3) Mount the critical virtual filesystems.
for i in /dev /dev/pts /proc /sys /run; do sudo mount -B $i /mnt$i; done

4) Chroot into your normal system device:
sudo chroot /mnt

5) Install GRUB 2 (substitute the correct device with sda, sdb, etc. Do not specify a partition number):
grub-install /dev/sdX

6) Recreate the GRUB 2 menu file (grub.cfg)
update-grub

7) Perform other tasks such as updates, install applications, configure, etc.
...

8) Exit chroot:
CTRL-D on the keyboard

https://help.ubuntu.com/community/Grub2/Installing#via_ChRoot

share|improve this answer

I ended up using chroot to do this, but first I manually changed the UUID of my target disk in grub.cfg on the target disk. I used the blkdev command to find out the UUID of the target disk partition. It looks something like this ($targetdir is the target filesyste mmounted, $diskdev is the disk device as seen from the host system, e.g. /dev/sdb, and $partitiondev is the disk partition, e.g. /dev/sdb1). At this point I've already partitioned the disk, done mkfs, mounted it, then copied the desired contents of the new filesystem to the disk.

targetuuid=`blkid -s UUID -o value $partitiondev`
test -n "$targetuuid" || error "Could not determine UUID for $partitiondev"
sed --in-place=orig 's/search .*/search --no-floppy --fs-uuid --set=root $targetuuid/' $targetdir/boot/grub/gub.cfg || error "Error setting correct UUID in $targetdir/boot/grub/grub.cfg"
mount -o bind /dev $targetdir/dev 
# could also bind other pseudo-filesystems like /proc as in Diblo Dk's answer.
chroot $targetdir grub-install --no-floppy --recheck $diskdev
umount $targetdir/dev # don't forget to unmount this first otherwise you can't unmount $targetdir later

[By the way, in case anyone else encounters a few tangential problems I had in doing this -- I used the "buildroot" tool to make a minimal Linux system in an initramfs to load from a network boot (pxe). If you enable util-linux and select mount/umount under "install utilities", then a slightly broken version of mount is installed (replacing the built-in mount command in busybox), which seems to crash all the time. Also, I had to enable use of devtmpfs in kernel config, and select devtmpfs in buildroot, so that rebinding the /dev filesystem would work -- I guess in the version of busybox I have the bind option to mount only seems to work if you are rebinding a filesystem not a directory within a filesystem. This is all with buildroot version 2013.05, which uses busybox version 1.21.1, util-linux 2.22.2. Maybe these have been fixed since.]

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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