mkarchiso 8.11 KB
Newer Older
1
#!/bin/bash
2
3
4
5

PKGLIST=""
QUIET="y"
FORCE="n"
6
PACCONFIG="/etc/pacman.conf"
7
export LABEL="ARCH_$(date +%Y%m)"
8
9
PUBLISHER="Arch Linux <http://www.archlinux.org>"
APPLICATION="Arch Linux Live/Rescue CD"
10
COMPRESSION="xz"
11
CREATE_DEFAULT="n"
12
INSTALL_DIR="arch"
13
14

APPNAME=$(basename "${0}")
15
ARCH=$(uname -m)
16
17
18
19
20
21
22

# usage: usage <exitvalue>
usage ()
{
    echo "usage ${APPNAME} [options] command <command options>"
    echo " general options:"
    echo "    -f               Force overwrite of working files/squashfs image/bootable image"
23
    echo "    -p PACKAGE(S)    Additional package(s) to install, can be used multiple times"
24
    echo "    -C <file>        Config file for pacman. Default $PACCONFIG"
25
26
27
    echo "    -L <label>       Set a label for the disk"
    echo "    -P <publisher>   Set a publisher for the disk"
    echo "    -A <application> Set an application name for the disk"
28
    echo "    -c <compressor>  Set SquashFS compression type: gzip, xz or lzo. Default $COMPRESSION"
29
    echo "                     NOTES:"
30
    echo "                            xz: needs Linux >= 2.6.38"
31
    echo "                            lzo:  needs Linux >= 2.6.36"
32
33
34
    echo "    -D <install_dir> Set an install_dir. All files will by located here on ISO (except for syslinux)"
    echo "                     Default $INSTALL_DIR"
    echo "                     NOTE: Max 8 characters, use only [a-z0-9]"
35
36
37
    echo "    -d               Create default user directory /home/arch"
    echo "    -v               Enable verbose output"
    echo "    -h               This message"
38
    echo " commands:"
39
40
41
    echo "   create <dir>"
    echo "      create a base directory layout to work with"
    echo "      includes all specified packages"
42
    echo "   iso <dir> <image name>"
43
    echo "      build an iso image from the working dir"
44
45
46
    exit $1
}

47
while getopts 'p:C:L:P:A:c:D:dfvh' arg; do
48
49
    case "${arg}" in
        p) PKGLIST="${PKGLIST} ${OPTARG}" ;;
50
        C) PACCONFIG="${OPTARG}" ;;
51
52
53
        L) LABEL="${OPTARG}" ;;
        P) PUBLISHER="${OPTARG}" ;;
        A) APPLICATION="${OPTARG}" ;;
54
        c) COMPRESSION="${OPTARG}" ;;
55
        D) INSTALL_DIR="${OPTARG}" ;;
56
        d) CREATE_DEFAULT="y" ;;
57
58
59
60
61
62
63
        f) FORCE="y" ;;
        v) QUIET="n" ;;
        h|?) usage 0 ;;
        *) echo "invalid argument '${arg}'"; usage 1 ;;
    esac
done

64
65
66
#trim spaces
PKGLIST="$(echo $PKGLIST)"

67
68
69
70
71
72
73
74
shift $(($OPTIND - 1))

# do UID checking here so someone can at least get usage instructions
if [ "$EUID" != "0" ]; then
    echo "error: This script must be run as root."
    exit 1
fi

75
76
77
78
79
if [ ! -f "$PACCONFIG" ]; then
    echo "error: pacman config file '$PACCONFIG' does not exist"
    exit 1
fi

80
command_name="${1}"
81
82
83
work_dir=""
imgname=""

84
case "${command_name}" in
85
86
    create) work_dir="${2}"; imgname="none" ;;
    iso) work_dir="${2}"; imgname="${3}" ;;
87
88
89
    *) echo "invalid command name '${command_name}'"; usage 1 ;;
esac

90
91
[ "x${imgname}" = "x" ] && echo "Image name must be specified" && usage 1
[ "x${work_dir}" = "x" ] && echo "Please specify a working directory" && usage 1
92

93
94
95
96
echo "${APPNAME} : Configuration Settings"
echo "        working directory:   ${work_dir}"
echo "               image name:   ${imgname}"

97
98
99
100
101
# usage: _pacman <packages>...
_pacman ()
{
    local ret
    if [ "${QUIET}" = "y" ]; then
102
        mkarchroot -n -C "$PACCONFIG" -f "${work_dir}/root-image" $* 2>&1 >/dev/null
103
104
        ret=$?
    else
105
        mkarchroot -n -C "$PACCONFIG" -f "${work_dir}/root-image" $*
106
107
        ret=$?
    fi
108
109
110
111

    # Cleanup
    find "${work_dir}" -name *.pacnew -name *.pacsave -name *.pacorig -delete

112
113
114
115
116
    if [ $ret -ne 0 ]; then
        exit 1
    fi
}

