forked from fabiand/image-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_to_squashfs
74 lines (56 loc) · 1.97 KB
/
image_to_squashfs
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
#!/usr/bin/bash
usage() {
cat <<EOF
Usage: $0 <diskimage> [<squashfsfile>]"
This tool converts a raw disk or filesystem image
into a by dracut bootable squashfs.
It has some logic to strip the label and bootloader from a
disk image to retrieve the filesystem.
EOF
}
info() { echo $(date +%X) $@ ; }
die() { echo $@ >&2 ; exit 1 ; }
find_first_fs_offset_kib() {
DISKIMAGE=$1
START_BYTES=$(parted -ms "$DISKIMAGE" unit KiB print | sed -n "/^1:/ p" | cut -d: -f2 | tr -d kiB)
echo $START_BYTES
}
extract_first_fs() {
DISKIMAGE=$1
FSIMAGE=$2
OFFSET=$(find_first_fs_offset_kib "$DISKIMAGE")
nice ionice dd conv=sparse bs=1K skip=$OFFSET if="$DISKIMAGE" of="$FSIMAGE"
}
image_to_squashfs()
{
SRCIMAGE=$1
DSTIMAGE=$2
[[ -f $SRCIMAGE ]] || die "Source image '$SRCIMAGE' does not exist."
[[ -f $DSTIMAGE ]] && die "Destination image '$DSTIMAGE' already exists, please remove manually."
[[ -e squashfs-root ]] && die "squashfs-root already exists, please remove manually."
mkdir -p squashfs-root/LiveOS
# Check if it's a disk image, then we need to remove the label
# to get the partition, assumption: On partition
# FIXME The size of the mbr/label is hardcoded, works by removing the label from the disk image
[[ $(file $SRCIMAGE) =~ "boot sector" ]] && {
info "Found a disk image, extracting the filesystem ..."
extract_first_fs "$SRCIMAGE" "squashfs-root/LiveOS/rootfs.img"
}
# If the image is already afilesystem, take it directly
[[ $(file $SRCIMAGE) =~ "filesystem" ]] && {
info "Found a filesystem image, using this directly."
ln -v $SRCIMAGE squashfs-root/LiveOS/rootfs.img
}
[[ -f squashfs-root/LiveOS/rootfs.img ]] || die "Failed to create squashfs image."
info "Creating squashfs ..."
mksquashfs squashfs-root $DSTIMAGE -comp xz -noappend
info "Cleaning up"
rm -rvf squashfs-root
}
main() {
SRC=$1
DST=${2:-squashfs.img}
[[ -n $SRC ]] || die "No source given"
image_to_squashfs $SRC $DST
}
[[ -n $@ ]] && main $@ || usage