-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmkimg.sh
More file actions
executable file
·71 lines (59 loc) · 1.98 KB
/
mkimg.sh
File metadata and controls
executable file
·71 lines (59 loc) · 1.98 KB
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
#! /bin/sh
set -x
rootfs="$1"
image_name="$2"
arch="${3:-x86_64}"
bios="${4:-uefi}"
if [ "$bios" = "uefi" ]; then
umount /mnt/example/efi
fi
umount /mnt/example
set -ue
if [ "$bios" = "legacy" ]; then
if [ -f /etc/alpine-release ]; then
apk del grub-efi && apk add grub-bios
elif [ -f /etc/lsb-release ] || [ -f /etc/os-release ]; then
apt remove -y grub-efi && apt autoremove -y && apt install -y grub-pc
fi
fi
touch $image_name
dd if=/dev/null of=$image_name bs=1M seek=768
parted --script $image_name mklabel gpt
if [ "$bios" = "uefi" ]; then
parted --script $image_name mkpart ESP fat32 1M 65M
parted --script $image_name set 1 boot on
parted --script $image_name mkpart primary ext4 65M 449M
parted --script $image_name mkpart primary ext4 449M 768M
else
parted --script $image_name mkpart primary 1M 2M
parted --script $image_name set 1 bios_grub on
parted --script $image_name mkpart primary ext4 2M 386M
parted --script $image_name mkpart primary ext4 386M 768M
fi
device_name=`kpartx -a -v $image_name | awk 'NR==1 {print substr($3, 1, length($3)-2)}'`
rootfs_device_name="$device_name"p2
var_device_name="$device_name"p3
mkfs.ext4 /dev/mapper/$rootfs_device_name
mkfs.ext4 /dev/mapper/$var_device_name
mkdir /mnt/example
mount /dev/mapper/$rootfs_device_name /mnt/example/
if [ "$bios" = "uefi" ]; then
efi_device_name="$device_name"p1
mkfs.fat -F 32 /dev/mapper/$efi_device_name
mkdir -p /mnt/example/efi
mount /dev/mapper/$efi_device_name /mnt/example/efi
fi
cp -a $rootfs/* /mnt/example/
if [ "$bios" = "uefi" ]; then
mount -t efivarfs efivarfs /sys/firmware/efi/efivars
grub-install --boot-directory=/mnt/example/boot --target=${arch}-efi --efi-directory=/mnt/example/efi --bootloader-id=static-os --removable
else
grub-install /dev/${device_name} --boot-directory=/mnt/example/boot
fi
sync
if [ "$bios" = "uefi" ]; then
umount /mnt/example/efi
fi
umount /mnt/example
kpartx -d /dev/${device_name}
losetup -D