-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpartition_script.sh
executable file
·126 lines (110 loc) · 3.63 KB
/
partition_script.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/bin/sh
REQUIRED_CERT_FILES="cas.pem cert.pem key.pem dev-id"
function partition_replace_certs()
{
local certs_rootdir=${2}
tmp_dir=`mktemp -d`
mount /dev/$1 ${tmp_dir} 2>&1 >/dev/null
if [ $? -ne 0 ] ; then
echo "Failed to create/mount partition"
exit 2
fi
rm -rf ${tmp_dir}/lost\+found 2>&1 >/dev/null
for x in $REQUIRED_CERT_FILES ; do
echo "Copying cert file ${certs_rootdir}/$x to partition..."
cp ${certs_rootdir}/$x ${tmp_dir}
if [ $? -ne 0 ] ; then
echo "Failed to copy ${certs_rootdir}/$x to partition..."
umount /dev/$1
exit 3
fi
done
echo "MD5SUM after replace:"
md5sum ${tmp_dir}/*
sync;
echo && echo "### Certificates has been copied successfully!" && echo
sync &&
umount ${tmp_dir} 2>&1 >/dev/null
rm -rf ${tmp_dir} 2>&1 >/dev/null
}
function get_first_free_partition()
{
for x in $(seq 1 10) ; do (ls /dev/${dev_name}${x} 2>&1 >/dev/null) || break; done
return ${x}
}
function partition_create()
{
get_first_free_partition 2>&1 >/dev/null
local part_idx=$?
local dev_name=${1}
local part_offs_start=${2}
local part_offs_end=${3}
local part_guid=${4}
local part_name=${5}
local tmp_dir=
echo "Trying to create part idx ${part_idx}"
sgdisk -n ${part_idx}:${part_offs_start}:${part_offs_end} \
-t ${part_idx}:${part_guid} \
-c ${part_idx}:${part_name} \
/dev/${dev_name} 2>&1 >/dev/null && echo "Created partition" && \
partprobe &&
sync &&
mkfs.ext4 -F -L ${part_name} /dev/${dev_name}${part_idx} &&
partprobe &&
parted -l | grep ${part_name} && echo &&
tune2fs -l /dev/${dev_name}${part_idx} | grep ${part_name} &&
echo && echo "### Partition ${part_name} has been created at /dev/${dev_name}${part_idx}" && echo
if [ $? -ne 0 ] ; then
echo "Partition create failed..."
exit 1
fi
partition_replace_certs "${dev_name}${part_idx}" "${6}"
}
function check_cert_file_exists()
{
if [ ! -e "$1" ] ; then
echo "File <$1> does not exists, or provided root directory is invalid"
exit 1
fi
}
if [ ! -d "$1" ] ; then
echo "Root directory <$1> does not exists, or it's not a directory."
echo "Please specify a valid folder that holds device certificates to be installed."
exit 1
fi
for x in $REQUIRED_CERT_FILES ; do
echo "Checking if $1/$x exists..."
check_cert_file_exists "$1/$x"
done;
dmidecode | grep -i 'manufacturer' | grep QEMU 2>&1 >/dev/null
if [ $? -eq 0 ] ; then
dev_name="vda"
else
dev_name="sda"
fi
echo "Parted list before changes:"
parted -l
# Following are valid for as4630_54pe-r0
# Device name: base device "name" - vda for Virtual, sda for physical devices
# Part num: <dynamic>, starting sector: 34, end sector: 2047
# Partition type - 8300 Linux filesystem
# Label - 'ONIE-TIP-CA-CERT'
sgdisk -p /dev/${dev_name} | grep TIP-CA-CERT 2>&1 >/dev/null
if [ $? -ne 0 ] ; then
echo "No <ONIE-TIP-CA-CERT> part found, creating..."
partition_create $dev_name 34 2047 8300 "ONIE-TIP-CA-CERT" $1
if [ $? -ne 0 ] ; then
echo "Partition create failed"
exit 1
fi
else
echo "Partition 'ONIE-TIP-CA-CERT' already exists... not creating"
echo "Trying to replace certificate on existing partition with new ones..."
cert_part_idx=`sgdisk -p /dev/${dev_name} | grep TIP-CA-CERT | tail -1 | awk -F'[^0-9]+' '{ print $2 }' 2>/dev/null`
echo "Cert partition is ${dev_name}${cert_part_idx}"
partition_replace_certs "${dev_name}${cert_part_idx}" $1
fi
echo
echo "### Parted list after changes:"
parted -l
exit 0