This repository has been archived by the owner on Jul 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.sh
executable file
·86 lines (71 loc) · 2.61 KB
/
build.sh
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
#!/bin/bash
FS="magicOSfiles.tar.gz"
FILES="files"
BUILD="build"
ROOT_SIZE="5000"
SWAP_SIZE="500"
OFFSET="10"
IMG_OUTPUT="magicOS.img"
MBR="mbr.bin"
MNT="mnt"
error() {
echo "[ERROR]: $@"
exit 1
}
prepare() {
echo "Preparing environment"
[ ! -d "$BUILD" ] && mkdir -p $BUILD
[ ! -d "$MNT" ] && mkdir -p $MNT
[ ! -f "$FS" ] && error "filesystem file $FS does not exist, you need to download it, please see README"
sudo tar xzf $FS -C $BUILD
[ $? -eq 0 ] && echo "Filesystem directory prepared" || error "with $FS file, corrupted?"
}
copy_files() {
echo "Copying files"
[ ! -d "$BUILD" ] && error "$BUILD directory does not exist"
sudo cp -a $FILES/* $BUILD/
[ $? -eq 0 ] && echo "Custom files copied" || error "copying custom files"
}
build_img_partitions() {
echo "Creating images"
dd if=/dev/zero of=${IMG_OUTPUT}.root bs=1048576 count=$ROOT_SIZE
dd if=/dev/zero of=${IMG_OUTPUT}.offset bs=1048576 count=$OFFSET
dd if=/dev/zero of=${IMG_OUTPUT}.swap bs=1048576 count=$SWAP_SIZE
echo "Creating filesystems"
mkfs.ext4 ${IMG_OUTPUT}.root -U 00000000-0000-0000-0000-000000000069 -F -L magicOSroot
mkswap ${IMG_OUTPUT}.swap -U 00000000-0000-0000-0000-000000000070 -f
}
mount_root() {
echo "Mounting root image to $MNT"
sudo mount magicOS.img.root $MNT -o loop,rw
[ $? -ne 0 ] && error "Cannot mount root partition to $MNT!"
}
copy_root_files() {
echo "Copying files from $BUILD to $MNT"
sudo cp -a $BUILD/* $MNT/
}
build_final_img() {
echo "Creating final image"
cat $MBR ${IMG_OUTPUT}.root ${IMG_OUTPUT}.offset ${IMG_OUTPUT}.swap > ${IMG_OUTPUT}
parted -s ${IMG_OUTPUT} mklabel msdos
parted -s ${IMG_OUTPUT} unit MiB mkpart primary ext3 1 $(($ROOT_SIZE+$OFFSET))
parted -s ${IMG_OUTPUT} unit MiB mkpart primary linux-swap $(($ROOT_SIZE+$OFFSET)) $(($ROOT_SIZE+$OFFSET+${SWAP_SIZE}))
}
clean() {
sudo umount $MNT
rm -f ${IMG_OUTPUT}.root ${IMG_OUTPUT}.swap ${IMG_OUTPUT}.offset
}
print_help() {
echo "[MagiOS creator]"
echo " Available commands:"
echo -e "\tprepare\t\tprepare the environment, create partitions and mount root partition"
echo -e "\tsyncbuild\tcopy root partition files from $BUILD and generate final image"
echo -e "\tclean\t\tclean temporay files and umount root partition"
echo -e "\tall\t\tdo prepare and syncbuild"
echo -e "\n You may be asked for your password due a sudo command execution"
}
[ "$1" == "prepare" ] && prepare && build_img_partitions && mount_root
[ "$1" == "syncbuild" ] && copy_root_files && build_final_img
[ "$1" == "clean" ] && clean
[ "$1" == "all" ] && prepare && build_img_partitions && mount_root && copy_root_files && build_final_img && clean
[ -z "$1" ] && print_help