117
118
command_create () {
    echo "====> Creating working directory: ${work_dir}"
119
    mkdir -p "${work_dir}/iso/${INSTALL_DIR}/${ARCH}"
120
    mkdir -p "${work_dir}/root-image/"
121

122
123
124
    if [ "${PKGLIST}" != "" ]; then
        echo "====> Installing packages to '${work_dir}/root-image/'"
        _pacman "${PKGLIST}"
125

126
127
128
        echo "Cleaning up what we can"
        if [ -d "${work_dir}/root-image/boot/" ]; then
            # remove the initcpio images that were generated for the host system
129
            find "${work_dir}/root-image/boot" -name '*.img' -delete
130
131
        fi

132
133
134
135
136
        if [ ${CREATE_DEFAULT} == "y" ]; then
            if [ -d "${work_dir}/root-image/home/" ]; then
                echo "Creating default home directory"
                install -d -o1000 -g100 -m0755 "${work_dir}/root-image/home/arch"
            fi
137
138
        fi

139
140
141
142
143
144
145
146
147
148
149
150
        # Delete pacman database sync cache files (*.tar.gz)
        find "${work_dir}/root-image/var/lib/pacman" -maxdepth 1 -type f -delete
        # Delete pacman database sync cache
        find "${work_dir}/root-image/var/lib/pacman/sync" -delete
        # Delete pacman package cache
        find "${work_dir}/root-image/var/cache/pacman/pkg" -type f -delete
        # Delete all log files, keeps empty dirs.
        find "${work_dir}/root-image/var/log" -type f -delete
        # Delete all temporary files and dirs
        find "${work_dir}/root-image/var/tmp" -mindepth 1 -delete
        # Delete all temporary files and dirs
        find "${work_dir}/root-image/tmp" -mindepth 1 -delete
151
    fi
152
}
153

154
155
156
157
158
159
160
# _mksquash dirname
_mksquash () {
    if [ ! -d "$1" ]; then
        echo "Error: '$1' is not a directory"
        return 1
    fi

161
    sqimg="${work_dir}/iso/${INSTALL_DIR}/${ARCH}/$(basename ${1}).sqfs"
162
163
    echo "====> Generating SquashFS image for '${1}'"
    if [ -e "${sqimg}" ]; then
164
165
166
        dirhaschanged=$(find ${1} -newer ${sqimg})
        if [ "${dirhaschanged}" != "" ]; then
            echo "SquashFS image '${sqimg}' is not up to date, rebuilding..."
167
            rm "${sqimg}"
168
        else
169
170
            echo "SquashFS image '${sqimg}' is up to date, skipping."
            return
171
172
173
        fi
    fi

174
    echo "Creating SquashFS image. This may take some time..."
175
176
    start=$(date +%s)
    if [ "${QUIET}" = "y" ]; then
177
        mksquashfs "${1}" "${sqimg}" -noappend -comp "${COMPRESSION}" >/dev/null
178
    else
179
        mksquashfs "${1}" "${sqimg}" -noappend -comp "${COMPRESSION}"
180
181
182
    fi
    minutes=$(echo $start $(date +%s) | awk '{ printf "%0.2f",($2-$1)/60 }')
    echo "Image creation done in $minutes minutes."
183
}
184

185
186
187
188
189
190
191
192
193
_imgcommon () {
    for d in $(find "${work_dir}" -maxdepth 1 -type d -name '[^.]*'); do
        if [ "$d" != "${work_dir}/iso" -a \
             "$(basename "$d")" != "iso" -a \
             "$d" != "${work_dir}" ]; then
            _mksquash "$d"
        fi
    done

194
    echo "====> Making bootable image"
195
196
197
198
199
200
201

    # Sanity checks
    if [ ! -d "${work_dir}/iso" ]; then
        echo "Error: '${work_dir}/iso' doesn't exist. What did you do?!"
        exit 1
    fi

202
    if [ ! -f "${work_dir}/iso/${INSTALL_DIR}/isomounts" ]; then
203
204
205
206
207
        echo "Error: the isomounts file doesn't exist. This image won't do anything"
        echo "  Protecting you from yourself and erroring out here..."
        exit 1
    fi

208
209
210
211
212
    if [ -e "${imgname}" ]; then
        if [ "${FORCE}" = "y" ]; then
            echo "Removing existing bootable image..."
            rm -rf "${imgname}"
        else
213
            echo "Error: Image '${imgname}' already exists, aborting."
214
215
216
217
            exit 1
        fi
    fi

218
    if ! sed "s|%ARCHISO_LABEL%|${LABEL}|g;
219
220
              s|%INSTALL_DIR%|${INSTALL_DIR}|g;
              s|%ARCH%|${ARCH}|g" -i ${work_dir}/iso/syslinux/syslinux.cfg; then
221
        echo "Error: ${work_dir}/iso/syslinux/syslinux.cfg, doesn't exist, aborting."
222
223
        exit 1
    fi
224
225
226
227
}

command_iso () {
    _imgcommon
Aaron Griffin's avatar
Aaron Griffin committed
228

229
230
    echo "Creating ISO image..."
    qflag=""
Gerhard Brauer's avatar
Gerhard Brauer committed
231
    [ "${QUIET}" = "y" ] && qflag="-quiet"
232
    mkisofs ${qflag} -r -l \
233
        -b syslinux/isolinux.bin -c syslinux/boot.cat \
234
        -uid 0 -gid 0 \
235
        -udf -allow-limited-size -iso-level 3 \
236
237
        -input-charset utf-8 -p "prepared by mkarchiso" \
        -no-emul-boot -boot-load-size 4 -boot-info-table \
238
239
        -publisher "${PUBLISHER}" \
        -A "${APPLICATION}" \
240
        -V "${LABEL}" \
241
        -o "${imgname}" "${work_dir}/iso/"
242
    isohybrid "${imgname}"
243
244
}

245
246
# Go through the main commands in order. If 'all' was specified, then we want
# to do everything. Start with 'install'.
247
248
if [ "${command_name}" = "create"  ]; then
    command_create
249
fi
250
251
if [ "${command_name}" = "iso" ]; then
    command_iso
252
fi
253
254

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