/*
* Copyright 2014 Objectos, Fábrica de Software LTDA.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package br.com.objectos.rio;
import java.util.List;
import br.com.objectos.way.base.io.Directory;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
/**
* @author marcio.endo@objectos.com.br (Marcio Endo)
*/
class GentooPrepareCommandInitramfs extends AbstractGentooPrepareCommand {
public GentooPrepareCommandInitramfs(GentooPrepare installer) {
super(installer);
}
@Override
void execute() {
Directory mountDir = pojo.getMountDir();
String kernelVersion = pojo.getKernelVersion();
String script = resourceToPrintf("/gentoo/initramfs/bin/simple.script");
List<String> _init = ImmutableList.<String> builder()
.add("#!/bin/busybox sh")
.add("echo 'Trying to mount fs'")
.add("mount -t proc proc /proc")
.add("mount -t devtmpfs udev /dev")
.add("mount -t sysfs none /sys")
.add("busybox --install -s")
.add("echo 'Trying to obtain IP'")
.add("modprobe atl1c")
.add("modprobe atl1e")
.add("modprobe r8101")
.add("modprobe r8168")
.add("ifconfig eth0 up")
.add("udhcpc -t 5 -q -s /bin/simple.script")
.add("echo 'Trying mount root'")
.add(fmt("mount -o nolock -t nfs 192.168.0.10:/iro/%s /mnt/root", pojo.getPxe()))
.add("umount /proc")
.add("umount /sys")
.add("echo 'Trying to switch to nfs mount'")
.add("exec switch_root /mnt/root /sbin/init")
.add("")
.build();
String init = Joiner.on("\n").join(_init);
infoAction("initramfs");
info("Preparing initramfs.");
chrootAt(mountDir)
.add("source /etc/profile")
.add("export PS1=\"(chroot) $PS1\"")
.add("rm -rf /usr/src/initramfs")
.add("mkdir /usr/src/initramfs")
.add("cd /usr/src/initramfs")
.add("mkdir -p bin dev etc lib lib64 mnt/root proc root sbin sys")
.add("cp -a /dev/{null,console,tty,sda1} /usr/src/initramfs/dev/")
.add("mkdir -p /usr/src/initramfs/lib/modules")
.add("cp -a /lib/modules/* /usr/src/initramfs/lib/modules")
.add("USE=\"static\" emerge -uv busybox")
.add("cp -a /bin/busybox /usr/src/initramfs/bin/busybox")
.add("printf \"%s\" > /usr/src/initramfs/bin/simple.script", script)
.add("chmod +x /usr/src/initramfs/bin/simple.script")
.add("printf \"%s\" > /usr/src/initramfs/init", init)
.add("chmod +x /usr/src/initramfs/init")
.add("cd /usr/src/initramfs")
.add("emerge -uv cpio")
.add("find . | cpio --create --format='newc' > /boot/initrd-%s", kernelVersion)
.add("gzip -f /boot/initrd-%s", kernelVersion)
.exec();
}
private String fmt(String format, Object... args) {
return String.format(format, args);
}
}