~smoser/maas-images/trunk.lp1582410

« back to all changes in this revision

Viewing changes to bin/maas-cloudimg2eph2

  • Committer: Scott Moser
  • Date: 2016-05-04 22:20:04 UTC
  • Revision ID: smoser@ubuntu.com-20160504222004-5hy1mft1r7zczbu9
bin/maas-cloudimg2eph2: add support for source as squashfs image

Simply add the ability for the source to be a squashfs image.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 
3
3
VERBOSITY=1
4
4
TEMP_D=""
5
 
VALID_FORMATS=( auto img-tar root-image root-image-gz root-tar )
 
5
VALID_FORMATS=( auto img-tar root-image root-image-gz root-tar squashfs-image )
6
6
 
7
7
error() { echo "$@" 1>&2; }
8
8
fail() { [ $# -eq 0 ] || error "$@"; exit 1; }
74
74
    mv "$found" "$out" && rm -Rf "$mtmp"
75
75
}
76
76
 
 
77
squashfs_to_image() {
 
78
    local squashimg="$1" output="$2" tempd="$3" size="4G"
 
79
    local mtmp=$(mktemp -d "${tempd}/root_tar_to_image.XXXXXX")
 
80
    local xout="" ret=""
 
81
    local srcmp="$mtmp/src" trgmp="$mtmp/target"
 
82
    truncate "--size=$size" "$output" || {
 
83
        error "failed to create file of $size in $output"
 
84
        return 1
 
85
    }
 
86
    mkdir "$srcmp" "$trgmp" || {
 
87
        error "failed mkdir $srcmp $trgmp"
 
88
        return 1;
 
89
    }
 
90
        
 
91
    out=$(mkfs.ext4 -F "$output" -L "cloudimg-rootfs" 2>&1) ||
 
92
        { error "failed mkfs.ext4 -F '$output'"; return 1; }
 
93
    debug 1 "turning squahsfs image in $squashimg to ext4 image in $output"
 
94
    sudo bash -ec 'src="$1"; srcmp="$2"; img="$3"; trgmp="$4";
 
95
        mounts=""
 
96
        cleanup() { for m in $mounts; do umount "$m"; done; }
 
97
        trap cleanup EXIT
 
98
        mount -o loop "$src" "$srcmp"
 
99
        mounts="$srcmp"
 
100
        mount -o loop "$img" "$trgmp"
 
101
        mounts="$srcmp $trgmp"
 
102
        rsync --archive --sparse "$srcmp/" "$trgmp/"' \
 
103
        "squashimg-to-image" "$squashimg" "$srcmp" "$output" "$trgmp"
 
104
    ret=$?
 
105
    rm -Rf "$mtmp" || return
 
106
    return $ret
 
107
}
 
108
 
77
109
root_tar_to_image() {
78
110
    local tball="$1" output="$2" tempd="$3" size="4G"
79
111
    local mtmp=$(mktemp -d "${tempd}/root_tar_to_image.XXXXXX")
110
142
                ;;
111
143
            "POSIX tar"*) fmt="img-tar";;
112
144
            *ext[234]\ filesystem*) fmt="root-image";;
 
145
            *[Ss]quashfs*) fmt="squashfs-image";;
113
146
            *)
114
147
                # if the above failed (on trusty a .tar.gz file was reported
115
148
                # as a Minux file system) then try filename based heuristics
117
150
                    *-root.t??|*-root.tar|*-root.tar.??) fmt="root-tar";;
118
151
                    *.tar.gz|*.tgz|*.tar) fmt="img-tar";;
119
152
                    *.gz) fmt="root-image-gz";;
 
153
                    *.squashfs) fmt="squashfs-image";;
120
154
                    *) 
121
155
                        error "WARN: file '$input' did not match name hueristics"
122
156
                        fmt="root-image";;
134
168
            ln -s "$(readlink -f "$input")" "$output";;
135
169
        root-tar)
136
170
            root_tar_to_image "$input" "$output" "$tempd";;
 
171
        squashfs-image)
 
172
            squashfs_to_image "$input" "$output" "$tempd";;
137
173
        *)
138
 
            error "Unknonwn format '$fmt'";
 
174
            error "Unknown format '$fmt'";
139
175
            return 1;;
140
176
    esac
141
177
    _RET="$fmt"