summaryrefslogtreecommitdiffstats
path: root/mkarchroot
blob: d735cf068f07c2e9339c5b84021eb8fb5cf3b5dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/bin/sh
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

FORCE="n"
RUN=""

working_dir=""

APPNAME=$(basename "${0}")

# usage: usage <exitvalue>
usage ()
{
    echo "usage ${APPNAME} [options] working-dir [package-list | app]"
    echo " options:"
    echo "    -r <app>      Run 'app' within the context of the chroot"
    echo "    -u            Update the chroot via pacman"
    echo "    -f            Force overwrite of files in the working-dir"
    echo "    -h            This message"
    exit $1
}

while getopts 'r:ufh' arg; do
    case "${arg}" in
        r) RUN="$OPTARG" ;;
        u) RUN="pacman -Syu" ;;
        f) FORCE="y" ;;
        h|?) usage 0 ;;
        *) echo "invalid argument '${arg}'"; usage 1 ;;
    esac
done

if [ "$EUID" != "0" ]; then
    echo "error: this script must be run as root."
    exit 1
fi

shift $(($OPTIND - 1))

if [ "$RUN" == "" -a $# -lt 2 ]; then
    echo "you must specify a directory and one or more packages"
    usage 1
elif [ $# -lt 1 ]; then
    echo "you must specify a directory"
    usage 1
fi

working_dir="$(readlink -f ${1})"
shift 1

[ "${working_dir}" = "" ] && echo "error: please specify a working directory" && usage 1

# {{{ functions 

function chroot_mount ()
{
    echo "mounting sysfs : /sys"
    [ -e "${working_dir}/sys" ] || mkdir "${working_dir}/sys"
    mount -t sysfs sysfs "${working_dir}/sys"

    echo "mounting procfs : /proc"
    [ -e "${working_dir}/proc" ] || mkdir "${working_dir}/proc"
    mount -t proc proc "${working_dir}/proc"

    echo "binding device nodes : /dev"
    [ -e "${working_dir}/dev" ] || mkdir "${working_dir}/dev"
    mount -o bind /dev "${working_dir}/dev"

    echo "binding pacman cache : /var/cache/pacman"
    [ -e "${working_dir}/var/cache/pacman" ] || mkdir -p "${working_dir}/var/cache/pacman"
    mount -o bind /var/cache/pacman "${working_dir}/var/cache/pacman"

    trap 'chroot_umount' 0 1 2 15
}

function chroot_umount ()
{
    echo "cleaning up mounts"
    umount "${working_dir}/proc"
    umount "${working_dir}/sys"
    umount "${working_dir}/dev"
    umount "${working_dir}/var/cache/pacman"
}
# }}}

if [ "$RUN" != "" ]; then
# run chroot {{{
    #Sanity check
    if [ ! -f "${working_dir}/.arch-chroot" ]; then
        echo "error: '${working_dir}' does not appear to be a Arch chroot"
        echo "  please build the image using mkarchchroot"
        exit 1
    fi

    chroot_mount

    echo "starting chroot ($RUN)"
    chroot "${working_dir}" "${RUN}"

# }}}
else
# {{{ build chroot
    if [ -e "${working_dir}" -a "${FORCE}" = "n" ]; then
        echo "error: working dir '${working_dir}' already exists - try using -f"
        exit 1
    fi

    mkdir -p "${working_dir}/var/lib/pacman/"

    chroot_mount

    pacargs="--noconfirm -v " #--noprogressbar -v
    pacargs="$pacargs --root=${working_dir}"
    # pacman takes these as relative to the given root
    pacargs="$pacargs --dbpath=/var/lib/pacman"
    pacargs="$pacargs --cachedir=/var/cache/pacman/pkg"

    if [ $# -eq 0 ]; then
        echo "no packages to install"
    else
        echo "installing packages:"
        for i in $@; do echo -e "\t$i"; done

        op="-Sy"
        if [ "$FORCE" == "y" ]; then
            op="${op}f"
        fi
        if ! pacman ${op} ${pacargs} $@; then
            echo "error: failed to install all packages"
            exit 1
        fi
    fi

    if [ -d "${working_dir}/lib/modules" ]; then
        echo "running ldconfig"
        ldconfig -r "${working_dir}"
    fi

    echo "generating default locales"
    sed -i "s|^#\(.*$LANG\)|\1|g" "${working_dir}/etc/locale.gen"
    chroot "${working_dir}" locale-gen

    if [ ! -e "${working_dir}/.arch-chroot" ]; then
        date +%s > "${working_dir}/.arch-chroot"
    fi
# }}}
fi

# vim:ft=sh:ts=4:sw=4:et: