-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmake-img.sh
More file actions
executable file
·87 lines (79 loc) · 2.03 KB
/
make-img.sh
File metadata and controls
executable file
·87 lines (79 loc) · 2.03 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#! /bin/bash
# exit immediately if any command fails
set -e
IMG_NAME="make-image-output.img"
IMG_SIZE=60
usage() {
U=""
if [[ -n "$1" ]]; then
U="${U}$1\n\n"
fi
U="${U}Usage: $0 [options]\n\n"
U="${U}Options:\n"
U="$U -s | --size <GB>: Size of disk image in GBs (default: ${IMG_SIZE}g)\n"
U="$U -f | --fs <file>: rootfs tar.xz\n"
U="$U -o | --output <file>: output file name (default: ${IMG_NAME})\n"
echo -e "$U" >&2
}
while :
do
case "$1" in
-s | --size)
IMG_SIZE="$2"
shift 2
;;
-f | --fs)
FS_TAR="$2"
shift 2
;;
-o | --output)
IMG_NAME="$2"
shift 2
;;
-h | --help)
usage ""
exit 1
;;
--) # End of all options
shift
break
;;
-*) # Unknown option
echo "Error: Unknown option: $1" >&2
exit 1
;;
*)
break
;;
esac
done
if [[ -z "$FS_TAR" ]]; then
echo "Please supply rootfs tar.xz" >&2
exit 1
fi
# create temp directory for mounting
TMP_DIR=$(mktemp -d --tmpdir=.)
# temp file for modifying /etc/passwd in the image
TMP_FILE=$(mktemp --tmpdir=.)
truncate -s ${IMG_SIZE}g ${IMG_NAME}
mkfs.ext4 ${IMG_NAME}
sudo mount ${IMG_NAME} ${TMP_DIR}
sudo tar xvf ${FS_TAR} -C ${TMP_DIR}
sudo sync
sudo touch ${TMP_DIR}/etc/cloud/cloud-init.disabled
# copy /etc/passwd to TMP_FILE but without the first line
sudo /bin/bash -c "tail -n +2 ${TMP_DIR}/etc/passwd > ${TMP_FILE}"
# remove the original /etc/passwd
sudo rm ${TMP_DIR}/etc/passwd
# add the first line (no root password) to TMP_FILE
sudo /bin/bash -c "echo root::0:0:root:/root:/bin/bash > ${TMP_DIR}/etc/passwd"
# copy the TMP_FILE to create the new /etc/passwd
sudo /bin/bash -c "cat ${TMP_FILE} >> ${TMP_DIR}/etc/passwd"
# force DNS server ip
sudo /bin/bash -c "echo \"DNS=8.8.8.8\" >> ${TMP_DIR}/etc/systemd/resolved.conf"
sudo sync
sudo umount ${TMP_DIR}
# remove the temp file
rm ${TMP_FILE}
# remove the temp directory
rm -rf ${TMP_DIR}