aboutsummaryrefslogtreecommitdiff
path: root/build.sh
blob: f79206c6b9956578d6cc77b5de6e7a550ddcafc2 (plain)
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
127
128
129
#!/bin/bash

if [[ ! -z $SUDO_USER ]]; then
  user=$SUDO_USER
else
  user=$USER
fi

if [[ ! -z $SUDO_USER ]]; then
  user=$SUDO_USER
elif [[ $USER == "root" ]]; then
  user=$USER
else
  echo "Must be launch as root"
  exit 1
fi

help () {
  echo "$0 --net --qcow2 [size] --ssh [key_path]"
  echo ""
  echo "    --net enable dhcpcd"
  echo "    --qcow2 create a qcow2 image instead an iso one"
  echo "        [size] the size of the image you want. By default it don't resize"
  echo "    --ssh enable ssh at boot"
}

while [ -n "$1" ]; do

  case "$1" in

  -h)
    help
    exit 0
    ;;

  --net)

    net=1
    ;;

  --qcow2)

    qcow2=1
    if [[ $2 != --* ]]; then
      qcow2_param="$2"
      shift
    fi
    ;;

  --ssh)

    ssh=1
    ;;

  *)

    invalid_option=1
    error_msg="'$1' is not an recognized option"
    ;;

  esac

  shift

done

if [[ $invalid_option -eq 1 ]]; then
  echo $error_msg
  help
  exit 1
fi

rm -rf build
mkdir build

cp -r /usr/share/archiso/configs/baseline/ build/archlive

if [[ $ssh -eq 1 ]]; then
  mkdir -p build/archlive/airootfs/etc/systemd/system/multi-user.target.wants
  ln -s /usr/lib/systemd/system/sshd.service build/archlive/airootfs/etc/systemd/system/multi-user.target.wants/
fi

install_package () {
  package_name=$1
  cat build/archlive/packages.x86_64 | grep "$package_name" > /dev/null
  if [[ $? -eq 1 ]]; then
    echo $package_name >> build/archlive/packages.x86_64
  fi
}

install_package "cloud-init"

mkdir -p build/archlive/airootfs/etc/systemd/system/cloud-init.target.wants
ln -s /usr/lib/systemd/system/cloud-init.service build/archlive/airootfs/etc/systemd/system/cloud-init.target.wants/cloud-init.service

if [[ $net -eq 1 ]]; then
  install_package "dhcpcd"
  ln -s /usr/lib/systemd/system/dhcpcd.service build/archlive/airootfs/etc/systemd/system/multi-user.target.wants/dhcpcd.service
fi

rm -rf build/out
mkdir -p build/out
rm -rf /tmp/archiso-tmp

mkarchiso -v -w /tmp/archiso-tmp -o build/out build/archlive

xorriso -as genisoimage -output build/seed.iso -volid CIDATA -joliet -rock cloudinit_seed/user-data cloudinit_seed/meta-data
last_build=$(ls -tr build/out | tail -1)
mv build/out/$last_build archlinux_cloud.iso

rm -f archlinux_cloud.qcow2

if [[ $qcow2 -eq 1 ]]; then
  qemu-img convert -f raw -O qcow2 archlinux_cloud.iso archlinux_cloud.qcow2
  rm archlinux_cloud.iso
fi

if [[ ! -z "$qcow2_param" ]]; then
  qemu-img resize archlinux_cloud.qcow2 $qcow2_param
fi

chown -R $user: build
chown -R $user: archlinux_cloud.*

if [[ $qcow2 -eq 1 ]]; then
  echo "Image build at archlinux_cloud.qcow2"
else
  echo "Image build at archlinux_cloud.iso"
fi