Shell脚本
我喜欢制作一个bash shell脚本,可以在安装的Ubuntu系统和持久化的live系统中自动执行,并且也适用于基于Debian Jessie的小型'9w'系统。我希望它对你以及将来的其他人有用。
该脚本假设应该有两个可写的驱动器(大容量存储设备),即live驱动器和目标驱动器。不要使用toram,它会破坏对live驱动器的识别。
这个shell脚本调用了dus-live
,它是mkusb的一个执行部分。因此,它取代了交互式的shell脚本dus
。
dus-live
可以管理以下类型的源文件:
- file.iso
- file.img
- file.img.gz
- file.img.xz
目标系统可以同时具有MSDOS和GUID分区表(当目标驱动器的大小大于图像中原始系统的大小时)。
我使用了名称
autoclone
:
#!/bin/bash
# Date Sign Comment
# 2017-06-30 sudodus Created
# 2017-07-01 sudodus checking that there is 1 possible target drive
wait=30
read -t "$wait" -p "WARNING: After $wait seconds '$0' will CLONE AUTOMATICALLY;
from the file given as parameter to the first detected drive
(except the live drive), so it will be overwritten.
If this is not what you want or if you are not sure, PRESS {ctrl + C} NOW !
...
"
function power_off {
read -t "$wait" -p "WARNING: After $wait seconds '$0' will POWEROFF automatically;
If this is not what you want or if you are not sure, PRESS {ctrl + C} NOW !
...
"
sudo poweroff
}
user=$(whoami)
if [ "$user" != "root" ]
then
echo "run $0 with sudo or as root"
exit
fi
if [ "$1" == "" ] || ! test -s "$1"
then
echo "Usage: sudo $0 <with an iso file or image file as parameter>"
echo "Example: sudo $0 \"file.iso\""
echo "Example: sudo $0 \"file.img\""
echo "Example: sudo $0 \"file.img.xz\""
exit
fi
source="$1"
livedrive=$(lsblk -lo name,type,mountpoint | \
grep -m1 -e /$ -e /cdrom$ -e /lib/live/mount/medium -e /lib/live/mount/persistence)
#echo "$livedrive"
livedrive=${livedrive:0:3}
echo "livedrive=/dev/$livedrive"
number_of_targets=$(lsblk -ldo name,type|grep -v "$livedrive"|grep -v zram|grep disk|wc -l)
target=$(lsblk -ldo name,type|grep -v "$livedrive"|grep -v zram|grep disk)
#echo "target=$target"
# echo "number_of_targets=$number_of_targets"
# sudo lsblk -o name,model,size,type,fstype,label,mountpoint | \
# grep -v "$livedrive"|grep -v zram| grep -v loop|grep -v ' rom '
####
if [ $number_of_targets -ne 1 ]
then
echo '--------------------------------------------------------'
sudo lsblk -o name,model,size,type,fstype,label | \
grep -v "$livedrive"|grep -v zram| grep -v loop|grep -v ' rom '
echo "$number_of_targets possible target drives found, but
1 possible target drive should be found
for '$0' to work correctly."
if [ $number_of_targets -gt 1 ]
then
echo "--------------------------------------------------------
- Please remove every other possible target drive!
- Or are you running in the wrong computer?"
fi
echo "--------------------------------------------------------"
wait=60
power_off
exit
fi
target=/dev/${target:0:3}
echo "target=$target"
umount "$target"?*
paramfromfile=$(mktemp)
echo "$source
$target" > "$paramfromfile"
# do it with dus-live, which comes with mkusb version 12-
echo "calling 'dus-live' ..."
dus-live pff:"$paramfromfile"
power_off
在持久化的实时系统中进行测试
我在两个持久化的实时系统中进行了测试,这些系统可以从压缩的镜像文件中安装。
请点击此链接查看更多关于该方法的详细信息。
自动启动
当在Lubuntu中通过以下命令将以下行追加到自动启动时,此脚本可以自动启动:
echo 'xterm -fa default -fs 12 -title autoclone -e sudo /home/lubuntu/autoclone dd_no-biggie-68MB.img.xz' >> ~/.config/lxsession/Lubuntu/autostart
修改它以匹配实际的 'dd' 图像
/path/name
和 shell 脚本的实际位置。
在 9w 中对应的命令是:
echo '@xterm -fa default -fs 12 -title autoclone -e sudo /root/autoclone /root/dd_no-biggie-68MB.img.xz' >> ./.config/lxsession/LXDE/autostart
您自己的定制系统
您可能想要或必须创建您自己的定制系统(实时、持久性实时或安装在USB驱动器或存储卡中)。如果您要安装的系统使用MSDOS分区表,您可以使用简单的dd
命令行来替换dus-live
。
免责声明
这个shell脚本是我比较快地制作的,经过我的测试是有效的,但还有很多未经测试的情况,可能会发生意外情况。因此,请在没有重要数据的驱动器上进行测试,以免被覆盖。
屏幕截图
Lubuntu的三张屏幕截图
在前30秒内,您可以使用热键组合ctrl + C停止自动克隆。
![enter image description here](https://istack.dev59.com/PtRlM.webp)
控制台输出:当前阶段系统正在同步(将缓冲区刷新到目标驱动器)。
![enter image description here](https://istack.dev59.com/SL9hg.webp)
在30秒内,您可以使用热键组合
Ctrl + C来避免关机(如果您希望保持在Lubuntu桌面会话中)。
![enter image description here](https://istack.dev59.com/6cFaF.webp)
9w的相应截图
![enter image description here](https://istack.dev59.com/TuZe9.webp)