Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/mkusbimg
blob: 5e655b24edff329bfc7b091edb8ac9999c73769c (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
#!/bin/bash
#
#    mkusbimg - creates a bootable disk image
#    Copyright (C) 2008  Simo Leone <simo@archlinux.org>
#
#    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, either version 3 of the License, or
#    (at your option) any later version.
#
#    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.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

# next_avail_loop()
# prints the next available loopback device
# returns 0 on success
#         1 on failure
# XXX: this is only necessary because
#      the cryptoloop patch for losetup
#      destroys losetup -f
next_avail_loop()
{
    for i in /dev/loop/*; do
        echo `losetup -a|cut -d':' -f1` | grep -q $i
        if [ $? -eq 1 ]; then
            echo $i
            return 0
        fi
    done
    return 1
}

# usage(exitvalue)
# outputs a usage message and exits with value
APPNAME=$(basename "${0}")
usage()
{
    echo "usage: ${APPNAME} <imageroot> <imagefile>"
    exit $1
}

##################################################

if [ $# -ne 2 ]; then
    usage 1
fi

IMG="${2}"
IMGROOT="${1}"
LOOPDEV=$(next_avail_loop)
TMPDIR=$(mktemp -d)

# TODO: there are better ways to do this
#       than adding 25% to the rootsize
#  XXX: doesn't seem to boot if we cut it too
#       close. even if everything fits...
# IMGSZ >= filesystem overhead + rootsize + 512bytes
# must hold or there will be insufficient space
rootsize=$(du -bs ${IMGROOT}|cut -f1)
IMGSZ=$(( (${rootsize}*5)/4 + 512 ))

# create the image file
dd if=/dev/zero of="$IMG" bs="$IMGSZ" count=1

# loop mount the disk image
losetup "$LOOPDEV" "$IMG"

# create a partition table
# if this looks like voodoo, it's because it is
echo "63,,,*,"|sfdisk -uS "$LOOPDEV"

# loop mount the partition we just made
# that magic number (offset in bytes to first partition) is more voodoo
losetup -d "$LOOPDEV"
losetup -o32256 "$LOOPDEV" "$IMG"

# create a filesystem on our partition
mke2fs -m 0 "$LOOPDEV"

# mount the filesystem and copy data
mount "$LOOPDEV" "$TMPDIR"
cp -a "$IMGROOT"/* "$TMPDIR"

# unmount filesystem and loopback
umount "$TMPDIR"
losetup -d "$LOOPDEV"

# install grub on the image
grub --no-floppy --batch << EOF
device (hd0) $IMG
root (hd0,0)
setup (hd0)
EOF

# all done :)
rm -fr "$TMPDIR"