forked from davstott/ubuntu-usb-merge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergeusb
executable file
·78 lines (66 loc) · 2.42 KB
/
mergeusb
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
#!/bin/bash
set -e; set -u
# Confirms that arguments are set
if test $# -ne 1
then
echo "Usage $0 /path/to/mountedUSBDrive"
exit 1
fi
# Check for required packages
if ! which mksquashfs >/dev/null 2>&1; then
echo "mksquashfs is required, but was not found; please install mksquashfs-tools"
exit 1
fi
if ! dpkg -s overlayroot >/dev/null 2>&1; then
echo "overlayfs is required, but was not found; please install overlayroot"
exit 1
fi
# adding a trailing / here so we don't accidentally start with / later on
usbpath=$1/
#check for root
#look up disk space requirements from usb and check
#option for making safer by copying from usb 1st to make a backup
usblive=$(mktemp -d /var/tmp/usblive.XXXXXX)
cd "${usblive}"
mkdir readonly readwrite merged
echo "Mounting ${usbpath}casper/filesystem.squashfs"
mount "${usbpath}"casper/filesystem.squashfs readonly -t squashfs -o loop,ro
echo "Checking ${usbpath}casper-rw"
fsck ${usbpath}casper-rw || exit 1
echo "Mounting ${usbpath}casper-rw"
mount "${usbpath}"casper-rw readwrite -o loop,rw || exit 1
# different versions of Ubuntu put the upper half of the filesystem in different places
upperpath=readwrite
echo "Path to rw part of filesystem detected as ${upperpath}"
if test -d readwrite/work
then
workpath=${upperpath}/work
echo "Path to *work* path of filesystem detected as ${workpath}"
fi
if test -d readwrite/upper
then
upperpath=${upperpath}/upper
echo "Path to upper path of filesystem detected as ${upperpath}"
fi
echo "Mounting overlayfs"
mount none merged -o lowerdir=readonly,upperdir=${upperpath},workdir=${workpath} -t overlayfs || exit 1
echo "Making new squashfs"
mksquashfs merged newfilesystem.squashfs -comp xz
echo "Unmounting merged, casper-rw and old squashfs"
umount merged
umount readwrite
umount readonly
echo "mounting new squashfs and making new manifest"
mount newfilesystem.squashfs readonly -o loop,ro -t squashfs
chroot readonly dpkg-query -W --showformat='${Package} ${Version}\n' > newfilesystem.manifest
printf $(du -sx --block-size=1 readonly | cut -f1) > newfilesystem.size
echo "unmounting new squashfs and copying back to usb"
umount readonly
cp newfilesystem.squashfs "${usbpath}"casper/filesystem.squashfs
cp newfilesystem.size "${usbpath}"casper/filesystem.size
cp newfilesystem.manifest "${usbpath}"casper/filesystem.manifest
echo "resetting casper-rw"
mkfs -t ext3 "${usbpath}"casper-rw
echo "cleaning up"
cd /var/tmp
rm -rf "${usblive}"