7

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?

2 Answers 2

12

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

The chroot is used to move an operating system to a new disk without rebooting, installing an operating system, and other tasks that require installation of a new file system.

How to chroot with the correct mounts, correspond to the present system (Kernel) to a new file system. Install and setup GRUB 2 and possibly doing more tasks.

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

2) If you are using a separate boot disk or partition, it must be mounted (where sdXY is the /boot partition designation):
sudo mount /dev/sdXY /mnt/boot

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

4) Chroot into the new file system:
sudo chroot /mnt

5) Install GRUB 2 on the drive that the BIOS is set up to boot. If the drive is part of a RAID setup, repeat the command with all drive letters used in the array.
(Only specify a drive letter because the master boot record is installed in a very specific way):
grub-install /dev/sdX

6) Generate 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

1

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.]

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .