Package br.com.objectos.rio.eto.os

Source Code of br.com.objectos.rio.eto.os.OsInstall

/*
* 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.eto.os;

import java.io.File;

import br.com.objectos.rio.AbstractRioCommand;
import br.com.objectos.rio.HttpServer;
import br.com.objectos.rio.core.os.Chroot;
import br.com.objectos.rio.core.os.ChrootMount;
import br.com.objectos.rio.core.os.ChrootUmount;
import br.com.objectos.rio.core.os.Procs;
import br.com.objectos.way.base.io.Directory;
import br.com.objectos.way.base.io.Stdout;

import com.google.inject.Inject;

/**
* @author marcio.endo@objectos.com.br (Marcio Endo)
*/
class OsInstall extends AbstractRioCommand<OsInstallOptions> implements OsInstallCommand {

  private final EtoOsDirs dirs;

  @Inject
  public OsInstall(EtoOsDirs dirs) {
    this.dirs = dirs;
  }

  @Override
  protected String getCommandName() {
    return "eto os install";
  }

  @Override
  protected OsInstallOptions newOptions() {
    return new OsInstallOptions();
  }

  @Override
  protected void executeCommand(OsInstallOptions options) {
    if (options.download) {
      download(options);
    }

    if (options.partition) {
      partition(options);
    }

    if (options.unpack) {
      unpack(options);
    }

    if (options.grub) {
      grub(options);
    }
  }

  private void download(OsInstallOptions options) {
    HttpServer server = options.fileServer();
    String stage3Name = options.getStage3Name();
    Directory osVar = dirs.osVar();

    infoAction("download");
    info("Downloading required files.");

    server.download(stage3Name)
        .toDir(osVar);
  }

  private void partition(OsInstallOptions options) {
    Directory osVar = dirs.osVar();
    String sfdiskName = options.sfdiskName();
    String dev1 = options.dev(1);
    String dev2 = options.dev(2);
    String dev3 = options.dev(3);

    infoAction("partition");
    info("Partitioning target device.");

    resourcesAt("/os/disk")
        .add(sfdiskName)
        .copyTo(osVar);

    Procs.newCommand()
        .add("sh")
        .add("-c")
        .add("sfdisk %s < %s", options.device, osVar.fileAt(sfdiskName).getAbsolutePath())
        .exec();

    mk2fs(dev1);
    mk4fs(dev3);
    mkswap(dev2);
    swapon(dev2);
  }

  private void unpack(OsInstallOptions options) {
    File stage3File = options.getStage3File(dirs);
    String dev1 = options.dev(1);
    String dev3 = options.dev(3);

    infoAction("unpack");
    info("Upacking to target device.");

    Directory installMountPoint = dirs.installMountDir();
    mount(dev3).to(installMountPoint);
    mount(dev1).to(installMountPoint.dirAt("boot"));

    untar(stage3File)
        .preserve()
        .gunzip()
        .toDir(installMountPoint);

    umount(installMountPoint.dirAt("boot"));
    umount(installMountPoint);
  }

  private void grub(OsInstallOptions options) {
    String dev1 = options.dev(1);
    String dev3 = options.dev(3);
    Directory installMountPoint = dirs.installMountDir();
    mount(dev3).to(installMountPoint);
    mount(dev1).to(installMountPoint.dirAt("boot"));

    infoAction("grub");
    info("Installing grub2.");

    ChrootMount mount = ChrootMount.at(installMountPoint).mount();

    try {

      Chroot chroot = Chroot.at(installMountPoint)
          .add("source /etc/profile")
          .add("export PS1=\"(chroot) $PS1\"")
          .add("grub2-install %s", options.device)
          .add("grub2-mkconfig -o /boot/grub/grub.cfg")
          .exec();

      Stdout.print(chroot);

    } finally {

      ChrootUmount umount = mount.umount();
      Stdout.print(umount);
      umount(installMountPoint.dirAt("boot"));
      umount(installMountPoint);

    }
  }

}
TOP

Related Classes of br.com.objectos.rio.eto.os.OsInstall

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.