~ev/goget-ubuntu-touch/root-size-option

« back to all changes in this revision

Viewing changes to ubuntu-emulator/common.go

  • Committer: Sergio Schvezov
  • Date: 2014-01-07 15:19:28 UTC
  • Revision ID: sergio.schvezov@canonical.com-20140107151928-6l3dsvkkcdyuk7af
Initial code migration

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// ubuntu-emu - Tool to download and run Ubuntu Touch emulator instances
 
3
//
 
4
// Copyright (c) 2013 Canonical Ltd.
 
5
//
 
6
// Written by Sergio Schvezov <sergio.schvezov@canonical.com>
 
7
//
 
8
package main
 
9
 
 
10
// This program is free software: you can redistribute it and/or modify it
 
11
// under the terms of the GNU General Public License version 3, as published
 
12
// by the Free Software Foundation.
 
13
//
 
14
// This program is distributed in the hope that it will be useful, but
 
15
// WITHOUT ANY WARRANTY; without even the implied warranties of
 
16
// MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
17
// PURPOSE.  See the GNU General Public License for more details.
 
18
//
 
19
// You should have received a copy of the GNU General Public License along
 
20
// with this program.  If not, see <http://www.gnu.org/licenses/>.
 
21
 
 
22
import (
 
23
        "errors"
 
24
        "fmt"
 
25
        "io/ioutil"
 
26
        "launchpad.net/goget-ubuntu-touch/bootimg"
 
27
        "os"
 
28
        "path/filepath"
 
29
        "strings"
 
30
)
 
31
 
 
32
func getDeviceTar(files []string) (string, error) {
 
33
        for _, file := range files {
 
34
                if strings.Contains(file, "device") {
 
35
                        return file, nil
 
36
                }
 
37
        }
 
38
        return "", errors.New("Could not find device specific tar")
 
39
}
 
40
 
 
41
func extractBoot(dataDir string) error {
 
42
        bootPath := filepath.Join(dataDir, "boot.img")
 
43
        imgBytes, err := ioutil.ReadFile(bootPath)
 
44
        if err != nil {
 
45
                return errors.New(fmt.Sprintf("Cannot read %s", bootPath))
 
46
        }
 
47
        boot, err := bootimg.New(imgBytes)
 
48
        if err != nil {
 
49
                return err
 
50
        }
 
51
        ramdiskPath := filepath.Join(dataDir, "ramdisk.img")
 
52
        kernelPath := filepath.Join(dataDir, kernelName)
 
53
        if err := boot.WriteRamdisk(ramdiskPath); err != nil {
 
54
                return err
 
55
        }
 
56
        if boot.WriteKernel(kernelPath); err != nil {
 
57
                return err
 
58
        }
 
59
        return nil
 
60
}
 
61
 
 
62
func instanceExists(path string) bool {
 
63
        f, err := os.Stat(path)
 
64
        if err == nil {
 
65
                if f.IsDir() {
 
66
                        return true
 
67
                }
 
68
        }
 
69
        return false
 
70
}
 
71
 
 
72
func getInstanceDataDir(instanceName string) (dataDir string) {
 
73
        dataDir = os.Getenv("XDG_DATA_HOME")
 
74
        if dataDir == "" {
 
75
                dataDir = "$HOME/.local/share"
 
76
        }
 
77
        dataDir = os.ExpandEnv(dataDir)
 
78
        return filepath.Join(dataDir, filepath.Base(os.Args[0]), instanceName)
 
79
}