-
Notifications
You must be signed in to change notification settings - Fork 1
/
d8zroot.sh
executable file
·270 lines (216 loc) · 7.84 KB
/
d8zroot.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
###############################################################################
#
# Debian Jessie (version 8) installation script for ZFS root filesystem
#
# Copyright : Francois Scala @2016
# License : GPL v3
#
#
# Disk schema :
# +-- DISK -------------------------------------------------------------------+
# | +-Part 4---+ +-Part 1--+ +-Part 2----+ +-Part 3-------------------------+ |
# | | BIOS EFI | | Ext4 | | Swap | | ZFS root | |
# | | | | /boot | | | | | |
# | +----------+ +---------+ +-----------+ +--------------------------------+ |
# +---------------------------------------------------------------------------+
#
###############################################################################
# Debian
DEBIANDIST=jessie
#DEBIANMIRROR="http://ftp.us.debian.org/debian"
DEBIANMIRROR="http://ftp.ch.debian.org/debian"
# ZFS
POOL=rpool
ZFSRELEASE=zfsonlinux_6_all.deb
# Default grub options
BOOT_OPTIONS="quiet vga=794"
###############################################################################
# customize your root password, user account here and other parameters
function do_user {
# Set root password
echo 'root:root' | chpasswd
# Create a user
adduser --gecos "Some User" --shell /bin/bash --disabled-password user
echo 'user:user' | chpasswd
#mkdir /home/user/.ssh
#cat > /home/user/.ssh/authorized_keys << _EOF
#ssh-rsa AAAAB3NzaC1yc2EAAAABJQA.....
#_EOF
#chown -R user.user /home/user/.ssh
# Locales
apt-get install -y locales
locale-gen en_US.UTF-8
locale-gen C.UTF-8
export LANG=C.UTF-8
# Misc
DEBIAN_FRONTEND=noninteractive apt-get install -y tree htop ssh strace mc vim
# Automation (puppet, cfengine, ...)
}
###############################################################################
# customize your partition scheme here
function do_partitions {
DISK=/dev/sda
PARTBOOT=${DISK}1
PARTSWAP=${DISK}2
PARTZROOT=${DISK}3
BOOTPARAM=${DISK}
apt-get update
apt-get install -y gdisk
sgdisk --clear ${DISK}
sgdisk -n 1::+512M -c 1:"BOOT" -t 1:8300 ${DISK}
sgdisk -n 2::+2G -c 2:"SWAP" -t 2:8200 ${DISK}
sgdisk -n 3:: -c 3:"ZROOT" -t 3:bf00 ${DISK}
sgdisk -n 4:34:2047 -c 4:"BIOS" -t 4:ef02 ${DISK}
# DEBUG : to reinstall on existing system
#dd if=/dev/zero of=${DISK}1 bs=1M count=1
#dd if=/dev/zero of=${DISK}2 bs=1M count=1
#dd if=/dev/zero of=${DISK}3 bs=1M count=1
# /boot
mkfs.ext4 -L BOOT ${PARTBOOT}
# Swap
mkswap -f -L SWAP ${PARTSWAP}
#swapon -va
}
###############################################################################
# customize your rpool setup
function do_rpool {
zpool create -o ashift=12 -o altroot=/mnt -m none ${POOL} ${PARTZROOT}
zfs set atime=off ${POOL}
zfs create -o mountpoint=none ${POOL}/ROOT
zfs create -o mountpoint=/ ${POOL}/ROOT/debian-1
zpool set bootfs=${POOL}/ROOT/debian-1 ${POOL}
zfs create -o mountpoint=/home ${POOL}/home
#zfs create -o mountpoint=/usr ${POOL}/usr # separated /usr unsupported by systemd
zfs create -o mountpoint=/var ${POOL}/var
zfs create -o compression=lz4 -o atime=on ${POOL}/var/mail
zfs create -o compression=lz4 -o setuid=off -o exec=off ${POOL}/var/log
zfs create -o compression=lz4 -o setuid=off -o exec=off ${POOL}/var/tmp
zfs create -o mountpoint=/tmp -o compression=lz4 -o setuid=off ${POOL}/tmp
}
###############################################################################
###############################################################################
###############################################################################
###############################################################################
###############################################################################
# Nothing should be change after this line
echo "==========================================================="
echo "==="
echo "=== Partitioning"
echo "==="
do_partitions
###############################################################################
echo "==========================================================="
echo "==="
echo "=== Install ZFS On Linux on Live image"
echo "==="
if [ ! -f ${ZFSRELEASE} ]
then
wget http://archive.zfsonlinux.org/debian/pool/main/z/zfsonlinux/${ZFSRELEASE}
fi
dpkg -i ${ZFSRELEASE}
apt-get update
apt-get install -y linux-image-amd64 debian-zfs
modprobe zfs
###############################################################################
echo "==========================================================="
echo "==="
echo "=== Create ZFS Pool ${POOL}"
echo "==="
do_rpool
zpool export ${POOL}
zpool import -d /dev/disk/by-id -R /mnt ${POOL}
mkdir -p /mnt/etc/zfs/
zpool set cachefile=/mnt/etc/zfs/zpool.cache ${POOL}
mkdir -p /mnt/boot
mount /dev/disk/by-partlabel/BOOT /mnt/boot
###############################################################################
echo "==========================================================="
echo "==="
echo "=== Bootstrap Debian ${DEBIANDIST}"
echo "==="
apt-get install -y debootstrap
debootstrap ${DEBIANDIST} /mnt ${DEBIANMIRROR}
cp /etc/hostname /mnt/etc/
cp /etc/hosts /mnt/etc/
cp ${ZFSRELEASE} /mnt/tmp/
echo "Generate fstab"
cat > /mnt/etc/fstab << _EOF
LABEL=BOOT /boot ext4 noatime 0 1
LABEL=SWAP none swap defaults 0 0
_EOF
echo "Generate network/interfaces"
cat > /mnt/etc/network/interfaces << _EOF
# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
_EOF
# Generate chroot script
declare -f do_user > /mnt/tmp/chroot.sh
cat >> /mnt/tmp/chroot.sh << _EOF
cd /tmp/
do_user
echo "==========================================================="
echo "==="
echo "=== Install ZFS On Linux on target system"
echo "==="
apt-get install -y lsb-release
dpkg -i /tmp/${ZFSRELEASE}
apt-get update
apt-get install -y linux-image-amd64 debian-zfs
DEBIAN_FRONTEND=noninteractive apt-get install -y grub2-common grub-pc zfs-initramfs
grub-install --target=i386-pc --force ${BOOTPARAM}
mkdir -vp /boot/grub/
grub-mkconfig -o /boot/grub/grub.cfg
unset UCF_FORCE_CONFFOLD
export UCF_FORCE_CONFFNEW=YES
ucf --purge /boot/grub/menu.lst
apt-get update
DEBIAN_FRONTEND=noninteractive apt-get -o Dpkg::Options::="--force-confnew" --force-yes -fuy dist-upgrade
###############################################################################
# patch grub
echo "Set grub default config"
cp /etc/default/grub /etc/default/grub.orig
echo "default/grub : GRUB_CMDLINE_LINUX_DEFAULT"
sed -i "s-GRUB_CMDLINE_LINUX_DEFAULT=\"quiet-GRUB_CMDLINE_LINUX_DEFAULT=\"${BOOT_OPTIONS}-" /etc/default/grub
update-grub
_EOF
###############################################################################
echo "==========================================================="
echo "==="
echo "=== Chroot installation step"
echo "==="
mount --bind /dev /mnt/dev
mount --bind /proc /mnt/proc
mount --bind /sys /mnt/sys
chroot /mnt bash /tmp/chroot.sh 2>&1 | tee /mnt/root/install-chroot.log
# XXX Uncomment this to inspect/hack the system before the final unmount and reboot
#echo "XXXXXXXXXXXXXXXXXXXXXXX Confirm to unmount"
#read bla
umount /mnt/boot
umount /mnt/dev
umount /mnt/proc
umount /mnt/sys
zfs umount -a
zpool export ${POOL}
echo "==========================================================="
echo "==========================================================="
echo "==========================================================="
echo "==="
echo "==="
echo "==="
echo "==="
echo "==="
echo "==="
echo "==="
echo "==="
echo "==="
echo "==="
echo "==="
echo "=== Installation completed."
echo "==="
echo "=== You can now remove the boot device and reboot to"
echo "=== your new installation"
echo "==="
###############################################################################